diff --git a/index.html b/index.html index 504b029..c24740d 100644 --- a/index.html +++ b/index.html @@ -486,6 +486,7 @@ pairs of a multi-run (global) fit."> contenteditable="true" spellcheck="false" oninput="handleMSREditorInput();" + onkeydown="handleMSREditorKeydown(event);" aria-label="MSR editor"> diff --git a/muSRFitlib.js b/muSRFitlib.js index 8057858..c6209dd 100644 --- a/muSRFitlib.js +++ b/muSRFitlib.js @@ -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 + //
element (and execCommand insertText can too), and a
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) { diff --git a/tests/msr-generation.test.js b/tests/msr-generation.test.js index 183de8e..62e84d0 100644 --- a/tests/msr-generation.test.js +++ b/tests/msr-generation.test.js @@ -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);