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.

111 lines
2.3 KiB

10 months ago
  1. <template>
  2. <ace ref="editor"
  3. :value="value"
  4. :lang="lang"
  5. :width="width === 0 ? '100%' : width"
  6. :height="height === 0 ? '100%' : height"
  7. :theme="theme"
  8. :options="options"
  9. @init="initEditor"
  10. v-bind="config">
  11. </ace>
  12. </template>
  13. <script>
  14. import ace from 'vue2-ace-editor'
  15. export default {
  16. name: 'Editor',
  17. components: {
  18. ace
  19. },
  20. props: {
  21. value: {
  22. type: String,
  23. default: ''
  24. },
  25. width: {
  26. type: Number,
  27. default: 0
  28. },
  29. height: {
  30. type: Number,
  31. default: 300
  32. },
  33. readOnly: {
  34. type: Boolean,
  35. default: false
  36. },
  37. theme: {
  38. type: String,
  39. default: 'iplastic'
  40. },
  41. lang: {
  42. type: String,
  43. default: 'sh'
  44. },
  45. config: {
  46. type: Object,
  47. default: () => {
  48. return {
  49. enableLiveAutocompletion: true,
  50. fontSize: 16
  51. }
  52. }
  53. }
  54. },
  55. computed: {
  56. options() {
  57. return {
  58. enableBasicAutocompletion: true,
  59. enableSnippets: true,
  60. showPrintMargin: false,
  61. fontSize: this.config.fontSize,
  62. enableLiveAutocompletion: this.config.enableLiveAutocompletion,
  63. readOnly: this.readOnly
  64. }
  65. }
  66. },
  67. created() {
  68. this.initEditor()
  69. },
  70. methods: {
  71. initEditor(editor) {
  72. require('brace/ext/language_tools')
  73. // 设置语言
  74. require('brace/mode/sh')
  75. require('brace/mode/json')
  76. require('brace/mode/xml')
  77. require('brace/mode/yaml')
  78. require('brace/mode/properties')
  79. require('brace/snippets/sh')
  80. require('brace/snippets/json')
  81. require('brace/snippets/xml')
  82. require('brace/snippets/yaml')
  83. require('brace/snippets/properties')
  84. // 设置主题
  85. // 浅色 iplastic sqlserver tomorrow xcode
  86. // 深色 dracula gruvbox idle_fingers merbivore terminal tomorrow_night_bright
  87. require('brace/theme/iplastic')
  88. // 监听值的变化
  89. // editor.getSession().on('change', () => {
  90. // this.$emit('change', editor.getValue())
  91. // })
  92. },
  93. getValue() {
  94. return this.$refs.editor.editor.getValue()
  95. },
  96. setValue(value) {
  97. this.$refs.editor.editor.session.setValue(value)
  98. },
  99. clear() {
  100. this.$refs.editor.editor.session.setValue('')
  101. }
  102. }
  103. }
  104. </script>
  105. <style scoped>
  106. </style>