From f9cca97f00e4b7827f7056cd7f1644345a4be953 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Thu, 9 Nov 2023 13:02:46 +0000 Subject: [PATCH] Fix firefox newline crash (#5549) --- .changeset/chatty-chefs-sneeze.md | 5 +++++ packages/slate-react/src/plugin/react-editor.ts | 11 +++++++++++ 2 files changed, 16 insertions(+) create mode 100644 .changeset/chatty-chefs-sneeze.md diff --git a/.changeset/chatty-chefs-sneeze.md b/.changeset/chatty-chefs-sneeze.md new file mode 100644 index 0000000000..40478e09ba --- /dev/null +++ b/.changeset/chatty-chefs-sneeze.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Firefox compat: Fix incorrect focus.offset when text node ends with \n diff --git a/packages/slate-react/src/plugin/react-editor.ts b/packages/slate-react/src/plugin/react-editor.ts index 42269103a3..6c4d5e8179 100644 --- a/packages/slate-react/src/plugin/react-editor.ts +++ b/packages/slate-react/src/plugin/react-editor.ts @@ -943,6 +943,17 @@ export const ReactEditor: ReactEditorInterface = { ) } + // COMPAT: Firefox sometimes includes an extra \n (rendered by TextString + // when isTrailing is true) in the focusOffset, resulting in an invalid + // Slate point. (2023/11/01) + if ( + IS_FIREFOX && + focusNode.textContent?.endsWith('\n\n') && + focusOffset === focusNode.textContent.length + ) { + focusOffset-- + } + // COMPAT: Triple-clicking a word in chrome will sometimes place the focus // inside a `contenteditable="false"` DOM node following the word, which // will cause `toSlatePoint` to throw an error. (2023/03/07)