Skip to content

Build your personal knowledge base with Trilium Notes

fix(presentation): style the slide editor's toolbars and controls

Elian Doran84115b5e5b4f
diff --git a/apps/client/src/widgets/collections/presentation/slide_editor.spec.ts b/apps/client/src/widgets/collections/presentation/slide_editor.spec.ts
new file mode 100644
index 0000000000..9773984ac8
--- /dev/null
+++ b/apps/client/src/widgets/collections/presentation/slide_editor.spec.ts
@@ -0,0 +1,48 @@
+import { describe, expect, it } from "vitest";
+
+import { collectEditorStylesheet } from "./slide_editor";
+
+describe("collectEditorStylesheet", () => {
+ it("copies the editor's UI rules and leaves out the content rules", () => {
+ const sheet = new CSSStyleSheet();
+ sheet.replaceSync(`
+ .ck.ck-toolbar { color: red; }
+ .ck-editor__editable .ck-code-block__type-around__button svg { width: 10px; }
+ .ck-content h2 { font-size: 3em; }
+ .note-detail-editable-text h3 { font-size: 2em; }
+ :root, :host { --ck-color-text: black; }
+ @media (max-width: 600px) {
+ .ck-balloon-panel { max-width: 90vw; }
+ .ck-content p { margin: 0; }
+ }
+ @media print {
+ .ck-content table { border: 0; }
+ }
+ @keyframes ck-highlight { from { opacity: 0; } }
+ `);
+
+ const css = collectEditorStylesheet([ sheet ]);
+
+ expect(css).toContain(".ck.ck-toolbar");
+ expect(css).toContain(".ck-code-block__type-around__button svg");
+ expect(css).toContain("@media (max-width: 600px)");
+ expect(css).toContain(".ck-balloon-panel");
+ expect(css).toContain("@keyframes ck-highlight");
+ expect(css).not.toContain(".ck-content");
+ expect(css).not.toContain(".note-detail-editable-text");
+ expect(css).not.toContain("--ck-color-text");
+ expect(css).not.toContain("@media print");
+ });
+
+ it("skips a stylesheet whose rules cannot be read", () => {
+ const crossOrigin = {
+ get cssRules(): CSSRuleList {
+ throw new DOMException("cross-origin", "SecurityError");
+ }
+ } as CSSStyleSheet;
+ const sheet = new CSSStyleSheet();
+ sheet.replaceSync(".ck-toolbar { color: red; }");
+
+ expect(collectEditorStylesheet([ crossOrigin, sheet ])).toContain(".ck-toolbar");
+ });
+});
diff --git a/apps/client/src/widgets/collections/presentation/slide_editor.tsx b/apps/client/src/widgets/collections/presentation/slide_editor.tsx
index 696d88f1aa..56e3ac87cc 100644
--- a/apps/client/src/widgets/collections/presentation/slide_editor.tsx
+++ b/apps/client/src/widgets/collections/presentation/slide_editor.tsx
@@ -1,17 +1,17 @@
import { Suspense } from "preact/compat";
-import { useEffect, useState } from "preact/hooks";
+import { useState } from "preact/hooks";
import FNote from "../../../entities/fnote";
import TextEmbedEditor from "../../type_widgets/text/TextEmbedEditor";
/**
* 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. The editor's toolbars open in the document, as for any
- * floating toolbar. Escape finishes editing, unless the editor already handled it.
+ * 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 }) {
- const editorStylesheet = useEditorStylesheet();
- if (!editorStylesheet) return null;
+ // This module imports the editor, so its stylesheets are in the document by now.
+ const [ editorStylesheet ] = useState(collectEditorStylesheet);
return (
<div
@@ -31,16 +31,45 @@ export default function SlideEditor({ note, onFinish }: { note: FNote; onFinish(
);
}
-function useEditorStylesheet() {
- const [ stylesheet, setStylesheet ] = useState<string>();
+/**
+ * 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
+ * when its selector names a CKEditor class other than `.ck-content`, so the rules that style the
+ * content stay out and the slide's theme styles the text. `@keyframes` are copied for the editor's
+ * animations.
+ */
+export function collectEditorStylesheet(styleSheets: Iterable<CSSStyleSheet> = document.styleSheets) {
+ const css: string[] = [];
+ for (const sheet of styleSheets) {
+ let rules: CSSRuleList;
+ try {
+ rules = sheet.cssRules;
+ } catch {
+ // A cross-origin stylesheet does not expose its rules.
+ continue;
+ }
+ collectRules(rules, css);
+ }
+ return css.join("\n");
+}
- useEffect(() => {
- void import("@triliumnext/ckeditor5/src/editor_stylesheet").then(({ default: css }) => {
- // Without `:host`, the editor's tokens are inherited from the document, where
- // ckeditor-theme.css sets Trilium's values for them.
- setStylesheet(css.replaceAll(":root, :host {", ":root {"));
- });
- }, []);
+function collectRules(rules: CSSRuleList, css: string[]) {
+ for (const rule of rules) {
+ if (rule instanceof CSSStyleRule) {
+ if (isEditorSelector(rule.selectorText)) css.push(rule.cssText);
+ } else if (rule instanceof CSSKeyframesRule) {
+ css.push(rule.cssText);
+ } else if (rule instanceof CSSMediaRule || rule instanceof CSSSupportsRule) {
+ const inner: string[] = [];
+ collectRules(rule.cssRules, inner);
+ if (inner.length) {
+ const condition = rule instanceof CSSMediaRule ? `@media ${rule.conditionText}` : `@supports ${rule.conditionText}`;
+ css.push(`${condition} {\n${inner.join("\n")}\n}`);
+ }
+ }
+ }
+}
- return stylesheet;
+function isEditorSelector(selector: string) {
+ return selector.match(/\.ck[\w-]*/g)?.some((className) => className !== ".ck-content") ?? false;
}
diff --git a/packages/ckeditor5/src/editor_stylesheet.ts b/packages/ckeditor5/src/editor_stylesheet.ts
deleted file mode 100644
index 74621f2f96..0000000000
--- a/packages/ckeditor5/src/editor_stylesheet.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/**
- * CKEditor's styles for its UI and editing view, without the content styles, as a string. An
- * editor inside a shadow root needs them in that root, because the global stylesheets stop at it.
- */
-export { default } from "ckeditor5/ckeditor5-editor.css?inline";