Skip to content

Build your personal knowledge base with Trilium Notes

feat(clipper): ask for a note about the page in every mode (closes #5350)

Elian Doran2ee916408f93
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html
index 8f1577f8e5..389035dc77 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html
@@ -34,8 +34,8 @@
<li>
<p>The popup shows what will be saved from the current page: the note's title,
which can be edited before saving, the website, and below them a preview,
- which starts with the page's description. A switch above the preview chooses
- what to save, among what the page offers:</p>
+ which starts with a box for your own note and the page's description. A
+ switch above the preview chooses what to save, among what the page offers:</p>
<ul>
<li><em>Selection</em>, when text is selected on the page: <em>Save selection</em> saves
it into the note of that page's clippings, as the context menu does.</li>
@@ -55,11 +55,17 @@
of the note. When it states when it was last modified, that date shows
below, marked by the <span class="tn-icon bx bx-history"></span> icon, and
is saved in the <code>#modifiedDate</code> label.</li>
- <li><em>Bookmark</em>, always: a text box for a note about the page replaces
- the preview, and <em>Save bookmark</em> (or <kbd>Ctrl</kbd>+<kbd>Enter</kbd>)
- saves a link to the page with that note. Left empty, the title stays the
- page's title.</li>
+ <li><em>Bookmark</em>, always: there is no preview, and <em>Save bookmark</em> saves
+ a link to the page with your note as the content of the note. Left empty,
+ the title stays the page's title.</li>
</ul>
+ <p>The box for your note, marked by the <span class="tn-icon bx bx-info-circle"></span> icon,
+ has the cursor when the popup opens, so a note (such as why the page is
+ worth keeping) can be typed right away. It grows with its text, and keeps
+ it when switching between <em>Selection</em>, <em>Page</em> and <em>Bookmark</em>.
+ A note on a selection or a page is saved above the clipped content, in
+ a <em>Note</em> admonition. Left empty, nothing is added. <kbd>Ctrl</kbd>+<kbd>Enter</kbd> in
+ any of the popup's fields saves, whichever is chosen.</p>
<p>The description, marked by the <span class="tn-icon bx bx-align-left"></span> icon,
comes from the page's <code>description</code> meta tag, or the one it gives
social sites. It can be edited or cleared before saving, or written in
@@ -102,8 +108,8 @@
scrolling (such as cookie banners and chat buttons) are left out, while
a background that fills the whole window repeats on every screen. A long
page is saved as several images, one below the other in the note. A page
- longer than 60,000 pixels (30,000 on a display scaled to 200%) is cut off
- there, and the message after saving says so.</p>
+ longer than 50 screens is cut off there, and the message after saving says
+ so.</p>
</li>
<li>
<p>If the current page was already clipped, a notice at the top offers to
diff --git a/apps/web-clipper/entrypoints/popup/main.tsx b/apps/web-clipper/entrypoints/popup/main.tsx
index ca14085c10..bbe69a8a10 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 ImagePortrait from "@boxicons/js/icons/ImagePortrait";
+import InfoCircle from "@boxicons/js/icons/InfoCircle";
import Lock from "@boxicons/js/icons/Lock";
import Paperclip from "@boxicons/js/icons/Paperclip";
import RefreshCw from "@boxicons/js/icons/RefreshCw";
@@ -391,7 +392,7 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
}, [ tabId, unreadVariant ]);
useEffect(() => {
- if (mode === "note") textAreaRef.current?.focus();
+ textAreaRef.current?.focus();
}, [ mode ]);
/** Keeps the popup open when the note could not be saved, so its text is not lost. */
@@ -411,20 +412,18 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
if (mode === "page") {
labels = withLabel(labels, "pageAuthor", savedAuthor);
}
- const titled = { ...content, title: shownTitle.trim() || content.title, ...(labels && { labels }) };
+ const titled = {
+ ...content,
+ title: shownTitle.trim() || content.title,
+ content: withNote(content.content, text),
+ ...(labels && { labels })
+ };
void sendMessage(mode === "selection"
? { name: "save-selection", selection: titled }
: { name: "save-whole-page", page: titled });
window.close();
}
- function saveNoteOnCtrlEnter(e: KeyboardEvent) {
- if (mode === "note" && e.key === "Enter" && e.ctrlKey) {
- e.preventDefault();
- void saveNote();
- }
- }
-
let onSave: (() => void) | undefined;
if (mode === "note") {
onSave = () => void saveNote();
@@ -432,6 +431,13 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
onSave = () => saveClip(clip);
}
+ function saveOnCtrlEnter(e: KeyboardEvent) {
+ if (onSave && e.key === "Enter" && e.ctrlKey) {
+ e.preventDefault();
+ onSave();
+ }
+ }
+
const pageUrl = mode === "note" ? tab?.url : clip?.pageUrl;
const pageLabels = mode === "page" ? pageReading?.labels : undefined;
const published = pageLabels?.publishedDate;
@@ -451,7 +457,7 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
placeholder="Note title"
value={shownTitle}
onInput={(e) => onDraftChange({ ...draft, title: e.currentTarget.value })}
- onKeyDown={saveNoteOnCtrlEnter}
+ onKeyDown={saveOnCtrlEnter}
/>
{pageUrl && <div className="page-meta">{new URL(pageUrl).hostname}</div>}
</div>
@@ -474,7 +480,27 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
<div className="page-body">
{mode && (
- <div className="page-fields">
+ <div className={mode === "note" ? "page-fields bookmark" : "page-fields"}>
+ <div className="page-field">
+ <span
+ className="page-field-icon"
+ title={mode === "note"
+ ? "Your note about the page. It is saved as the content of the note."
+ : "Your note about the page. It is saved above the clipped content."}
+ >
+ <Icon icon={InfoCircle} />
+ </span>
+ <textarea
+ ref={textAreaRef}
+ className="note-text"
+ aria-label="Your note"
+ placeholder="Add a note about this page"
+ rows={1}
+ value={text}
+ onInput={(e) => onDraftChange({ ...draft, text: e.currentTarget.value })}
+ onKeyDown={saveOnCtrlEnter}
+ />
+ </div>
<div className="page-field">
<span
className="page-field-icon"
@@ -489,7 +515,7 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
rows={2}
value={shownDescription}
onInput={(e) => onDraftChange({ ...draft, description: e.currentTarget.value })}
- onKeyDown={saveNoteOnCtrlEnter}
+ onKeyDown={saveOnCtrlEnter}
/>
</div>
{mode === "page" && (
@@ -507,6 +533,7 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
placeholder="Add an author"
value={shownAuthor}
onInput={(e) => onDraftChange({ ...draft, author: e.currentTarget.value })}
+ onKeyDown={saveOnCtrlEnter}
/>
</div>
)}
@@ -534,16 +561,6 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
)}
</div>
)}
- {mode === "note" && (
- <textarea
- ref={textAreaRef}
- className="note-text"
- value={text}
- placeholder="Add a note about this page"
- onInput={(e) => onDraftChange({ ...draft, text: e.currentTarget.value })}
- onKeyDown={saveNoteOnCtrlEnter}
- />
- )}
{mode === "page" && page && (
<div className="page-variant" role="group" aria-label="How to read the page">
{(Object.keys(VARIANT_LABELS) as PageVariant[]).map((option) => (
@@ -585,6 +602,12 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {
);
}
+/** A clip's content with the user's note above it as an admonition, or as it is without a note. */
+function withNote(content: string, note: string) {
+ const text = note.trim();
+ return text ? `<aside class="admonition note">${textToHtml(text)}</aside>${content}` : content;
+}
+
/** A clip's labels with the label `name` as the user left it, or without it when `value` is empty. */
function withLabel(labels: Record<string, string> | undefined, name: string, value: string) {
const { [name]: _, ...others } = labels ?? {};
diff --git a/apps/web-clipper/entrypoints/popup/popup.css b/apps/web-clipper/entrypoints/popup/popup.css
index 36927a6e52..7d144d1730 100644
--- a/apps/web-clipper/entrypoints/popup/popup.css
+++ b/apps/web-clipper/entrypoints/popup/popup.css
@@ -181,6 +181,14 @@ body {
flex: none;
padding: 6px 8px 6px 6px;
border-bottom: 1px solid var(--main-border-color);
+
+ &.bookmark {
+ border-bottom: 0;
+
+ .note-text {
+ min-height: 6lh;
+ }
+ }
}
.page-field {
@@ -202,6 +210,7 @@ body {
}
}
+ .note-text,
.page-description,
.page-author {
--input-background-color: transparent;
@@ -223,6 +232,11 @@ body {
}
}
+ .note-text {
+ max-height: 10lh;
+ color: var(--main-text-color);
+ }
+
.page-date {
padding: 4px 6px;
color: var(--muted-text-color);
@@ -239,25 +253,6 @@ body {
background: var(--main-background-color);
}
- .note-text {
- --input-background-color: transparent;
- --input-hover-background: transparent;
- --input-focus-background: transparent;
-
- display: block;
- flex: 1 0 120px;
- width: 100%;
- padding: 8px;
- border: 0;
- border-radius: 6px;
- resize: none;
-
- &:focus {
- outline: 0;
- box-shadow: inset 0 0 0 2px var(--input-focus-outline-color);
- }
- }
-
.clip-hint {
padding-inline: 6px;
color: var(--muted-text-color);
diff --git a/apps/web-clipper/entrypoints/popup/popup.spec.tsx b/apps/web-clipper/entrypoints/popup/popup.spec.tsx
index 336f589efa..c51bb44e26 100644
--- a/apps/web-clipper/entrypoints/popup/popup.spec.tsx
+++ b/apps/web-clipper/entrypoints/popup/popup.spec.tsx
@@ -227,12 +227,12 @@ describe("popup", () => {
expect(heading?.querySelector<HTMLInputElement>(".page-title")?.value).toBe("An article");
expect(heading?.querySelector(".page-meta")?.textContent).toBe("example.com");
expect(publishedDate()).toBe("Published 2024-05-01");
- const dateIcon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(3) > .page-field-icon");
+ const dateIcon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(4) > .page-field-icon");
expect(dateIcon?.querySelector("svg.icon path")).not.toBeNull();
expect(dateIcon?.title).toBe(
"The date the page was published. It is saved in the #publishedDate label of the note.");
expect(modifiedDate()).toBe("Updated 2024-06-12");
- const modifiedIcon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(4) > .page-field-icon");
+ const modifiedIcon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(5) > .page-field-icon");
expect(modifiedIcon?.querySelector("svg.icon path")).not.toBeNull();
expect(modifiedIcon?.title).toBe(
"The date the page was last modified. It is saved in the #modifiedDate label of the note.");
@@ -259,6 +259,8 @@ describe("popup", () => {
expect(variants()).toEqual([ [ "Article", "false", false ], [ "Whole page", "true", false ] ]);
expect(placeholder()).toBe("Reading the page…");
expect(button("Save page to Trilium")?.disabled).toBe(true);
+ await ctrlEnter(".note-text");
+ expect(sendMessage).not.toHaveBeenCalledWith(expect.objectContaining({ name: "save-whole-page" }));
await act(() => finishReading());
await flush();
@@ -329,7 +331,7 @@ describe("popup", () => {
pageReadings = { article: { ...PAGE, labels: {} }, wholePage: WHOLE_PAGE };
await rerender();
expect(container.querySelector(".page-meta")?.textContent).toBe("example.com");
- expect(container.querySelectorAll(".page-fields > .page-field")).toHaveLength(2);
+ expect(container.querySelectorAll(".page-fields > .page-field")).toHaveLength(3);
expect(publishedDate()).toBeUndefined();
expect(modifiedDate()).toBeUndefined();
});
@@ -447,14 +449,14 @@ describe("popup", () => {
it("writes a bookmark in the card, keeping the note across modes", async () => {
tabsQuery.mockResolvedValueOnce([ { id: 7, title: "A page", url: "https://example.com/post" } ]);
await rerender();
- expect(container.querySelector(".note-text")).toBeNull();
await click("Bookmark");
expect(modes()).toEqual([ [ "Page", "false" ], [ "Bookmark", "true" ] ]);
- const textArea = container.querySelector(".page-body .note-text");
+ const textArea = container.querySelector(".page-body .page-fields.bookmark .note-text");
expect(textArea).not.toBeNull();
expect(document.activeElement).toBe(textArea);
- expect(textArea?.getAttribute("placeholder")).toBe("Add a note about this page");
+ expect(container.querySelector<HTMLElement>(".page-field:first-child > .page-field-icon")?.title)
+ .toBe("Your note about the page. It is saved as the content of the note.");
expect(container.querySelector(".page-body iframe")).toBeNull();
expect(container.querySelector<HTMLInputElement>(".page-title")?.value).toBe("An article");
expect(container.querySelector(".page-meta")?.textContent).toBe("example.com");
@@ -464,7 +466,7 @@ describe("popup", () => {
await type(".page-body .note-text", "Worth a read");
await click("Page");
- expect(container.querySelector(".note-text")).toBeNull();
+ expect(container.querySelector(".page-fields.bookmark")).toBeNull();
await click("Bookmark");
expect(container.querySelector<HTMLTextAreaElement>(".page-body .note-text")?.value).toBe("Worth a read");
expect(closeWindow).not.toHaveBeenCalled();
@@ -488,7 +490,7 @@ describe("popup", () => {
expect(description()?.value).toBe("All about the post");
expect(description()?.getAttribute("aria-label")).toBe("Page description");
expect(description()?.getAttribute("placeholder")).toBe("Add a description");
- const icon = container.querySelector<HTMLElement>(".page-fields > .page-field:first-child > .page-field-icon");
+ const icon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(2) > .page-field-icon");
expect(icon?.querySelector("svg.icon path")).not.toBeNull();
expect(icon?.title).toBe(
"The page's description. It is saved in the #pageDescription label of the note.");
@@ -571,7 +573,7 @@ describe("popup", () => {
expect(author()?.value).toBe("Jane Doe");
expect(author()?.getAttribute("aria-label")).toBe("Author");
expect(author()?.getAttribute("placeholder")).toBe("Add an author");
- const icon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(2) > .page-field-icon");
+ const icon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(3) > .page-field-icon");
expect(icon?.querySelector("svg.icon path")).not.toBeNull();
expect(icon?.title).toBe("The page's author. It is saved in the #pageAuthor label of the note.");
@@ -696,10 +698,47 @@ describe("popup", () => {
expect(container.querySelector(".page-body .note-text")).not.toBeNull();
expect(closeWindow).not.toHaveBeenCalled();
- const calls = sendMessage.mock.calls.length;
await click("Page");
await ctrlEnter(".page-title");
- expect(sendMessage).toHaveBeenCalledTimes(calls);
+ expect(sendMessage).toHaveBeenLastCalledWith({ name: "save-whole-page", page: PAGE });
+ expect(closeWindow).toHaveBeenCalledOnce();
+ });
+
+ it("asks for a note in every mode, and saves it above the clipped content", async () => {
+ selection = SELECTION;
+ await rerender();
+ const note = () => container.querySelector<HTMLTextAreaElement>(".page-fields > .page-field:first-child > .note-text");
+ expect(note()).not.toBeNull();
+ expect(document.activeElement).toBe(note());
+ expect(note()?.getAttribute("aria-label")).toBe("Your note");
+ expect(note()?.getAttribute("placeholder")).toBe("Add a note about this page");
+ const icon = container.querySelector<HTMLElement>(".page-fields > .page-field:first-child > .page-field-icon");
+ expect(icon?.querySelector("svg.icon path")).not.toBeNull();
+ expect(icon?.title).toBe("Your note about the page. It is saved above the clipped content.");
+
+ await type(".note-text", " Worth <b>reading</b>\nlater ");
+ await click("Save selection");
+ const aside = `<aside class="admonition note"><p>Worth &lt;b&gt;reading&lt;/b&gt;</p><p>later</p></aside>`;
+ expect(sendMessage).toHaveBeenLastCalledWith({
+ name: "save-selection",
+ selection: { ...SELECTION, title: "An article", content: aside + SELECTION.content }
+ });
+
+ await click("Page");
+ expect(document.activeElement).toBe(note());
+ expect(note()?.value).toBe(" Worth <b>reading</b>\nlater ");
+ await ctrlEnter(".note-text");
+ expect(sendMessage).toHaveBeenLastCalledWith({
+ name: "save-whole-page",
+ page: { ...PAGE, content: aside + PAGE.content }
+ });
+ sendMessage.mockClear();
+ await ctrlEnter(".page-author");
+ expect(sendMessage).toHaveBeenCalledOnce();
+
+ await type(".note-text", " \n ");
+ await click("Save page to Trilium");
+ expect(sendMessage).toHaveBeenLastCalledWith({ name: "save-whole-page", page: PAGE });
});
it("renders itself into the page's root element", async () => {
diff --git a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md
index 0fbaf4db96..1ead1a158d 100644
--- a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md
+++ b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md
@@ -29,18 +29,20 @@ The extension is available from the official browser web stores:
Clicking the extension's button in the browser toolbar opens its popup:
-* The popup shows what will be saved from the current page: the note's title, which can be edited before saving, the website, and below them a preview, which starts with the page's description. A switch above the preview chooses what to save, among what the page offers:
+* The popup shows what will be saved from the current page: the note's title, which can be edited before saving, the website, and below them a preview, which starts with a box for your own note and the page's description. A switch above the preview chooses what to save, among what the page offers:
* _Selection_, when text is selected on the page: _Save selection_ saves it into the note of that page's clippings, as the context menu does.
* _Page_: _Save page to Trilium_ saves the page's text and images. A smaller switch at the top of the preview chooses how much of the page to keep: _Article_ keeps only the page's main text, without its menus, sidebars and footers, and _Whole page_ keeps everything the page shows. _Whole page_ is for pages that are not an article (such as a gallery or a page of cards), where _Article_ can leave out part of the content; the preview shows what each one keeps. _Article_ is unavailable on a page where Trilium finds no article. The author the page names shows under the description, marked by the <span class="tn-icon bx bx-user"></span> icon. It can be edited or cleared before saving, or written in when the page names none, and is saved in the `#pageAuthor` label of the note. When the page states when it was published, that date shows under the author, marked by the <span class="tn-icon bx bx-calendar"></span> icon, and is saved in the `#publishedDate` label of the note. When it states when it was last modified, that date shows below, marked by the <span class="tn-icon bx bx-history"></span> icon, and is saved in the `#modifiedDate` label.
- * _Bookmark_, always: a text box for a note about the page replaces the preview, and _Save bookmark_ (or <kbd>Ctrl</kbd>+<kbd>Enter</kbd>) saves a link to the page with that note. Left empty, the title stays the page's title.
+ * _Bookmark_, always: there is no preview, and _Save bookmark_ saves a link to the page with your note as the content of the note. Left empty, the title stays the page's title.
+
+ The box for your note, marked by the <span class="tn-icon bx bx-info-circle"></span> icon, has the cursor when the popup opens, so a note (such as why the page is worth keeping) can be typed right away. It grows with its text, and keeps it when switching between _Selection_, _Page_ and _Bookmark_. A note on a selection or a page is saved above the clipped content, in a _Note_ admonition. Left empty, nothing is added. <kbd>Ctrl</kbd>+<kbd>Enter</kbd> in any of the popup's fields saves, whichever is chosen.
The description, marked by the <span class="tn-icon bx bx-align-left"></span> icon, comes from the page's `description` meta tag, or the one it gives social sites. It can be edited or cleared before saving, or written in when the page has none, and is saved in the `#pageDescription` label of the note. Clippings saved with the keyboard shortcuts or the context menu, screenshots and images included, get the description as the page gives it. Each clipping that brings a description updates the label. The description, the author and the dates are shown by clicking the _Web clip_ badge next to the title of the note (see <a class="reference-link" href="../Basic%20Concepts%20and%20Features/Notes/Note%20badges.md">Note badges</a>).
The popup opens on the selection if there is one, otherwise on _Page_, showing the article, or the whole page when the page has no article. On a page that shows nothing to save, it opens on _Bookmark_ and says so. The keyboard shortcuts and the context menu still save right away, without a preview: saving the whole page that way saves the article, or the whole page when the page has no article.
* Pages the extension cannot access (the browser's own pages, the extension stores, and pages that were open before the extension was installed until they are reloaded) cannot be saved or captured, so the popup says so in place of the preview, and only _Tabs_ remains available there.
* Below the preview, a row of buttons offers the other actions: _Crop_ (a screenshot of an area you select), _Screenshot_ (of the visible part of the page), _Full page_ (a screenshot of the whole page) and _Tabs_ (saves the links of every tab in the current window as a list of <a class="reference-link" href="../Note%20Types/Text/Link%20Previews.md">Link Previews</a>, or of plain links when _Autogenerate link previews_ is turned off). Hovering over a button shows its full name and, if it has one, its keyboard shortcut.
-* _Full page_ scrolls through the page, takes a screenshot of each part and joins them into one image, then scrolls back to where the page was. The scrollbars are hidden while it does, so they do not appear in the image. Stay on the tab until it is saved: switching to another tab stops the screenshot. A header that stays at the top of the window appears once, at the top of the image, and headings that stick while scrolling appear where they are in the page. Other elements that stay in place while scrolling (such as cookie banners and chat buttons) are left out, while a background that fills the whole window repeats on every screen. A long page is saved as several images, one below the other in the note. A page longer than 60,000 pixels (30,000 on a display scaled to 200%) is cut off there, and the message after saving says so.
+* _Full page_ scrolls through the page, takes a screenshot of each part and joins them into one image, then scrolls back to where the page was. The scrollbars are hidden while it does, so they do not appear in the image. Stay on the tab until it is saved: switching to another tab stops the screenshot. A header that stays at the top of the window appears once, at the top of the image, and headings that stick while scrolling appear where they are in the page. Other elements that stay in place while scrolling (such as cookie banners and chat buttons) are left out, while a background that fills the whole window repeats on every screen. A long page is saved as several images, one below the other in the note. A page longer than 50 screens is cut off there, and the message after saving says so.
* If the current page was already clipped, a notice at the top offers to open the note in Trilium.
* If Trilium cannot be found, the popup explains why instead of showing the actions, with buttons to look for Trilium again and to open the options.
* The bottom of the popup shows whether the extension is connected to Trilium, as a colored dot (green when connected, amber when the versions are not compatible, red when Trilium was not found) next to a description of the connection, followed by the buttons for the extension's options and for this help page. A button to check the connection again appears next to them when hovering over the bar, and stays visible while Trilium cannot be reached or is not compatible.