Skip to content

Build your personal knowledge base with Trilium Notes

server/share pages: add support for highlighting blocks

Adorian Doran2dd80169d2e8
diff --git a/.claude/skills/developing-share-pages/SKILL.md b/.claude/skills/developing-share-pages/SKILL.md
index 2d07e84604..1dbe2f388f 100644
--- a/.claude/skills/developing-share-pages/SKILL.md
+++ b/.claude/skills/developing-share-pages/SKILL.md
@@ -69,7 +69,9 @@ imports the model as `@triliumnext/share-theme/model/page`). The theme has no bu
5. **Browser** — `tree.js`, the first entry of `jsToLoad` and the only `blocking="render"` one,
restores the tree's expansion, scroll position and clicked clone before the first paint.
`scripts.js` then wires the expand buttons, search, ToC scroll tracking, theme switch, footer
- date, math, Mermaid, link previews and tabs. `boot_script.ejs` runs inline in `<head>` before the
+ date, math, Mermaid, link previews and tabs. A `?block=` address loads the app's
+ `block_reference_read_only.js` (and its CSS) on demand, which scrolls to the blocks and flashes
+ them as the app does; it searches `document.body`, so it does not rely on the theme's markup. `boot_script.ejs` runs inline in `<head>` before the
first paint (theme class, collapsed panes, `window.glob`).
The static export calls `renderNoteForExport()` with `isStatic: true`, which also expands embeds
diff --git a/apps/client/src/services/block_reference.css b/apps/client/src/services/block_reference.css
index 1549d00cd0..de77def110 100644
--- a/apps/client/src/services/block_reference.css
+++ b/apps/client/src/services/block_reference.css
@@ -1,52 +1,7 @@
:where(:root) {
- --block-reference-flash-color: rgb(255 200 0 / 40%);
--block-reference-broken-color: var(--muted-text-color);
}
-.block-reference-flash {
- /* The duration matches FLASH_DURATION_MS in block_reference.ts. */
- animation: block-reference-flash 1.5s ease-out;
-}
-
-@keyframes block-reference-flash {
- from,
- 30% {
- box-shadow:
- 0 0 0 4px var(--block-reference-flash-color),
- inset 0 0 0 100vmax var(--block-reference-flash-color);
- }
-
- to {
- box-shadow: 0 0 0 4px transparent, inset 0 0 0 100vmax transparent;
- }
-}
-
-.block-reference-highlight {
- box-shadow:
- 0 0 0 4px var(--block-reference-flash-color),
- inset 0 0 0 100vmax var(--block-reference-flash-color);
-}
-
-/* A tab is `display: contents` (tabs.css), so its title and panel paint the effect instead. */
-.trilium-tab.block-reference-flash > :is(.trilium-tab-title, .trilium-tab-panel) {
- animation: block-reference-flash-tab 1.5s ease-out;
-}
-
-.trilium-tab.block-reference-highlight > :is(.trilium-tab-title, .trilium-tab-panel) {
- box-shadow: inset 0 0 0 100vmax var(--block-reference-flash-color);
-}
-
-@keyframes block-reference-flash-tab {
- from,
- 30% {
- box-shadow: inset 0 0 0 100vmax var(--block-reference-flash-color);
- }
-
- to {
- box-shadow: inset 0 0 0 100vmax transparent;
- }
-}
-
.block-reference-broken {
color: var(--block-reference-broken-color);
font-style: italic;
diff --git a/apps/client/src/services/block_reference.spec.ts b/apps/client/src/services/block_reference.spec.ts
index d21c81aa78..5ffbca9f5b 100644
--- a/apps/client/src/services/block_reference.spec.ts
+++ b/apps/client/src/services/block_reference.spec.ts
@@ -1,13 +1,12 @@
import type { CKTextEditor } from "@triliumnext/ckeditor5";
-import { applyTabs } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { CommandNames } from "../components/app_context.js";
import type { MenuCommandItem, MenuItem } from "../menus/context_menu.js";
import {
buildBlockReferenceMenuItems, consumeBlockReference, copyBlockReference, copyBlockShareLink,
- copyTabReference, getBlockRangeElements, getClipboardBlockReference, highlightBlockReference,
- openBlockHandleMenu, revealHighlightedBlocks
+ copyTabReference, getClipboardBlockReference, highlightBlockReference, openBlockHandleMenu,
+ revealHighlightedBlocks
} from "./block_reference.js";
import type { ViewScope } from "./link.js";
@@ -51,85 +50,28 @@ afterEach(() => {
});
describe("consumeBlockReference", () => {
- it("reveals a block, flashes it for a moment and consumes the reference", () => {
- const container = buildContainer(
- "<details><summary>s</summary><p data-trilium-block-id=\"a\">A</p></details>"
- );
- const block = container.querySelector("p");
+ it("shows the blocks and consumes the reference", () => {
+ const container = buildContainer("<p data-trilium-block-id=\"a\">A</p>");
const viewScope: ViewScope = { block: "a" };
consumeBlockReference(container, viewScope);
- expect(container.querySelector("details")?.open).toBe(true);
- expect(scrollIntoView).toHaveBeenCalledWith({ behavior: "smooth", block: "center" });
- expect(scrollIntoView.mock.contexts[0]).toBe(block);
- expect(block?.classList.contains("block-reference-flash")).toBe(true);
+ expect(scrollIntoView.mock.contexts[0]).toBe(container.querySelector("p"));
+ expect(getFlashed(container)).toEqual([ "A" ]);
expect(viewScope.block).toBeUndefined();
expect(showError).not.toHaveBeenCalled();
-
- vi.advanceTimersByTime(1500);
- expect(block?.classList.contains("block-reference-flash")).toBe(false);
- });
-
- it("shows the tab that holds the block", () => {
- const container = buildContainer(
- `<div class="trilium-tabs">` +
- `<section class="trilium-tab"><p class="trilium-tab-title">A</p><div class="trilium-tab-panel"></div></section>` +
- `<section class="trilium-tab"><p class="trilium-tab-title">B</p><div class="trilium-tab-panel"><p data-trilium-block-id="a">A</p></div></section>` +
- `</div>`
- );
- applyTabs(container, { placeholder: "" });
-
- consumeBlockReference(container, { block: "a" });
-
- expect(container.querySelector(".trilium-tab--active > .trilium-tab-title")?.textContent).toBe("B");
});
- it("scrolls to the title of a linked tab, which has no box of its own", () => {
- const container = buildContainer(
- `<div class="trilium-tabs"><section class="trilium-tab" data-trilium-block-id="t">` +
- `<p class="trilium-tab-title">A</p><div class="trilium-tab-panel"><p>a</p></div>` +
- `</section></div>`
- );
- const title = container.querySelector(".trilium-tab-title");
- expect(title).not.toBeNull();
-
- consumeBlockReference(container, { block: "t" });
-
- expect(scrollIntoView).toHaveBeenCalledTimes(1);
- expect(scrollIntoView.mock.contexts[0]).toBe(title);
- });
-
- it("flashes the outermost elements of a range", () => {
- const container = buildContainer(
- "<p>before</p><p data-trilium-block-id=\"a\">A</p><blockquote><p>quote</p></blockquote>"
- + "<ul><li><p data-trilium-block-id=\"b\">B</p><p>rest of item</p></li>"
- + "<li>after</li></ul>"
- );
-
- consumeBlockReference(container, { block: "b:a" });
-
- expect(getFlashed(container)).toEqual([ "A", "quote", "B" ]);
- });
-
- it("shows the block still found of a broken range, and says that it is broken", () => {
+ it("says so when a block is missing or the reference is malformed", () => {
const container = buildContainer("<p data-trilium-block-id=\"b\">B</p>");
consumeBlockReference(container, { block: "gone:b" });
-
- expect(getFlashed(container)).toEqual([ "B" ]);
- expect(scrollIntoView).toHaveBeenCalledTimes(1);
- expect(showError).toHaveBeenCalledWith("block_reference.not_found");
- });
-
- it("only says so when no block is found, or the reference is malformed", () => {
- const container = buildContainer("<p data-trilium-block-id=\"a\">A</p>");
-
consumeBlockReference(container, { block: "gone" });
consumeBlockReference(container, { block: "a:b:c" });
- expect(scrollIntoView).not.toHaveBeenCalled();
- expect(showError).toHaveBeenCalledTimes(2);
+ expect(getFlashed(container)).toEqual([ "B" ]);
+ expect(showError).toHaveBeenCalledTimes(3);
+ expect(showError).toHaveBeenCalledWith("block_reference.not_found");
});
it("waits for the content, and does nothing without a reference", () => {
@@ -144,20 +86,6 @@ describe("consumeBlockReference", () => {
});
});
-describe("getBlockRangeElements", () => {
- it("returns a single block, or the blocks between two in a list", () => {
- const container = buildContainer(
- "<ol><li data-trilium-block-id=\"a\">A</li><li>B</li>"
- + "<li data-trilium-block-id=\"c\">C</li><li>D</li></ol>"
- );
- const [ first, , third ] = container.querySelectorAll("li");
-
- expect(getBlockRangeElements(first, first)).toEqual([ first ]);
- expect(getBlockRangeElements(first, third).map((element) => element.textContent))
- .toEqual([ "A", "B", "C" ]);
- });
-});
-
describe("highlightBlockReference", () => {
it("highlights a range and opens the collapsed blocks at its ends", () => {
const container = buildContainer(
diff --git a/apps/client/src/services/block_reference.ts b/apps/client/src/services/block_reference.ts
index 86ac33d8db..d7f2643491 100644
--- a/apps/client/src/services/block_reference.ts
+++ b/apps/client/src/services/block_reference.ts
@@ -1,5 +1,7 @@
import type { CKTextEditor } from "@triliumnext/ckeditor5";
-import { CLASSES as TAB_CLASSES } from "@triliumnext/ckeditor5/src/plugins/tabs/constants.js";
+import {
+ flashBlocks, getBlockBoxes, getBlockRangeElements, showBlockReference
+} from "@triliumnext/ckeditor5/src/plugins/block_reference/block_reference_read_only.js";
import {
type BlockRange, encodeBlockParameter, formatBlockRange, parseBlockRange, resolveBlockRange,
resolveBlockReference
@@ -17,9 +19,6 @@ import { t } from "./i18n.js";
import { calculateHash, parseNavigationStateFromUrl, type ViewScope } from "./link.js";
import toast from "./toast.js";
-const FLASH_CLASS = "block-reference-flash";
-/** The duration of the `block-reference-flash` animation in `block_reference.css`. */
-const FLASH_DURATION_MS = 1500;
const HIGHLIGHT_CLASS = "block-reference-highlight";
/** A reference to blocks of a note, read from the clipboard. */
@@ -206,16 +205,9 @@ export function consumeBlockReference(
return;
}
- const { start, end } = resolveBlockReference<HTMLElement>(container, viewScope.block);
+ const isFound = showBlockReference(container, viewScope.block);
viewScope.block = undefined;
-
- const first = start ?? end;
- if (first) {
- revealElement(first);
- getBlockBoxes(first).top.scrollIntoView({ behavior: "smooth", block: "center" });
- flashBlocks(start && end ? getBlockRangeElements(start, end) : [ first ]);
- }
- if (!start || !end) {
+ if (!isFound) {
toast.showError(t("block_reference.not_found"));
}
}
@@ -259,53 +251,6 @@ export function revealHighlightedBlocks(container: HTMLElement) {
container.scrollTop += top - viewportTop - margin;
}
-/**
- * Returns the elements that render the top and bottom of `block`. A tab is `display: contents`, so
- * its title and panel render it.
- */
-function getBlockBoxes(block: Element): { top: Element; bottom: Element } {
- if (block.classList.contains(TAB_CLASSES.tab)) {
- const title = block.querySelector(`:scope > .${TAB_CLASSES.tabTitle}`);
- const panel = block.querySelector(`:scope > .${TAB_CLASSES.tabPanel}`);
- if (title && panel) {
- return { top: title, bottom: panel };
- }
- }
- return { top: block, bottom: block };
-}
-
-/** The outermost elements inside the range from `start` to `end`, both included. */
-export function getBlockRangeElements(start: Element, end: Element) {
- const range = start.ownerDocument.createRange();
- range.setStartBefore(start);
- range.setEndAfter(end);
- return getElementsInRange(range, range.commonAncestorContainer);
-}
-
-function getElementsInRange(range: Range, parent: Node): Element[] {
- const elements: Element[] = [];
- for (const child of Array.from(parent.childNodes)) {
- if (!(child instanceof Element) || !range.intersectsNode(child)) {
- continue;
- }
-
- if (isInRange(range, child)) {
- elements.push(child);
- } else {
- elements.push(...getElementsInRange(range, child));
- }
- }
-
- return elements;
-}
-
-function isInRange(range: Range, node: Node) {
- const nodeRange = range.cloneRange();
- nodeRange.selectNode(node);
- return range.compareBoundaryPoints(Range.START_TO_START, nodeRange) <= 0
- && range.compareBoundaryPoints(Range.END_TO_END, nodeRange) >= 0;
-}
-
function getCopyItem(count: number, copyReference: () => void): MenuItem<CommandNames> {
return {
title: t("block_reference.copy", { count }),
@@ -432,10 +377,3 @@ function parseBlockReferenceHref(href: string): ClipboardBlockReference | null {
return { noteId, block, href: calculateHash({ notePath, viewScope: { block } }) };
}
-
-function flashBlocks(elements: Element[]) {
- for (const element of elements) {
- element.classList.add(FLASH_CLASS);
- setTimeout(() => element.classList.remove(FLASH_CLASS), FLASH_DURATION_MS);
- }
-}
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Block references.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Block references.html
index f3541295e6..d039779936 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Block references.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Block references.html
@@ -25,7 +25,8 @@
<p>If the note is <a href="#root/_help_R9pX4DGra2Vt">shared</a>, the menu
of the block handle also offers <em>Copy public link to this block</em>.
It copies the web address of the shared page with the reference added,
- to send to people who read the note outside Trilium.</p>
+ to send to people who read the note outside Trilium. Opening the address
+ scrolls the shared page to the referenced blocks and flashes them.</p>
<p>The <em>Paste</em> submenu of the context menu offers two more ways to insert
a copied reference:</p>
<ul>
diff --git a/docs/User Guide/User Guide/Note Types/Text/Block references.md b/docs/User Guide/User Guide/Note Types/Text/Block references.md
index 93266af284..1c3f1a9826 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Block references.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Block references.md
@@ -12,7 +12,7 @@ Inside a <a class="reference-link" href="Content%20tabs.md">Content tabs</a> blo
The link shows the title of the note, followed by the beginning of the referenced text. A link to blocks of the same note shows only the beginning of the referenced text.
-If the note is [shared](../../Advanced%20Usage/Sharing.md), the menu of the block handle also offers _Copy public link to this block_. It copies the web address of the shared page with the reference added, to send to people who read the note outside Trilium.
+If the note is [shared](../../Advanced%20Usage/Sharing.md), the menu of the block handle also offers _Copy public link to this block_. It copies the web address of the shared page with the reference added, to send to people who read the note outside Trilium. Opening the address scrolls the shared page to the referenced blocks and flashes them.
The _Paste_ submenu of the context menu offers two more ways to insert a copied reference:
diff --git a/packages/ckeditor5/src/plugins/block_reference/block_reference_read_only.spec.ts b/packages/ckeditor5/src/plugins/block_reference/block_reference_read_only.spec.ts
new file mode 100644
index 0000000000..c5dc8be7e4
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/block_reference/block_reference_read_only.spec.ts
@@ -0,0 +1,116 @@
+import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from "vitest";
+
+import { applyTabs } from "../tabs/tabs_read_only.js";
+import { getBlockRangeElements, showBlockReference } from "./block_reference_read_only.js";
+
+let scrollIntoView: MockInstance<Element["scrollIntoView"]>;
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ scrollIntoView = vi.spyOn(Element.prototype, "scrollIntoView").mockImplementation(() => {});
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+});
+
+describe("showBlockReference", () => {
+ it("opens the collapsed blocks around a block, centers it and flashes it for a moment", () => {
+ const container = buildContainer(
+ "<details><summary>s1</summary><details><summary>s2</summary>"
+ + "<p data-trilium-block-id=\"a\">A</p></details></details>"
+ );
+ const block = container.querySelector("p");
+
+ expect(showBlockReference(container, "a")).toBe(true);
+
+ expect([ ...container.querySelectorAll("details") ].map((details) => details.open))
+ .toEqual([ true, true ]);
+ expect(scrollIntoView).toHaveBeenCalledExactlyOnceWith({
+ behavior: "smooth", block: "center"
+ });
+ expect(scrollIntoView.mock.contexts[0]).toBe(block);
+ expect(getFlashed(container)).toEqual([ "A" ]);
+
+ vi.advanceTimersByTime(1500);
+ expect(getFlashed(container)).toEqual([]);
+ });
+
+ it("shows the tab that holds a block, and scrolls to the title of a linked tab", () => {
+ const container = buildContainer(
+ "<div class=\"trilium-tabs\">"
+ + "<section class=\"trilium-tab\"><p class=\"trilium-tab-title\">A</p>"
+ + "<div class=\"trilium-tab-panel\"></div></section>"
+ + "<section class=\"trilium-tab\" data-trilium-block-id=\"t\">"
+ + "<p class=\"trilium-tab-title\">B</p><div class=\"trilium-tab-panel\">"
+ + "<p data-trilium-block-id=\"a\">a</p></div></section>"
+ + "<section class=\"trilium-tab\" data-trilium-block-id=\"bare\"><p>c</p></section>"
+ + "</div>"
+ );
+ applyTabs(container, { placeholder: "" });
+ const activeTitle = () =>
+ container.querySelector(".trilium-tab--active > .trilium-tab-title");
+ const titleB = container.querySelectorAll(".trilium-tab-title")[1];
+ expect(activeTitle()?.textContent).toBe("A");
+
+ showBlockReference(container, "a");
+ expect(activeTitle()?.textContent).toBe("B");
+
+ showBlockReference(container, "t");
+ expect(scrollIntoView.mock.contexts[1]).toBe(titleB);
+
+ showBlockReference(container, "bare");
+ expect(scrollIntoView.mock.contexts[2])
+ .toBe(container.querySelector("[data-trilium-block-id=bare]"));
+ });
+
+ it("flashes the outermost elements of a range, in document order", () => {
+ const container = buildContainer(
+ "<p>before</p><p data-trilium-block-id=\"a\">A</p><blockquote><p>quote</p></blockquote>"
+ + "<ul><li><p data-trilium-block-id=\"b\">B</p><p>rest of item</p></li>"
+ + "<li>after</li></ul>"
+ );
+
+ expect(showBlockReference(container, "b:a")).toBe(true);
+
+ expect(getFlashed(container)).toEqual([ "A", "quote", "B" ]);
+ });
+
+ it("shows the block found of a broken range, and nothing for a missing block", () => {
+ const container = buildContainer("<p data-trilium-block-id=\"b\">B</p><p>other</p>");
+
+ expect(showBlockReference(container, "gone")).toBe(false);
+ expect(showBlockReference(container, "a:b:c")).toBe(false);
+ expect(scrollIntoView).not.toHaveBeenCalled();
+
+ expect(showBlockReference(container, "gone:b")).toBe(false);
+ expect(scrollIntoView.mock.contexts[0]).toBe(container.querySelector("p"));
+ expect(getFlashed(container)).toEqual([ "B" ]);
+ });
+});
+
+describe("getBlockRangeElements", () => {
+ it("returns a single block, or the blocks between two in a list", () => {
+ const container = buildContainer(
+ "<ol><li data-trilium-block-id=\"a\">A</li><li>B</li>"
+ + "<li data-trilium-block-id=\"c\">C</li><li>D</li></ol>"
+ );
+ const [ first, , third ] = container.querySelectorAll("li");
+
+ expect(getBlockRangeElements(first, first)).toEqual([ first ]);
+ expect(getBlockRangeElements(first, third).map((element) => element.textContent))
+ .toEqual([ "A", "B", "C" ]);
+ });
+});
+
+function buildContainer(html: string) {
+ const container = document.createElement("div");
+ container.innerHTML = html;
+ return container;
+}
+
+function getFlashed(container: HTMLElement) {
+ return [ ...container.querySelectorAll(".block-reference-flash") ]
+ .map((element) => element.textContent);
+}
diff --git a/packages/ckeditor5/src/plugins/block_reference/block_reference_read_only.ts b/packages/ckeditor5/src/plugins/block_reference/block_reference_read_only.ts
new file mode 100644
index 0000000000..5bd8ca719b
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/block_reference/block_reference_read_only.ts
@@ -0,0 +1,93 @@
+import { resolveBlockReference } from "@triliumnext/commons/src/lib/block_reference.js";
+
+import "../../theme/block_reference.css";
+import { CLASSES as TAB_CLASSES } from "../tabs/constants.js";
+import { revealTab } from "../tabs/tabs_read_only.js";
+
+const FLASH_CLASS = "block-reference-flash";
+/** The duration of the `block-reference-flash` animation in `block_reference.css`. */
+const FLASH_DURATION_MS = 1500;
+
+/**
+ * Scrolls to the blocks that `value`, a `block` link parameter, points at in `container` and
+ * flashes them. Of a broken range, the block found is shown. Returns `false` when a block is
+ * missing.
+ *
+ * The module imports nothing from CKEditor, so pages that only display content can load it.
+ */
+export function showBlockReference(container: Element, value: string) {
+ const { start, end } = resolveBlockReference<HTMLElement>(container, value);
+ const first = start ?? end;
+ if (first) {
+ reveal(first);
+ getBlockBoxes(first).top.scrollIntoView({ behavior: "smooth", block: "center" });
+ flashBlocks(start && end ? getBlockRangeElements(start, end) : [ first ]);
+ }
+
+ return !!start && !!end;
+}
+
+/** Flashes `elements` for a moment. */
+export function flashBlocks(elements: Element[]) {
+ for (const element of elements) {
+ element.classList.add(FLASH_CLASS);
+ setTimeout(() => element.classList.remove(FLASH_CLASS), FLASH_DURATION_MS);
+ }
+}
+
+/**
+ * Returns the elements that render the top and bottom of `block`. A tab is `display: contents`, so
+ * its title and panel render it.
+ */
+export function getBlockBoxes(block: Element): { top: Element; bottom: Element } {
+ if (block.classList.contains(TAB_CLASSES.tab)) {
+ const title = block.querySelector(`:scope > .${TAB_CLASSES.tabTitle}`);
+ const panel = block.querySelector(`:scope > .${TAB_CLASSES.tabPanel}`);
+ if (title && panel) {
+ return { top: title, bottom: panel };
+ }
+ }
+ return { top: block, bottom: block };
+}
+
+/** The outermost elements inside the range from `start` to `end`, both included. */
+export function getBlockRangeElements(start: Element, end: Element) {
+ const range = start.ownerDocument.createRange();
+ range.setStartBefore(start);
+ range.setEndAfter(end);
+ return getElementsInRange(range, range.commonAncestorContainer);
+}
+
+/** Opens the `<details>` and shows the tabs that enclose `element`. */
+function reveal(element: Element) {
+ let details = element.closest("details");
+ while (details) {
+ details.open = true;
+ details = details.parentElement?.closest("details") ?? null;
+ }
+ revealTab(element);
+}
+
+function getElementsInRange(range: Range, parent: Node): Element[] {
+ const elements: Element[] = [];
+ for (const child of Array.from(parent.childNodes)) {
+ if (!(child instanceof Element) || !range.intersectsNode(child)) {
+ continue;
+ }
+
+ if (isInRange(range, child)) {
+ elements.push(child);
+ } else {
+ elements.push(...getElementsInRange(range, child));
+ }
+ }
+
+ return elements;
+}
+
+function isInRange(range: Range, node: Node) {
+ const nodeRange = range.cloneRange();
+ nodeRange.selectNode(node);
+ return range.compareBoundaryPoints(Range.START_TO_START, nodeRange) <= 0
+ && range.compareBoundaryPoints(Range.END_TO_END, nodeRange) >= 0;
+}
diff --git a/packages/ckeditor5/src/theme/block_reference.css b/packages/ckeditor5/src/theme/block_reference.css
new file mode 100644
index 0000000000..d5b4e011c1
--- /dev/null
+++ b/packages/ckeditor5/src/theme/block_reference.css
@@ -0,0 +1,47 @@
+:where(:root) {
+ --block-reference-flash-color: rgb(255 200 0 / 40%);
+}
+
+.block-reference-flash {
+ /* The duration matches FLASH_DURATION_MS in block_reference_read_only.ts. */
+ animation: block-reference-flash 1.5s ease-out;
+}
+
+@keyframes block-reference-flash {
+ from,
+ 30% {
+ box-shadow:
+ 0 0 0 4px var(--block-reference-flash-color),
+ inset 0 0 0 100vmax var(--block-reference-flash-color);
+ }
+
+ to {
+ box-shadow: 0 0 0 4px transparent, inset 0 0 0 100vmax transparent;
+ }
+}
+
+.block-reference-highlight {
+ box-shadow:
+ 0 0 0 4px var(--block-reference-flash-color),
+ inset 0 0 0 100vmax var(--block-reference-flash-color);
+}
+
+/* A tab is `display: contents` (tabs.css), so its title and panel paint the effect instead. */
+.trilium-tab.block-reference-flash > :is(.trilium-tab-title, .trilium-tab-panel) {
+ animation: block-reference-flash-tab 1.5s ease-out;
+}
+
+.trilium-tab.block-reference-highlight > :is(.trilium-tab-title, .trilium-tab-panel) {
+ box-shadow: inset 0 0 0 100vmax var(--block-reference-flash-color);
+}
+
+@keyframes block-reference-flash-tab {
+ from,
+ 30% {
+ box-shadow: inset 0 0 0 100vmax var(--block-reference-flash-color);
+ }
+
+ to {
+ box-shadow: inset 0 0 0 100vmax transparent;
+ }
+}
diff --git a/packages/share-theme/src/index.spec.ts b/packages/share-theme/src/index.spec.ts
index ad7579213d..651c947857 100644
--- a/packages/share-theme/src/index.spec.ts
+++ b/packages/share-theme/src/index.spec.ts
@@ -16,6 +16,7 @@ const mocks = vi.hoisted(() => ({
setupMermaid: vi.fn(),
applyTabs: vi.fn(),
revealFragment: vi.fn(),
+ showBlockReference: vi.fn(),
enhanceLinkPreviews: vi.fn(),
setupAppView: vi.fn()
}));
@@ -39,6 +40,9 @@ vi.mock("@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js", () => ({
applyTabs: mocks.applyTabs,
revealFragment: mocks.revealFragment
}));
+vi.mock("@triliumnext/ckeditor5/src/plugins/block_reference/block_reference_read_only.js", () => ({
+ showBlockReference: mocks.showBlockReference
+}));
vi.mock("@triliumnext/commons/src/lib/link_embed_dom.js", () => ({
enhanceLinkPreviews: mocks.enhanceLinkPreviews
}));
@@ -108,6 +112,28 @@ describe("share theme entry", () => {
expect(panel.scrollIntoView).toHaveBeenCalledTimes(2);
});
+ it("shows the blocks the address links to once the page is shown, after the tabs", async () => {
+ document.body.className = "type-text";
+ document.body.innerHTML = `<div id="content"></div>`;
+ history.replaceState(null, "", "?block=a%201:b");
+
+ document.dispatchEvent(new Event("DOMContentLoaded"));
+
+ await vi.waitFor(() => expect(mocks.showBlockReference)
+ .toHaveBeenCalledExactlyOnceWith(document.body, "a 1:b"));
+ expect(mocks.whenActivated).toHaveBeenCalledOnce();
+ expect(mocks.applyTabs.mock.invocationCallOrder[0])
+ .toBeLessThan(mocks.showBlockReference.mock.invocationCallOrder[0]);
+
+ mocks.showBlockReference.mockClear();
+ history.replaceState(null, "", "?other=1");
+ document.dispatchEvent(new Event("DOMContentLoaded"));
+ await vi.dynamicImportSettled();
+
+ expect(mocks.showBlockReference).not.toHaveBeenCalled();
+ history.replaceState(null, "", location.pathname);
+ });
+
it("treats content styled as text like a text note, with or without a content element", () => {
document.body.className = "type-book";
document.body.innerHTML = `<div id="content" class="ck-content"></div>`;
diff --git a/packages/share-theme/src/index.ts b/packages/share-theme/src/index.ts
index c6c5789ae5..a979c9fbc3 100644
--- a/packages/share-theme/src/index.ts
+++ b/packages/share-theme/src/index.ts
@@ -78,6 +78,7 @@ document.addEventListener("DOMContentLoaded", () => {
} else if (classList.contains("type-book")) {
$try(() => setupAppView(".share-collection", () => import("./content/collection_view.js")));
}
+ whenActivated(() => $try(showLinkedBlocks));
});
function setupTabs() {
@@ -91,3 +92,13 @@ function setupTabs() {
showFragment();
window.addEventListener("hashchange", showFragment);
}
+
+/** Scrolls to the blocks that the `block` URL parameter points at and flashes them. */
+async function showLinkedBlocks() {
+ const block = new URLSearchParams(location.search).get("block");
+ if (block) {
+ const { showBlockReference } = await import(
+ "@triliumnext/ckeditor5/src/plugins/block_reference/block_reference_read_only.js");
+ showBlockReference(document.body, block);
+ }
+}