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.

108 lines
3.3 KiB

  1. <template lang="pug">
  2. transition(:duration="400")
  3. .modal(v-show='isShown', v-cloak)
  4. transition(name='modal-background')
  5. .modal-background(v-show='isShown')
  6. .modal-container
  7. transition(name='modal-content')
  8. .modal-content.is-expanded(v-show='isShown')
  9. header.is-green
  10. span Insert Code Block
  11. section.is-gapless
  12. .columns.is-stretched
  13. .column.is-one-quarter.modal-sidebar.is-green(style={'max-width':'350px'})
  14. .model-sidebar-header Language
  15. .model-sidebar-content
  16. p.control.is-fullwidth
  17. select(v-model='modeSelected')
  18. option(v-for='mode in modes', v-bind:value='mode.name') {{ mode.caption }}
  19. .column.ace-container
  20. #codeblock-editor
  21. footer
  22. a.button.is-grey.is-outlined(v-on:click='cancel') Discard
  23. a.button.is-green(v-on:click='insertCode') Insert Code Block
  24. </template>
  25. <script>
  26. let codeEditor
  27. let ace
  28. export default {
  29. name: 'editor-codeblock',
  30. data () {
  31. return {
  32. modes: [],
  33. modeSelected: 'text',
  34. modelistLoaded: []
  35. }
  36. },
  37. computed: {
  38. content () {
  39. return this.$store.state.editorCodeblock.content
  40. },
  41. isShown () {
  42. return this.$store.state.editorCodeblock.shown
  43. }
  44. },
  45. watch: {
  46. modeSelected(val, oldVal) {
  47. this.loadMode(val)
  48. }
  49. },
  50. methods: {
  51. init () {
  52. let self = this
  53. self._.delay(() => {
  54. codeEditor = ace.edit('codeblock-editor')
  55. codeEditor.setTheme('ace/theme/tomorrow_night')
  56. codeEditor.getSession().setMode('ace/mode/' + self.modeSelected)
  57. codeEditor.setOption('fontSize', '14px')
  58. codeEditor.setOption('hScrollBarAlwaysVisible', false)
  59. codeEditor.setOption('wrap', true)
  60. codeEditor.setOption('showPrintMargin', false)
  61. codeEditor.setValue(self.content)
  62. codeEditor.focus()
  63. codeEditor.renderer.updateFull()
  64. }, 100)
  65. },
  66. loadMode (m) {
  67. let self = this
  68. if (self._.includes(self.modelistLoaded, m)) {
  69. codeEditor.getSession().setMode('ace/mode/' + m)
  70. } else {
  71. self.$http.get('/js/ace/mode-' + m + '.js').then(resp => {
  72. if(resp.ok) {
  73. eval(resp.bodyText)
  74. self.modelistLoaded.push(m)
  75. ace.acequire('ace/mode/' + m)
  76. codeEditor.getSession().setMode('ace/mode/' + m)
  77. }
  78. })
  79. }
  80. },
  81. cancel () {
  82. codeEditor.destroy()
  83. this.$store.dispatch('editorCodeblock/close')
  84. },
  85. insertCode () {
  86. let codeBlockText = '\n```' + this.modeSelected + '\n' + codeEditor.getValue() + '\n```\n'
  87. this.$store.dispatch('editor/insert', codeBlockText)
  88. this.$store.dispatch('alert', {
  89. style: 'blue',
  90. icon: 'inbox',
  91. msg: 'Your code block has been inserted.'
  92. })
  93. this.cancel()
  94. }
  95. },
  96. mounted() {
  97. FuseBox.import('/js/ace/ace.js', (acePkg) => {
  98. ace = acePkg
  99. this.modes = ace.acequire('ace/ext/modelist').modesByName
  100. })
  101. this.$root.$on('editorCodeblock/init', this.init)
  102. }
  103. }
  104. </script>