From 4f7bf29e6119ee074e2faf095bea47201e6effcf Mon Sep 17 00:00:00 2001 From: Alexey Koshelev Date: Fri, 18 Apr 2025 13:28:04 +0300 Subject: [PATCH] Add control methods for monaco editor(setReadonly, undo/redo, center position) --- .../common/main/lib/component/MonacoEditor.js | 24 +++++++++++++++++++ vendor/monaco/MonacoEditorCode.js | 9 ++++++- vendor/monaco/monaco.js | 17 +++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/apps/common/main/lib/component/MonacoEditor.js b/apps/common/main/lib/component/MonacoEditor.js index c971d559e0..bcecbc2304 100644 --- a/apps/common/main/lib/component/MonacoEditor.js +++ b/apps/common/main/lib/component/MonacoEditor.js @@ -155,6 +155,30 @@ define([], function () { }); }, + revealPositionInCenter: function() { + this._postMessage(this.iframe.contentWindow, { + command: 'revealPositionInCenter', + referer: 'monaco-editor', + data: {} + }); + }, + + undo: function() { + this._postMessage(this.iframe.contentWindow, { + command: 'undo', + referer: 'monaco-editor', + data: {} + }); + }, + + redo: function() { + this._postMessage(this.iframe.contentWindow, { + command: 'redo', + referer: 'monaco-editor', + data: {} + }); + }, + enablePointerEvents: function(enable) { this.iframe && (this.iframe.style.pointerEvents = enable ? "" : "none"); }, diff --git a/vendor/monaco/MonacoEditorCode.js b/vendor/monaco/MonacoEditorCode.js index 3065ced39b..39d422e70e 100644 --- a/vendor/monaco/MonacoEditorCode.js +++ b/vendor/monaco/MonacoEditorCode.js @@ -68,6 +68,7 @@ var _postMessage = function(msg) { window.isDisable = true; codeEditor.setValue(data.value || ''); codeEditor.setPosition({lineNumber: data.currentPos ? data.currentPos.row : 0, column: data.currentPos ? data.currentPos.column : 0}); + codeEditor.setReadonly(!!data.readonly); codeEditor.setFocus(); window.isDisable = false; }; @@ -129,7 +130,13 @@ var _postMessage = function(msg) { onThemeChanged(cmd.data); }else if (cmd.command==='disableDrop') { editorDisableDrop(cmd.data); - } + } else if(cmd.command==='revealPositionInCenter') { + codeEditor.revealPositionInCenter(); + } else if(cmd.command==='undo') { + codeEditor.undo(); + } else if(cmd.command==='redo') { + codeEditor.redo(); + } } }; diff --git a/vendor/monaco/monaco.js b/vendor/monaco/monaco.js index a4e618a0e0..c6f95e0b12 100644 --- a/vendor/monaco/monaco.js +++ b/vendor/monaco/monaco.js @@ -53,6 +53,7 @@ language: "javascript", theme: this.theme ? this.theme : "vs-light", automaticLayout: true, + domReadOnly: true, suggest: { preview: false, showInlineDetails: true @@ -146,6 +147,22 @@ this.editor && this.editor.focus(); }; + MonacoEditor.prototype.setReadonly = function(value) { + this.editor && this.editor.updateOptions({readOnly: value}); + }; + + MonacoEditor.prototype.revealPositionInCenter = function() { + this.editor && this.editor.revealPositionInCenter(this.editor.getPosition()); + }; + + MonacoEditor.prototype.undo = function() { + this.editor && this.editor.trigger("codeEditor", "undo"); + }; + + MonacoEditor.prototype.redo = function() { + this.editor && this.editor.trigger("codeEditor", "redo"); + }; + MonacoEditor.prototype._loadEvents = function() { while (this.events.length>0) { let event = this.events.shift();