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.

75 lines
1.9 KiB

  1. /* global $, Vue, ace, mde, _ */
  2. let modelist = ace.require('ace/ext/modelist')
  3. let codeEditor = null
  4. // ACE - Mode Loader
  5. let modelistLoaded = []
  6. let loadAceMode = (m) => {
  7. return $.ajax({
  8. url: '/js/ace/mode-' + m + '.js',
  9. dataType: 'script',
  10. cache: true,
  11. beforeSend: () => {
  12. if (_.includes(modelistLoaded, m)) {
  13. return false
  14. }
  15. },
  16. success: () => {
  17. modelistLoaded.push(m)
  18. }
  19. })
  20. }
  21. // Vue Code Block instance
  22. let vueCodeBlock = new Vue({
  23. el: '#modal-editor-codeblock',
  24. data: {
  25. modes: modelist.modesByName,
  26. modeSelected: 'text',
  27. initContent: ''
  28. },
  29. watch: {
  30. modeSelected: (val, oldVal) => {
  31. loadAceMode(val).done(() => {
  32. ace.require('ace/mode/' + val)
  33. codeEditor.getSession().setMode('ace/mode/' + val)
  34. })
  35. }
  36. },
  37. methods: {
  38. open: (ev) => {
  39. $('#modal-editor-codeblock').addClass('is-active')
  40. _.delay(() => {
  41. codeEditor = ace.edit('codeblock-editor')
  42. codeEditor.setTheme('ace/theme/tomorrow_night')
  43. codeEditor.getSession().setMode('ace/mode/' + vueCodeBlock.modeSelected)
  44. codeEditor.setOption('fontSize', '14px')
  45. codeEditor.setOption('hScrollBarAlwaysVisible', false)
  46. codeEditor.setOption('wrap', true)
  47. codeEditor.setValue(vueCodeBlock.initContent)
  48. codeEditor.focus()
  49. codeEditor.renderer.updateFull()
  50. }, 300)
  51. },
  52. cancel: (ev) => {
  53. mdeModalOpenState = false // eslint-disable-line no-undef
  54. $('#modal-editor-codeblock').removeClass('is-active')
  55. vueCodeBlock.initContent = ''
  56. },
  57. insertCode: (ev) => {
  58. if (mde.codemirror.doc.somethingSelected()) {
  59. mde.codemirror.execCommand('singleSelection')
  60. }
  61. let codeBlockText = '\n```' + vueCodeBlock.modeSelected + '\n' + codeEditor.getValue() + '\n```\n'
  62. mde.codemirror.doc.replaceSelection(codeBlockText)
  63. vueCodeBlock.cancel()
  64. }
  65. }
  66. })