Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/canvas: fix some issues

Adorian Doran96e0cd14680b
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index 2970566ddf..c716dede42 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -1736,6 +1736,12 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t
padding: .75rem 1rem;
}
+/* Keeps the z-indexes of the content, such as those of a drawing's canvases, under the controls
+ of the embed, such as "Exit fullscreen". */
+.include-note-content > .rendered-content {
+ isolation: isolate;
+}
+
.include-note-content > .rendered-content > *:last-child {
margin-bottom: 0;
}
@@ -1769,7 +1775,7 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t
}
.include-note[data-hide-title="true"]:not([data-box-size="tiny"], [data-box-size="expandable"]) {
- > .include-note-wrapper > .include-note-title-row {
+ > .include-note-wrapper > :is(.include-note-title-row, .include-note-title) {
display: none;
}
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
index e4b72b31de..f946759d92 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
@@ -153,6 +153,11 @@
.include-note-content:fullscreen .canvas-drawing {
height: 100%;
+
+ /* Below the toolbar, clear of the "Exit fullscreen" button of the embed. */
+ .Island.App-menu__left {
+ top: calc(var(--lg-button-size) + 1rem);
+ }
}
.canvas-drawing-picture {
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index a0afe5d725..e5578f19e1 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -33,7 +33,7 @@ import { setEditorNoteId } from "../../react/NoteStore";
import { TypeWidgetProps } from "../type_widget";
import AttachmentSaves from "./attachment_saves";
import CKEditorWithWatchdog, { CKEditorApi, NotificationEventData, NotificationEventInfo } from "./CKEditorWithWatchdog";
-import { getContentEmbedTools, watchContentEmbedTools } from "./content_embed_tools";
+import { getContentEmbedTools } from "./content_embed_tools";
import getTemplates, { updateTemplateCache } from "./snippets.js";
import linkEmbedService from "../../../services/link_embed";
import { usesClassicToolbar } from "./toolbar";
@@ -317,14 +317,6 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
useEffect(() => () => stopWatchingEmbedsRef.current?.(), []);
- // The toolbar of an embed reads the buttons of its content when the editor updates, so
- // content that adds them after its embed was selected has the editor update again.
- useEffect(() => {
- const container = containerRef.current;
- if (!container) return;
- return watchContentEmbedTools(container, () => watchdogRef.current?.editor?.ui.update());
- }, []);
-
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
const editor = watchdogRef.current?.editor as CKTextEditor | null | undefined;
if (editor && note) {
@@ -567,7 +559,10 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
if (containerRef.current) {
setupImageOpening(containerRef.current, false);
stopWatchingEmbedsRef.current?.();
- stopWatchingEmbedsRef.current = watchContentEmbeds(containerRef.current);
+ stopWatchingEmbedsRef.current = watchContentEmbeds(
+ containerRef.current,
+ editor
+ );
}
editor.plugins.get("FileUploadEditing")
diff --git a/apps/client/src/widgets/type_widgets/text/utils.spec.ts b/apps/client/src/widgets/type_widgets/text/utils.spec.ts
index bce72029de..e28477798e 100644
--- a/apps/client/src/widgets/type_widgets/text/utils.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/utils.spec.ts
@@ -1,3 +1,4 @@
+import type { CKTextEditor } from "@triliumnext/ckeditor5";
import { attachmentIcon } from "@triliumnext/commons";
import type { VNode } from "preact";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -39,6 +40,7 @@ import { getDownloadAction, getOpenExternallyAction } from "../../../services/at
import content_renderer from "../../../services/content_renderer";
import froca from "../../../services/froca";
import link from "../../../services/link";
+import { registerContentEmbedTools } from "./content_embed_tools";
import ContentEmbed, {
getNoteActions,
type ContentEmbedAction,
@@ -352,6 +354,8 @@ describe("refreshEmbeddedNote", () => {
describe("watchContentEmbeds", () => {
let container: HTMLElement;
+ const update = vi.fn();
+ const editor = { ui: { update } } as unknown as CKTextEditor;
beforeEach(() => {
container = document.createElement("div");
@@ -399,6 +403,29 @@ describe("watchContentEmbeds", () => {
await flush();
expect(disposed()).toHaveLength(2);
});
+
+ it("updates the editor when content in an embed adds buttons to its toolbar", () => {
+ const provider = { getTools: () => [], execute: vi.fn(), subscribe: () => () => {} };
+ const [ inside, outside ] = [ container, document.body ].map((parent) => {
+ const element = document.createElement("div");
+ parent.append(element);
+ return element;
+ });
+ update.mockClear();
+ const stop = watchContentEmbeds(container, editor);
+
+ const unregister = [ registerContentEmbedTools(inside, provider) ];
+ expect(update).toHaveBeenCalledTimes(1);
+ unregister.push(registerContentEmbedTools(outside, provider));
+ stop();
+ unregister.push(registerContentEmbedTools(inside, provider));
+ expect(update).toHaveBeenCalledTimes(1);
+
+ for (const stopRegistration of unregister) {
+ stopRegistration();
+ }
+ outside.remove();
+ });
});
describe("openContentEmbedMenu", () => {
diff --git a/apps/client/src/widgets/type_widgets/text/utils.ts b/apps/client/src/widgets/type_widgets/text/utils.ts
index 3b6df551e6..a4d44880c2 100644
--- a/apps/client/src/widgets/type_widgets/text/utils.ts
+++ b/apps/client/src/widgets/type_widgets/text/utils.ts
@@ -1,3 +1,4 @@
+import type { CKTextEditor } from "@triliumnext/ckeditor5";
import { attachmentIcon } from "@triliumnext/commons";
import { h, type JSX } from "preact";
@@ -8,6 +9,7 @@ import { getEmbedCaption } from "../../../services/content_renderer_text";
import froca from "../../../services/froca";
import link, { ViewScope } from "../../../services/link";
import utils from "../../../services/utils";
+import { watchContentEmbedTools } from "./content_embed_tools";
import ContentEmbed, { getNoteActions, TinyContentEmbed } from "./ContentEmbed";
/**
@@ -192,14 +194,19 @@ export async function getAttachmentHref(attachmentId: string) {
}
/**
- * Unmounts the embed boxes, and what they show, once they leave `container`. The returned
- * function stops watching and unmounts the boxes still in it.
+ * Unmounts the embed boxes, and what they show, once they leave `container`, and updates `editor`,
+ * if any, when their content adds buttons to the toolbar of its embed. The returned function
+ * stops watching and unmounts the boxes still in it.
*/
-export function watchContentEmbeds(container: HTMLElement) {
+export function watchContentEmbeds(container: HTMLElement, editor?: CKTextEditor) {
const observer = new MutationObserver(disposeRemoved);
observer.observe(container, { childList: true, subtree: true });
+ // The toolbar of an embed reads the buttons of its content when the editor updates, so
+ // content that adds them after its embed was selected has the editor update again.
+ const stopWatchingTools = watchContentEmbedTools(container, () => editor?.ui.update());
return () => {
+ stopWatchingTools();
disposeRemoved(observer.takeRecords());
observer.disconnect();
content_renderer.disposeInteractiveContent($(container));
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 42b0a4cdfd..5f9c3c9550 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
@@ -422,6 +422,37 @@ describe("ContentEmbed", () => {
expect(editor.model.document.selection.getSelectedElement()).toBe(embed);
});
+ it("selects the widget when its content takes the focus, which stays there", () => {
+ const embed = insertContentEmbed(editor, "noteFocus", "small");
+ editor.model.change((writer) => {
+ const paragraph = embed.nextSibling;
+ if (!paragraph) {
+ throw new Error("Expected a paragraph after the embed.");
+ }
+ writer.setSelection(paragraph, 0);
+ });
+
+ const wrapper = editor.editing.view.getDomRoot()?.querySelector("div.include-note-wrapper");
+ if (!wrapper) {
+ throw new Error("Expected the wrapper of the embed.");
+ }
+ wrapper.innerHTML = `<div class="include-note-body">`
+ + `<div class="include-note-content" tabindex="-1"><button>Draw</button></div></div>`;
+ const content = wrapper.querySelector<HTMLElement>(".include-note-content");
+ const button = wrapper.querySelector<HTMLElement>("button");
+ const viewFocus = vi.spyOn(editor.editing.view, "focus");
+
+ content?.focus();
+ expect(document.activeElement).toBe(content);
+ expect(editor.model.document.selection.getSelectedElement()).toBe(embed);
+ expect(viewFocus).not.toHaveBeenCalled();
+
+ const change = vi.spyOn(editor.model, "enqueueChange");
+ button?.focus();
+ expect(document.activeElement).toBe(button);
+ expect(change).not.toHaveBeenCalled();
+ });
+
it("leaves a mousedown inside an embedded collection untouched so the live widget keeps working", () => {
insertContentEmbed(editor, "noteColl", "full");
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
index b8f16b79cd..186273410e 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
@@ -1213,7 +1213,19 @@ function preventCKEditorHandling( domElement: HTMLElement, editor: Editor ) {
selectContentEmbedWidget( domElement, editor );
}, { capture: true } );
- domElement.addEventListener( 'focus', stopEventPropagationAndHackRendererFocus, { capture: true } );
+ domElement.addEventListener( 'focus', ( evt: FocusEvent ) => {
+ stopEventPropagationAndHackRendererFocus( evt );
+
+ // Content that takes the focus without a press, as a canvas drawing just added, selects
+ // its widget as a press does, which shows its toolbar. The focus stays in the content.
+ const modelElement = getContentEmbedAt( editor, domElement );
+ const selection = editor.model.document.selection;
+ if ( modelElement && selection.getSelectedElement() !== modelElement ) {
+ editor.model.enqueueChange( { isUndoable: false }, writer => {
+ writer.setSelection( modelElement, 'on' );
+ } );
+ }
+ }, { capture: true } );
// Prevents TAB handling or other editor keys listeners which might be executed on editors selection.
// Listens in the bubble phase, so the embedded content handles its own keys first.