Remove default ctrl-enter keybinding from monaco editor

Fixes #3093
This commit is contained in:
Nick O'Leary 2021-07-30 11:50:21 +01:00
parent 889d23e9bd
commit 27ed81614b
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
1 changed files with 22 additions and 13 deletions

View File

@ -31,7 +31,7 @@
function setMode(mode, cb) function setMode(mode, cb)
function getRange(); function getRange();
function replace(range, text) function replace(range, text)
function selectAll function selectAll
function clearSelection function clearSelection
function getSelectedText() function getSelectedText()
function destroy() function destroy()
@ -153,9 +153,9 @@ RED.editor.codeEditor.monaco = (function() {
function init(options) { function init(options) {
//Handles orphaned models //Handles orphaned models
//ensure loaded models that are not explicitly destroyed by a call to .destroy() are disposed //ensure loaded models that are not explicitly destroyed by a call to .destroy() are disposed
RED.events.on("editor:close",function() { RED.events.on("editor:close",function() {
let models = window.monaco ? monaco.editor.getModels() : null; let models = window.monaco ? monaco.editor.getModels() : null;
if(models && models.length) { if(models && models.length) {
console.warn("Cleaning up monaco models left behind. Any node that calls createEditor() should call .destroy().") console.warn("Cleaning up monaco models left behind. Any node that calls createEditor() should call .destroy().")
@ -744,10 +744,10 @@ RED.editor.codeEditor.monaco = (function() {
//by default, set javascript editors to text mode. //by default, set javascript editors to text mode.
//when element becomes visible, it will be (re) set to javascript mode //when element becomes visible, it will be (re) set to javascript mode
//this is to ensure multiple editors sharing the model dont present its //this is to ensure multiple editors sharing the model dont present its
//consts & lets to each other //consts & lets to each other
if(editorOptions.language == "javascript") { if(editorOptions.language == "javascript") {
editorOptions._language = editorOptions.language; editorOptions._language = editorOptions.language;
editorOptions.language = "text" editorOptions.language = "text"
} }
@ -921,6 +921,15 @@ RED.editor.codeEditor.monaco = (function() {
/*********** Create the monaco editor ***************/ /*********** Create the monaco editor ***************/
var ed = monaco.editor.create(el, editorOptions); var ed = monaco.editor.create(el, editorOptions);
//Unbind ctrl-Enter (default action is to insert a newline in editor) This permits the shortcut to close the tray.
try {
ed._standaloneKeybindingService.addDynamicKeybinding(
'-editor.action.insertLineAfter', // command ID prefixed by '-'
null, // keybinding
() => {} // need to pass an empty handler
);
} catch (error) { }
ed.nodered = { ed.nodered = {
refreshModuleLibs: refreshModuleLibs //expose this for function node externalModules refresh refreshModuleLibs: refreshModuleLibs //expose this for function node externalModules refresh
} }
@ -967,7 +976,7 @@ RED.editor.codeEditor.monaco = (function() {
if (cb && typeof cb == "function") { if (cb && typeof cb == "function") {
cb(); cb();
} }
if(resize) { if(resize) {
this.resize(); //cause a call to layout() this.resize(); //cause a call to layout()
} }
} }
@ -1218,7 +1227,7 @@ RED.editor.codeEditor.monaco = (function() {
} }
ed._mode = editorOptions.language; ed._mode = editorOptions.language;
//as models are signleton, consts and let are avialable to other javascript instances //as models are signleton, consts and let are avialable to other javascript instances
//so when not focused, set editor mode to text temporarily to avoid multiple defs //so when not focused, set editor mode to text temporarily to avoid multiple defs
if(editorOptions._language) { if(editorOptions._language) {
@ -1240,15 +1249,15 @@ RED.editor.codeEditor.monaco = (function() {
try { try {
var options = { var options = {
root: $(element).closest("div.red-ui-tray-content")[0] || document, root: $(element).closest("div.red-ui-tray-content")[0] || document,
attributes: true, attributes: true,
childList: true, childList: true,
}; };
var observer = new IntersectionObserver(function(entries, observer) { var observer = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) { entries.forEach(function(entry) {
callback(entry.intersectionRatio > 0, 5, entry.target); callback(entry.intersectionRatio > 0, 5, entry.target);
}); });
}, options); }, options);
observer.observe(element); observer.observe(element);
} catch (e1) { } catch (e1) {
//browser not supporting IntersectionObserver? then fall back to polling! //browser not supporting IntersectionObserver? then fall back to polling!
try { try {
@ -1267,7 +1276,7 @@ RED.editor.codeEditor.monaco = (function() {
function onVisibilityChange(visible, delay, element) { function onVisibilityChange(visible, delay, element) {
if(visible) { if(visible) {
if(ed._mode == "javascript" && ed._tempMode == "text") { if(ed._mode == "javascript" && ed._tempMode == "text") {
ed._tempMode = ""; ed._tempMode = "";
setTimeout(function() { setTimeout(function() {
if(element.parentElement) { //ensure el is still in DOM if(element.parentElement) { //ensure el is still in DOM
ed.setMode('javascript', undefined, false); ed.setMode('javascript', undefined, false);
@ -1277,7 +1286,7 @@ RED.editor.codeEditor.monaco = (function() {
} else if(ed._mode == "javascript" && ed._tempMode != "text") { } else if(ed._mode == "javascript" && ed._tempMode != "text") {
if(element.parentElement) { //ensure el is still in DOM if(element.parentElement) { //ensure el is still in DOM
ed.setMode('text', undefined, false); ed.setMode('text', undefined, false);
ed._tempMode = "text"; ed._tempMode = "text";
} }
} }
} }
@ -1353,4 +1362,4 @@ RED.editor.codeEditor.monaco = (function() {
*/ */
create: create create: create
} }
})(); })();