Fix Enter in MSR editor to insert a text newline

This commit is contained in:
salman committed 2026-09-23 16:01:28 +02:00
1 parent efd8477c66
commit 8d27a61377
3 files changed
+137

No files matched your search

+1
View File
@@ -486,6 +486,7 @@ pairs of a multi-run (global) fit.">
contenteditable="true"
spellcheck="false"
oninput="handleMSREditorInput();"
onkeydown="handleMSREditorKeydown(event);"
aria-label="MSR editor"></pre>
</div>
</div>
+44
View File
@@ -3652,6 +3652,50 @@ function setMSREditorDirty(isDirty, source = window.MSREditor.activeSource) {
syncMSREditorLegacyState();
}
function handleMSREditorKeydown(event) {
// The editor is a contenteditable pre. Its default Enter handling inserts a
// <br> element (and execCommand insertText can too), and a <br> contributes
// no text to textContent: the highlight re-render on the following input
// event would read the content back without the line break and drop it.
// Intercept Enter, insert a literal "\n" text node at the caret instead, and
// dispatch the input event so highlighting and dirty/ownership handling run
// as for any other edit.
if (!event || event.key !== "Enter") {
return;
}
let editor = document.getElementById("msrEditorText");
if (!editor || !window.getSelection) {
return;
}
let selection = window.getSelection();
if (!selection || selection.rangeCount === 0) {
return;
}
let range = selection.getRangeAt(0);
if (!editor.contains(range.commonAncestorContainer)) {
return;
}
event.preventDefault();
range.deleteContents();
let textNode = document.createTextNode("\n");
range.insertNode(textNode);
range.setStartAfter(textNode);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
let inputEvent = null;
if (document.createEvent) {
inputEvent = document.createEvent("Event");
inputEvent.initEvent("input", true, false);
} else if (typeof Event === "function") {
inputEvent = new Event("input", { bubbles: true });
}
if (inputEvent) {
editor.dispatchEvent(inputEvent);
}
}
function handleMSREditorInput() {
highlightMSREditor();
if (window.MSREditor.suppressInput) {
+92
View File
@@ -2727,6 +2727,97 @@ function testDirtyEditorOverwriteGuard(lib) {
assert.strictEqual(lib.confirmOverwriteDirtyEditor("Plot"), true);
}
function testMSREditorEnterInsertsTextNewline(lib) {
let insertedNode = null;
let inputDispatched = null;
const editor = {
contains: function(node) {
return node === rangeContainer;
},
dispatchEvent: function(event) {
inputDispatched = event;
}
};
const rangeContainer = {};
const textNode = { nodeType: 3 };
const range = {
commonAncestorContainer: rangeContainer,
deleteContents: function() {
},
insertNode: function(node) {
insertedNode = node;
},
setStartAfter: function() {
},
collapse: function() {
}
};
const selection = {
rangeCount: 1,
getRangeAt: function() {
return range;
},
removeAllRanges: function() {
},
addRange: function() {
}
};
let prevented = false;
const previousWindowSelection = lib.window.getSelection;
const previousDocument = lib.document;
lib.document = {
getElementById: function(id) {
return id === "msrEditorText" ? editor : null;
},
createTextNode: function(data) {
return { nodeType: 3, data: data };
},
createEvent: function() {
const event = { type: "", initEvent: function(type) { this.type = type; } };
return event;
}
};
lib.window.getSelection = function() {
return selection;
};
lib.handleMSREditorKeydown({
key: "Enter",
preventDefault: function() {
prevented = true;
}
});
assert.strictEqual(prevented, true);
assert.strictEqual(insertedNode.data, "\n");
assert.strictEqual(inputDispatched.type, "input");
// Non-Enter keys are left to the browser default (no preventDefault).
prevented = false;
insertedNode = null;
lib.handleMSREditorKeydown({
key: "a",
preventDefault: function() {
prevented = true;
}
});
assert.strictEqual(prevented, false);
assert.strictEqual(insertedNode, null);
// Enter outside the editor (no selection inside it) leaves the default.
prevented = false;
selection.rangeCount = 0;
lib.handleMSREditorKeydown({
key: "Enter",
preventDefault: function() {
prevented = true;
}
});
assert.strictEqual(prevented, false);
lib.window.getSelection = previousWindowSelection;
lib.document = previousDocument;
}
function testLoadMSRIntoEditorClearsDirtyAndUpdatesTable(lib) {
resetMSREditorDocuments(lib);
const elements = {
@@ -3793,6 +3884,7 @@ testT0BgUseCommonIgnoresStaleRowBackground(lib);
testApplyMSRParametersUpdatesParameterTable(lib);
testSyncSeriesParametersFromCombinedMSR(lib);
testDirtyEditorOverwriteGuard(lib);
testMSREditorEnterInsertsTextNewline(lib);
testLoadMSRIntoEditorClearsDirtyAndUpdatesTable(lib);
testMSREditorSourceTabsKeepInactiveEdits(lib);
testManualEditorInputLocksEditorSource(lib);