wikijs-fork/client/js/components/editor-video.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-01-03 03:20:24 +00:00
const videoRules = {
2017-02-09 01:52:37 +00:00
'youtube': new RegExp(/(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/, 'i'),
'vimeo': new RegExp(/vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^\/]*)\/videos\/|album\/(?:\d+)\/video\/|)(\d+)(?:$|\/|\?)/, 'i'),
'dailymotion': new RegExp(/(?:dailymotion\.com(?:\/embed)?(?:\/video|\/hub)|dai\.ly)\/([0-9a-z]+)(?:[\-_0-9a-zA-Z]+(?:#video=)?([a-z0-9]+)?)?/, 'i')
}
2017-01-03 03:20:24 +00:00
// Vue Video instance
let vueVideo = new Vue({
2017-02-09 01:52:37 +00:00
el: '#modal-editor-video',
data: {
link: ''
},
methods: {
open: (ev) => {
$('#modal-editor-video').addClass('is-active')
$('#modal-editor-video input').focus()
},
cancel: (ev) => {
mdeModalOpenState = false
$('#modal-editor-video').removeClass('is-active')
vueVideo.link = ''
},
insertVideo: (ev) => {
if (mde.codemirror.doc.somethingSelected()) {
mde.codemirror.execCommand('singleSelection')
}
2017-01-03 03:20:24 +00:00
// Guess video type
2017-02-09 01:52:37 +00:00
let videoType = _.findKey(videoRules, (vr) => {
return vr.test(vueVideo.link)
})
if (_.isNil(videoType)) {
videoType = 'video'
}
2017-01-03 03:20:24 +00:00
// Insert video tag
2017-02-09 01:52:37 +00:00
let videoText = '[video](' + vueVideo.link + '){.' + videoType + '}\n'
2017-01-03 03:20:24 +00:00
2017-02-09 01:52:37 +00:00
mde.codemirror.doc.replaceSelection(videoText)
vueVideo.cancel()
}
}
})