From 26af8eab85890beb3c64b952d45735af50373f1d Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Thu, 9 Jan 2025 08:13:17 +0100 Subject: [PATCH] Fix macOS double-space-period suppression on Firefox See https://discuss.codemirror.net/t/dot-being-added-when-pressing-space-repeatedly/3899/13 --- src/domchange.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/domchange.ts b/src/domchange.ts index 7d85246..7d23d95 100644 --- a/src/domchange.ts +++ b/src/domchange.ts @@ -92,6 +92,12 @@ export function applyDOMChange(view: EditorView, domChange: DOMChange): boolean if (!change && domChange.typeOver && !sel.empty && newSel && newSel.main.empty) { // Heuristic to notice typing over a selected character change = {from: sel.from, to: sel.to, insert: view.state.doc.slice(sel.from, sel.to)} + } else if ((browser.mac || browser.android) && change && change.from == change.to && change.from == sel.head - 1 && + /^\. ?$/.test(change.insert.toString()) && view.contentDOM.getAttribute("autocorrect") == "off") { + // Detect insert-period-on-double-space Mac and Android behavior, + // and transform it into a regular space insert. + if (newSel && change.insert.length == 2) newSel = EditorSelection.single(newSel.main.anchor - 1, newSel.main.head - 1) + change = {from: change.from, to: change.to, insert: Text.of([change.insert.toString().replace(".", " ")])} } else if (change && change.from >= sel.from && change.to <= sel.to && (change.from != sel.from || change.to != sel.to) && (sel.to - sel.from) - (change.to - change.from) <= 4) { @@ -102,12 +108,6 @@ export function applyDOMChange(view: EditorView, domChange: DOMChange): boolean from: sel.from, to: sel.to, insert: view.state.doc.slice(sel.from, change.from).append(change.insert).append(view.state.doc.slice(change.to, sel.to)) } - } else if ((browser.mac || browser.android) && change && change.from == change.to && change.from == sel.head - 1 && - /^\. ?$/.test(change.insert.toString()) && view.contentDOM.getAttribute("autocorrect") == "off") { - // Detect insert-period-on-double-space Mac and Android behavior, - // and transform it into a regular space insert. - if (newSel && change.insert.length == 2) newSel = EditorSelection.single(newSel.main.anchor - 1, newSel.main.head - 1) - change = {from: sel.from, to: sel.to, insert: Text.of([" "])} } else if (browser.chrome && change && change.from == change.to && change.from == sel.head && change.insert.toString() == "\n " && view.lineWrapping) { // In Chrome, if you insert a space at the start of a wrapped