You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

monacoeditor.vue 6.1 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div ref="container" class="monaco-editor" style="height: calc(100vh - 42px)" />
  3. </template>
  4. <script>
  5. // import * as monaco from "monaco-editor";
  6. import "monaco-editor/min/vs/loader.js";
  7. import "monaco-editor/min/vs/editor/editor.main.nls.js";
  8. import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
  9. // import "monaco-editor/esm/vs/basic-languages/mysql/mysql.contribution";
  10. // import "monaco-editor/esm/vs/editor/contrib/suggest/suggestController.js";
  11. // import "monaco-editor/esm/vs/editor/contrib/bracketMatching/bracketMatching.js";
  12. // import "monaco-editor/esm/vs/editor/contrib/hover/hover.js";
  13. import { isBase64 } from "../utils.js";
  14. export default {
  15. name: "AcMonaco",
  16. inject: ["reload"],
  17. props: {
  18. opts: {
  19. type: Object,
  20. default() {
  21. return {};
  22. },
  23. },
  24. height: {
  25. type: Number,
  26. default: 300,
  27. },
  28. monacaValue: {
  29. type: String,
  30. default: "false",
  31. },
  32. onChange: {
  33. type: Function,
  34. default: new Function
  35. }
  36. },
  37. data() {
  38. return {
  39. isDiff: false,
  40. // 主要配置
  41. defaultOpts: {
  42. lineNumbersMinChars: 0,
  43. lineDecorationsWidth: 0,
  44. glyphMargin: false,
  45. value: "", // 编辑器的值
  46. wordWrap: "on",
  47. theme: "vs-dark", // 编辑器主题:vs, hc-black, or vs-dark,更多选择详见官网
  48. roundedSelection: false, // 右侧不显示编辑器预览框
  49. autoIndent: true, // 自动缩进
  50. quickSuggestions: true, // 快速搜索
  51. enableSplitViewResizing: false,
  52. scrollBeyondLastLine: false,
  53. quickSuggestions: false, //智能提示
  54. renderLineHighlight: false, //选中行外部边框
  55. lineHeight: 22,
  56. renderWhitespace: 'boundary',
  57. },
  58. // 编辑器对象
  59. monacoEditor: {},
  60. oldValue: "123123/n\n",
  61. newValue: "123121311111111",
  62. };
  63. },
  64. watch: {
  65. opts: {
  66. },
  67. monacaValue: {
  68. handler(value) {
  69. if(this.isDiff) return ;
  70. const editor = this.monacoEditor
  71. const output = isBase64(value)
  72. ? Buffer.from(value, "base64").toString("utf8")
  73. : value;
  74. const positions = this.monacoEditor.getPosition()
  75. this.monacoEditor.setValue(output);
  76. this.monacoEditor.setPosition({
  77. lineNumber: positions.lineNumber,
  78. column: positions.column
  79. })
  80. },
  81. deep: true
  82. },
  83. // monacaValue(value) {
  84. // alert(value)
  85. // console.log(isBase64);
  86. // const output = isBase64(value)
  87. // ? Buffer.from(value, "base64").toString("utf8")
  88. // : value;
  89. // this.monacoEditor.setValue(output);
  90. // },
  91. },
  92. beforeDestroy() {
  93. document.removeEventListener("keyup", this.onSaveHandler);
  94. },
  95. mounted() {
  96. document.addEventListener("keyup", this.onSaveHandler, false);
  97. this.init();
  98. },
  99. methods: {
  100. init() {
  101. try {
  102. // 初始化container的内容,销毁之前生成的编辑器
  103. this.$refs.container.innerHTML = "";
  104. // 生成编辑器配置
  105. const editorOptions = Object.assign(this.defaultOpts, this.opts);
  106. if (!this.isDiff) {
  107. // 初始化编辑器实例
  108. this.monacoEditor = monaco.editor.create(
  109. this.$refs.container,
  110. editorOptions
  111. );
  112. this.monacoEditor.onDidChangeModelContent(
  113. event => {
  114. this.$emit("onChange", this.monacoEditor.getValue())
  115. },
  116. );
  117. if (this.monacaValue) {
  118. const output = isBase64(this.monacaValue)
  119. ? Buffer.from(this.monacaValue, "base64").toString("utf8")
  120. : this.monacaValue;
  121. this.monacoEditor.setValue(output);
  122. }
  123. // 编辑器内容发生改变时触发
  124. } else {
  125. editorOptions.readOnly = true;
  126. editorOptions.language = "javascript";
  127. // editorOptions.inlineHints = true;
  128. // 初始化编辑器实例
  129. this.monacoDiffInstance = monaco.editor.createDiffEditor(
  130. this.$refs.container,
  131. editorOptions
  132. );
  133. this.monacoDiffInstance.setModel({
  134. original: monaco.editor.createModel(
  135. this.oldValue,
  136. editorOptions.language
  137. ),
  138. modified: monaco.editor.createModel(
  139. this.newValue,
  140. editorOptions.language
  141. ),
  142. });
  143. }
  144. } catch (error) {
  145. console.log("error-init:",error)
  146. }
  147. },
  148. upDateDiff(val) {
  149. this.monacoDiffInstance.updateOptions({
  150. renderSideBySide: !val,
  151. });
  152. },
  153. onSaveHandler(e) {
  154. if(this.isDiff) return;
  155. if (
  156. (window.navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey) &&
  157. e.keyCode === 83
  158. ) {
  159. e.preventDefault();
  160. this.$emit("handleSave", this.monacoEditor.getValue());
  161. }
  162. this.$emit("handleSave", this.monacoEditor.getValue());
  163. },
  164. // 供父组件调用手动获取值
  165. getVal() {
  166. return this.monacoEditor.getValue();
  167. },
  168. setLanguage(lang) {
  169. setTimeout(() => {
  170. if (monaco.editor.setModelLanguage) {
  171. monaco.editor.setModelLanguage(this.monacoEditor.getModel(), lang)
  172. }
  173. }), 300;
  174. // alert(JSON.stringify(this.monacoEditor.getModel()))
  175. },
  176. updateOptions(opts) {
  177. const editorOptions = Object.assign(this.defaultOpts, opts);
  178. this.monacoEditor.updateOptions(editorOptions);
  179. },
  180. setTheme(theme) {
  181. monaco.editor.setTheme(theme);
  182. },
  183. setDiff(oldContent, newContent) {
  184. this.oldValue = isBase64(oldContent) ? Buffer.from(oldContent, "base64").toString("utf8") : oldContent;
  185. this.newValue = isBase64(newContent) ? Buffer.from(newContent, "base64").toString("utf8") : newContent;
  186. this.isDiff = true;
  187. this.init();
  188. },
  189. setCloseDiff() {
  190. if (this.isDiff) {
  191. this.isDiff = false;
  192. this.init();
  193. }
  194. },
  195. },
  196. };
  197. </script>
  198. <style>
  199. .margin-view-overlays {
  200. background: #f5f5f5;
  201. }
  202. /* .view-lines {
  203. padding-left: 10px;
  204. } */
  205. </style>