Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): keep the selection drawn after pressing an included note (closes #11650)

Scott Campbell3c3bcd6374e6
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
index 17e21fb60f..f9e1eb76fd 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
@@ -327,6 +327,63 @@ describe("ContentEmbed", () => {
expect(selected?.name).toBe("contentEmbed");
});
+ describe("renderer focus after a press in the widget", () => {
+ /** Presses the embed of a focused editor, which turns `_renderer.isFocused` off. */
+ function pressWidgetOfFocusedEditor() {
+ setModelData(editor.model, "<paragraph>foo[]</paragraph>"
+ + "<contentEmbed noteId=\"noteFocus\" boxSize=\"small\"></contentEmbed>"
+ + "<paragraph>bar</paragraph>");
+ // A headless page cannot take the real focus, so set the flag the renderer reads.
+ editor.editing.view.document.isFocused = true;
+ pressWidget();
+ }
+
+ function pressWidget() {
+ const root = editor.editing.view.getDomRoot();
+ const wrapper = root?.querySelector("div.include-note-wrapper");
+ if (!wrapper) {
+ throw new Error("The embed was not rendered.");
+ }
+ wrapper.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true }));
+ expect(editor.model.document.selection.getSelectedElement()?.name).toBe("contentEmbed");
+ expect(rendererIsFocused()).toBe(false);
+ }
+
+ function rendererIsFocused() {
+ //@ts-expect-error: The renderer is a private field.
+ return editor.editing.view._renderer.isFocused as boolean;
+ }
+
+ it("turns the renderer's focus back on once the selection leaves the widget", () => {
+ pressWidgetOfFocusedEditor();
+
+ editor.model.change((writer) => {
+ const root = editor.model.document.getRoot();
+ if (!root) {
+ throw new Error("The editor has no root.");
+ }
+ writer.setSelection(root.getChild(2), "end");
+ });
+ expect(rendererIsFocused()).toBe(true);
+
+ // The next press turns it off again.
+ pressWidget();
+ });
+
+ it("turns the renderer's focus back on once the selected widget is deleted", () => {
+ pressWidgetOfFocusedEditor();
+
+ editor.execute("delete");
+ expect(getModelData(editor.model)).toBe(
+ "<paragraph>foo</paragraph><paragraph>[]</paragraph><paragraph>bar</paragraph>");
+ expect(rendererIsFocused()).toBe(true);
+ // The DOM selection follows the model selection into the new empty paragraph.
+ const domSelection = window.getSelection();
+ const caretParagraph = editor.editing.view.getDomRoot()?.children[1];
+ expect(caretParagraph?.contains(domSelection?.anchorNode ?? null)).toBe(true);
+ });
+ });
+
it("suppresses the native caret on a non-interactive mousedown but not on interactive targets", () => {
insertContentEmbed(editor, "noteCaret", "small");
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
index 516e17f6db..ffe8062674 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
@@ -373,6 +373,15 @@ class ContentEmbedEditing extends Plugin {
TOGGLE_EDITABLE_COMMAND_NAME, new ToggleContentEmbedEditableCommand( editor ) );
editor.model.document.registerPostFixer( writer => this.removeTinyCaptions( writer ) );
+
+ // `preventCKEditorHandling()` turns `_renderer.isFocused` off for a press in an embed.
+ // Without a reset once the selection leaves it, the DOM selection stops following the model.
+ const selection = editor.model.document.selection;
+ this.listenTo( selection, 'change:range', () => {
+ if ( !isContentEmbed( selection.getSelectedElement() ) ) {
+ restoreRendererFocus( editor );
+ }
+ } );
}
/** Keeps a copy of `caption`, for `embed` to show again. */
@@ -1295,6 +1304,13 @@ function preventCKEditorHandling( domElement: HTMLElement, editor: Editor ) {
}
}
+/** Sets `_renderer.isFocused` back to `view.document.isFocused`, undoing `hackRendererFocus()`. */
+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' );