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
client: fix an issue
Adorian Doran8b250c2ef060
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.tsindex c8a9287082..003a9c0087 100644--- a/packages/ckeditor5/src/plugins/block_reference/block_reference_read_only.ts+++ b/packages/ckeditor5/src/plugins/block_reference/block_reference_read_only.ts@@ -23,17 +23,24 @@ export function showBlockReference(container: Element, value: string) {const { start, end } = resolveBlockReference<HTMLElement>(container, value);const first = start ?? end;if (first) {- const last = start && end ? end : first;- // `first` is revealed last, so it stays shown when both are in different tabs of a block.- reveal(last);- reveal(first);- scrollToBlocks(first, last);- flashBlocks(getBlockRangeElements(first, last));+ showBlocks(first, start && end ? end : first);}return !!start && !!end;}+/**+ * Scrolls to the blocks from `first` to `last`, in document order, and flashes them, as+ * {@link showBlockReference} does.+ */+export function showBlocks(first: Element, last: Element) {+ // `first` is revealed last, so it stays shown when both are in different tabs of a block.+ reveal(last);+ reveal(first);+ scrollToBlocks(first, last);+ flashBlocks(getBlockRangeElements(first, last));+}+/*** Scrolls to the element whose `id` is `name` in `container`, such as an anchor, and flashes the* block that holds it, as {@link showBlockReference} does. Returns `false` when it is missing.@@ -42,15 +49,19 @@ export function showAnchor(container: Element, name: string) {// Exact comparison: an anchor name can hold characters that a CSS selector does not allow.const anchor = Array.from(container.querySelectorAll("[id]")).find((element) => element.id === name);- if (!anchor) {- return false;+ if (anchor) {+ showAnchorElement(anchor);}+ return !!anchor;+}++/** Scrolls to the block that holds `anchor` and flashes it, as {@link showAnchor} does. */+export function showAnchorElement(anchor: Element) {const block = anchor.closest(ANCHOR_BLOCKS) ?? anchor;reveal(block);scrollToBlocks(block, block);flashBlocks([ block ]);- return true;}/** Flashes `elements` for a moment. */diff --git a/packages/ckeditor5/src/plugins/note_link_toolbar.spec.ts b/packages/ckeditor5/src/plugins/note_link_toolbar.spec.tsindex e6361ff676..d03fc36e71 100644--- a/packages/ckeditor5/src/plugins/note_link_toolbar.spec.ts+++ b/packages/ckeditor5/src/plugins/note_link_toolbar.spec.ts@@ -156,13 +156,15 @@ describe("links within the note", () => {expect(["#root/x/abc?block=h", "#root/abc?block=p", "#root/abc?block=h:p",- "#root/abc?block=gone", "#root/x/abc?bookmark=top", "#root/abc?bookmark=gone",+ "#root/abc?block=gone", "#root/abc?block=a:b:c", "#root/x/abc?bookmark=top",+ "#root/abc?bookmark=gone","#root/abc", "#root/def?block=h", "#root/def?bookmark=top","#root/abc?viewMode=attachments&attachmentId=def&block=h","#top", "#gone", "https://a.example/#top"].map(linkTo)).toStrictEqual(["Jump to heading", "Jump to block", "Jump to block",- "Jump to block", "Jump to anchor", "Jump to anchor",+ "Jump to block", "Jump to block", "Jump to anchor",+ "Jump to anchor","Open note", "Open note", "Open note","Open attachment","Jump to anchor", "#gone", "https://a.example/#top"@@ -176,15 +178,31 @@ describe("links within the note", () => {return [ cancel.mock.calls.length, takeFlashed(editor) ];};expect([- "#top", "#root/x/abc?block=h", "#root/abc?block=h:p", "#root/x/abc?bookmark=top",+ "#top", "#root/x/abc?block=h", "#root/abc?block=h:p", "#root/abc?block=p:h",+ "#root/x/abc?bookmark=top","#root/def?block=h", "#root/def?bookmark=top", "#root/abc?block=h:gone",- "#root/abc?bookmark=gone"+ "#root/abc?block=a:b:c", "#root/abc?bookmark=gone"].map(navigate)).toEqual([- [ 1, [ "text" ] ], [ 1, [ "Intro" ] ], [ 1, [ "Intro", "text" ] ], [ 1, [ "text" ] ],+ [ 1, [ "text" ] ], [ 1, [ "Intro" ] ], [ 1, [ "Intro", "text" ] ],+ [ 1, [ "Intro", "text" ] ], [ 1, [ "text" ] ],[ 0, [] ], [ 0, [] ], [ 0, [] ],- [ 0, [] ]+ [ 0, [] ], [ 0, [] ]]);+ // The content of an included note, such as a copy of this note, renders in the editor+ // with the same block ids and anchor names.+ const copy = document.createElement("div");+ copy.innerHTML = "<h2 data-trilium-block-id=\"h\">Copy</h2>"+ + "<p data-trilium-block-id=\"gone\"><a id=\"top\"></a><a id=\"gone\"></a>Copy</p>";+ editor.editing.view.getDomRoot()?.prepend(copy);+ expect([+ "#root/abc?block=h", "#root/abc?block=gone", "#top", "#root/abc?bookmark=top",+ "#root/abc?bookmark=gone", "#gone"+ ].map(navigate)).toEqual([+ [ 1, [ "Intro" ] ], [ 0, [] ], [ 1, [ "text" ] ], [ 1, [ "text" ] ], [ 0, [] ], [ 0, [] ]+ ]);+ copy.remove();+const click = (element: HTMLElement | null | undefined, init: MouseEventInit = {}) => {let isCanceled = false;const record = (event: Event) => {diff --git a/packages/ckeditor5/src/plugins/note_link_toolbar.ts b/packages/ckeditor5/src/plugins/note_link_toolbar.tsindex 0b401b62be..eb21ebad3e 100644--- a/packages/ckeditor5/src/plugins/note_link_toolbar.ts+++ b/packages/ckeditor5/src/plugins/note_link_toolbar.ts@@ -1,13 +1,11 @@-import {- parseBlockRange, resolveBlockReference-} from '@triliumnext/commons/src/lib/block_reference.js';+import { parseBlockRange } from '@triliumnext/commons/src/lib/block_reference.js';import {BookmarkEditing, ButtonView, Command, type Editor, IconUnlink, LinkUI, type ModelElement,- Plugin, WidgetToolbarRepository+ Plugin, type ViewElement, WidgetToolbarRepository} from 'ckeditor5';import { BLOCK_ID } from './block_reference/block_reference_editing.js';-import { showAnchor, showBlockReference } from './block_reference/block_reference_read_only.js';+import { showAnchorElement, showBlocks } from './block_reference/block_reference_read_only.js';import { createCopyUrlButton } from './copy_link_url.js';import InternalLinkPlugin from './internallink.js';import {@@ -155,11 +153,8 @@ function getOpenLinkLabel(editor: Editor, href: string) {const target = getInNoteTarget(editor, href);if (target?.block) {- const range = parseBlockRange(target.block);- const block = range && range.startId === range.endId- ? findBlock(editor, range.startId)- : null;- return block && /^heading[1-6]$/.test(block.name)+ const [ first, last ] = findBlockRange(editor, target.block) ?? [];+ return first && first === last && /^heading[1-6]$/.test(first.name)? editor.t('Jump to heading'): editor.t('Jump to block');}@@ -181,6 +176,21 @@ function getInNoteTarget(editor: Editor, href: string) {return { block: params.get('block'), bookmark: params.get('bookmark') };}+/**+ * The first and the last block of the note in the editor that `value`, a `block` link parameter,+ * points at, in document order, or `null` when one is missing.+ */+function findBlockRange(editor: Editor, value: string): [ ModelElement, ModelElement ] | null {+ const range = parseBlockRange(value);+ const start = range && findBlock(editor, range.startId);+ const end = range && range.endId !== range.startId ? findBlock(editor, range.endId) : start;+ if (!start || !end) {+ return null;+ }++ return end.isBefore(start) ? [ end, start ] : [ start, end ];+}+/** The block of the note in the editor whose block id is `blockId`, or `null`. */function findBlock(editor: Editor, blockId: string) {const root = editor.model.document.getRoot() as ModelElement;@@ -195,17 +205,30 @@ function findBlock(editor: Editor, blockId: string) {/** The anchor of the document that `href` links to, or `null`. */function getLinkedAnchor(editor: Editor, href: string) {- if (!href.startsWith('#') || !editor.plugins.has(BookmarkEditing)) {- return null;- }+ return href.startsWith('#') ? findAnchor(editor, href.slice(1)) : null;+}- return editor.plugins.get(BookmarkEditing).getElementForBookmarkId(href.slice(1));+/** The anchor of the note in the editor named `name`, or `null`. */+function findAnchor(editor: Editor, name: string) {+ return editor.plugins.has(BookmarkEditing)+ ? editor.plugins.get(BookmarkEditing).getElementForBookmarkId(name)+ : null;+}++/** The element of the editing view's DOM that renders `element` of the model. */+function toDomElement(editor: Editor, element: ModelElement) {+ // Every element in the model document has a view element.+ const viewElement = editor.editing.mapper.toViewElement(element) as ViewElement;+ return editor.editing.view.domConverter.mapViewToDom(viewElement);}/*** Scrolls to the blocks or the anchor of the note that `href` links to and flashes them, as a link* within a page does. Returns whether it does. A click with Ctrl, Cmd or Shift is left to the* host, which opens the link in a new tab or window.+ *+ * The targets are found in the model, as the editing view also renders the content of included+ * notes, with block ids and anchor names of their own.*/function jumpInNote(editor: Editor, href: string) {// The `navigate` event of `LinkUI` does not pass the click, so it is read from `window`.@@ -214,16 +237,25 @@ function jumpInNote(editor: Editor, href: string) {return false;}- const root = editor.editing.view.getDomRoot() as HTMLElement;const target = getInNoteTarget(editor, href);if (target?.block) {// The host opens a broken range in the note context, which shows the error.- const { start, end } = resolveBlockReference(root, target.block);- return !!start && !!end && showBlockReference(root, target.block);+ const [ first, last ] = (findBlockRange(editor, target.block) ?? [])+ .map(block => toDomElement(editor, block));+ if (first && last) {+ showBlocks(first, last);+ }+ return !!first && !!last;}- const anchor = target?.bookmark ?? (getLinkedAnchor(editor, href) ? href.slice(1) : null);- return !!anchor && showAnchor(root, anchor);+ const anchor = target?.bookmark+ ? findAnchor(editor, target.bookmark)+ : getLinkedAnchor(editor, href);+ const element = anchor && toDomElement(editor, anchor);+ if (element) {+ showAnchorElement(element);+ }+ return !!element;}declare module 'ckeditor5' {