index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div class="codeEditorAlertBox" v-if="showCurrentTips || showDefaultTips">
  3. <el-alert
  4. :title="
  5. tip ||
  6. `// 当前输入语言为 ${
  7. language === 'get' || language === 'post' ? 'javascript' : language
  8. } , 请注意书写规范。`
  9. "
  10. v-if="showCurrentTips"
  11. />
  12. <el-alert :title="defaultTips" v-if="showDefaultTips" />
  13. </div>
  14. <div id="codeEditBox" :style="`height:${height ? height : '500px'}`"></div>
  15. </template>
  16. <script>
  17. import * as monaco from "monaco-editor";
  18. import { toRaw } from "vue";
  19. export default {
  20. props: {
  21. height: {
  22. type: String,
  23. default: "500px",
  24. },
  25. tip: {
  26. type: String,
  27. default: () => "",
  28. },
  29. code: {
  30. type: String,
  31. default: () => "",
  32. },
  33. showCurrentTips: {
  34. type: Boolean,
  35. default: () => true,
  36. },
  37. showDefaultTips: {
  38. type: Boolean,
  39. default: () => true,
  40. },
  41. language: {
  42. type: String,
  43. default: "javascript",
  44. },
  45. },
  46. data() {
  47. return {
  48. editorValue: "",
  49. monacoEditor: null,
  50. defaultTips:
  51. "// 每一种类型的事件都携带 2 个内置变量,变量名称为 [e 和 target] ,分别表示 鼠标原生event对象 和 精简后的组件对象(包含事件发生位置在页面内的坐标与触发事件的组件),如若需要,可自行直接使用其名称来进行代码书写。",
  52. activePen: {},
  53. };
  54. },
  55. mounted() {
  56. this.editorValue = this.code || "";
  57. this.init();
  58. },
  59. unmounted() {
  60. this.getEditorValue();
  61. },
  62. methods: {
  63. init() {
  64. // 使用 - 创建 monacoEditor 对象
  65. const language =
  66. this.language === "get" || this.language === "post"
  67. ? "javascript"
  68. : this.language;
  69. this.monacoEditor = monaco.editor.create(
  70. document.getElementById("codeEditBox"),
  71. {
  72. theme: "vs-dark", // 主题
  73. value: this.editorValue, // 默认显示的值
  74. language: language || "javascript",
  75. folding: true, // 是否折叠
  76. foldingHighlight: true, // 折叠等高线
  77. foldingStrategy: "indentation", // 折叠方式 auto | indentation
  78. showFoldingControls: "always", // 是否一直显示折叠 always | mouseover
  79. disableLayerHinting: true, // 等宽优化
  80. emptySelectionClipboard: false, // 空选择剪切板
  81. selectionClipboard: false, // 选择剪切板
  82. automaticLayout: true, // 自动布局
  83. codeLens: false, // 代码镜头
  84. scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
  85. colorDecorators: true, // 颜色装饰器
  86. accessibilitySupport: "off", // 辅助功能支持 "auto" | "off" | "on"
  87. lineNumbers: "on", // 行号 取值: "on" | "off" | "relative" | "interval" | function
  88. lineNumbersMinChars: 5, // 行号最小字符 number
  89. enableSplitViewResizing: false,
  90. readOnly: false, //是否只读 取值 true | false
  91. }
  92. );
  93. this.monacoEditor.onDidBlurEditorText(() =>{
  94. this.getEditorValue()
  95. })
  96. },
  97. getEditorValue() {
  98. this.$emit("change", toRaw(this.monacoEditor).getValue());
  99. },
  100. setEditorValue(newValue) {
  101. toRaw(this.monacoEditor).setValue(newValue);
  102. this.getEditorValue();
  103. },
  104. },
  105. watch: {
  106. code(value) {
  107. this.editorValue = value;
  108. toRaw(this.monacoEditor).setValue(this.editorValue);
  109. },
  110. },
  111. };
  112. </script>
  113. <style lang="less" scoped>
  114. #codeEditBox {
  115. width: 100%;
  116. }
  117. .codeEditorAlertBox {
  118. .el-alert {
  119. font-family: Consolas, "Courier New", monospace;
  120. font-weight: normal;
  121. font-size: 14px;
  122. font-feature-settings: "liga" 0, "calt" 0;
  123. line-height: 19px;
  124. width: 100%;
  125. background: #1e1e1e;
  126. color: #608b4e;
  127. text-indent: 4em;
  128. user-select: none;
  129. padding: 0 0 8px 0;
  130. border-radius: 0;
  131. }
  132. .el-alert.danger {
  133. color: #f25656;
  134. font-weight: 700;
  135. user-select: auto;
  136. }
  137. .el-alert:first-child {
  138. padding-top: 8px;
  139. }
  140. }
  141. </style>
  142. <style lang="less">
  143. .codeEditorAlertBox {
  144. .el-alert {
  145. .el-alert__content {
  146. width: calc(100% - 16px);
  147. }
  148. .el-alert__title {
  149. width: 98%;
  150. display: flex;
  151. }
  152. }
  153. }
  154. </style>