|
| 1 | +<script lang="ts" setup> |
| 2 | +import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker" |
| 3 | +import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker" |
| 4 | +import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker" |
| 5 | +import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker" |
| 6 | +import EditorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker" |
| 7 | +import * as monaco from "monaco-editor" |
| 8 | +import { nextTick, ref, onBeforeUnmount } from "vue" |
| 9 | +import { useRoute } from "vue-router" |
| 10 | +import { ElMessage } from "element-plus" |
| 11 | +
|
| 12 | +const text = ref("234") |
| 13 | +const route = useRoute() |
| 14 | +const language = ref("go") |
| 15 | +const msg = ref() |
| 16 | +const loading = ref(false) |
| 17 | +// |
| 18 | +// MonacoEditor start |
| 19 | +// |
| 20 | +onBeforeUnmount(() => { |
| 21 | + editor.dispose() |
| 22 | +}) |
| 23 | +// @ts-ignore |
| 24 | +self.MonacoEnvironment = { |
| 25 | + getWorker(_: string, label: string) { |
| 26 | + if (label === "json") { |
| 27 | + return new jsonWorker() |
| 28 | + } |
| 29 | + if (label === "css" || label === "scss" || label === "less") { |
| 30 | + return new cssWorker() |
| 31 | + } |
| 32 | + if (label === "html" || label === "handlebars" || label === "razor") { |
| 33 | + return new htmlWorker() |
| 34 | + } |
| 35 | + if (["typescript", "javascript"].includes(label)) { |
| 36 | + return new tsWorker() |
| 37 | + } |
| 38 | + return new EditorWorker() |
| 39 | + }, |
| 40 | +} |
| 41 | +let editor: monaco.editor.IStandaloneCodeEditor |
| 42 | +
|
| 43 | +const editorInit = () => { |
| 44 | + nextTick(() => { |
| 45 | + monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({ |
| 46 | + noSemanticValidation: true, |
| 47 | + noSyntaxValidation: false, |
| 48 | + }) |
| 49 | + monaco.languages.typescript.javascriptDefaults.setCompilerOptions({ |
| 50 | + target: monaco.languages.typescript.ScriptTarget.ES2016, |
| 51 | + allowNonTsExtensions: true, |
| 52 | + }) |
| 53 | +
|
| 54 | + !editor |
| 55 | + ? (editor = monaco.editor.create(document.getElementById("codeEditBox") as HTMLElement, { |
| 56 | + value: text.value, // 编辑器初始显示文字 |
| 57 | + language: "go", // 语言支持自行查阅demo |
| 58 | + automaticLayout: true, // 自适应布局 |
| 59 | + theme: "vs-dark", // 官方自带三种主题vs, hc-black, or vs-dark |
| 60 | + foldingStrategy: "indentation", |
| 61 | + renderLineHighlight: "all", // 行亮 |
| 62 | + selectOnLineNumbers: true, // 显示行号 |
| 63 | + minimap: { |
| 64 | + enabled: false, |
| 65 | + }, |
| 66 | + readOnly: false, // 只读 |
| 67 | + fontSize: 16, // 字体大小 |
| 68 | + scrollBeyondLastLine: false, // 取消代码后面一大段空白 |
| 69 | + overviewRulerBorder: false, // 不要滚动条的边框 |
| 70 | + })) |
| 71 | + : editor.setValue("") |
| 72 | + // console.log(editor) |
| 73 | + // 监听值的变化 |
| 74 | + editor.onDidChangeModelContent((val: any) => { |
| 75 | + text.value = editor.getValue() |
| 76 | + }) |
| 77 | + }) |
| 78 | +} |
| 79 | +editorInit() |
| 80 | +// @ts-ignore |
| 81 | +//切换语言 |
| 82 | +const changeLanguage = () => { |
| 83 | + monaco.editor.setModelLanguage(editor.getModel(), language.value) |
| 84 | +
|
| 85 | + // editor.updateOptions({ |
| 86 | + // language: "objective-c" |
| 87 | + // }); |
| 88 | +} |
| 89 | +//设置一个确认按钮,点击时调用接口 |
| 90 | +const submitCode = () => { |
| 91 | + loading.value = true |
| 92 | + //调用接口 |
| 93 | + // api.submitCode(text.value, route.query.identity).then((res) => { |
| 94 | + // loading.value = false |
| 95 | + // if (res.data.code == 200) { |
| 96 | + // msg.value = res.data.data.msg |
| 97 | +
|
| 98 | + // if (res.data.data.status == 1) { |
| 99 | + // ElMessage.success(res.data.data.msg) |
| 100 | + // } else { |
| 101 | + // ElMessage.warning(res.data.data.msg) |
| 102 | + // } |
| 103 | + // } else { |
| 104 | + // ElMessage.error(res.data.msg) |
| 105 | + // } |
| 106 | + // }) |
| 107 | +} |
| 108 | +/*** |
| 109 | + * editor.setValue(newValue) |
| 110 | +
|
| 111 | +editor.getValue() |
| 112 | +
|
| 113 | +editor.onDidChangeModelContent((val)=>{ //监听值的变化 }) |
| 114 | +
|
| 115 | +editor.getAction('editor.action.formatDocument').run() //格式化代码 |
| 116 | +
|
| 117 | +editor.dispose() //销毁实例 |
| 118 | +
|
| 119 | +editor.onDidChangeOptions //配置改变事件 |
| 120 | +
|
| 121 | +editor.onDidChangeLanguage //语言改变事件 |
| 122 | + */ |
| 123 | +</script> |
| 124 | +<template> |
| 125 | + <div id="codeEditBox"></div> |
| 126 | +</template> |
| 127 | +<style scoped> |
| 128 | +#codeEditBox { |
| 129 | + height: 100%; |
| 130 | +} |
| 131 | +</style> |
0 commit comments