Skip to content

Commit 00d2112

Browse files
committed
video elements are block elements now
1 parent 31dcb4b commit 00d2112

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"license": "ISC",
1717
"dependencies": {
1818
"exenv": "^1.2.1",
19-
"fetch": "^1.0.1",
20-
"markdown-it-video": "^0.3.1",
19+
"markdown-it-video": "github:nsfmc/markdown-it-video#video-block",
2120
"prosemirror": "github:spaced-out/prosemirror#upstream-master",
2221
"react": "^15.0.2",
2322
"whatwg-fetch": "^1.0.0"

src/video-url.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
export const yt_regex = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
4+
export function youtube_parser (url) {
5+
var match = url.match(yt_regex);
6+
return match && match[7].length === 11 ? match[7] : url;
7+
}
8+
9+
/*eslint-disable max-len */
10+
export const vimeo_regex = /https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/;
11+
/*eslint-enable max-len */
12+
export function vimeo_parser (url) {
13+
var match = url.match(vimeo_regex);
14+
return match && typeof match[3] === 'string' ? match[3] : url;
15+
}
16+
17+
export const vine_regex = /^http(?:s?):\/\/(?:www\.)?vine\.co\/v\/([a-zA-Z0-9]{1,13}).*/;
18+
export function vine_parser (url) {
19+
var match = url.match(vine_regex);
20+
return match && match[1].length === 11 ? match[1] : url;
21+
}
22+
23+
export const videoUrlParser = (videoUrl) => {
24+
if (videoUrl.match(yt_regex)) {
25+
return {service: 'youtube', videoId: youtube_parser(videoUrl)};
26+
}
27+
if (videoUrl.match(vimeo_regex)) {
28+
return {service: 'vimeo', videoId: vimeo_parser(videoUrl)};
29+
}
30+
}

src/video.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {Inline, Attribute} from 'prosemirror/dist/model';
1+
import {Inline, Block, Attribute} from 'prosemirror/dist/model';
22
import MarkdownVideoPlugin from 'markdown-it-video';
33
import ExecutionEnvironment from 'exenv';
4+
import {videoUrlParser} from './video-url';
45

56

67
let Dropdown = function(){}
@@ -13,16 +14,20 @@ if (ExecutionEnvironment.canUseDOM) {
1314
}
1415

1516

16-
export class VideoEmbed extends Inline {
17+
18+
19+
export class VideoEmbed extends Block {
1720
// Not sure what this is for other than tagging
1821
get attrs() {
1922
return {
23+
// url: new Attribute({label: 'video url'}),
2024
videoId: new Attribute({label: 'video id'}),
2125
service: new Attribute({label: 'hosting service'}),
2226
width: new Attribute({label: 'embed width', default: 320}),
2327
height: new Attribute({label: 'embed height', default: 240}),
2428
}
2529
}
30+
get contains() { return null }
2631
}
2732

2833
const generateVideoUrl = (attrs) => {
@@ -35,22 +40,22 @@ const generateVideoUrl = (attrs) => {
3540
}
3641
}
3742

38-
const generateTempVideoNode = (attrs) => {
43+
const generateTempVideoNode = (s, attrs) => {
3944
let {service, videoId} = attrs;
4045
const videoUrl = generateVideoUrl(attrs);
4146

42-
return elt('span', {
47+
return s.elt('span', {
4348
'class': `prosemirror-video-embed prosemirror-video-embed-${service}`,
4449
'contenteditable': 'false'
45-
}, elt('span', {
50+
}, s.elt('span', {
4651
'class': `prosemirror-video-embed-label`
4752
}, `video at ${videoUrl}`))
4853
};
4954

5055
VideoEmbed.prototype.serializeMarkdown = (state, node) => {
5156
state.write(`@[${node.attrs.service}](${node.attrs.videoId})`)
5257
}
53-
VideoEmbed.prototype.serializeDOM = (node) => {
58+
VideoEmbed.prototype.serializeDOM = (node, serializer) => {
5459
const {service, videoId} = node.attrs;
5560
if (['vimeo', 'youtube'].indexOf(service.toLowerCase()) < 0) {
5661
return;
@@ -64,7 +69,7 @@ VideoEmbed.prototype.serializeDOM = (node) => {
6469
// })
6570
// }
6671

67-
return generateTempVideoNode(node.attrs);
72+
return serializer.elt('div', null, generateTempVideoNode(serializer, node.attrs));
6873
}
6974

7075

@@ -86,14 +91,10 @@ VideoEmbed.register("command", "insert", {
8691
})
8792

8893
VideoEmbed.register('parseMarkdown', 'video', {parse: function(state, tok) {
89-
state.addNode(this, {service: tok.service, videoId: tok.videoID});
94+
state.addNode(this, {service: tok.service, videoId: tok.videoID, markup: '<div>'});
9095
}})
9196

9297

9398
VideoEmbed.register('configureMarkdown', 'video', parser => {
9499
return parser.use(MarkdownVideoPlugin);
95100
})
96-
97-
// export const dynamicMenu = new Dropdown({label: 'Insert dynamic field'}, new MenuCommandGroup('dynamic'))
98-
99-

0 commit comments

Comments
 (0)