Skip to content

Build your personal knowledge base with Trilium Notes

feat(relation_map): draw notes as cards styled like the board's

Elian Dorana6c10f330ea6
diff --git a/apps/client/src/widgets/collections/board/board_drag.ts b/apps/client/src/widgets/collections/board/board_drag.ts
index c34688541e..eaae324c61 100644
--- a/apps/client/src/widgets/collections/board/board_drag.ts
+++ b/apps/client/src/widgets/collections/board/board_drag.ts
@@ -855,7 +855,7 @@ function lift(held: Gesture, container: HTMLElement) {
*/
function countPreview(count: number) {
const preview = document.createElement("div");
- preview.className = "board-note board-drag-count";
+ preview.className = "board-note tn-note-card board-drag-count";
preview.dataset.layers = String(Math.min(count, STACK_LAYERS));
preview.textContent = String(count);
return preview;
diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsx
index 741275e87e..ef0fcb8602 100644
--- a/apps/client/src/widgets/collections/board/card.tsx
+++ b/apps/client/src/widgets/collections/board/card.tsx
@@ -298,7 +298,7 @@ function Card({
return (<>
<div
ref={cardRef}
- className={clsx("board-note", colorClass, {
+ className={clsx("board-note tn-note-card", colorClass, {
shortcut: !!(redirectTo ?? legacyRedirectTo),
dragging: isDragging,
editing: isEditing,
diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.css
index ba1037128c..427dff935a 100644
--- a/apps/client/src/widgets/collections/board/index.css
+++ b/apps/client/src/widgets/collections/board/index.css
@@ -4,12 +4,6 @@
--board-column-background-saturation: 26%;
--board-column-background-lightness: 86%;
- --board-item-background-saturation: 100%;
- --board-item-background-lightness: 96%;
-
- --board-item-hover-background-saturation: 100%;
- --board-item-hover-background-lightness: 98.5%;
-
/* The background of a card being edited. Set to the hover lightness so the colour does not
change when the pointer leaves the card. */
--board-item-editing-background-lightness: var(--board-item-hover-background-lightness);
@@ -42,11 +36,6 @@
--board-collapse-duration-quick: 0.3s;
--board-expand-duration: 0.2s;
- /* What a card at rest casts on the column it stands in. */
- --board-item-shadow: 1px 1px 4px rgba(0, 0, 0, 0.1);
- /* The lift a card takes under the pointer, named so that a picked-out card can carry its ring
- and this shadow together. */
- --board-item-hover-shadow: 2px 4px 8px rgba(0, 0, 0, 0.1);
/* What a carried card casts, which the stack of a carried selection is drawn behind. */
--board-drag-shadow: 4px 8px 16px rgba(0, 0, 0, 0.5);
/* What a carried copy is drawn at, and what it fades to once it stands against the board's
@@ -61,8 +50,6 @@
carry less weight than the values alone suggest. */
--board-drop-placeholder-background: rgba(0, 0, 0, 0.15);
--board-column-drop-placeholder-background: rgba(0, 0, 0, 0.1);
- /* The ring a picked-out card carries. */
- --board-selected-ring-color: var(--link-color);
/* The wedge in a card's foot corner while cards are being selected, until the card is. */
--board-selection-wedge-size: 14px;
--board-selection-wedge-color: var(--main-border-color);
@@ -75,19 +62,12 @@
than hiding what is behind it. */
--board-edit-backdrop: rgba(0, 0, 0, 0.15);
- /* What the board writes its text and icons in, and what it marks the quieter ones with. A
- column's heading has a pair of its own, standing as it does on the column rather than on a
- card. These are what a plain board paints; on a coloured one the pairs below replace them. */
- --board-text-color: var(--main-text-color);
- --board-muted-text-color: var(--muted-text-color);
+ /* What a column's heading writes its text in, standing as it does on the column rather than on a
+ card (see note_card.css for the card's own). On a coloured column the pairs below replace them. */
--board-column-header-text-color: var(--main-text-color);
--board-column-header-muted-text-color: var(--muted-text-color);
- /* How each is composed where something around carries a hue. The colour lends its hue alone:
- the lightness is the theme's, so a coloured board reads as light as a plain one. */
- --board-text-saturation: 30%;
- --board-text-lightness: 8%;
- --board-muted-text-saturation: 20%;
- --board-muted-text-lightness: 40%;
+ /* How each is composed where the column carries a hue. The colour lends its hue alone: the
+ lightness is the theme's, so a coloured board reads as light as a plain one. */
--board-column-header-text-saturation: 30%;
--board-column-header-text-lightness: 8%;
--board-column-header-muted-text-saturation: 20%;
@@ -469,25 +449,13 @@ body.mobile .board-view-container .board-add-column {
--board-item-hover-saturation: var(--board-item-hover-background-saturation);
}
+/* The colours are composed in note_card.css, from the hue set above. */
.board-view-container .board-note {
- /* Overridden above by anything carrying a hue; without one the item is drained of colour. */
- --board-item-hue: 0;
- --board-item-saturation: 0%;
- --board-item-hover-saturation: 0%;
-
- background-color: hsl(
- var(--board-item-hue),
- var(--board-item-saturation),
- var(--board-item-background-lightness)
- );
+ background-color: var(--note-card-background);
}
.board-view-container .board-note:hover {
- background-color: hsl(
- var(--board-item-hue),
- var(--board-item-hover-saturation),
- var(--board-item-hover-background-lightness)
- );
+ background-color: var(--note-card-hover-background);
}
/* Stated after the rule above, which it shares a specificity with, so a card being edited keeps
@@ -534,16 +502,8 @@ body.mobile .board-view-container .board-add-column {
.board-view-container .board-column.with-hue .board-note,
.board-view-container .board-note.with-hue {
- --board-text-color: hsl(
- var(--board-item-hue),
- var(--board-text-saturation),
- var(--board-text-lightness)
- );
- --board-muted-text-color: hsl(
- var(--board-item-hue),
- var(--board-muted-text-saturation),
- var(--board-muted-text-lightness)
- );
+ --board-text-color: var(--note-card-text-color);
+ --board-muted-text-color: var(--note-card-muted-text-color);
--board-focus-outline-color: hsl(
var(--board-item-hue),
var(--board-focus-outline-saturation),
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index a7547b9e39..9ab4bdf6ac 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -1,3 +1,4 @@
+import "../../note_card.css";
import "./index.css";
import clsx from "clsx";
diff --git a/apps/client/src/widgets/note_card.css b/apps/client/src/widgets/note_card.css
new file mode 100644
index 0000000000..b4e96c203e
--- /dev/null
+++ b/apps/client/src/widgets/note_card.css
@@ -0,0 +1,85 @@
+/*
+ * The surface a note is drawn on as a card: an item of a board collection, a box on a relation
+ * map. The tokens keep the board's names, which themes already set.
+ *
+ * A card is coloured by `--board-item-hue` and the two saturations: its own note's colour where it
+ * has one (`with-hue`, from `FNote.getColorClass()`), otherwise grey. A host can set them from
+ * something else as well, as the board does for the column a card stands in. The defaults below are
+ * wrapped in `:where()` so that any host rule outweighs them.
+ */
+
+/* Defaults for a theme that sets none of its own; the light values, the app's own default look. */
+:where(:root) {
+ --board-item-background-saturation: 100%;
+ --board-item-background-lightness: 96%;
+
+ --board-item-hover-background-saturation: 100%;
+ --board-item-hover-background-lightness: 98.5%;
+
+ /* What a card at rest casts on what it stands on. */
+ --board-item-shadow: 1px 1px 4px rgba(0, 0, 0, 0.1);
+ /* The lift a card takes under the pointer, named so that a picked-out card can carry its ring
+ and this shadow together. */
+ --board-item-hover-shadow: 2px 4px 8px rgba(0, 0, 0, 0.1);
+ /* The ring a picked-out card carries. */
+ --board-selected-ring-color: var(--link-color);
+
+ /* What a card writes its text and icons in, and what it marks the quieter ones with. These are
+ what a card without a hue paints; with one, the pairs below replace them. */
+ --board-text-color: var(--main-text-color);
+ --board-muted-text-color: var(--muted-text-color);
+ /* How each is composed where the card carries a hue. The colour lends its hue alone: the
+ lightness is the theme's, so a coloured card reads as light as a plain one. */
+ --board-text-saturation: 30%;
+ --board-text-lightness: 8%;
+ --board-muted-text-saturation: 20%;
+ --board-muted-text-lightness: 40%;
+}
+
+:where(.tn-note-card) {
+ --board-item-hue: 0;
+ --board-item-saturation: 0%;
+ --board-item-hover-saturation: 0%;
+
+ color: var(--board-text-color);
+ background-color: var(--note-card-background);
+ box-shadow: var(--board-item-shadow);
+ border-radius: 5px;
+}
+
+:where(.tn-note-card:hover) {
+ background-color: var(--note-card-hover-background);
+ box-shadow: var(--board-item-hover-shadow);
+}
+
+:where(.tn-note-card.with-hue) {
+ --board-item-hue: var(--custom-color-hue);
+ --board-item-saturation: var(--board-item-background-saturation);
+ --board-item-hover-saturation: var(--board-item-hover-background-saturation);
+ --board-text-color: var(--note-card-text-color);
+ --board-muted-text-color: var(--note-card-muted-text-color);
+}
+
+/* Composed on the card itself, so they read the hue the card ends up with. */
+.tn-note-card {
+ --note-card-background: hsl(
+ var(--board-item-hue),
+ var(--board-item-saturation),
+ var(--board-item-background-lightness)
+ );
+ --note-card-hover-background: hsl(
+ var(--board-item-hue),
+ var(--board-item-hover-saturation),
+ var(--board-item-hover-background-lightness)
+ );
+ --note-card-text-color: hsl(
+ var(--board-item-hue),
+ var(--board-text-saturation),
+ var(--board-text-lightness)
+ );
+ --note-card-muted-text-color: hsl(
+ var(--board-item-hue),
+ var(--board-muted-text-saturation),
+ var(--board-muted-text-lightness)
+ );
+}
diff --git a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.css b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.css
new file mode 100644
index 0000000000..56e429d6ec
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.css
@@ -0,0 +1,74 @@
+.note-detail-relation-map .note-box {
+ position: absolute !important;
+ z-index: 4;
+ display: flex;
+ align-items: flex-start;
+ gap: 0.5em;
+ box-sizing: border-box;
+ min-width: 150px;
+ max-width: 200px;
+ /* Room at the end for the relation handle. */
+ padding: 0.6em 1.8em 0.6em 0.6em;
+ font-size: 0.9em;
+ line-height: 1.2;
+ cursor: pointer;
+ user-select: none;
+
+ > .note-box-icon {
+ flex-shrink: 0;
+ font-size: 1.1em;
+ line-height: inherit;
+ }
+
+ > .note-box-title {
+ min-width: 0;
+ overflow-wrap: anywhere;
+ }
+
+ > .endpoint {
+ position: absolute;
+ top: 50%;
+ inset-inline-end: 0.6em;
+ width: 0.7em;
+ height: 0.7em;
+ translate: 0 -50%;
+ border-radius: 50%;
+ background-color: var(--board-muted-text-color);
+ opacity: 0.4;
+ cursor: crosshair;
+ }
+
+ &:hover > .endpoint {
+ opacity: 1;
+ }
+
+ &.selected {
+ box-shadow: inset 0 0 0 2px var(--board-selected-ring-color);
+ }
+
+ &.selected:hover {
+ box-shadow:
+ inset 0 0 0 2px var(--board-selected-ring-color),
+ var(--board-item-hover-shadow);
+ }
+
+ &.dragHover {
+ outline: 2px solid var(--board-selected-ring-color);
+ }
+
+ &.relation-map-ghost-note {
+ outline: 1px dashed var(--board-muted-text-color);
+ pointer-events: none;
+ opacity: 0;
+
+ &.visible {
+ opacity: 0.6;
+ }
+ }
+}
+
+/* The two ends of the relation under the pointer. */
+.note-detail-relation-map .note-box:is(.jtk-source-hover, .jtk-target-hover) {
+ background-color: var(--note-card-hover-background);
+ box-shadow: var(--board-item-hover-shadow);
+}
diff --git a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.spec.tsx
new file mode 100644
index 0000000000..0b971d5b10
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.spec.tsx
@@ -0,0 +1,112 @@
+import { render } from "preact";
+import { useRef } from "preact/hooks";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+
+import Component from "../../../components/component";
+import FAttribute from "../../../entities/fattribute";
+import cssClassManager from "../../../services/css_class_manager";
+import froca from "../../../services/froca";
+import LoadResults from "../../../services/load_results";
+import noteAttributeCache from "../../../services/note_attribute_cache";
+import utils from "../../../services/utils";
+import { buildNote } from "../../../test/easy-froca";
+import { ParentComponent } from "../../react/react_utils";
+import type RelationMapApi from "./api";
+import { NoteBox } from "./NoteBox";
+
+describe("relation map NoteBox", () => {
+ let container: HTMLElement | undefined;
+ let component: Component;
+
+ beforeEach(() => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ component = new Component();
+ buildNote({ id: "boxnote", title: "Specification", "#iconClass": "bx bx-bug" });
+ });
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ function Harness({ selected }: { selected: boolean }) {
+ const mapApiRef = useRef<RelationMapApi>(null);
+ return <NoteBox noteId="boxnote" x={10} y={20} mapApiRef={mapApiRef} selected={selected} />;
+ }
+
+ async function mount(selected = false) {
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={component}>
+ <Harness selected={selected} />
+ </ParentComponent.Provider>,
+ container as HTMLElement
+ );
+ });
+ await settle();
+ }
+
+ async function settle() {
+ await act(async () => {
+ for (let i = 0; i < 5; i++) await new Promise((resolve) => setTimeout(resolve));
+ });
+ }
+
+ const box = () => container?.querySelector<HTMLElement>(".note-box") ?? null;
+
+ it("shows the note's icon and title as a card, not as a link", async () => {
+ await mount();
+
+ expect(box()?.classList.contains("tn-note-card")).toBe(true);
+ expect(box()?.querySelector(".note-box-icon")?.classList.contains("bx-bug")).toBe(true);
+ expect(box()?.querySelector(".note-box-title")?.textContent).toBe("Specification");
+ expect(box()?.querySelector("a")).toBeNull();
+ });
+
+ it("follows the note's colour and the selection without dropping the classes jsPlumb added", async () => {
+ await mount();
+ const coloured = cssClassManager.createClassForColor("#ff0000");
+ box()?.classList.add("jtk-draggable");
+ expect(box()?.className).not.toContain(coloured);
+
+ await addColor("#ff0000");
+ await mount(true);
+
+ expect(box()?.className).toContain(coloured);
+ expect(box()?.classList.contains("with-hue")).toBe(true);
+ expect(box()?.classList.contains("selected")).toBe(true);
+ expect(box()?.classList.contains("jtk-draggable")).toBe(true);
+
+ await mount(false);
+ expect(box()?.classList.contains("selected")).toBe(false);
+ expect(box()?.className).toContain(coloured);
+ });
+
+ /** Adds a `#color` label to the note and announces it, as a sync or the color picker would. */
+ async function addColor(value: string) {
+ const attributeId = utils.randomString(12);
+ const noteId = "boxnote";
+ const attribute = new FAttribute(froca, {
+ noteId, attributeId, type: "label", name: "color", value, position: 0, isInheritable: false
+ });
+ froca.attributes[attributeId] = attribute;
+ froca.notes[noteId].attributes.push(attributeId);
+ noteAttributeCache.attributes[noteId] = [ ...(noteAttributeCache.attributes[noteId] ?? []), attribute ];
+
+ const entity = { attributeId, noteId, type: "label", name: "color", value, isDeleted: false };
+ const loadResults = new LoadResults([ {
+ entityName: "attributes", entityId: attributeId, entity, hash: "", isSynced: true, isErased: false
+ } ]);
+ loadResults.addAttribute(attributeId, "someOtherComponent");
+
+ await act(async () => {
+ await component.handleEvent("entitiesReloaded", { loadResults });
+ });
+ await settle();
+ }
+});
diff --git a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx
index 285414736b..e2b325b3e5 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx
@@ -1,14 +1,18 @@
+import "../../note_card.css";
+import "./NoteBox.css";
+
+import clsx from "clsx";
+import { RefObject } from "preact";
import { useEffect, useMemo, useState } from "preact/hooks";
-import { useNoteProperty } from "../../react/hooks";
+
+import FNote from "../../../entities/fnote";
import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
-import { JsPlumbItem } from "./jsplumb";
-import FNote from "../../../entities/fnote";
+import { useNoteColorClass, useNoteIcon, useNoteProperty } from "../../react/hooks";
import RelationMapApi, { MapDataNoteEntry } from "./api";
-import { RefObject } from "preact";
-import NoteLink from "../../react/NoteLink";
-import { idToNoteId, noteIdToId } from "./utils";
import { buildNoteContextMenuHandler } from "./context_menu";
+import { JsPlumbItem } from "./jsplumb";
+import { idToNoteId, noteIdToId } from "./utils";
const NOTE_BOX_SOURCE_CONFIG = {
filter: ".endpoint",
@@ -35,6 +39,8 @@ interface NoteBoxProps extends MapDataNoteEntry {
export function NoteBox({ noteId, x, y, mapApiRef, selected }: NoteBoxProps) {
const [ note, setNote ] = useState<FNote | null>();
const title = useNoteProperty(note, "title");
+ const icon = useNoteIcon(note);
+ const colorClass = useNoteColorClass(note);
useEffect(() => {
froca.getNote(noteId).then(setNote);
}, [ noteId ]);
@@ -46,10 +52,10 @@ export function NoteBox({ noteId, x, y, mapApiRef, selected }: NoteBoxProps) {
return note && (
<JsPlumbItem
id={noteIdToId(noteId)}
- className={`note-box ${note?.getCssClass()}`}
+ className="note-box tn-note-card"
+ dynamicClassName={clsx(colorClass, note.getCssClass(), selected && "selected")}
onContextMenu={contextMenuHandler}
x={x} y={y}
- selected={selected}
draggable={{
start() {},
drag() {},
@@ -62,7 +68,8 @@ export function NoteBox({ noteId, x, y, mapApiRef, selected }: NoteBoxProps) {
sourceConfig={NOTE_BOX_SOURCE_CONFIG}
targetConfig={NOTE_BOX_TARGET_CONFIG}
>
- <NoteLink className="title" title={title} notePath={noteId} noTnLink noContextMenu />
+ <span className={clsx("note-box-icon", icon)} />
+ <span className="note-box-title">{title}</span>
<div className="endpoint" title={t("relation_map.start_dragging_relations")} />
</JsPlumbItem>
)
@@ -75,8 +82,9 @@ export function NoteBox({ noteId, x, y, mapApiRef, selected }: NoteBoxProps) {
*/
export function GhostNoteBox({ elementRef }: { elementRef: RefObject<HTMLDivElement | null> }) {
return (
- <div ref={elementRef} className="note-box relation-map-ghost-note" aria-hidden="true">
- <span className="title">{t("relation_map.default_new_note_title")}</span>
+ <div ref={elementRef} className="note-box tn-note-card relation-map-ghost-note" aria-hidden="true">
+ <span className="note-box-icon bx bx-note" />
+ <span className="note-box-title">{t("relation_map.default_new_note_title")}</span>
</div>
);
}
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.css b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.css
index 354e0c0e57..6daffc0237 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.css
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.css
@@ -14,37 +14,7 @@
outline: none; /* remove dotted outline on click */
}
-.note-detail-relation-map .note-box {
- padding: 16px;
- position: absolute !important;
- background-color: var(--accented-background-color);
- color: var(--main-text-color);
- z-index: 4;
- border: 1px solid #666;
- box-shadow: 2px 2px 19px #999;
- border-radius: 8px;
- opacity: 0.8;
- font-size: 11px;
- width: auto;
- height: auto;
- max-width: 200px;
- min-width: 150px;
- max-height: 200px;
- overflow: hidden;
-}
-
-.note-detail-relation-map .note-box:hover {
- background-color: var(--more-accented-background-color);
-}
-
-.note-detail-relation-map .note-box .title {
- font-size: larger;
- font-weight: 600;
-}
-
-.note-detail-relation-map .connection-label.jtk-hover,
-.jtk-source-hover,
-.jtk-target-hover {
+.note-detail-relation-map .connection-label.jtk-hover {
background-color: var(--more-accented-background-color);
}
@@ -58,25 +28,6 @@
cursor: pointer;
}
-.note-detail-relation-map .endpoint {
- position: absolute;
- bottom: 37%;
- inset-inline-end: 5px;
- width: 1em;
- height: 1em;
- background-color: #333;
- cursor: pointer;
- box-shadow: 0 0 2px black;
-}
-
-.note-detail-relation-map .endpoint:hover {
- box-shadow: 0 0 6px black;
-}
-
-.note-detail-relation-map .dragHover {
- border: 2px solid orange;
-}
-
.note-detail-relation-map path,
.note-detail-relation-map .jtk-endpoint {
cursor: pointer;
@@ -89,19 +40,3 @@
.relation-map-wrapper.placing-note {
cursor: crosshair;
}
-
-.note-detail-relation-map .note-box.relation-map-ghost-note {
- border-style: dashed;
- pointer-events: none;
- opacity: 0;
-
- &.visible {
- opacity: 0.5;
- }
-}
-
-.note-detail-relation-map .note-box.selected {
- border-color: var(--input-focus-outline-color);
- box-shadow: 0 0 0 2px var(--input-focus-outline-color);
- opacity: 1;
-}
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
index 7601480ad9..d25329081e 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
@@ -12,11 +12,12 @@ describe("relation map canvas clicks", () => {
const onPlace = vi.fn();
const onSelectNote = vi.fn();
const onClickEmpty = vi.fn();
+ const onOpenNote = vi.fn();
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
- for (const fn of [ onPlace, onSelectNote, onClickEmpty ]) fn.mockClear();
+ for (const fn of [ onPlace, onSelectNote, onClickEmpty, onOpenNote ]) fn.mockClear();
});
afterEach(() => {
@@ -30,12 +31,12 @@ describe("relation map canvas clicks", () => {
/** The wrapper, the panned canvas with a box on it, and a toolbar standing over the canvas. */
function Harness({ placing }: { placing: boolean }) {
const containerRef = useRef<HTMLDivElement>(null);
- const clickProps = useCanvasClicks({ containerRef, placing, onPlace, onSelectNote, onClickEmpty });
+ const clickProps = useCanvasClicks({ containerRef, placing, onPlace, onSelectNote, onClickEmpty, onOpenNote });
return (
<div className="wrapper" {...clickProps}>
<div ref={containerRef} className="canvas">
<div id={noteIdToId("boxnote")} className="note-box">
- <a className="title" href="#root/boxnote">Box</a>
+ <span className="title">Box</span>
</div>
</div>
<button className="toolbar" type="button" />
@@ -54,14 +55,14 @@ describe("relation map canvas clicks", () => {
}
/** A press and release at the same spot, or `moved` pixels apart. */
- function click(target: HTMLElement, { moved = 0, ...init }: MouseEventInit & { moved?: number } = {}) {
+ function click(target: HTMLElement, { moved = 0, type = "click", ...init }: MouseEventInit & { moved?: number; type?: string } = {}) {
target.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, clientX: 10, clientY: 10 }));
- const event = new MouseEvent("click", { bubbles: true, cancelable: true, clientX: 10 + moved, clientY: 10, ...init });
+ const event = new MouseEvent(type, { bubbles: true, cancelable: true, clientX: 10 + moved, clientY: 10, ...init });
act(() => { target.dispatchEvent(event); });
return event;
}
- /** Clicks that reach the document, where `link.ts` handles a click on a link. */
+ /** Clicks that reach the document, where the app's own click handlers listen. */
const reachedDocument = vi.fn();
beforeEach(() => {
reachedDocument.mockClear();
@@ -69,7 +70,7 @@ describe("relation map canvas clicks", () => {
});
afterEach(() => document.removeEventListener("click", reachedDocument));
- it("selects the box clicked, keeping its title link from navigating", () => {
+ it("selects the box clicked, and keeps the click to the map", () => {
const { title } = mount();
const event = click(title);
@@ -79,16 +80,21 @@ describe("relation map canvas clicks", () => {
expect(reachedDocument).not.toHaveBeenCalled();
});
- it("leaves a modified click to the link, and a pan or drag to the map", () => {
+ it("opens the note on a modified or middle click, and leaves a pan or drag to the map", () => {
const { title, canvas } = mount();
- click(title, { ctrlKey: true });
- expect(reachedDocument).toHaveBeenCalledTimes(1);
+ const ctrlClick = click(title, { ctrlKey: true });
+ const middleClick = click(title, { type: "auxclick", button: 1 });
+ expect(onOpenNote.mock.calls).toEqual([ [ "boxnote", ctrlClick ], [ "boxnote", middleClick ] ]);
+ expect(reachedDocument).not.toHaveBeenCalled();
+
click(title, { moved: 20 });
+ click(title, { moved: 20, ctrlKey: true });
click(canvas, { moved: 20 });
expect(onSelectNote).not.toHaveBeenCalled();
expect(onClickEmpty).not.toHaveBeenCalled();
+ expect(onOpenNote).toHaveBeenCalledTimes(2);
});
it("closes the pane on empty canvas, but not on what stands over the map", () => {
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
index 76cd366019..4160ba2d7d 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
@@ -10,8 +10,11 @@ import panzoom, { PanZoom, PanZoomOptions } from "panzoom";
import { HTMLAttributes, RefObject } from "preact";
import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";
+import appContext from "../../../components/app_context";
import FNote from "../../../entities/fnote";
+import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
+import { goToLinkExt } from "../../../services/link";
import note_create from "../../../services/note_create";
import server from "../../../services/server";
import toast from "../../../services/toast";
@@ -126,7 +129,8 @@ export default function RelationMap({ note, noteContext, ntxId, parentComponent
placing: placement.placing,
onPlace: placement.placeAt,
onSelectNote: (noteId) => setSelection({ noteId }),
- onClickEmpty: () => paneRef.current?.close()
+ onClickEmpty: () => paneRef.current?.close(),
+ onOpenNote: openNoteFromBox
});
const dragProps = useNoteDragging({ containerRef, mapApiRef });
@@ -447,31 +451,45 @@ function useNotePlacement({ ntxId, note, containerRef, mapApiRef, onArm, onCreat
/**
* Routes a click on the map: it places a new note while the map is armed, selects the box it lands
- * on, or closes the pane when it lands on empty canvas. A click that ends a pan or a drag counts as
+ * on, or closes the pane when it lands on empty canvas. A modified or middle click on a box opens its
+ * note the way a link would (a new tab, a new window). A click that ends a pan or a drag counts as
* none of these, and clicks on whatever stands over the map (toolbars, the pane, the relation name
* popover) are left alone.
- *
- * Captured, so a box's title link does not navigate away from the map. A modified click on the
- * title still reaches the link, which opens the note in a new tab or window.
*/
-export function useCanvasClicks({ containerRef, placing, onPlace, onSelectNote, onClickEmpty }: {
+export function useCanvasClicks({ containerRef, placing, onPlace, onSelectNote, onClickEmpty, onOpenNote }: {
containerRef: RefObject<HTMLDivElement | null>;
placing: boolean;
onPlace(e: MouseEvent): void;
onSelectNote(noteId: string): void;
onClickEmpty(): void;
-}): Pick<HTMLAttributes<HTMLDivElement>, "onPointerDownCapture" | "onClickCapture"> {
+ onOpenNote(noteId: string, e: MouseEvent): void;
+}): Pick<HTMLAttributes<HTMLDivElement>, "onPointerDownCapture" | "onClickCapture" | "onAuxClickCapture"> {
const pressedAt = useRef<{ x: number; y: number }>(null);
+ /** Whether the click happened on the map itself, without the pointer moving since the press. */
+ const isPlainClickOnCanvas = (e: MouseEvent, container: HTMLDivElement) => {
+ const pressed = pressedAt.current;
+ return isOnCanvas(e, container)
+ && !(pressed && Math.hypot(e.clientX - pressed.x, e.clientY - pressed.y) > CLICK_TOLERANCE);
+ };
+ const boxAt = (e: MouseEvent) => e.target instanceof Element ? e.target.closest<HTMLElement>(".note-box") : null;
+
return {
onPointerDownCapture(e) {
pressedAt.current = { x: e.clientX, y: e.clientY };
},
+ onAuxClickCapture(e) {
+ const container = containerRef.current;
+ const box = boxAt(e);
+ if (!container || e.button !== 1 || placing || !box || !isPlainClickOnCanvas(e, container)) return;
+
+ e.preventDefault();
+ e.stopPropagation();
+ onOpenNote(idToNoteId(box.id), e);
+ },
onClickCapture(e) {
const container = containerRef.current;
- const pressed = pressedAt.current;
- if (!container || !isOnCanvas(e, container) || e.button !== 0) return;
- if (pressed && Math.hypot(e.clientX - pressed.x, e.clientY - pressed.y) > CLICK_TOLERANCE) return;
+ if (!container || e.button !== 0 || !isPlainClickOnCanvas(e, container)) return;
if (placing) {
e.preventDefault();
@@ -480,16 +498,19 @@ export function useCanvasClicks({ containerRef, placing, onPlace, onSelectNote,
return;
}
- const box = e.target instanceof Element ? e.target.closest<HTMLElement>(".note-box") : null;
+ const box = boxAt(e);
if (!box) {
onClickEmpty();
return;
}
- if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
e.preventDefault();
e.stopPropagation();
- onSelectNote(idToNoteId(box.id));
+ if (e.ctrlKey || e.metaKey || e.shiftKey) {
+ onOpenNote(idToNoteId(box.id), e);
+ } else {
+ onSelectNote(idToNoteId(box.id));
+ }
}
};
}
@@ -545,6 +566,16 @@ const PLACEMENT_OFFSET = { x: 80, y: 15 };
/** How far, in pixels, the pointer can move between press and release for the click to count. */
const CLICK_TOLERANCE = 4;
+/**
+ * Opens the note of a box the way a link to it opens: Ctrl or the middle button in a new tab, Shift
+ * in a new window (see `goToLinkExt`).
+ */
+function openNoteFromBox(noteId: string, e: MouseEvent) {
+ const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId;
+ const notePath = froca.getNoteFromCache(noteId)?.getBestNotePathString(hoistedNoteId);
+ goToLinkExt(e, `#${notePath || noteId}`);
+}
+
/** The map position, in unzoomed map pixels, of a box placed under the pointer. */
function boxPositionAt(e: MouseEvent, container: HTMLDivElement) {
const { x, y } = getMousePosition(e, container, getZoom(container));
diff --git a/apps/client/src/widgets/type_widgets/relation_map/jsplumb.tsx b/apps/client/src/widgets/type_widgets/relation_map/jsplumb.tsx
index 9c3e91e664..0f72917480 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/jsplumb.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/jsplumb.tsx
@@ -55,16 +55,16 @@ export function JsPlumb({ className, props, children, containerRef: externalCont
)
}
-export function JsPlumbItem({ x, y, children, draggable, sourceConfig, targetConfig, selected, ...restProps }: {
+export function JsPlumbItem({ x, y, children, draggable, sourceConfig, targetConfig, dynamicClassName, ...restProps }: {
x: number;
y: number;
children: ComponentChildren;
draggable?: DragOptions;
sourceConfig?: object;
targetConfig?: object;
- /** Adds the `selected` class. Toggled on the element rather than through `className`, which
- * would drop the classes jsPlumb adds to it. */
- selected?: boolean;
+ /** Classes that change while the item is on the map. Added to the element directly rather than
+ * through `className`, whose changes would drop the classes jsPlumb adds to it. */
+ dynamicClassName?: string;
} & Pick<HTMLAttributes<HTMLDivElement>, "id" | "className" | "onContextMenu">) {
const containerRef = useRef<HTMLDivElement>(null);
const apiRef = useContext(JsPlumbInstance);
@@ -85,8 +85,13 @@ export function JsPlumbItem({ x, y, children, draggable, sourceConfig, targetCon
}, [ targetConfig ]);
useEffect(() => {
- containerRef.current?.classList.toggle("selected", !!selected);
- }, [ selected, restProps.className ]);
+ const element = containerRef.current;
+ const classes = dynamicClassName?.split(" ").filter(Boolean) ?? [];
+ if (!element || !classes.length) return;
+
+ element.classList.add(...classes);
+ return () => element.classList.remove(...classes);
+ }, [ dynamicClassName ]);
return (
<div
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Relation Map.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Relation Map.html
index 1fe07716d9..1ae2190257 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Relation Map.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Relation Map.html
@@ -24,7 +24,7 @@
position.</li>
</ul>
</li>
- <li>To create a relationship, hold the mouse on the box on the right of a
+ <li>To create a relationship, hold the mouse on the dot on the right of a
note and then:
<ul>
<li>Drag it over another note to create a relationship pointing from the first
@@ -37,11 +37,14 @@
or click elsewhere on the map.</li>
</ul>
</li>
+ <li>Each note is shown as a card with its icon and title, colored in the note's
+ color the same way as the cards of a <a class="reference-link" href="#root/_help_CtBQqbwXDx1w">Kanban Board</a>.</li>
<li>To view or edit a note, click it on the map. This opens the note panel
(see below).
<ul>
- <li>To open the note in a new tab instead, <kbd>Ctrl</kbd>+click its title,
- or use the right click menu.</li>
+ <li>To open the note in a new tab instead, <kbd>Ctrl</kbd>+click it or click
+ it with the middle mouse button. <kbd>Shift</kbd>+click opens it in a new
+ window. The right click menu offers the same options.</li>
</ul>
</li>
<li>To edit the title of a note or to delete it (either from the map, or delete
diff --git a/docs/User Guide/User Guide/Note Types/Relation Map.md b/docs/User Guide/User Guide/Note Types/Relation Map.md
index c8dc236d7d..6456e6bd2e 100644
--- a/docs/User Guide/User Guide/Note Types/Relation Map.md
+++ b/docs/User Guide/User Guide/Note Types/Relation Map.md
@@ -9,12 +9,13 @@ Relation map is a type of note which visualizes notes and their [relations](..
* An existing note can also be dragged from the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a>. It will be placed at the position it's dragged on.
* Multiple notes can also be dragged via <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree/Multiple%20selection.md">Multiple selection</a>. The notes will be positioned near the dragged position without overlapping.
* The dragged note can be a sub-child of the map, or it can be at any arbitrary position.
-* To create a relationship, hold the mouse on the box on the right of a note and then:
+* To create a relationship, hold the mouse on the dot on the right of a note and then:
* Drag it over another note to create a relationship pointing from the first note to the second one.
* Drag over the same note to create a self-referencing relationship (represented as a loop).
* Once dragged, a popover next to the relationship asks for its name. Type a new name and press <kbd>Enter</kbd>, or pick a relation name already in use from the suggestions. To cancel, press <kbd>Esc</kbd>, close the popover or click elsewhere on the map.
+* Each note is shown as a card with its icon and title, colored in the note's color the same way as the cards of a <a class="reference-link" href="../Collections/Kanban%20Board.md">Kanban Board</a>.
* To view or edit a note, click it on the map. This opens the note panel (see below).
- * To open the note in a new tab instead, <kbd>Ctrl</kbd>+click its title, or use the right click menu.
+ * To open the note in a new tab instead, <kbd>Ctrl</kbd>+click it or click it with the middle mouse button. <kbd>Shift</kbd>+click opens it in a new window. The right click menu offers the same options.
* To edit the title of a note or to delete it (either from the map, or delete it completely), right click the note.
* To rename or delete a relationship, right click it and select the corresponding option.