Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(text): keep formatted words free of spellcheck squiggles (closes #12394)
Elian Doranb43231e1b33d
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.tsindex 3346738323..2332da9af5 100644--- a/packages/ckeditor5/src/plugins.ts+++ b/packages/ckeditor5/src/plugins.ts@@ -14,6 +14,7 @@ import InternalLinkPlugin from "./plugins/internallink.js";import InsertDateTimePlugin from "./plugins/insert_date_time.js";import ReferenceLink from "./plugins/referencelink.js";import NoPoweredByBadge from "./plugins/no_powered_by_badge.js";+import InlineFillerSpellcheck from "./plugins/inline_filler_spellcheck.js";import RemoveFormatLinksPlugin from "./plugins/remove_format_links.js";import IndentBlockShortcutPlugin from "./plugins/indent_block_shortcut.js";import MarkdownImportPlugin from "./plugins/markdownimport.js";@@ -198,7 +199,8 @@ export const CORE_PLUGINS: typeof Plugin[] = [TriliumMentionUI,MentionCustomization,ReferenceLink,- NoPoweredByBadge+ NoPoweredByBadge,+ InlineFillerSpellcheck];/**diff --git a/packages/ckeditor5/src/plugins/inline_filler_spellcheck.spec.ts b/packages/ckeditor5/src/plugins/inline_filler_spellcheck.spec.tsnew file mode 100644index 0000000000..e37ca4a115--- /dev/null+++ b/packages/ckeditor5/src/plugins/inline_filler_spellcheck.spec.ts@@ -0,0 +1,62 @@+import {+ _setModelData as setModelData, _VIEW_INLINE_FILLER as INLINE_FILLER, Bold, ClassicEditor, env,+ Essentials, Paragraph+} from "ckeditor5";+import { describe, expect, it } from "vitest";++import { createTestEditor } from "../../test/editor-kit.js";+import InlineFillerSpellcheck from "./inline_filler_spellcheck.js";++describe("InlineFillerSpellcheck", () => {+ // The renderer checks the filler before it writes the new text, so the filler goes on the+ // render after the one that typed past it.+ it("removes the inline filler once text is typed after it", async () => {+ const editor = await createEditor();++ endBoldRun(editor);+ expect(paragraphText(editor)).toBe(`words${INLINE_FILLER}`);++ editor.execute("insertText", { text: " " });+ expect(paragraphText(editor)).toBe(`words${INLINE_FILLER} `);++ editor.execute("insertText", { text: "here" });+ expect(paragraphText(editor)).toBe("words here");+ expect(editor.getData()).toBe("<p><strong>words</strong> here</p>");+ });++ it("keeps the inline filler while nothing follows it", async () => {+ const editor = await createEditor();++ endBoldRun(editor);+ editor.editing.view.forceRender();+ expect(paragraphText(editor)).toBe(`words${INLINE_FILLER}`);+ });++ it("leaves the renderer's own behavior on Android", async () => {+ const { isAndroid } = env;+ Object.defineProperty(env, "isAndroid", { value: true });+ try {+ const editor = await createEditor();+ //@ts-expect-error: We are accessing a private field.+ const renderer = editor.editing.view._renderer;+ expect(Object.hasOwn(renderer, "_isSelectionInInlineFiller")).toBe(false);+ } finally {+ Object.defineProperty(env, "isAndroid", { value: isAndroid });+ }+ });+});++function createEditor() {+ return createTestEditor([Essentials, Paragraph, Bold, InlineFillerSpellcheck]);+}++/** Puts the caret after a bold word with Bold switched off, where the renderer adds the filler. */+function endBoldRun(editor: ClassicEditor) {+ editor.editing.view.focus();+ setModelData(editor.model, `<paragraph><$text bold="true">words</$text>[]</paragraph>`);+ editor.model.change((writer) => writer.removeSelectionAttribute("bold"));+}++function paragraphText(editor: ClassicEditor) {+ return editor.editing.view.getDomRoot()?.querySelector("p")?.textContent;+}diff --git a/packages/ckeditor5/src/plugins/inline_filler_spellcheck.ts b/packages/ckeditor5/src/plugins/inline_filler_spellcheck.tsnew file mode 100644index 0000000000..78bcca3c34--- /dev/null+++ b/packages/ckeditor5/src/plugins/inline_filler_spellcheck.ts@@ -0,0 +1,43 @@+import { _isInlineViewFiller as isInlineFiller, env, Plugin } from "ckeditor5";++/**+ * Stops the browser spellchecker underlining the word before a formatting boundary.+ *+ * Where the caret ends a formatting run (`**bold**` autoformatted, or Bold toggled off), the+ * renderer inserts `INLINE_FILLER`, seven U+2060 WORD JOINER characters, and keeps it as long as+ * the caret stays in that text node. A word joiner belongs to the word before it, so Chromium+ * checks `bold` and the filler as a single misspelled word, for as long as the rest of the+ * paragraph is being typed.+ *+ * The renderer keeps the filler while `_isSelectionInInlineFiller()` returns `true`. This plugin+ * narrows that check to a text node that holds only the filler, so the filler is removed as soon+ * as text follows it. Desktop browsers skip rendering during an IME composition, so the narrowed+ * check never runs mid-composition there; Android keyboards compose every word, so Android keeps+ * the renderer's own behavior.+ */+export default class InlineFillerSpellcheck extends Plugin {++ static get pluginName() {+ return "InlineFillerSpellcheck" as const;+ }++ init() {+ if (env.isAndroid) {+ return;+ }++ //@ts-expect-error: We are accessing a private field.+ const renderer: FillerRenderer = this.editor.editing.view._renderer;+ const isSelectionInInlineFiller = renderer._isSelectionInInlineFiller.bind(renderer);+ renderer._isSelectionInInlineFiller = () =>+ isSelectionInInlineFiller() && isInlineFiller(renderer._inlineFiller);+ }++}++/** The private members of the editing view's `Renderer` that this plugin reads and replaces. */+interface FillerRenderer {+ /** Never `null` here: `render()` calls `_isSelectionInInlineFiller()` only while it is set. */+ _inlineFiller: Text;+ _isSelectionInInlineFiller(): boolean;+}