You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB

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