Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(clipper): show the saved note's icon in the popup preview
Elian Doranbf0f5f5abdff
diff --git a/apps/web-clipper/entrypoints/popup/main.tsx b/apps/web-clipper/entrypoints/popup/main.tsxindex 46bd4bd711..ae59c5876c 100644--- a/apps/web-clipper/entrypoints/popup/main.tsx+++ b/apps/web-clipper/entrypoints/popup/main.tsx@@ -13,6 +13,7 @@ import HelpCircle from "@boxicons/js/icons/HelpCircle";import Highlight from "@boxicons/js/icons/Highlight";import History from "@boxicons/js/icons/History";import Lock from "@boxicons/js/icons/Lock";+import Paperclip from "@boxicons/js/icons/Paperclip";import RefreshCw from "@boxicons/js/icons/RefreshCw";import Screenshot from "@boxicons/js/icons/Screenshot";import Tabs from "@boxicons/js/icons/Tabs";@@ -127,6 +128,8 @@ const PREVIEW_STYLE = ``;const MODE_LABELS: Record<ClipMode, string> = { selection: "Selection", page: "Page", note: "Bookmark" };+/** The icon of the note each mode saves, as Trilium draws it from the note's `#clipType`. */+const MODE_ICONS: Record<ClipMode, IconDefinition> = { selection: Paperclip, page: Globe, note: Bookmark };const VARIANT_LABELS: Record<PageVariant, string> = { article: "Article", wholePage: "Whole page" };@@ -433,7 +436,7 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {<div className="page-preview">{mode && (<div className="page-heading">- <span className="page-icon"><Icon icon={Globe} /></span>+ <span className="page-icon"><Icon icon={MODE_ICONS[mode]} /></span><div className="page-heading-text"><inputtype="text"diff --git a/apps/web-clipper/entrypoints/popup/popup.spec.tsx b/apps/web-clipper/entrypoints/popup/popup.spec.tsxindex 5a82e59e03..17075a1237 100644--- a/apps/web-clipper/entrypoints/popup/popup.spec.tsx+++ b/apps/web-clipper/entrypoints/popup/popup.spec.tsx@@ -1,4 +1,7 @@+import Bookmark from "@boxicons/js/icons/Bookmark";import Crop from "@boxicons/js/icons/Crop";+import Globe from "@boxicons/js/icons/Globe";+import Paperclip from "@boxicons/js/icons/Paperclip";import { readFileSync } from "fs";import { join } from "path";import { render } from "preact";@@ -380,6 +383,8 @@ describe("popup", () => {expect(tabsSendMessage).toHaveBeenCalledWith(7, { name: "trilium-save-selection" });expect(modes()).toEqual([ [ "Selection", "true" ], [ "Page", "false" ], [ "Bookmark", "false" ] ]);expect(frameDocument()).toBe(previewDocument(SELECTION));+ // The heading shows the icon Trilium gives the note each mode saves.+ expect(headingIcon()).toEqual(pathsOf(iconVariant(Paperclip).content));expect(shortcutOf("Save selection").keys).toEqual([ "Ctrl", "Shift", "S" ]);expect(container.querySelector(".page-meta")?.textContent).toBe("example.com");expect(publishedDate()).toBeUndefined();@@ -388,10 +393,14 @@ describe("popup", () => {await click("Page");expect(modes()).toEqual([ [ "Selection", "false" ], [ "Page", "true" ], [ "Bookmark", "false" ] ]);expect(frameDocument()).toBe(previewDocument(PAGE));+ expect(headingIcon()).toEqual(pathsOf(iconVariant(Globe).content));expect(publishedDate()).toBe("Published 2024-05-01");await click("Save page to Trilium");expect(sendMessage).toHaveBeenLastCalledWith({ name: "save-whole-page", page: PAGE });+ await click("Bookmark");+ expect(headingIcon()).toEqual(pathsOf(iconVariant(Bookmark).content));+await click("Selection");await type(".page-title", " Quote ");await click("Save selection");@@ -890,6 +899,18 @@ function modes() {.map((option) => [ option.textContent, option.getAttribute("aria-pressed") ]);}+/** The path data of the icon in the preview heading. */+function headingIcon() {+ const icon = container.querySelector(".page-heading .page-icon svg.icon");+ expect(icon).not.toBeNull();+ return pathsOf(icon?.innerHTML ?? "");+}++/** The path data of an icon's markup, which the DOM serializes differently from Boxicons. */+function pathsOf(markup: string) {+ return [ ...markup.matchAll(/ d="([^"]*)"/g) ].map(([ , d ]) => d);+}+/** How many times the popup read the whole page. */function wholePageReads() {return executeScript.mock.calls.filter(([ injection ]) => injection.args?.[0] === "wholePage").length;