123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- class CodeEditor {
- constructor() { }
- dataID = "";
- set dataType(value) {
- this._dataType = value;
- if (value == 0) {
- this.language = "html";
- } else if (value == 1 || value == 2) {
- this.language = "css";
- } else if (value == 3) {
- this.language = "javascript";
- } else if (value == 4) {
- this.language = "json";
- }
- if (this.editor) {
- let model = this.editor.getModel();
- monaco.editor.setModelLanguage(model, this.language);
- }
- }
- /**
- * 初始化编辑器
- * @param {*} container 创建编辑器的主容器,通常为DIV
- * @param {*} language 编辑器支持的语言,html,css,javascript三选一
- * @param {*} contextmenu 是否开启鼠标右键菜单,默认false
- * @param {*} minimap 是否开启缩略图,默认false
- * @param {*} btnItems 自定义按钮,格式为[{name: "自定义按钮", callback: function(){}}]
- * @param {*} showSupplementCodeList 是否显示补充代码列表,默认false
- */
- InitEditor(
- container,
- contextmenu = true,
- minimap = false,
- btnItems = [],
- showSupplementCodeList = false
- ) {
- let _this = this;
- this.container = container;
- this.showSupplementCodeList = showSupplementCodeList;
- if (!container) {
- throw new Error("codeEditor.InitEditor()必须传入container参数");
- }
- //初始化容器
- let html = ``;
- if (this.showSupplementCodeList) {
- html = `<div id="supplementCodeList" style="overflow: auto;height:calc(50% - 50px);"></div>
- <div style="width:100%;height:50%;">
- <div id="html_editor" style="height:100%;border: 1px solid #d4d4d4;"></div>
- </div>`;
- } else {
- html = `<div style="width:100%;height:100%;">
- <div id="html_editor" style="height:100%;border: 1px solid #d4d4d4;"></div>
- </div>`;
- }
- this.container.innerHTML = html;
- btnItems.forEach((btnItem) => {
- let btn = document.createElement("button");
- btn.classList.add("code-btn");
- container.appendChild(btn);
- btn.innerText = btnItem.name;
- btn.onclick = function (e) {
- btnItem.callback(
- e,
- _this._dataType,
- _this.dataID,
- _this.editor.getValue(),
- _this.oldData
- );
- };
- });
- //#region 初始化编辑器
- this.waitShowCode = "";
- let editorContainer = this.container.querySelector(`#html_editor`);
- require.config({ "vs/nls": { availableLanguages: { "*": "zh-cn" } } });
- require.config({
- paths: { vs: "../../lib/zlExpressEditor/monaco-editor/min/vs" },
- });
- require(["vs/editor/editor.main"], function () {
- _this.editor = monaco.editor.create(editorContainer, {
- language: _this.language ?? "html",
- value: "",
- // automaticLayout: true,
- minimap: {
- enabled: minimap,
- },
- contextmenu: contextmenu,
- });
- let editorModel = _this.editor.getModel();
- editorModel.setValue(_this.waitShowCode);
- _this.editor.getAction("editor.action.formatDocument").run();
- });
- }
- /**
- * 设置编辑器代码
- * @param {*} code 代码
- */
- SetCode(code) {
- if (this.editor) {
- let editorModel = this.editor.getModel();
- editorModel.setValue(code ?? "");
- // this.editor.getAction("editor.action.formatDocument").run();
- } else {
- this.waitShowCode = code ?? "";
- }
- }
- /**获取代码 */
- GetCode() {
- return this.editor.getValue();
- }
- #decorationsIds = [];
- CreateHighlight({ startLine, startColumn, endLine, endColumn }) {
- // 创建装饰
- const decorations = [
- {
- range: new monaco.Range(startLine, startColumn, endLine, endColumn),
- options: {
- isWholeLine: false,
- className: "monaco-highlight",
- },
- },
- ];
- // 添加装饰并保存 ID
- let decorationsIds = this.editor.deltaDecorations([], decorations);
- this.#decorationsIds.push(...decorationsIds);
- // 确保装饰的范围滚动到视图中
- const rangeToReveal = new monaco.Range(startLine, startColumn, endLine, endColumn);
- this.editor.revealRangeInCenterIfOutsideViewport(rangeToReveal);
- // 返回取消装饰的回调函数
- return () => {
- this.#decorationsIds = this.#decorationsIds.filter(id => !decorationsIds.includes(id));
- this.editor.deltaDecorations(decorationsIds, []); // 取消装饰
- };
- }
- RemoveAllHighlight() {
- this.editor.deltaDecorations(this.#decorationsIds, []);
- }
- }
- window.CodeEditor = CodeEditor;
|