index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div :id="id" v-loading="loading" class="fm-code-editor" :style="{width: width, height: height}"></div>
  3. </template>
  4. <script>
  5. import {loadJs} from '../../util'
  6. export default {
  7. props: {
  8. width: {
  9. type: String,
  10. default: '100%'
  11. },
  12. height: {
  13. type: String,
  14. default: '100%'
  15. },
  16. mode: {
  17. type: String,
  18. default: 'xml'
  19. },
  20. value: {
  21. type: [String, Object, Array]
  22. }
  23. },
  24. data () {
  25. return {
  26. id: 'code_' + Math.random().toString(36).slice(-8),
  27. codeValue: this.value,
  28. loading: true,
  29. observer: null,
  30. editor: null
  31. }
  32. },
  33. computed: {
  34. aceMode () {
  35. switch (this.mode) {
  36. case 'xml':
  37. return 'ace/mode/xml'
  38. case 'html':
  39. return 'ace/mode/html'
  40. case 'json':
  41. return 'ace/mode/json'
  42. case 'css':
  43. return 'ace/mode/css'
  44. case 'javascript':
  45. return 'ace/mode/javascript'
  46. case 'tsx':
  47. return 'ace/mode/tsx'
  48. default:
  49. return 'ace/mode/xml'
  50. }
  51. }
  52. },
  53. mounted () {
  54. console.log('code editor mounted.')
  55. setTimeout(() => {
  56. // if (window.ace) {
  57. // console.log(window.ace);
  58. // this.loadEditor()
  59. // } else {
  60. // loadJs(`${window.FormMaking_OPTIONS.aceurl}/ace.js`).then(() => {
  61. // loadJs(`${window.FormMaking_OPTIONS.aceurl}/ext-language_tools.js`).then(() => {
  62. // this.loadEditor()
  63. // })
  64. // })
  65. // }
  66. loadJs(`${window.FormMaking_OPTIONS.aceurl}/ace.js`).then(() => {
  67. loadJs(`${window.FormMaking_OPTIONS.aceurl}/ext-language_tools.js`).then(() => {
  68. this.loadEditor()
  69. })
  70. })
  71. }, 0)
  72. },
  73. methods: {
  74. loadEditor () {
  75. this.loading = false
  76. ace.require("ace/ext/language_tools")
  77. this.editor = ace.edit(this.id)
  78. // const beautify = ace.require("ace/ext/beautify")
  79. this.editor.session.setMode(this.aceMode)
  80. this.editor.setFontSize(13)
  81. this.editor.getSession().setTabSize(2)
  82. this.editor.setShowPrintMargin(false)
  83. // editor.setReadOnly(true)
  84. this.editor.setOptions({
  85. // enableBasicAutoCompletion : true,
  86. enableSnippets : true,
  87. enableLiveAutocompletion : true
  88. });
  89. this.editor.commands.addCommand({
  90.     name: 'myCommand',
  91.     bindKey: {win: 'Ctrl-Enter',  mac: 'Command-Enter'},
  92.     exec: function(editor) {
  93. const currentCursor = editor.selection.getCursor();
  94. editor.selection.moveCursorLineEnd()
  95. editor.selection.moveTo(editor.selection.getCursor().row, editor.selection.getCursor().column)
  96. editor.session.insert(editor.getCursorPosition(), '\n')
  97.     }
  98. });
  99. this.editor.setValue(typeof this.codeValue === 'string' ? this.codeValue : JSON.stringify(this.codeValue, null, '\t'), -1)
  100. this.editor.on('change', (e) => {
  101. this.codeValue = this.editor.getValue()
  102. })
  103. this.editor.on('paste', (e) => {
  104. this.mode === 'json' && setTimeout(() => {
  105. this.editor.setValue(JSON.stringify(JSON.parse(e.text), null, 2), -1)
  106. })
  107. })
  108. },
  109. setValue (value) {
  110. this.editor.setValue(JSON.stringify(JSON.parse(value), null, 2), -1)
  111. }
  112. },
  113. watch: {
  114. value (val) {
  115. this.codeValue = val
  116. },
  117. codeValue (val) {
  118. this.$emit('input', val)
  119. }
  120. },
  121. beforeUnmount () {
  122. this.editor.destroy()
  123. this.editor.container.remove()
  124. },
  125. }
  126. </script>
  127. <style lang="scss">
  128. .fm-code-editor{
  129. border: 1px solid #DCDFE6;
  130. }
  131. </style>