editor-video.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const videoRules = {
  2. 'youtube': new RegExp(/(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/, 'i'),
  3. 'vimeo': new RegExp(/vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^\/]*)\/videos\/|album\/(?:\d+)\/video\/|)(\d+)(?:$|\/|\?)/, 'i'),
  4. 'dailymotion': new RegExp(/(?:dailymotion\.com(?:\/embed)?(?:\/video|\/hub)|dai\.ly)\/([0-9a-z]+)(?:[\-_0-9a-zA-Z]+(?:#video=)?([a-z0-9]+)?)?/, 'i')
  5. };
  6. // Vue Video instance
  7. let vueVideo = new Vue({
  8. el: '#modal-editor-video',
  9. data: {
  10. link: ''
  11. },
  12. methods: {
  13. open: (ev) => {
  14. $('#modal-editor-video').addClass('is-active');
  15. $('#modal-editor-video input').focus();
  16. },
  17. cancel: (ev) => {
  18. mdeModalOpenState = false;
  19. $('#modal-editor-video').removeClass('is-active');
  20. vueVideo.link = '';
  21. },
  22. insertVideo: (ev) => {
  23. if(mde.codemirror.doc.somethingSelected()) {
  24. mde.codemirror.execCommand('singleSelection');
  25. }
  26. // Guess video type
  27. let videoType = _.findKey(videoRules, (vr) => {
  28. return vr.test(vueVideo.link);
  29. });
  30. if(_.isNil(videoType)) {
  31. videoType = 'video';
  32. }
  33. // Insert video tag
  34. let videoText = '[video](' + vueVideo.link + '){.' + videoType + '}\n';
  35. mde.codemirror.doc.replaceSelection(videoText);
  36. vueVideo.cancel();
  37. }
  38. }
  39. });