function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } /* Analyzed bindings: { "modelValue": "props", "theme": "props", "language": "props", "codeTips": "props", "ref": "setup-const", "onMounted": "setup-const", "watchEffect": "setup-const", "watch": "setup-const", "monaco": "setup-const", "props": "setup-reactive-const", "emit": "setup-const", "dom": "setup-ref", "codeTipsMap": "setup-ref", "instance": "setup-let", "editorFormat": "setup-const", "insert": "setup-const" } */ import { openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"; import { ref, onMounted, watchEffect, watch } from 'vue'; import * as monaco from 'monaco-editor'; const __sfc_main__ = { props: { modelValue: [String, Number], theme: { type: String, default: 'vs-dark' }, language: { type: String, default: 'json' }, codeTips: { type: Array, default: () => [] } }, emits: ['update:modelValue'], setup(__props, { expose, emit }) { const props = __props; const dom = ref(); const codeTipsMap = ref(new Map()); let instance; // codeTipItem.dispose() // 销毁自定义提示 onMounted(() => { const _model = monaco.editor.createModel(props.modelValue, props.language); instance = monaco.editor.create(dom.value, { model: _model, tabSize: 2, automaticLayout: true, scrollBeyondLastLine: false, theme: props.theme, formatOnPaste: true, }); instance.onDidChangeModelContent(() => { const value = instance.getValue(); emit('update:modelValue', value); }); }); /** * 代码格式化 */ const editorFormat = () => { if (!instance) return; _optionalChain([instance, 'access', _ => _.getAction, 'call', _2 => _2('editor.action.formatDocument'), 'optionalAccess', _3 => _3.run, 'call', _4 => _4()]); }; watchEffect(() => { setTimeout(() => { editorFormat(); }, 300); }); /** * 光标位置插入内容 * @param {String} val */ const insert = (val) => { if (!instance) return; const position = instance.getPosition(); instance.executeEdits(instance.getValue(), [ { range: new monaco.Range(_optionalChain([position, 'optionalAccess', _5 => _5.lineNumber]), _optionalChain([position, 'optionalAccess', _6 => _6.column]), _optionalChain([position, 'optionalAccess', _7 => _7.lineNumber]), _optionalChain([position, 'optionalAccess', _8 => _8.column])), text: val, }, ]); }; watch(() => props.modelValue, (val) => { if (!instance) return; // setValue之前获取光标位置 const position = instance.getPosition(); // setValue之后光标位置改变 instance.setValue(val); // 设置光标位置为setValue之前的位置 instance.setPosition(position); }); expose({ editorFormat, insert, }); return (_ctx, _cache) => { return (_openBlock(), _createElementBlock("div", { ref_key: "dom", ref: dom, class: "editor" }, null, 512 /* NEED_PATCH */)); }; } }; export default __sfc_main__;