Skip to content

Build your personal knowledge base with Trilium Notes

fix(tree): draw one drop line between two notes, not two

Elian Dorand507e34d806b
diff --git a/apps/client/src/stylesheets/tree.css b/apps/client/src/stylesheets/tree.css
index cd59fbe5ee..26f018e5fe 100644
--- a/apps/client/src/stylesheets/tree.css
+++ b/apps/client/src/stylesheets/tree.css
@@ -282,6 +282,19 @@ span.fancytree-node:hover .add-note-button {
height: 0;
}
+/*
+ * Both hit modes name the same place to drop into, so both draw on the boundary between the two
+ * rows: the title above's bottom edge moves down to reach it, the title below's top edge moves up.
+ * The distance is measured per row by publishDropMarkerShift() in note_tree.ts.
+ */
+#fancytree-drop-marker.fancytree-drop-after {
+ --drop-marker-shift: var(--tree-drop-marker-shift, 0px);
+}
+
+#fancytree-drop-marker.fancytree-drop-before {
+ --drop-marker-shift: calc(-1 * var(--tree-drop-marker-shift, 0px));
+}
+
/*
* The line, and the ring capping the end it is measured from. Both are drawn by pseudo-elements so
* the marker's own box stays at zero height: dnd5 positions that box, and a border on it would
@@ -292,7 +305,7 @@ span.fancytree-node:hover .add-note-button {
content: "";
position: absolute;
inset-inline: 0;
- top: -1px;
+ top: calc(-1px + var(--drop-marker-shift, 0px));
height: 2px;
border-radius: 1px;
background: var(--muted-text-color);
@@ -303,7 +316,7 @@ span.fancytree-node:hover .add-note-button {
content: "";
position: absolute;
inset-inline-start: -2px;
- top: -4px;
+ top: calc(-4px + var(--drop-marker-shift, 0px));
box-sizing: border-box;
width: 8px;
height: 8px;
diff --git a/apps/client/src/widgets/note_tree.spec.ts b/apps/client/src/widgets/note_tree.spec.ts
index 120d04d99c..2cfc5f26af 100644
--- a/apps/client/src/widgets/note_tree.spec.ts
+++ b/apps/client/src/widgets/note_tree.spec.ts
@@ -4,7 +4,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import hoistedNoteService from "../services/hoisted_note.js";
import noteCreateService from "../services/note_create.js";
import treeService from "../services/tree.js";
-import NoteTreeWidget from "./note_tree.js";
+import NoteTreeWidget, { publishDropMarkerShift } from "./note_tree.js";
describe("fancytree scrollIntoView patch", () => {
it("resolves instead of crashing for a node without rendered markup (#10407)", async () => {
@@ -110,3 +110,39 @@ describe("NoteTreeWidget", () => {
expect(isHoisted).toHaveBeenCalledWith("_taskStates");
});
});
+
+describe("the drop marker's distance from the row boundary", () => {
+ const shift = () => document.body.style.getPropertyValue("--tree-drop-marker-shift");
+
+ /** A row of `rowHeight` around a title of `titleHeight`, which is all the measurement reads. */
+ function nodeWithRow(rowHeight: number, titleHeight: number) {
+ const row = document.createElement("span");
+ const title = document.createElement("span");
+ title.className = "fancytree-title";
+ row.appendChild(title);
+
+ vi.spyOn(row, "getBoundingClientRect").mockReturnValue({ height: rowHeight } as DOMRect);
+ vi.spyOn(title, "getBoundingClientRect").mockReturnValue({ height: titleHeight } as DOMRect);
+
+ return { span: row } as Fancytree.FancytreeNode;
+ }
+
+ it("is half the room the row leaves around its title", () => {
+ // dnd5 anchors on the title's edge, which is that far from the boundary the note lands on.
+ publishDropMarkerShift(nodeWithRow(34, 18));
+ expect(shift()).toBe("8px");
+
+ // A larger tree font fills more of its row, so the two edges are closer together.
+ publishDropMarkerShift(nodeWithRow(48, 40));
+ expect(shift()).toBe("4px");
+ });
+
+ it("leaves the last measurement standing for a node that has no markup", () => {
+ publishDropMarkerShift(nodeWithRow(34, 18));
+ // A node can be dragged over before fancytree has rendered it; measuring nothing would put
+ // the line back on the title's edge rather than on the boundary.
+ publishDropMarkerShift({ span: undefined } as unknown as Fancytree.FancytreeNode);
+
+ expect(shift()).toBe("8px");
+ });
+});
diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts
index 15baa86a30..d90d5131f7 100644
--- a/apps/client/src/widgets/note_tree.ts
+++ b/apps/client/src/widgets/note_tree.ts
@@ -549,6 +549,8 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
return true; // allow dragging to start
},
dragEnter: (node, data) => {
+ publishDropMarkerShift(node);
+
if (node.data.noteType === "search") {
return false;
} else if (node.data.noteId === "_lbRoot") {
@@ -1981,6 +1983,30 @@ function buildEnhanceTitle() {
};
}
+/**
+ * Tells the drop marker how far it sits from the boundary between two rows, in pixels.
+ *
+ * dnd5 anchors the marker on the target *title* — its bottom edge for "after", its top edge for
+ * "before" — and those are half the row's spare height either side of the boundary the note
+ * actually lands on. Left alone, dropping between two notes draws the line in one of two places
+ * depending on which of them the pointer is over.
+ *
+ * Measured rather than written down: the distance is the row height less the title's line box, and
+ * both follow whichever family and size the tree font names.
+ */
+export function publishDropMarkerShift(node: Fancytree.FancytreeNode) {
+ const row = node.span;
+ const title = row?.querySelector(".fancytree-title");
+ if (!row || !title) {
+ return;
+ }
+
+ const shift = (row.getBoundingClientRect().height - title.getBoundingClientRect().height) / 2;
+ // On the body, since the marker is parented there and custom properties inherit; it is also
+ // created lazily, so it cannot be relied on to exist when the first note is dragged over.
+ document.body.style.setProperty("--tree-drop-marker-shift", `${shift}px`);
+}
+
type ScrollIntoViewFn = (this: Fancytree.FancytreeNode, effects?: boolean | object, options?: object) => JQueryPromise<unknown>;
/**