123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <template>
- <div class="codeEditorAlertBox" v-if="showCurrentTips || showDefaultTips">
- <el-alert
- :title="
- tip ||
- `// 当前输入语言为 ${
- language === 'get' || language === 'post' ? 'javascript' : language
- } , 请注意书写规范。`
- "
- v-if="showCurrentTips"
- />
- <el-alert :title="defaultTips" v-if="showDefaultTips" />
- </div>
- <div id="codeEditBox" :style="`height:${height ? height : '500px'}`"></div>
- </template>
- <script>
- import * as monaco from "monaco-editor";
- import { toRaw } from "vue";
- export default {
- props: {
- height: {
- type: String,
- default: "500px",
- },
- tip: {
- type: String,
- default: () => "",
- },
- code: {
- type: String,
- default: () => "",
- },
- showCurrentTips: {
- type: Boolean,
- default: () => true,
- },
- showDefaultTips: {
- type: Boolean,
- default: () => true,
- },
- language: {
- type: String,
- default: "javascript",
- },
- },
- data() {
- return {
- editorValue: "",
- monacoEditor: null,
- defaultTips:
- "// 每一种类型的事件都携带 2 个内置变量,变量名称为 [e 和 target] ,分别表示 鼠标原生event对象 和 精简后的组件对象(包含事件发生位置在页面内的坐标与触发事件的组件),如若需要,可自行直接使用其名称来进行代码书写。",
- activePen: {},
- };
- },
- mounted() {
- this.editorValue = this.code || "";
- this.init();
- },
- unmounted() {
- this.getEditorValue();
- },
- methods: {
- init() {
-
- const language =
- this.language === "get" || this.language === "post"
- ? "javascript"
- : this.language;
- this.monacoEditor = monaco.editor.create(
- document.getElementById("codeEditBox"),
- {
- theme: "vs-dark",
- value: this.editorValue,
- language: language || "javascript",
- folding: true,
- foldingHighlight: true,
- foldingStrategy: "indentation",
- showFoldingControls: "always",
- disableLayerHinting: true,
- emptySelectionClipboard: false,
- selectionClipboard: false,
- automaticLayout: true,
- codeLens: false,
- scrollBeyondLastLine: false,
- colorDecorators: true,
- accessibilitySupport: "off",
- lineNumbers: "on",
- lineNumbersMinChars: 5,
- enableSplitViewResizing: false,
- readOnly: false,
- }
- );
- this.monacoEditor.onDidBlurEditorText(() =>{
- this.getEditorValue()
- })
-
- },
- getEditorValue() {
- this.$emit("change", toRaw(this.monacoEditor).getValue());
- },
- setEditorValue(newValue) {
- toRaw(this.monacoEditor).setValue(newValue);
- this.getEditorValue();
- },
- },
- watch: {
- code(value) {
- this.editorValue = value;
- toRaw(this.monacoEditor).setValue(this.editorValue);
- },
- },
- };
- </script>
- <style lang="less" scoped>
- #codeEditBox {
- width: 100%;
- }
- .codeEditorAlertBox {
- .el-alert {
- font-family: Consolas, "Courier New", monospace;
- font-weight: normal;
- font-size: 14px;
- font-feature-settings: "liga" 0, "calt" 0;
- line-height: 19px;
- width: 100%;
- background: #1e1e1e;
- color: #608b4e;
- text-indent: 4em;
- user-select: none;
- padding: 0 0 8px 0;
- border-radius: 0;
- }
- .el-alert.danger {
- color: #f25656;
- font-weight: 700;
- user-select: auto;
- }
- .el-alert:first-child {
- padding-top: 8px;
- }
- }
- </style>
- <style lang="less">
- .codeEditorAlertBox {
- .el-alert {
- .el-alert__content {
- width: calc(100% - 16px);
- }
- .el-alert__title {
- width: 98%;
- display: flex;
- }
- }
- }
- </style>
|