Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): show the tools of an embed selected before its content rendered

Elian Doran76d2a6c37ca7
diff --git a/apps/client/src/widgets/type_widgets/text/content_embed_tools.spec.tsx b/apps/client/src/widgets/type_widgets/text/content_embed_tools.spec.tsx
index ce755548a3..2c79a28214 100644
--- a/apps/client/src/widgets/type_widgets/text/content_embed_tools.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/content_embed_tools.spec.tsx
@@ -1,9 +1,12 @@
import { render } from "preact";
import { useRef } from "preact/hooks";
import { act } from "preact/test-utils";
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
-import { useContentEmbedDisplayMode } from "./content_embed_tools";
+import {
+ type ContentEmbedToolProvider, registerContentEmbedTools, useContentEmbedDisplayMode,
+ watchContentEmbedTools
+} from "./content_embed_tools";
const containers: HTMLElement[] = [];
@@ -15,6 +18,27 @@ afterEach(() => {
containers.length = 0;
});
+describe("watchContentEmbedTools", () => {
+ it("hears of content that adds its buttons apart, once it joins the container", async () => {
+ const container = document.createElement("div");
+ document.body.append(container);
+ containers.push(container);
+ const callback = vi.fn();
+ const stopWatching = watchContentEmbedTools(container, callback);
+
+ const content = document.createElement("div");
+ const unregister = registerContentEmbedTools(content, {} as ContentEmbedToolProvider);
+ expect(callback).not.toHaveBeenCalled();
+
+ container.append(content);
+ await vi.waitFor(() => expect(callback).toHaveBeenCalledOnce());
+
+ unregister();
+ expect(callback).toHaveBeenCalledTimes(2);
+ stopWatching();
+ });
+});
+
describe("useContentEmbedDisplayMode", () => {
it("follows the mode of the embed, and is null while it has none", async () => {
const figure = document.createElement("figure");
diff --git a/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts b/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts
index f36a2350cc..8ea224dead 100644
--- a/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts
+++ b/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts
@@ -70,8 +70,10 @@ export function registerContentEmbedTools(
provider: ContentEmbedToolProvider
) {
providers.set(element, provider);
- notifyWatchers(element);
+ // Content rendered apart, such as inline source, reaches its watchers once in the page.
+ const stopWaiting = whenInPage(element, () => notifyWatchers(element));
return () => {
+ stopWaiting();
providers.delete(element);
notifyWatchers(element);
};
@@ -184,20 +186,9 @@ function useEmbedState<T>(
update();
};
- let pageObserver: MutationObserver | undefined;
- if (element.isConnected) {
- watch();
- } else {
- pageObserver = new MutationObserver(() => {
- if (!element.isConnected) return;
- pageObserver?.disconnect();
- watch();
- });
- pageObserver.observe(document, { childList: true, subtree: true });
- }
-
+ const stopWaiting = whenInPage(element, watch);
return () => {
- pageObserver?.disconnect();
+ stopWaiting();
attributeObserver?.disconnect();
};
}, [ ref ]);
@@ -229,3 +220,22 @@ export function useContentEmbedEvent(
return () => document.removeEventListener(name, listener);
}, [ ref, name ]);
}
+
+/**
+ * Calls `callback` once `element` is in the page: at once, or when it joins it. Stops waiting
+ * when the returned function is called.
+ */
+function whenInPage(element: HTMLElement, callback: () => void) {
+ if (element.isConnected) {
+ callback();
+ return () => {};
+ }
+
+ const observer = new MutationObserver(() => {
+ if (!element.isConnected) return;
+ observer.disconnect();
+ callback();
+ });
+ observer.observe(document, { childList: true, subtree: true });
+ return () => observer.disconnect();
+}