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.

86 lines
2.2 KiB

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