Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/links: fix an issue

Adorian Doran2870115cc041
diff --git a/apps/client/src/services/link.spec.ts b/apps/client/src/services/link.spec.ts
index a4b5bf5e7c..25de04c760 100644
--- a/apps/client/src/services/link.spec.ts
+++ b/apps/client/src/services/link.spec.ts
@@ -1346,14 +1346,21 @@ describe("block references", () => {
expect(linkService.getReferenceLinkTitleSync(href("gone"), "Stored", live))
.toBe("block_reference.broken");
- // The editor of an excerpt holds only some blocks; the others are read from the note.
- const excerpt = document.createElement("div");
- excerpt.innerHTML = content;
- const $outside = $("<span>");
- await linkService.loadReferenceLinkTitle($outside, href("h"), undefined, undefined, excerpt);
- expect($outside.html()).toBe("Saved");
- expect($outside.hasClass("block-reference-broken")).toBe(false);
- expect(linkService.getReferenceLinkTitleSync(href("h"), "Stored", excerpt)).toBe("Saved");
+ // The editor of an excerpt holds only some blocks, and the others are read from the note.
+ // In an editor of the whole note, a block missing from it is gone, whatever the note saved.
+ const partial = document.createElement("div");
+ partial.innerHTML = content;
+ const $excerpt = $("<span>");
+ const $whole = $("<span>");
+ await linkService.loadReferenceLinkTitle(
+ $excerpt, href("h"), undefined, undefined, partial, true);
+ await linkService.loadReferenceLinkTitle($whole, href("h"), undefined, undefined, partial);
+ expect([ $excerpt, $whole ].map(($el) => [
+ $el.text(), $el.hasClass("block-reference-broken")
+ ])).toStrictEqual([ [ "Saved", false ], [ "block_reference.broken", true ] ]);
+ expect([ true, false ].map((isExcerpt) =>
+ linkService.getReferenceLinkTitleSync(href("h"), "Stored", partial, isExcerpt)))
+ .toStrictEqual([ "Saved", "block_reference.broken" ]);
});
it("adds no label to a reference to the blocks of a note that cannot be read", async () => {
diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.ts
index ff513ac9d2..d3513fd271 100644
--- a/apps/client/src/services/link.ts
+++ b/apps/client/src/services/link.ts
@@ -726,15 +726,17 @@ function linkContextMenu(e: PointerEvent) {
/**
* Fills `$el` with the label of the reference link to `href`. A link to blocks of `hostNoteId`, the
* note the link is in, shows only the text of the blocks, read from `content` when it holds them,
- * as an editor does. A link to a note that does not exist shows `storedTitle`, the title the
- * content holds for it.
+ * as an editor does. When `isExcerpt`, `content` holds only some blocks of the note, and the others
+ * are read from the saved note. A link to a note that does not exist shows `storedTitle`, the title
+ * the content holds for it.
*/
async function loadReferenceLinkTitle(
$el: JQuery<HTMLElement>,
href: string | null | undefined = null,
hostNoteId?: string,
storedTitle?: string,
- content?: ParentNode | null
+ content?: ParentNode | null,
+ isExcerpt = false
) {
const $link = $el[0].tagName === "A" ? $el : $el.find("a");
@@ -758,9 +760,7 @@ async function loadReferenceLinkTitle(
if (viewScope?.block && note && (content || noteId === hostNoteId)) {
const liveLabel = content ? getBlockReferenceLabel(content, viewScope.block) : null;
- // The editor of an excerpt holds only some blocks of the note, so the others are read
- // from the saved note.
- const label = liveLabel && !liveLabel.isBroken
+ const label = liveLabel && !(liveLabel.isBroken && isExcerpt)
? liveLabel
: await loadBlockReferenceLabel(note, viewScope.block) ?? liveLabel;
if (label) {
@@ -851,10 +851,11 @@ async function getReferenceLinkTitle(href: string) {
/**
* The label of the reference link to `href`, from the notes already in froca. A link to a note that
* is not there gets `storedTitle`, the title the content holds for it. A link to blocks of
- * `content`, the content the link is in, gets their text.
+ * `content`, the content the link is in, gets their text. When `isExcerpt`, `content` holds only
+ * some blocks of the note, and the others are read from the saved note.
*/
function getReferenceLinkTitleSync(
- href: string, storedTitle?: string, content?: ParentNode | null
+ href: string, storedTitle?: string, content?: ParentNode | null, isExcerpt = false
) {
const { noteId, viewScope } = parseNavigationStateFromUrl(href);
if (!noteId) {
@@ -864,8 +865,7 @@ function getReferenceLinkTitleSync(
const note = froca.getNoteFromCache(viewScope?.card || noteId);
if (content && viewScope?.block) {
const label = getBlockReferenceLabel(content, viewScope.block);
- // As in `loadReferenceLinkTitle()`, blocks outside an excerpt are read from the saved note.
- const savedLabel = label.isBroken && note
+ const savedLabel = label.isBroken && isExcerpt && note
? getCachedBlockReferenceLabel(note, viewScope.block)
: null;
return (savedLabel ?? label).text;
diff --git a/apps/client/src/widgets/type_widgets/text/CKEditor.spec.tsx b/apps/client/src/widgets/type_widgets/text/CKEditor.spec.tsx
index 50188465b6..8755c79a97 100644
--- a/apps/client/src/widgets/type_widgets/text/CKEditor.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/CKEditor.spec.tsx
@@ -192,13 +192,15 @@ describe("CKEditor", () => {
await vi.waitFor(() => expect(buildConfig).toHaveBeenCalled());
const opts = vi.mocked(buildConfig).mock.lastCall?.[0];
expect(opts?.getNoteId?.()).toBe("host1");
+ expect(opts?.isExcerpt?.()).toBe(false);
mocks.note = { noteId: "host2", title: "Other" };
await act(async () => {
- render(<CKEditor {...props} className="force-rerender" />, host);
+ render(<CKEditor {...props} className="force-rerender" isExcerpt />, host);
});
expect(opts?.getNoteId?.()).toBe("host2");
+ expect(opts?.isExcerpt?.()).toBe(true);
});
it("lists the headings of its editor and gives one a block id, while it has one", async () => {
diff --git a/apps/client/src/widgets/type_widgets/text/CKEditor.tsx b/apps/client/src/widgets/type_widgets/text/CKEditor.tsx
index 0134efbb00..10101e5c87 100644
--- a/apps/client/src/widgets/type_widgets/text/CKEditor.tsx
+++ b/apps/client/src/widgets/type_widgets/text/CKEditor.tsx
@@ -90,13 +90,15 @@ interface CKEditorProps extends Pick<HTMLAttributes<HTMLDivElement>, "className"
contentEmbedHandlers?: ContentEmbedHandlers;
/** Shows the link dialog, as `internalLink.showDialog` does. */
showLinkDialog?: (request: LinkDialogRequest) => (() => void) | null;
+ /** Whether the editor holds only some blocks of its note, as the editor of an excerpt does. */
+ isExcerpt?: boolean;
}
export default function CKEditor({
containerRef: externalContainerRef, contentLanguage, className, tabIndex, isClassicEditor,
editorRef: externalEditorRef, onNotificationWarning, onNotificationInfo, onEditorError,
onChange, onEditorInitialized, editorApi, templates, rebuildKey, contentEmbedHandlers,
- showLinkDialog
+ showLinkDialog, isExcerpt
}: CKEditorProps) {
const containerRef = useSyncedRef<HTMLDivElement>(externalContainerRef, null);
const editorRef = useRef<CKTextEditor>(null);
@@ -169,6 +171,8 @@ export default function CKEditor({
contentEmbedHandlersRef.current = contentEmbedHandlers;
const showLinkDialogRef = useRef(showLinkDialog);
showLinkDialogRef.current = showLinkDialog;
+ const isExcerptRef = useRef(isExcerpt);
+ isExcerptRef.current = isExcerpt;
/** The open icon picker request and its balloon container, or `null` when none is open. */
const [ iconPickerRequest, setIconPickerRequest ] = useState<IconPickerRequest | null>(null);
@@ -354,6 +358,7 @@ export default function CKEditor({
return { notePath: parentNotePath, isProtected: parentNote.isProtected, ntxId };
},
getNoteId: () => noteRef.current?.noteId,
+ isExcerpt: () => !!isExcerptRef.current,
getComponentId: () => parentComponent?.componentId,
triggerHostCommand: (command, data) => parentComponent?.triggerCommand(command, data),
showIconPicker,
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index bafc6d8787..c50cab3626 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -538,6 +538,7 @@ export default function EditableText({
rebuildKey={editorRebuildKey}
contentEmbedHandlers={contentEmbedHandlers}
showLinkDialog={showLinkDialog}
+ isExcerpt={block !== undefined}
onChange={() => {
// `getData()` serializes the whole note, so saves wait for typing to pause.
spacedUpdate.resetUpdateTimer();
diff --git a/apps/client/src/widgets/type_widgets/text/config.spec.ts b/apps/client/src/widgets/type_widgets/text/config.spec.ts
index ee537a348f..62c201cfd7 100644
--- a/apps/client/src/widgets/type_widgets/text/config.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.spec.ts
@@ -543,21 +543,35 @@ describe("CK config - tables", () => {
it("titles reference links through the link service, in the note open at the time", async () => {
const loadTitle = vi.spyOn(linkService, "loadReferenceLinkTitle").mockResolvedValue();
- onTestFinished(() => loadTitle.mockRestore());
+ const getTitleSync = vi.spyOn(linkService, "getReferenceLinkTitleSync")
+ .mockReturnValue("Title");
+ onTestFinished(() => {
+ loadTitle.mockRestore();
+ getTitleSync.mockRestore();
+ });
let noteId = "n1";
- const referenceLink = (await buildConfig(baseOpts({ getNoteId: () => noteId }))).referenceLink;
+ let isExcerpt = false;
+ const referenceLink = (await buildConfig(baseOpts({
+ getNoteId: () => noteId, isExcerpt: () => isExcerpt
+ }))).referenceLink;
const element = document.createElement("span");
const content = document.createElement("div");
referenceLink?.renderTitle?.(element, "#root/n2", "Stored", content);
+ expect(referenceLink?.getTitleSync?.("#root/n2", "Stored", content)).toBe("Title");
noteId = "n3";
+ isExcerpt = true;
referenceLink?.renderTitle?.(element, "#root/n2");
+ referenceLink?.getTitleSync?.("#root/n2");
expect(referenceLink?.getTitle).toBe(linkService.getReferenceLinkTitle);
- expect(referenceLink?.getTitleSync).toBe(linkService.getReferenceLinkTitleSync);
expect(loadTitle.mock.calls.map(([ $el, ...rest ]) => [ $el[0], ...rest ])).toEqual([
- [ element, "#root/n2", "n1", "Stored", content ],
- [ element, "#root/n2", "n3", undefined, undefined ]
+ [ element, "#root/n2", "n1", "Stored", content, false ],
+ [ element, "#root/n2", "n3", undefined, undefined, true ]
+ ]);
+ expect(getTitleSync.mock.calls).toEqual([
+ [ "#root/n2", "Stored", content, false ],
+ [ "#root/n2", undefined, undefined, true ]
]);
});
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index 3d0359715e..bf015c1859 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -66,6 +66,11 @@ export interface BuildEditorOptions {
getChildNoteParent?: () => ChildNoteParent | undefined;
/** The ID of the note the editor is open on. A getter for the same reason as `getNoteLocation`. */
getNoteId?: () => string | undefined;
+ /**
+ * Whether the editor holds only some blocks of its note, as the editor of an excerpt does. A
+ * getter for the same reason as `getNoteLocation`.
+ */
+ isExcerpt?: () => boolean;
/** The ID of the component that hosts the editor, which changes the editor makes are sent as. */
getComponentId?: () => string | undefined;
/** Triggers `command` with `data` on the component that hosts the editor. */
@@ -252,10 +257,12 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
formatDateTime: (date, format) => formatDateTime(date, format ?? options.get("customDateTimeFormat")),
referenceLink: {
getTitle: linkService.getReferenceLinkTitle,
- getTitleSync: linkService.getReferenceLinkTitleSync,
+ getTitleSync: (href, storedTitle, content) => linkService.getReferenceLinkTitleSync(
+ href, storedTitle, content, opts.isExcerpt?.()
+ ),
renderTitle: (element, href, storedTitle, content) => {
void linkService.loadReferenceLinkTitle(
- $(element), href, opts.getNoteId?.(), storedTitle, content
+ $(element), href, opts.getNoteId?.(), storedTitle, content, opts.isExcerpt?.()
);
},
fixLink: (storedTitle, fix) => {