From 8cfa3e89fd4f1197d08579373af0e51a5e7af5b5 Mon Sep 17 00:00:00 2001 From: Arno Gourdol Date: Thu, 13 Jun 2024 16:58:58 -0700 Subject: [PATCH] fix: fixed #2397 --- CHANGELOG.md | 2 ++ src/editor-mathfield/keyboard-input.ts | 34 ++++++++++++----------- src/editor-mathfield/mathfield-private.ts | 6 +--- src/editor-mathfield/mode-editor-math.ts | 8 ++---- src/editor-model/model-private.ts | 3 +- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 389a6005b..f8f4547a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Issues Resolved +- **#2397** When a `beforeinput` event was canceled, the text would still + be inserted when using the physical keyboard. - **#2398** When a placeholder was the only element in a group, i.e. `{\placeholder{}}`, the placeholder was not automatically selected. diff --git a/src/editor-mathfield/keyboard-input.ts b/src/editor-mathfield/keyboard-input.ts index d6b8e3413..ac5b220cc 100644 --- a/src/editor-mathfield/keyboard-input.ts +++ b/src/editor-mathfield/keyboard-input.ts @@ -543,15 +543,14 @@ export function onInput( // If the selection is not collapsed, the content will be deleted first // - if (!model.selectionIsCollapsed) model.deleteAtoms(range(model.selection)); - if (model.mode === 'latex') { model.deferNotifications( { content: true, selection: true, data: text, type: 'insertText' }, () => { removeSuggestion(mathfield); - for (const c of graphemes) ModeEditor.insert(model, c); + for (const c of graphemes) + ModeEditor.insert(model, c, { insertionMode: 'replaceSelection' }); mathfield.snapshot('insert-latex'); @@ -560,7 +559,8 @@ export function onInput( ); } else if (model.mode === 'text') { const style = { ...getSelectionStyle(model), ...mathfield.defaultStyle }; - for (const c of graphemes) ModeEditor.insert(model, c, { style }); + for (const c of graphemes) + ModeEditor.insert(model, c, { style, insertionMode: 'replaceSelection' }); mathfield.snapshot('insert-text'); } else if (model.mode === 'math') for (const c of graphemes) insertMathModeChar(mathfield, c); @@ -629,11 +629,14 @@ function insertMathModeChar(mathfield: _Mathfield, c: string): void { ) { // We are inserting a digit into an empty superscript // If smartSuperscript is on, insert the digit, and exit the superscript. - clearSelection(model); - ModeEditor.insert(model, c, { style }); - mathfield.snapshot(); + if ( + !ModeEditor.insert(model, c, { style, insertionMode: 'replaceSelection' }) + ) { + mathfield.undoManager.pop(); + return; + } + mathfield.snapshot('insert-mord'); moveAfterParent(model); - mathfield.snapshot(); return; } @@ -666,18 +669,17 @@ function insertMathModeChar(mathfield: _Mathfield, c: string): void { else if (input === '\\') input = '\\backslash'; // General purpose character insertion - ModeEditor.insert(model, input, { style }); + if ( + !ModeEditor.insert(model, input, { + style, + insertionMode: 'replaceSelection', + }) + ) + return; mathfield.snapshot(`insert-${model.at(model.position).type}`); } -function clearSelection(model: _Model) { - if (!model.selectionIsCollapsed) { - model.deleteAtoms(range(model.selection)); - model.mathfield.snapshot('delete'); - } -} - function getSelectionStyle(model: _Model): Readonly