Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Improve list mind map drag targets and toolbar interactions https://github.com/siyuan-note/siyuan/issues/18831

Vanessa70400dca3372
diff --git a/app/src/assets/scss/protyle/_list-mindmap.scss b/app/src/assets/scss/protyle/_list-mindmap.scss
index 89f013b3a3..28d46abfa1 100644
--- a/app/src/assets/scss/protyle/_list-mindmap.scss
+++ b/app/src/assets/scss/protyle/_list-mindmap.scss
@@ -42,6 +42,13 @@
}
&__toolbar {
+ position: absolute;
+ top: 0;
+ left: 0;
+ right: 0;
+ z-index: 5;
+ opacity: 0;
+ pointer-events: none;
gap: 4px;
overflow-x: auto;
scrollbar-width: none;
@@ -51,6 +58,16 @@
}
}
+ &:hover &__toolbar,
+ &__toolbar:focus-within {
+ opacity: 1;
+ }
+
+ &:hover &__toolbar .block__icon,
+ &__toolbar:focus-within .block__icon {
+ pointer-events: auto;
+ }
+
&__spacer {
flex: 1;
}
@@ -125,7 +142,7 @@
}
&--selected {
- box-shadow: inset 0 0 0 2px var(--b3-theme-primary);
+ box-shadow: inset 0 0 0 2px var(--b3-theme-primary-light);
}
&--relation {
@@ -151,6 +168,8 @@
&[data-mindmap-drop="after"]::after {
content: "";
position: absolute;
+ z-index: 6;
+ pointer-events: none;
right: -5px;
left: -5px;
height: 3px;
@@ -352,8 +371,7 @@
&__ghost {
z-index: 5;
- opacity: .8;
- outline: 2px solid var(--b3-theme-primary);
+ opacity: .5;
pointer-events: none;
}
@@ -374,11 +392,11 @@
cursor: pointer;
&--hover {
- background: var(--b3-list-hover);
+ box-shadow: inset 0 0 0 1px var(--b3-theme-primary);
}
&--selected {
- outline: 2px solid var(--b3-theme-primary);
+ outline: inset 0 0 0 2px solid var(--b3-theme-primary);
}
}
diff --git a/app/src/protyle/render/listMindmap/drop.test.ts b/app/src/protyle/render/listMindmap/drop.test.ts
new file mode 100644
index 0000000000..7d356a4746
--- /dev/null
+++ b/app/src/protyle/render/listMindmap/drop.test.ts
@@ -0,0 +1,29 @@
+import * as assert from "node:assert/strict";
+import test from "node:test";
+import {findMindmapDrop} from "./drop";
+
+const positions = [
+ {id: "d", parentId: "first", x: 600, y: 150, width: 128, height: 78},
+ {id: "33", parentId: "root", x: 393, y: 400, width: 128, height: 78},
+];
+
+test("blank space to the right prefers the nearby lower sibling over a distant aligned child", () => {
+ assert.deepEqual(findMindmapDrop(positions, 680, 455, () => true), {id: "33", placement: "after"});
+});
+
+test("sibling targets remain available above and below nodes and exclude invalid moves", () => {
+ assert.deepEqual(findMindmapDrop(positions, 400, 350, () => true), {id: "33", placement: "before"});
+ assert.deepEqual(findMindmapDrop(positions, 400, 600, () => true), {id: "33", placement: "after"});
+ assert.equal(findMindmapDrop(positions, 680, 455, () => false), undefined);
+});
+
+test("blank space aligned with a node center chooses its child slot", () => {
+ const nodes = [
+ {id: "two", parentId: "root", x: 437, y: 276, width: 128, height: 78},
+ {id: "33", parentId: "root", x: 437, y: 400, width: 128, height: 78},
+ {id: "nested", parentId: "33", x: 645, y: 400, width: 175, height: 78},
+ ];
+ assert.deepEqual(findMindmapDrop(nodes, 740, 315, () => true), {id: "two", placement: "child"});
+ assert.deepEqual(findMindmapDrop(nodes, 570, 378, () => true), {id: "33", placement: "before"});
+ assert.notEqual(findMindmapDrop(nodes, 740, 315, id => id !== "two")?.id, "two");
+});
diff --git a/app/src/protyle/render/listMindmap/drop.ts b/app/src/protyle/render/listMindmap/drop.ts
new file mode 100644
index 0000000000..cd540742b4
--- /dev/null
+++ b/app/src/protyle/render/listMindmap/drop.ts
@@ -0,0 +1,34 @@
+import type {ListMindmapPosition} from "./model";
+
+// 空白区域同时比较上下方的同级落点和右侧中部的子节点落点。
+export const findMindmapDrop = (positions: Iterable<ListMindmapPosition>, x: number, y: number,
+ canDrop: (id: string) => boolean) => {
+ let result: {id: string, placement: "before" | "after" | "child"} | undefined;
+ let nearest = Infinity;
+ for (const position of positions) {
+ if (!position.parentId || !canDrop(position.id)) {
+ continue;
+ }
+ const dx = Math.max(position.x - x, 0, x - position.x - position.width);
+ const before = Math.abs(y - position.y);
+ const after = Math.abs(y - position.y - position.height);
+ const distance = Math.hypot(dx, Math.min(before, after));
+ if (distance < nearest) {
+ nearest = distance;
+ result = {id: position.id, placement: before < after ? "before" : "after"};
+ }
+ const centerY = position.y + position.height / 2;
+ const right = position.x + position.width;
+ if (x > right && Math.abs(y - centerY) <= position.height / 6) {
+ // 将子节点落点投影到下一列,避免已有下一列节点抢占与父节点对齐的空白。
+ const childLeft = right + 40;
+ const childDx = Math.max(childLeft - x, 0, x - childLeft - position.width);
+ const childDistance = Math.hypot(childDx, y - centerY);
+ if (childDistance < nearest) {
+ nearest = childDistance;
+ result = {id: position.id, placement: "child"};
+ }
+ }
+ }
+ return result;
+};
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 1398f396e7..75516d99a4 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -10,6 +10,7 @@ import {routeMindmapRelation, MindmapRoutePoint} from "./routing";
import {mathRender} from "../mathRender";
import {getAVRichTextSafeURL} from "../av/richTextValue";
import {Constants} from "../../../constants";
+import {findMindmapDrop} from "./drop";
export interface ListMindmapViewOptions {
host: HTMLElement;
@@ -850,7 +851,7 @@ export class ListMindmapView {
if (!this.ghost) {
const source = this.nodeElements.get(pointer.id);
this.ghost = source.cloneNode(true) as HTMLDivElement;
- this.ghost.className = "list-mindmap__node list-mindmap__ghost";
+ this.ghost.className = "list-mindmap__node list-mindmap__node--selected list-mindmap__ghost";
this.ghost.removeAttribute("data-mindmap-id");
this.ghost.querySelectorAll(".list-mindmap__fold, .list-mindmap__add-child, .list-mindmap__add-bridge")
.forEach(button => button.remove());
@@ -861,15 +862,39 @@ export class ListMindmapView {
this.ghost.style.left = `${source.x + dx / this.scale}px`;
this.ghost.style.top = `${source.y + dy / this.scale}px`;
this.clearDrop();
- const target = document.elementFromPoint(event.clientX, event.clientY)?.closest<HTMLElement>(".list-mindmap__node");
- const targetId = target?.dataset.mindmapId;
+ const hit = document.elementFromPoint(event.clientX, event.clientY);
+ let target = hit?.closest<HTMLElement>(".list-mindmap__node");
+ let blankPlacement: PointerState["placement"];
+ // 直接命中节点时保留原有操作,空白处再比较同级和子节点落点的二维距离。
+ if (!target && hit && this.viewport.contains(hit)) {
+ const bounds = this.viewport.getBoundingClientRect();
+ const x = (event.clientX - bounds.left - this.offsetX) / this.scale;
+ const y = (event.clientY - bounds.top - this.offsetY) / this.scale;
+ const drop = findMindmapDrop(this.positions.values(), x, y, id => this.canDrop(pointer.id, id));
+ if (drop) {
+ target = this.nodeElements.get(drop.id);
+ blankPlacement = drop.placement;
+ }
+ }
+ let targetId = target?.dataset.mindmapId;
if (!targetId || !this.world.contains(target) || !this.canDrop(pointer.id, targetId)) {
return;
}
const rect = target.getBoundingClientRect();
const part = (event.clientY - rect.top) / rect.height;
const node = this.model.nodes.get(targetId);
- const placement = node.virtual ? "child" : part < .25 ? "before" : part > .75 ? "after" : "child";
+ let placement: PointerState["placement"] = blankPlacement || (node.virtual ? "child" :
+ part < .25 ? "before" : part > .75 ? "after" : "child");
+ // 同一个兄弟间隙统一显示在后一个节点之前,避免最近节点切换时插入线跳动。
+ if (placement === "after" && node.parentId) {
+ const siblings = this.model.nodes.get(node.parentId).children;
+ const next = siblings[siblings.indexOf(node) + 1];
+ if (next && this.positions.has(next.id) && this.canDrop(pointer.id, next.id)) {
+ targetId = next.id;
+ target = this.nodeElements.get(next.id);
+ placement = "before";
+ }
+ }
pointer.targetId = targetId;
pointer.placement = placement;
target.dataset.mindmapDrop = placement;
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index c17b6692c0..4a67c58f9b 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -225,9 +225,11 @@ export class Toolbar {
if (!this.rangePosition || !this.range) {
return;
}
- // 单元格内的浮动工具栏使用外层编辑器边界,避免被短单元格挤到选区上。
+ // 内嵌编辑器的浮动工具栏使用外层容器边界,避免被短单元格或脑图节点挤到选区上。
const cellEditor = protyle.element.closest(".table__cell-editor");
- const protyleRect = (cellEditor?.parentElement.closest(".protyle") || protyle.element).getBoundingClientRect();
+ const mindmap = protyle.element.closest(".list-mindmap");
+ const protyleRect = (mindmap || cellEditor?.parentElement.closest(".protyle") ||
+ protyle.element).getBoundingClientRect();
const viewportBoundary = element.dataset.positionBoundary === "viewport";
const topBoundary = viewportBoundary ? 8 : protyleRect.top + 30;
const bottomBoundary = viewportBoundary ? window.innerHeight - 8 :
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 1420c718f5..7289c0b348 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -2029,8 +2029,8 @@ export class WYSIWYG {
// 内容区域使用浏览器原生选区,跨块选择时保留各行内元素自身的选中样式。
if (!startsFromPadding && !tableBlockElement) {
- documentSelf.onmouseup = (mouseUpEvent) => {
- documentSelf.onmouseup = null;
+ // 捕获节点编辑器外的松手事件,避免脑图等容器阻止冒泡后遗漏选区工具栏。
+ documentSelf.addEventListener("mouseup", (mouseUpEvent) => {
if (this.element.contains(mouseUpEvent.target as Node)) {
return;
}
@@ -2047,7 +2047,7 @@ export class WYSIWYG {
}
}
});
- };
+ }, {capture: true, once: true});
return;
}
if (startsFromPadding) {