Skip to content

Build your personal knowledge base with Trilium Notes

feat(mind-map): open the link dialog on the link being changed

Elian Doran375c32301cb6
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index da0359a49e..9c277c934a 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -55,6 +55,7 @@
},
"add_link": {
"add_link": "Add link",
+ "edit_link": "Edit link",
"help_on_links": "Help on links",
"note": "Note",
"search_note": "search for note by its name",
@@ -63,7 +64,8 @@
"link_title": "Link title",
"anchor": "Anchor (optional)",
"anchor_none": "None (link to note)",
- "button_add_link": "Add link"
+ "button_add_link": "Add link",
+ "button_edit_link": "Save link"
},
"branch_prefix": {
"edit_branch_prefix": "Edit branch prefix",
diff --git a/apps/client/src/widgets/dialogs/add_link.tsx b/apps/client/src/widgets/dialogs/add_link.tsx
index ead35263dd..a90dc61d67 100644
--- a/apps/client/src/widgets/dialogs/add_link.tsx
+++ b/apps/client/src/widgets/dialogs/add_link.tsx
@@ -45,6 +45,15 @@ export interface AddLinkOpts {
* `linkTitle` is handed over all the same, so a caller minding it need not be told twice.
*/
targetOnly?: boolean;
+ /**
+ * The link the dialog opens on, in the shape a pick comes back in.
+ *
+ * A caller changing a link that is already there says what it points at, and the field opens
+ * filled with it: changing a link is then the same gesture as making one, and a dialog left as
+ * it was applies what was already pointed at rather than refusing for want of a pick. The
+ * dialog says it is editing rather than adding, too.
+ */
+ currentLink?: Suggestion;
addLink(notePath: string, linkTitle: string | null, externalLink?: boolean): Promise<void>;
}
@@ -70,6 +79,11 @@ export default function AddLinkDialog() {
} else {
setLinkType("reference-link");
}
+
+ // Taken as though it had just been picked, so that a dialog opened on a link and left alone
+ // has one to apply — and so that everything a pick settles (the title, the anchors a note
+ // offers, whether it is a link outside Trilium at all) is settled for it as well.
+ setSuggestion(opts?.currentLink ?? null);
}, [ opts ]);
async function setDefaultLinkTitle(noteId: string) {
@@ -134,12 +148,28 @@ export default function AddLinkDialog() {
}
}, [selectedBookmark, noteTitle]);
- function onShown() {
+ async function onShown() {
const $autocompleteEl = refToJQuerySelector(autocompleteRef);
- if (!opts?.text) {
+ const currentNotePath = opts?.currentLink?.notePath;
+ const currentNoteId = currentNotePath && tree.getNoteIdFromUrl(currentNotePath);
+
+ // A link already there fills the field the way text handed to the dialog does: a note by the
+ // name it goes by, an address as it stands. Through the autocomplete rather than around it —
+ // a value the field is given behind its back is one it does not know it holds, so nothing is
+ // searched for it and it is wiped the moment the field is left.
+ const text = (currentNoteId ? await tree.getNoteTitle(currentNoteId) : opts?.currentLink?.externalLink)
+ || opts?.text;
+
+ if (!text) {
note_autocomplete.showRecentNotes($autocompleteEl);
} else {
- note_autocomplete.setText($autocompleteEl, opts.text);
+ note_autocomplete.setText($autocompleteEl, text);
+
+ // What `setText` fills the field with is something being typed, which has nothing picked
+ // behind it; the note the dialog opens on is picked already.
+ if (currentNotePath) {
+ $autocompleteEl.setSelectedNotePath(currentNotePath);
+ }
}
// to be able to quickly remove entered text
@@ -166,9 +196,9 @@ export default function AddLinkDialog() {
className="add-link-dialog"
size="lg"
maxWidth={1000}
- title={t("add_link.add_link")}
+ title={opts?.currentLink ? t("add_link.edit_link") : t("add_link.add_link")}
helpPageId="QEAPj01N5f7w"
- footer={<Button text={t("add_link.button_add_link")} keyboardShortcut="Enter" />}
+ footer={<Button text={opts?.currentLink ? t("add_link.button_edit_link") : t("add_link.button_add_link")} keyboardShortcut="Enter" />}
onSubmit={onSubmit}
onShown={onShown}
onHidden={() => {
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx
index 94c651bfe0..a10fa929c3 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx
@@ -754,9 +754,9 @@ describe("NodePanel", () => {
linkField(container)?.click();
// A node reads as its own topic, so the dialog is asked for what is pointed at and nothing
- // else — no title beside it, and nothing to open the field on for a node linked to nothing.
+ // else — no title to write beside it.
expect(triggerCommand).toHaveBeenCalledWith("showAddLinkDialog",
- expect.objectContaining({ targetOnly: true, hasSelection: false, text: "" }));
+ expect.objectContaining({ targetOnly: true, hasSelection: false }));
// What comes back is stored the way the map stores it: a note as an in-app address, an
// address as one we could follow. Every node the panel was handed takes it.
@@ -775,20 +775,27 @@ describe("NodePanel", () => {
expect(reshapeNode).not.toHaveBeenCalled();
});
- it("opens the dialog on the address a node already carries, a note having none to type", () => {
+ it("opens the dialog on the link the selection already carries", () => {
const triggerCommand = vi.spyOn(appContext, "triggerCommand").mockReturnValue(undefined);
onTestFinished(() => triggerCommand.mockRestore());
- const openOn = (link: string) => {
- const nodes = [buildNode({ hyperLink: link })];
+ const openOn = (...nodes: NodeObj[]) => {
linkField(renderInto(<NodePanel mind={buildMind(nodes).mind} noteId="mapNote" nodes={nodes} />))?.click();
- return (triggerCommand.mock.lastCall?.[1] as AddLinkOpts).text;
+ return (triggerCommand.mock.lastCall?.[1] as AddLinkOpts).currentLink;
};
- // An address is what someone typed, so it is handed back to be edited; the address a note is
- // stored as is not, and the dialog opens on the notes last visited instead.
- expect(openOn("https://example.com/page")).toBe("https://example.com/page");
- expect(openOn("#root/abc123")).toBe("");
+ // Handed over as the pick it was, so that the dialog opens on it and offers to change it.
+ expect(openOn(buildNode({ hyperLink: "#root/abc123" }))).toEqual({ notePath: "root/abc123" });
+ expect(openOn(buildNode({ hyperLink: "https://example.com/page" })))
+ .toEqual({ externalLink: "https://example.com/page" });
+
+ // Nothing to open on: a node linked to nothing, and a selection pointing several ways at
+ // once — there is no one link there to be changed.
+ expect(openOn(buildNode())).toBeUndefined();
+ expect(openOn(
+ buildNode({ id: "a", hyperLink: "https://example.com" }),
+ buildNode({ id: "b" })
+ )).toBeUndefined();
});
it("unlinks the selection from the button beside the field, which is only there to be unlinked", () => {
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx
index 7e2794c958..1ad857741b 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx
@@ -21,7 +21,7 @@ import IconPicker from "../../react/IconPicker";
import SegmentedChoice from "../../react/SegmentedChoice";
import TabStrip, { type TabStripTabDefinition } from "../../react/TabStrip";
import { fitNodeImage, getNodeImageShape, nearestNodeImageWidth, NODE_IMAGE_SHAPES, NODE_IMAGE_WIDTHS, type NodeImage as NodeImageData, type NodeImageShape, shapeNodeImage, uploadNodeImage } from "./images";
-import { describeExternalLink, linkFromSuggestion } from "./links";
+import { describeExternalLink, linkFromSuggestion, suggestionFromLink } from "./links";
import NodeMemo, { getNodeMemo } from "./NodeMemo";
/**
@@ -586,7 +586,8 @@ function buildImageShapeOptions() {
* no title to write beside it. A dialog rather than a menu hung off the field, which is where the
* picker used to sit — the panel stands in the corner of the map, and a menu anchored there had a
* strip of screen to search a whole tree in, growing and shrinking under the pointer as the results
- * came in.
+ * came in. Where the node points somewhere already, the dialog opens on it, so that changing a link
+ * starts from the one being changed rather than from an empty field.
*
* Unlinking stands beside the field rather than inside what it opens, as it does for the picture
* above: dropping a link is not one more note to pick from.
@@ -603,12 +604,13 @@ function NodeLink({ currentValue, indeterminate, onChange }: {
text={<NodeLinkFace link={currentValue} indeterminate={indeterminate} />}
title={t("mind-map.choose-link")}
onClick={() => void appContext.triggerCommand("showAddLinkDialog", {
- // An address is handed back for editing as it stands; a note is not — what is
- // stored for one is a path rather than anything anyone typed, and the dialog
- // opens on the notes last visited instead.
- text: (currentValue && !parseMindMapNoteLink(currentValue)) ? currentValue : "",
+ text: "",
hasSelection: false,
targetOnly: true,
+ // What the node points at now, so that the dialog opens on it and says it is
+ // being changed rather than made. A selection that disagrees points at nothing
+ // one dialog could open on, and asks to be linked afresh instead.
+ currentLink: !indeterminate ? suggestionFromLink(currentValue) : undefined,
async addLink(target, _linkTitle, externalLink) {
// Only a target we can store is worth taking, as it was when the field was
// picked through directly.
diff --git a/apps/client/src/widgets/type_widgets/mind_map/links.spec.ts b/apps/client/src/widgets/type_widgets/mind_map/links.spec.ts
index 4059597778..4599e51ad2 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/links.spec.ts
+++ b/apps/client/src/widgets/type_widgets/mind_map/links.spec.ts
@@ -1,7 +1,7 @@
import type { Suggestion } from "../../../services/note_autocomplete";
import { describe, expect, it } from "vitest";
-import { describeExternalLink, getNodeLinkHref, linkFromSuggestion, renderNodeLinks } from "./links";
+import { describeExternalLink, getNodeLinkHref, linkFromSuggestion, renderNodeLinks, suggestionFromLink } from "./links";
/** Builds the anchor Mind Elixir gives a node that carries a link. */
function buildNodes(...links: string[]) {
@@ -36,6 +36,22 @@ describe("linkFromSuggestion", () => {
});
});
+describe("suggestionFromLink", () => {
+ it("hands a stored link back as the pick it was, for a picker to open on", () => {
+ expect(suggestionFromLink("#root/abc123")).toEqual({ notePath: "root/abc123" });
+ expect(suggestionFromLink("https://example.com/page")).toEqual({ externalLink: "https://example.com/page" });
+
+ // A node linked to nothing opens the picker on nothing in particular.
+ expect(suggestionFromLink(null)).toBeUndefined();
+ expect(suggestionFromLink("")).toBeUndefined();
+
+ // What it hands over is what the picker gives back, for every link we would store.
+ for (const link of [ "#root/abc123", "https://example.com/page" ]) {
+ expect(linkFromSuggestion(suggestionFromLink(link) ?? null)).toBe(link);
+ }
+ });
+});
+
describe("getNodeLinkHref", () => {
it("follows notes and addresses, and refuses what Trilium would not open", () => {
expect(getNodeLinkHref("#root/abc123")).toBe("#root/abc123");
diff --git a/apps/client/src/widgets/type_widgets/mind_map/links.ts b/apps/client/src/widgets/type_widgets/mind_map/links.ts
index c2d60dcbe7..51f9b7696a 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/links.ts
+++ b/apps/client/src/widgets/type_widgets/mind_map/links.ts
@@ -26,6 +26,19 @@ export function linkFromSuggestion(suggestion: Suggestion | null | undefined): s
return (suggestion?.externalLink ? normalizeExternalUrl(suggestion.externalLink) : null);
}
+/**
+ * What a link a node already carries reads as to the picker it is changed in — the inverse of the
+ * above, for opening that picker on what is already there.
+ */
+export function suggestionFromLink(link: string | null | undefined): Suggestion | undefined {
+ if (!link) {
+ return undefined;
+ }
+
+ const notePath = parseMindMapNoteLink(link)?.notePath;
+ return (notePath ? { notePath } : { externalLink: link });
+}
+
/** The address a link may be followed to, or `null` where it may not be followed at all. */
export function getNodeLinkHref(link: string | null | undefined): string | null {
if (!link) {