Skip to content

Build your personal knowledge base with Trilium Notes

perf(presentation): build the slide editor's styles once

Elian Doranfc4036938264
diff --git a/apps/client/src/widgets/collections/presentation/slide_editor.spec.ts b/apps/client/src/widgets/collections/presentation/slide_editor.spec.ts
index 9773984ac8..9c24307c8f 100644
--- a/apps/client/src/widgets/collections/presentation/slide_editor.spec.ts
+++ b/apps/client/src/widgets/collections/presentation/slide_editor.spec.ts
@@ -1,6 +1,29 @@
import { describe, expect, it } from "vitest";
-import { collectEditorStylesheet } from "./slide_editor";
+import { adoptEditorStyleSheet, collectEditorStylesheet } from "./slide_editor";
+
+describe("adoptEditorStyleSheet", () => {
+ it("shares one stylesheet between edits and removes only its own", () => {
+ const first = document.createElement("div").attachShadow({ mode: "open" });
+ const second = document.createElement("div").attachShadow({ mode: "open" });
+ const other = new CSSStyleSheet();
+ first.adoptedStyleSheets = [ other ];
+
+ const removeFirst = adoptEditorStyleSheet(first);
+ const removeSecond = adoptEditorStyleSheet(second);
+ const [ , sheet ] = first.adoptedStyleSheets;
+ expect(sheet).toBeInstanceOf(CSSStyleSheet);
+ expect(second.adoptedStyleSheets).toHaveLength(1);
+ expect(second.adoptedStyleSheets[0]).toBe(sheet);
+
+ removeFirst();
+ expect(first.adoptedStyleSheets).toHaveLength(1);
+ expect(first.adoptedStyleSheets[0]).toBe(other);
+ expect(second.adoptedStyleSheets[0]).toBe(sheet);
+ removeSecond();
+ expect(second.adoptedStyleSheets).toHaveLength(0);
+ });
+});
describe("collectEditorStylesheet", () => {
it("copies the editor's UI rules and leaves out the content rules", () => {
diff --git a/apps/client/src/widgets/collections/presentation/slide_editor.tsx b/apps/client/src/widgets/collections/presentation/slide_editor.tsx
index 56e3ac87cc..9c4d2c84e8 100644
--- a/apps/client/src/widgets/collections/presentation/slide_editor.tsx
+++ b/apps/client/src/widgets/collections/presentation/slide_editor.tsx
@@ -1,20 +1,28 @@
import { Suspense } from "preact/compat";
-import { useState } from "preact/hooks";
+import { useLayoutEffect, useRef } from "preact/hooks";
import FNote from "../../../entities/fnote";
import TextEmbedEditor from "../../type_widgets/text/TextEmbedEditor";
+let editorStyleSheet: CSSStyleSheet | undefined;
+
/**
* Edits a text slide in place, inside the slide's frame in the shadow root, so that the slide's
* theme styles the text being edited. Escape finishes editing, unless the editor already handled
* it.
*/
export default function SlideEditor({ note, onFinish }: { note: FNote; onFinish(): void }) {
- // This module imports the editor, so its stylesheets are in the document by now.
- const [ editorStylesheet ] = useState(collectEditorStylesheet);
+ const rootRef = useRef<HTMLDivElement>(null);
+
+ // A layout effect, so the editor's styles are in the shadow root before the editor paints.
+ useLayoutEffect(() => {
+ const root = rootRef.current?.getRootNode();
+ if (root instanceof ShadowRoot) return adoptEditorStyleSheet(root);
+ }, []);
return (
<div
+ ref={rootRef}
className="slide-editor-root"
onKeyDown={(e) => {
if (e.key === "Escape" && !e.defaultPrevented) {
@@ -23,7 +31,6 @@ export default function SlideEditor({ note, onFinish }: { note: FNote; onFinish(
}
}}
>
- <style>{editorStylesheet}</style>
<Suspense fallback={null}>
<TextEmbedEditor note={note} hasFixedToolbar={false} onClose={() => {}} />
</Suspense>
@@ -31,6 +38,23 @@ export default function SlideEditor({ note, onFinish }: { note: FNote; onFinish(
);
}
+/**
+ * Adds the editor's styles to `root` and returns the function that removes them. The stylesheet
+ * is built on the first edit and shared by every later one, so only the first edit collects and
+ * parses it. This module imports the editor, so its stylesheets are in the document by then.
+ */
+export function adoptEditorStyleSheet(root: ShadowRoot) {
+ if (!editorStyleSheet) {
+ editorStyleSheet = new CSSStyleSheet();
+ editorStyleSheet.replaceSync(collectEditorStylesheet());
+ }
+ const sheet = editorStyleSheet;
+ root.adoptedStyleSheets = [ ...root.adoptedStyleSheets, sheet ];
+ return () => {
+ root.adoptedStyleSheets = root.adoptedStyleSheets.filter((adopted) => adopted !== sheet);
+ };
+}
+
/**
* Copies the document's rules for the editor UI, which CKEditor mounts in the shadow root along
* with its balloons and toolbars, where the document's stylesheets do not apply. A rule is copied