Skip to content

Build your personal knowledge base with Trilium Notes

refactor(text): restore the renderer focus beside the hack that clears it

Elian Doran94ce0f1adfd6
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
index 2eb9daeb79..b685c6274f 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
@@ -27,6 +27,7 @@ import noteIcon from '../../icons/note.svg?raw';
import { encodeBlockParameter, parseBlockRange } from '@triliumnext/commons';
import { getAttachmentId, getNoteId } from '../referencelink.js';
import ResizableWidgets, { SIZE_ATTRIBUTES } from '../resizable_widgets/resizable_widgets.js';
+import { restoreRendererFocusOnInput } from '../widget_utils.js';
import './nested_editor_events.js';
export const COMMAND_NAME = 'insertContentEmbed';
@@ -374,12 +375,7 @@ class ContentEmbedEditing extends Plugin {
editor.model.document.registerPostFixer( writer => this.removeTinyCaptions( writer ) );
- // Widget UI sets `_renderer.isFocused` to false on a press. The view ignores its events
- // (`data-cke-ignore-events`), so a press or key that the view sees is back in the text.
- const viewDocument = editor.editing.view.document;
- const restore = () => restoreRendererFocus( editor );
- this.listenTo( viewDocument, 'mousedown', restore, { priority: 'highest' } );
- this.listenTo( viewDocument, 'keydown', restore, { priority: 'highest' } );
+ restoreRendererFocusOnInput( this );
}
/** Keeps a copy of `caption`, for `embed` to show again. */
@@ -1302,13 +1298,6 @@ function preventCKEditorHandling( domElement: HTMLElement, editor: Editor ) {
}
}
-/** Sets `_renderer.isFocused` back to `view.document.isFocused`. */
-function restoreRendererFocus( editor: Editor ) {
- const view = editor.editing.view;
- //@ts-expect-error: We are accessing a private field.
- view._renderer.isFocused = view.document.isFocused;
-}
-
/** Whether `target` is in the content box of an embed, rather than in its title row. */
function isInEmbedContent( target: EventTarget | null ): boolean {
return target instanceof Element && !!target.closest( '.include-note-content' );
diff --git a/packages/ckeditor5/src/plugins/link_embed/link_embed_editing.spec.ts b/packages/ckeditor5/src/plugins/link_embed/link_embed_editing.spec.ts
index 1df1d641ad..f74d4864cf 100644
--- a/packages/ckeditor5/src/plugins/link_embed/link_embed_editing.spec.ts
+++ b/packages/ckeditor5/src/plugins/link_embed/link_embed_editing.spec.ts
@@ -255,4 +255,28 @@ describe("LinkEmbedEditing", () => {
expect(modelPosition.parent.is("element", "paragraph")).toBe(true);
expect(modelPosition.offset).toBe(4);
});
+
+ it("turns the renderer's focus back on for a key in the text after a press on a mention", () => {
+ setModelData(editor.model, "<paragraph>foo[]"
+ + "<linkMention url=\"https://e.com/\" embedType=\"opengraph\"></linkMention>"
+ + "</paragraph>");
+ // A headless page cannot take the real focus, so set the flag the renderer reads.
+ editor.editing.view.document.isFocused = true;
+ const domRoot = editor.editing.view.getDomRoot();
+ const mention = domRoot?.querySelector(".link-mention-inner");
+ if (!domRoot || !mention) {
+ throw new Error("The link mention was not rendered.");
+ }
+
+ mention.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true }));
+ expect(rendererIsFocused()).toBe(false);
+
+ domRoot.dispatchEvent(new KeyboardEvent("keydown", { key: "Backspace" }));
+ expect(rendererIsFocused()).toBe(true);
+ });
+
+ function rendererIsFocused() {
+ //@ts-expect-error: The renderer is a private field.
+ return editor.editing.view._renderer.isFocused as boolean;
+ }
});
diff --git a/packages/ckeditor5/src/plugins/link_embed/link_embed_editing.ts b/packages/ckeditor5/src/plugins/link_embed/link_embed_editing.ts
index 2ba86b656f..359568dfa6 100644
--- a/packages/ckeditor5/src/plugins/link_embed/link_embed_editing.ts
+++ b/packages/ckeditor5/src/plugins/link_embed/link_embed_editing.ts
@@ -1,5 +1,5 @@
import { Plugin, toWidget, viewToModelPositionOutsideModelElement, Widget } from 'ckeditor5';
-import { preventCKEditorHandling } from '../widget_utils.js';
+import { preventCKEditorHandling, restoreRendererFocusOnInput } from '../widget_utils.js';
import {
CHANGE_LINK_DISPLAY_COMMAND,
CHANGE_LINK_PREVIEW_TITLE_COMMAND,
@@ -38,6 +38,8 @@ export default class LinkEmbedEditing extends Plugin {
this.editor.commands.add(CHANGE_LINK_DISPLAY_COMMAND, new ChangeLinkDisplayCommand(this.editor));
this.editor.commands.add(REMOVE_LINK_EMBED_COMMAND, new RemoveLinkEmbedCommand(this.editor));
this.editor.commands.add(CHANGE_LINK_PREVIEW_TITLE_COMMAND, new ChangeLinkPreviewTitleCommand(this.editor));
+
+ restoreRendererFocusOnInput(this);
}
_defineSchema() {
diff --git a/packages/ckeditor5/src/plugins/widget_utils.ts b/packages/ckeditor5/src/plugins/widget_utils.ts
index 0e01f9ee87..24f1fba130 100644
--- a/packages/ckeditor5/src/plugins/widget_utils.ts
+++ b/packages/ckeditor5/src/plugins/widget_utils.ts
@@ -1,4 +1,4 @@
-import type { Editor } from "ckeditor5";
+import type { Editor, Plugin } from "ckeditor5";
/**
* Hack coming from https://github.com/ckeditor/ckeditor5/issues/4465
@@ -25,6 +25,21 @@ export function preventCKEditorHandling(domElement: HTMLElement, editor: Editor)
}
}
+/**
+ * Sets `_renderer.isFocused` back to `view.document.isFocused` on a press or key in the text,
+ * after `preventCKEditorHandling()` turned it off. The view ignores the events of widget UI
+ * (`data-cke-ignore-events`), so a press or key that the view sees is back in the text.
+ */
+export function restoreRendererFocusOnInput(plugin: Plugin) {
+ const view = plugin.editor.editing.view;
+ const restore = () => {
+ //@ts-expect-error: We are accessing a private field.
+ view._renderer.isFocused = view.document.isFocused;
+ };
+ plugin.listenTo(view.document, "mousedown", restore, { priority: "highest" });
+ plugin.listenTo(view.document, "keydown", restore, { priority: "highest" });
+}
+
/**
* Finds the closest widget ancestor of a DOM element and selects it in the
* editor model. This makes the widget toolbar appear when clicking inside