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): suppress stray caret when clicking an include note widget
Elian Doranb7903bab1135
diff --git a/packages/ckeditor5/src/plugins/includenote.spec.ts b/packages/ckeditor5/src/plugins/includenote.spec.tsindex 3bb6a56d36..2089711d03 100644--- a/packages/ckeditor5/src/plugins/includenote.spec.ts+++ b/packages/ckeditor5/src/plugins/includenote.spec.ts@@ -289,6 +289,35 @@ describe("IncludeNote", () => {expect(selected?.name).toBe("includeNote");});+ it("suppresses the native caret on a non-interactive mousedown but not on interactive targets", () => {+ insertIncludeNote(editor, "noteCaret", "small");++ const domRoot = editor.editing.view.getDomRoot();+ const wrapper = domRoot?.querySelector("div.include-note-wrapper");+ expect(wrapper).not.toBeNull();+ if (!wrapper) {+ return;+ }++ // Clicking a plain (non-interactive) area should preventDefault so the browser does not+ // drop a caret next to the contenteditable=false widget.+ const plainEvt = new MouseEvent("mousedown", { bubbles: true, cancelable: true });+ const plainPrevent = vi.spyOn(plainEvt, "preventDefault");+ wrapper.dispatchEvent(plainEvt);+ expect(plainPrevent).toHaveBeenCalled();++ // Clicking a link must keep native behaviour, so preventDefault must NOT be called (the+ // capture-phase handler on the wrapper still sees the event via its child target).+ const innerLink = document.createElement("a");+ innerLink.href = "#root/abc";+ wrapper.appendChild(innerLink);++ const linkEvt = new MouseEvent("mousedown", { bubbles: true, cancelable: true });+ const linkPrevent = vi.spyOn(linkEvt, "preventDefault");+ innerLink.dispatchEvent(linkEvt);+ expect(linkPrevent).not.toHaveBeenCalled();+ });+it("stops propagation on focus and keydown inside the wrapper", () => {insertIncludeNote(editor, "noteKbd", "small");diff --git a/packages/ckeditor5/src/plugins/includenote.ts b/packages/ckeditor5/src/plugins/includenote.tsindex 448dc24429..abfb0493e1 100644--- a/packages/ckeditor5/src/plugins/includenote.ts+++ b/packages/ckeditor5/src/plugins/includenote.ts@@ -262,12 +262,21 @@ function preventCKEditorHandling( domElement: HTMLElement, editor: Editor ) {// commenting out click events to allow link click handler to still work//domElement.addEventListener( 'click', stopEventPropagationAndHackRendererFocus, { capture: true } );- domElement.addEventListener( 'mousedown', ( evt: Event ) => {+ domElement.addEventListener( 'mousedown', ( evt: MouseEvent ) => {evt.stopPropagation();// This prevents rendering changed view selection thus preventing to changing DOM selection while inside a widget.//@ts-expect-error: We are accessing a private field.editor.editing.view._renderer.isFocused = false;+ // Suppress the browser's native caret for clicks on non-interactive areas. The widget's+ // <section> is contenteditable=false inside an editable root, so the default mousedown action+ // drops a caret next to it that visibly moves as the user clicks around. Interactive targets+ // (links, buttons, form controls, embedded editables) keep their native behaviour — `click`+ // and focus still fire because preventing the default of `mousedown` does not cancel them.+ if ( !isInteractiveTarget( evt.target, domElement ) ) {+ evt.preventDefault();+ }+// Select the widget so the toolbar can appearselectIncludeNoteWidget( domElement, editor );}, { capture: true } );@@ -285,6 +294,27 @@ function preventCKEditorHandling( domElement: HTMLElement, editor: Editor ) {}}+/**+ * Whether a mousedown target needs the browser's native handling (focus, caret placement, media+ * controls) to keep working. Used to decide when it is safe to `preventDefault()` on a widget click+ * in order to suppress the stray native caret without breaking interactive embedded content.+ *+ * The match is bounded to within `boundary` (the widget wrapper) so the editable editor root — an+ * ancestor with `contenteditable="true"` — is never mistaken for an interactive target.+ */+function isInteractiveTarget( target: EventTarget | null, boundary: HTMLElement ): boolean {+ if ( !( target instanceof Element ) ) {+ return false;+ }++ const match = target.closest(+ 'a, button, input, textarea, select, label, audio, video, ' ++ '[role="button"], [role="textbox"], [contenteditable]:not([contenteditable="false"])'+ );++ return !!match && boundary.contains( match );+}+function selectIncludeNoteWidget( domElement: HTMLElement, editor: Editor ) {// Find the parent section element (the widget container)const sectionElement = domElement.closest( 'section.include-note' ) as HTMLElement | null;