Skip to content

Build your personal knowledge base with Trilium Notes

feat(web-clipper): draw each key of a shortcut as its own key cap

Elian Doranbeeddf0834d2
diff --git a/apps/web-clipper/entrypoints/popup/main.tsx b/apps/web-clipper/entrypoints/popup/main.tsx
index 38c1a9964e..d5eccf7576 100644
--- a/apps/web-clipper/entrypoints/popup/main.tsx
+++ b/apps/web-clipper/entrypoints/popup/main.tsx
@@ -16,7 +16,7 @@ import Screenshot from "@boxicons/js/icons/Screenshot";
import Tabs from "@boxicons/js/icons/Tabs";
import Unlink from "@boxicons/js/icons/Unlink";
import type { ComponentChildren } from "preact";
-import { render } from "preact";
+import { Fragment, render } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";
import type { TriliumSearchNoteStatus, TriliumSearchStatus } from "../background/trilium_server_facade";
@@ -320,8 +320,20 @@ function PagePreview({ page, disabled, shortcut }: {
);
}
+/**
+ * A shortcut as the app draws it: a key cap per key, joined by "+". The browser formats the shortcut;
+ * on macOS, Chrome gives the glyphs without a "+" (`⌥⇧S`), which stay in one key cap there too.
+ */
function Shortcut({ keys }: { keys: string | undefined }) {
- return keys ? <kbd>{keys}</kbd> : null;
+ if (!keys) return null;
+
+ return (
+ <span className="shortcut">
+ {keys.split("+").map((key, index) => (
+ <Fragment key={index}>{index > 0 && "+"}<kbd>{key}</kbd></Fragment>
+ ))}
+ </span>
+ );
}
function TriliumNotFound() {
diff --git a/apps/web-clipper/entrypoints/popup/popup.css b/apps/web-clipper/entrypoints/popup/popup.css
index 7e5513faca..d5a4774931 100644
--- a/apps/web-clipper/entrypoints/popup/popup.css
+++ b/apps/web-clipper/entrypoints/popup/popup.css
@@ -163,6 +163,20 @@ body {
}
}
+ button.btn .shortcut {
+ display: inline-flex;
+ align-items: center;
+ gap: 1px;
+ margin-inline-start: 0.5em;
+ font-size: 0.6em;
+ letter-spacing: 0.5pt;
+
+ kbd {
+ margin: 0;
+ font-size: inherit;
+ }
+ }
+
.toolbar {
display: grid;
grid-template-columns: repeat(4, 1fr);
diff --git a/apps/web-clipper/entrypoints/popup/popup.spec.tsx b/apps/web-clipper/entrypoints/popup/popup.spec.tsx
index b5dca0e3b0..ae99a2710e 100644
--- a/apps/web-clipper/entrypoints/popup/popup.spec.tsx
+++ b/apps/web-clipper/entrypoints/popup/popup.spec.tsx
@@ -169,9 +169,9 @@ describe("popup", () => {
expect(openWindow).toHaveBeenCalledWith("https://docs.triliumnotes.org/user-guide/setup/web-clipper", "_blank");
});
- it("shows the keyboard shortcut of each action that has one", () => {
+ it("shows the keyboard shortcut of each action that has one", async () => {
expect(getAllCommands).toHaveBeenCalledOnce();
- expect(shortcutOf("Save page to Trilium")).toBe("Alt+Shift+S");
+ expect(shortcutOf("Save page to Trilium")).toEqual({ text: "Alt+Shift+S", keys: [ "Alt", "Shift", "S" ] });
expect(toolbar()).toEqual([
{ label: "Crop", title: "Crop screenshot (Ctrl+Shift+E)" },
{ label: "Screenshot", title: "Visible area screenshot" },
@@ -179,6 +179,10 @@ describe("popup", () => {
{ label: "Tabs", title: "All tabs in window" }
]);
expect(container.querySelectorAll(".toolbar-action kbd")).toHaveLength(0);
+
+ getAllCommands.mockResolvedValueOnce([ { name: "saveWholePage", shortcut: "⌥⇧S" } ]);
+ await rerender();
+ expect(shortcutOf("Save page to Trilium")).toEqual({ text: "⌥⇧S", keys: [ "⌥⇧S" ] });
});
it("previews the readable page with its title, source and date", () => {
@@ -274,8 +278,7 @@ describe("popup", () => {
expect(captureButtons()).toHaveLength(0);
expect(container.querySelector(".view-header h4")?.textContent).toBe("Link with a note");
expect(textArea?.placeholder).toBe("The first sentence becomes the note's title, the rest its text.");
- expect(shortcutOf("Save")).toBe("Ctrl+Enter");
- expect(button("Save")?.querySelectorAll("kbd")).toHaveLength(1);
+ expect(shortcutOf("Save")).toEqual({ text: "Ctrl+Enter", keys: [ "Ctrl", "Enter" ] });
expect(container.querySelector(".hint")).toBeNull();
await click("Back");
@@ -481,8 +484,14 @@ async function click(text: string) {
await act(() => target?.click());
}
+/** The shortcut shown on a button: its text, and the keys drawn as separate key caps. */
function shortcutOf(text: string) {
- return button(text)?.querySelector("kbd")?.textContent;
+ const shortcut = button(text)?.querySelector(".shortcut");
+ expect(shortcut, text).not.toBeNull();
+ return {
+ text: shortcut?.textContent,
+ keys: [ ...shortcut?.querySelectorAll(":scope > kbd") ?? [] ].map((key) => key.textContent)
+ };
}
function toolbar() {