Add control methods for monaco editor(setReadonly, undo/redo, center position)

This commit is contained in:
Alexey Koshelev
2025-04-18 13:28:04 +03:00
parent b6d8a8d683
commit 4f7bf29e61
3 changed files with 49 additions and 1 deletions

View File

@ -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");
},

View File

@ -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();
}
}
};

View File

@ -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();