Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/drawing canvas: fix an issue

Adorian Doran071056aedbf3
diff --git a/apps/client/src/widgets/type_widgets/canvas/excalidraw_contract.spec.ts b/apps/client/src/widgets/type_widgets/canvas/excalidraw_contract.spec.ts
index ef5a28aca4..c255fc8546 100644
--- a/apps/client/src/widgets/type_widgets/canvas/excalidraw_contract.spec.ts
+++ b/apps/client/src/widgets/type_widgets/canvas/excalidraw_contract.spec.ts
@@ -105,6 +105,12 @@ describe("Excalidraw contract of the canvas drawing", () => {
if (renderConfig.exportWithDarkMode && fileData.mimeType !== MIME_TYPES.svg) {
use.setAttribute("filter", IMAGE_INVERT_FILTER);
}`));
+ // `saveDrawingPicture()` finds the image that each `<use>` places through its `<symbol>`.
+ expect(source).toContain(compact(`
+ symbol.id = symbolId;
+ const image = svgRoot.ownerDocument.createElementNS(SVG_NS, "image");
+ image.setAttribute("href", fileData.dataURL);`));
+ expect(source).toContain(compact("symbol.appendChild(image);"));
});
it("opens the library and the search in the sidebar that the menus name", () => {
diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx
index c517855580..056f605ed6 100644
--- a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx
@@ -528,7 +528,8 @@ describe("saveDrawingPicture", () => {
it("saves the picture of a drawing, titled after it, in the colors of its page", async () => {
const post = vi.spyOn(server, "post").mockResolvedValue(undefined);
- vi.mocked(exportToSvg).mockResolvedValueOnce(svgOf("picture"));
+ const exported = pictureWithImages();
+ vi.mocked(exportToSvg).mockResolvedValueOnce(exported);
const files = { f1: IMAGE };
const content = JSON.stringify({
elements: [ image, { id: "gone", type: "line", isDeleted: true } ],
@@ -547,13 +548,15 @@ describe("saveDrawingPicture", () => {
expect(post).toHaveBeenCalledExactlyOnceWith("notes/n1/attachments?matchBy=title", {
role: "image", mime: "image/svg+xml", title: "d1.svg", content: expect.any(String)
});
- // Dark while its page is dark, as Excalidraw's dark export: pictures in it keep their colors.
+ // Dark while its page is dark, as Excalidraw's dark export: bitmaps in it keep their colors.
const [ picture ] = getSavedPictures(post);
expect(picture).toMatch(/^<svg id="picture" class="trilium-drawing-picture"><style>/);
expect(picture).toContain("@media (prefers-color-scheme: dark) { "
+ "svg.trilium-drawing-picture { filter: invert(93%) hue-rotate(180deg); } "
- + `svg.trilium-drawing-picture image:not([href^="data:image/svg+xml"]) { `
- + "filter: invert(100%) hue-rotate(180deg) saturate(1.25); } }</style></svg>");
+ + "svg.trilium-drawing-picture use.trilium-drawing-bitmap { "
+ + "filter: invert(100%) hue-rotate(180deg) saturate(1.25); } }</style>");
+ expect([ ...exported.querySelectorAll("use") ].map((use) => use.getAttribute("class")))
+ .toEqual([ "trilium-drawing-bitmap", null, "trilium-drawing-bitmap" ]);
// An empty drawing shows nothing.
await saveDrawingPicture(drawing, JSON.stringify({ elements: [] }));
@@ -613,3 +616,31 @@ function svgOf(id: string) {
svg.id = id;
return svg;
}
+
+/**
+ * A picture as `exportToSvg()` draws images: each in a `<symbol>`, placed by `<use>`. The first
+ * image is a bitmap, used twice, and the second an SVG image.
+ */
+function pictureWithImages() {
+ const svg = svgOf("picture");
+ const create = (name: string, attributes: Record<string, string>) => {
+ const element = document.createElementNS("http://www.w3.org/2000/svg", name);
+ for (const [ key, value ] of Object.entries(attributes)) {
+ element.setAttribute(key, value);
+ }
+ return element;
+ };
+ const symbols = [
+ [ "image-f1", "data:image/png;base64,AA==" ],
+ [ "image-f2", "data:image/svg+xml;base64,AA==" ]
+ ];
+ for (const [ id, href ] of symbols) {
+ const symbol = create("symbol", { id });
+ symbol.append(create("image", { href }));
+ svg.append(symbol);
+ }
+ for (const id of [ "image-f1", "image-f2", "image-f1" ]) {
+ svg.append(create("use", { href: `#${id}` }));
+ }
+ return svg;
+}
diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.ts b/apps/client/src/widgets/type_widgets/canvas/persistence.ts
index bf41718000..9653896c9b 100644
--- a/apps/client/src/widgets/type_widgets/canvas/persistence.ts
+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.ts
@@ -522,17 +522,20 @@ export const IMAGE_DARK_FILTER = "invert(100%) hue-rotate(180deg) saturate(1.25)
/** The class of a saved picture, which scopes its own style. */
const SAVED_PICTURE_CLASS = "trilium-drawing-picture";
+/** The class of a `<use>` element that places a bitmap in a saved picture. */
+const BITMAP_CLASS = "trilium-drawing-bitmap";
/**
* Draws a saved picture in the dark colors of Excalidraw while the page that shows it has a dark
- * color scheme, with the filters of Excalidraw's dark export: the root inverted, the pictures in
- * it, except SVG ones, inverted back.
+ * color scheme, with the filters of Excalidraw's dark export: the root inverted, the bitmaps in it
+ * inverted back. The filter of a bitmap is on its `<use>` element, as Excalidraw sets it: a filter
+ * on the `<image>` in the `<symbol>` does not apply.
*/
const SAVED_PICTURE_STYLE = `@media (prefers-color-scheme: dark) {
svg.${SAVED_PICTURE_CLASS} {
filter: ${DRAWING_DARK_FILTER};
}
- svg.${SAVED_PICTURE_CLASS} image:not([href^="data:image/svg+xml"]) {
+ svg.${SAVED_PICTURE_CLASS} use.${BITMAP_CLASS} {
filter: ${IMAGE_DARK_FILTER};
}
}`;
@@ -567,6 +570,7 @@ export async function saveDrawingPicture(
style.textContent = SAVED_PICTURE_STYLE;
picture.classList.add(SAVED_PICTURE_CLASS);
picture.prepend(style);
+ markBitmaps(picture);
}
await server.post(`notes/${ownerId}/attachments?matchBy=title`, {
role: IMAGE_ROLE,
@@ -583,6 +587,25 @@ export async function saveDrawingPicture(
}
}
+/**
+ * Adds `BITMAP_CLASS` to the `<use>` elements of `picture` that place a bitmap. Excalidraw puts each
+ * image in a `<symbol>`; an SVG image keeps its colors in the dark export.
+ */
+function markBitmaps(picture: SVGSVGElement) {
+ const bitmapHrefs = new Set<string>();
+ for (const image of picture.querySelectorAll("symbol > image")) {
+ const symbol = image.parentElement;
+ if (symbol && !image.getAttribute("href")?.startsWith("data:image/svg+xml")) {
+ bitmapHrefs.add(`#${symbol.id}`);
+ }
+ }
+ for (const use of picture.querySelectorAll("use")) {
+ if (bitmapHrefs.has(use.getAttribute("href") ?? "")) {
+ use.classList.add(BITMAP_CLASS);
+ }
+ }
+}
+
/**
* The content of a canvas drawing. Unlike a canvas note, a drawing keeps its images inline, except
* the ones it refers to by file ID.