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.

55 lines
1.6 KiB

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