Skip to content
This repository was archived by the owner on Dec 13, 2017. It is now read-only.

Commit f08eea4

Browse files
Merge pull request #879 from wordpress-mobile/issue/implement_video_shortcode_self_hosted
Implement video shortcode self hosted
2 parents 4ddd483 + be1b9c2 commit f08eea4

5 files changed

Lines changed: 93 additions & 9 deletions

File tree

Assets/ZSSRichTextEditor.js

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,18 +1363,54 @@ ZSSEditor.removeVideo = function(videoNodeIdentifier) {
13631363
};
13641364

13651365
ZSSEditor.replaceVideoPressVideosForShortcode = function ( html) {
1366-
// call methods to restore any transformed content from its visual presentation to its source code.
1366+
13671367
var regex = /<video[^>]*data-wpvideopress="([\s\S]+?)"[^>]*>*<\/video>/g;
1368+
var str = html.replace( regex, ZSSEditor.removeVideoPressVisualFormattingCallback );
1369+
1370+
return str;
1371+
}
1372+
1373+
ZSSEditor.removeVideoPressVisualFormattingCallback = function( match, content ) {
1374+
return "[wpvideo " + content + "]";
1375+
}
1376+
1377+
ZSSEditor.replaceVideosForShortcode = function ( html) {
1378+
1379+
var regex = /<video(?:(?!data-wpvideopress).)*><\/video>/g;
13681380
var str = html.replace( regex, ZSSEditor.removeVideoVisualFormattingCallback );
13691381

13701382
return str;
13711383
}
13721384

13731385
ZSSEditor.removeVideoVisualFormattingCallback = function( match, content ) {
1374-
return "[wpvideo " + content + "]";
1386+
var videoElement = $.parseHTML(match)[0];
1387+
1388+
// Remove editor playback attributes
1389+
videoElement.removeAttribute("onclick");
1390+
videoElement.removeAttribute("controls");
1391+
videoElement.removeAttribute("webkit-playsinline");
1392+
if (videoElement.getAttribute("preload") == "metadata") {
1393+
// The "metadata" setting is the WP default and is usually automatically stripped from the shortcode.
1394+
// If it's present, it was probably set by this editor and we should remove it. Even if it wasn't, removing it
1395+
// won't affect anything as it's the default setting for the preload attribute.
1396+
videoElement.removeAttribute("preload");
1397+
}
1398+
1399+
// If filetype attributes exist, the src attribute wasn't there originally and we should remove it
1400+
for (var i = 0; i < ZSSEditor.videoShortcodeFormats.length; i++) {
1401+
var format = ZSSEditor.videoShortcodeFormats[i];
1402+
if (videoElement.hasAttribute(format)) {
1403+
videoElement.removeAttribute("src");
1404+
break;
1405+
}
1406+
}
1407+
1408+
var shortcode = videoElement.outerHTML.replace(/</g, "[");
1409+
shortcode = shortcode.replace(/>/g, "]");
1410+
return shortcode;
13751411
}
13761412

1377-
ZSSEditor.applyVideoFormattingCallback = function( match ) {
1413+
ZSSEditor.applyVideoPressFormattingCallback = function( match ) {
13781414
if (match.attrs.numeric.length == 0) {
13791415
return match.content;
13801416
}
@@ -1387,6 +1423,46 @@ ZSSEditor.applyVideoFormattingCallback = function( match ) {
13871423
return out;
13881424
}
13891425

1426+
// Video format tags supported by the [video] shortcode: https://codex.wordpress.org/Video_Shortcode
1427+
// mp4, m4v and webm prioritized since they're supported by the stock player as of Android API 23
1428+
ZSSEditor.videoShortcodeFormats = ["mp4", "m4v", "webm", "ogv", "wmv", "flv"];
1429+
1430+
ZSSEditor.applyVideoFormattingCallback = function( match ) {
1431+
// Find the tag containing the video source
1432+
var srcTag = "";
1433+
1434+
if (match.attrs.named['src']) {
1435+
srcTag = "src";
1436+
} else {
1437+
for (var i = 0; i < ZSSEditor.videoShortcodeFormats.length; i++) {
1438+
var format = ZSSEditor.videoShortcodeFormats[i];
1439+
if (match.attrs.named[format]) {
1440+
srcTag = format;
1441+
break;
1442+
}
1443+
}
1444+
}
1445+
1446+
if (srcTag.length == 0) {
1447+
return match.content;
1448+
}
1449+
1450+
var out = '<video webkit-playsinline src="' + match.attrs.named[srcTag] + '"';
1451+
1452+
// Preserve all existing tags
1453+
for (var item in match.attrs.named) {
1454+
out += ' ' + item + '="' + match.attrs.named[item] + '"';
1455+
}
1456+
1457+
if (!match.attrs.named['preload']) {
1458+
out += ' preload="metadata"';
1459+
}
1460+
1461+
out += ' onclick="" controls="controls"></video>';
1462+
1463+
return out;
1464+
}
1465+
13901466
/**
13911467
* @brief Sets the VidoPress video URL and poster URL on a video tag.
13921468
* @details When switching between source and visual the wpvideo shortcode are replace by a video tag. Unfortunaly there
@@ -1891,7 +1967,8 @@ ZSSEditor.removeCaptionFormattingCallback = function( match, content ) {
18911967
*/
18921968
ZSSEditor.applyVisualFormatting = function( html ) {
18931969
var str = wp.shortcode.replace( 'caption', html, ZSSEditor.applyCaptionFormatting );
1894-
str = wp.shortcode.replace( 'wpvideo', str, ZSSEditor.applyVideoFormattingCallback );
1970+
str = wp.shortcode.replace( 'wpvideo', str, ZSSEditor.applyVideoPressFormattingCallback );
1971+
str = wp.shortcode.replace( 'video', str, ZSSEditor.applyVideoFormattingCallback );
18951972
return str;
18961973
}
18971974

@@ -1907,6 +1984,7 @@ ZSSEditor.removeVisualFormatting = function( html ) {
19071984
str = ZSSEditor.removeImageSelectionFormattingFromHTML( str );
19081985
str = ZSSEditor.removeCaptionFormatting( str );
19091986
str = ZSSEditor.replaceVideoPressVideosForShortcode( str );
1987+
str = ZSSEditor.replaceVideosForShortcode( str );
19101988
return str;
19111989
}
19121990

Classes/WPEditorFormatbarView.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ - (id)initWithFrame:(CGRect)frame
4545
}
4646

4747
- (void)awakeFromNib {
48+
[super awakeFromNib];
4849
[self baseInit];
4950
return;
5051
}

Classes/WPEditorToolbarButton.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
static CGFloat HighlightedAlpha = 0.1;
1010
static CGFloat NormalAlpha = 1.0;
1111

12-
@interface WPEditorToolbarButton ()
12+
@interface WPEditorToolbarButton () <CAAnimationDelegate>
1313

1414
@property (nonatomic, weak, readonly) id target;
1515
@property (nonatomic, assign, readonly) SEL selector;

Example/EditorDemo/WPViewController.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ - (void)viewDidLoad
3535
- (void)customizeAppearance
3636
{
3737
[super customizeAppearance];
38+
// WORKAROUND: Preload the Noto regular font to ensure it is not overridden
39+
// by any of the Noto varients. Size is arbitrary.
40+
// See: https://github.com/wordpress-mobile/WordPress-Shared-iOS/issues/79
41+
// Remove this when #79 is resolved.
42+
[WPFontManager notoRegularFontOfSize:16.0];
3843
[WPFontManager loadNotoFontFamily];
3944

4045
self.placeholderColor = [WPStyleGuide grey];
@@ -457,7 +462,7 @@ - (void)timerFireMethod:(NSTimer *)timer
457462
[self.editorView replaceLocalVideoWithID:videoID
458463
forRemoteVideo:videoURL
459464
remotePoster:posterURL
460-
videoPress:videoID];
465+
videoPress:@""];
461466
[self.videoPressCache setObject:@ {@"source":videoURL, @"poster":posterURL} forKey:videoID];
462467
[timer invalidate];
463468
}

Example/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PODS:
1212
- CocoaLumberjack (~> 2.2.0)
1313
- NSObject-SafeExpectations (~> 0.0.2)
1414
- WordPressCom-Analytics-iOS (~> 0.1.0)
15-
- WordPress-iOS-Shared (0.8.0):
15+
- WordPress-iOS-Shared (0.8.2):
1616
- CocoaLumberjack (~> 2.2.0)
1717
- WordPressCom-Analytics-iOS (0.1.15)
1818

@@ -24,13 +24,13 @@ DEPENDENCIES:
2424

2525
EXTERNAL SOURCES:
2626
WordPress-iOS-Editor:
27-
:path: "../"
27+
:path: ../
2828

2929
SPEC CHECKSUMS:
3030
CocoaLumberjack: 17fe8581f84914d5d7e6360f7c70022b173c3ae0
3131
NSObject-SafeExpectations: 7d7f48df90df4e11da7cfe86b64f45eff7a7f521
3232
WordPress-iOS-Editor: 69c5d1e83aaa6e5ab6c80e067f1858de8dab4017
33-
WordPress-iOS-Shared: 4d073fb8efa96f3c902d1e1ac2557bb720f416d7
33+
WordPress-iOS-Shared: 999f5bfcf5744f94ebe9cb7459a138f5990fbf3a
3434
WordPressCom-Analytics-iOS: e1a7111255e98561c4b5a33ef4baa75a68e0d5d3
3535

3636
PODFILE CHECKSUM: c5af8aa4a9723c2a470c04ad3589d2b2934feb38

0 commit comments

Comments
 (0)