Skip to content

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

:bug: Improve list mind map selection and editing https://github.com/siyuan-note/siyuan/issues/18831

Vanessa62d1cf081fb3
diff --git a/app/src/assets/scss/protyle/_list-mindmap.scss b/app/src/assets/scss/protyle/_list-mindmap.scss
index 9f628db161..0be442b943 100644
--- a/app/src/assets/scss/protyle/_list-mindmap.scss
+++ b/app/src/assets/scss/protyle/_list-mindmap.scss
@@ -184,10 +184,10 @@
}
&--untitled:not(.list-mindmap__node--editing) {
- width: 28px;
- min-width: 28px;
- height: 28px;
- min-height: 28px;
+ width: 32px;
+ min-width: 32px;
+ height: 32px;
+ min-height: 32px;
padding: 0;
> .list-mindmap__content {
@@ -266,7 +266,7 @@
min-height: 24px;
overflow-wrap: anywhere;
- &--empty:not(.list-mindmap__editor) {
+ .list-mindmap__node:not(.list-mindmap__node--editing) > &--empty:not(.list-mindmap__editor) {
> * {
display: none;
}
diff --git a/app/src/protyle/render/listMindmap/editor.ts b/app/src/protyle/render/listMindmap/editor.ts
index 71cd068261..de8e01ae71 100644
--- a/app/src/protyle/render/listMindmap/editor.ts
+++ b/app/src/protyle/render/listMindmap/editor.ts
@@ -74,7 +74,6 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {
const fragment = mountProtyleLiteFragment(host, {
app: owner.app,
initialBlockHTML,
- placeholder: window.siyuan.languages.listMindmapPlaceholder,
protyleOptions: {notebookId: owner.notebookId, toolbar, hint},
runtimeCapabilities: {
upload: false, websocket: false, pluginExtensions: false, customBlockRender: false,
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.ts
index 7e26c04b7e..b766056a76 100644
--- a/app/src/protyle/render/listMindmap/index.ts
+++ b/app/src/protyle/render/listMindmap/index.ts
@@ -13,6 +13,7 @@ import {completeTabsListSource} from "../../wysiwyg/tabsList";
import {waitForPendingTransactions} from "../../util/transactionQueue";
import {hideAllElements, hideElements} from "../../ui/hideElements";
import {globalClickHideMenu} from "../../../boot/globalEvent/click";
+import {countBlockWord} from "../../../layout/status";
import {openLink} from "../../../editor/openLink";
import {matchHotKey} from "../../util/hotKey";
import {setFullscreen} from "../../breadcrumb/action";
@@ -103,6 +104,12 @@ class ListMindmapController {
if (!target.closest(".protyle-toolbar, .protyle-util, [data-sub-element-source]")) {
hideAllElements(["toolbar", "util", "gutter"]);
hideElements(["hint"], this.owner);
+ // 进入脑图交互时清除外层块选区,同时保留多选工具栏的操作上下文。
+ if (event.button === 0 && !this.owner.toolbar.isMultiSelectMode() &&
+ this.owner.wysiwyg.element.querySelector(".protyle-wysiwyg--select, .protyle-wysiwyg--select-mode")) {
+ hideElements(["select"], this.owner);
+ countBlockWord([], this.owner);
+ }
}
}, {capture: true});
this.host.addEventListener("keydown", event => {
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 82f69d6dbf..baed48d622 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -122,7 +122,7 @@ test("layout rejects cyclic or invalid trees and supports deep nesting without r
assert.equal(layoutListMindmap(root).nodes.size, 5001);
});
-const browserCases = async (sourceCode: string, css: string, taskSource: string, taskCSS: string) => {
+const browserCases = async (sourceCode: string, css: string, taskSource: string, taskCSS: string, dragSource: string) => {
const check = require("node:assert/strict");
const api = new Function("mathRender", "Constants", "highlightRender", sourceCode + "; return {readListMindmap, moveListMindmapNode, addListMindmapNode, " +
"deleteListMindmapNode, replaceListMindmapContent, cleanListMindmapHTML, remapListMindmapIDs, writeListMindmapMetadata, " +
@@ -1940,6 +1940,126 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
tabView.destroy();
api.destroyTabsRender(taskParent);
taskParent.remove();
+
+ // 使用编辑器的实际划选监听器,验证从两侧进入脑图、跨块移动及松手清理。
+ const dragParent = document.createElement("div");
+ dragParent.style.cssText = "position:fixed;inset:0;background:white;z-index:9999;overflow:auto";
+ const dragEditor = document.createElement("div");
+ dragEditor.className = "protyle-wysiwyg";
+ dragEditor.style.cssText = "padding:20px 64px;box-sizing:border-box";
+ dragEditor.innerHTML = lute.Md2BlockDOM("Before\n\n- Alpha\n- Beta\n\nAfter\n");
+ const dragList = dragEditor.querySelector<HTMLElement>(".list");
+ dragList.setAttribute("custom-sy-list-mindmap", "1");
+ dragList.dataset.listMindmapRendered = "true";
+ const dragHost = document.createElement("div");
+ dragHost.className = "list-mindmap";
+ dragHost.style.height = "260px";
+ dragList.append(dragHost);
+ const dragView = new api.ListMindmapView({host: dragHost, model: api.readListMindmap(dragList)});
+ const dragOverlay = document.createElement("div");
+ dragOverlay.className = "fn__none";
+ dragOverlay.style.position = "absolute";
+ const dragStyle = document.createElement("style");
+ dragStyle.textContent = ".fn__none {display:none} .drag-selection {position:absolute;pointer-events:none}";
+ dragOverlay.classList.add("drag-selection");
+ dragParent.append(dragEditor, dragOverlay, dragStyle);
+ document.body.append(dragParent);
+ const noop = () => {};
+ const absent = () => false;
+ const selectedCounts: string[][] = [];
+ const services = {
+ Constants: {ZWSP: "\u200b"},
+ getAVTemplateInteractiveElement: absent, getAVSelectionRoot: absent, isAVDragSelectSupported: absent,
+ isTableLikeView: absent, isMobile: absent, shouldFoldEmbeddedListByAlt: absent,
+ shouldOpenListItemAttr: absent, isHiddenTabContent: absent,
+ isOnlyMeta: (event: MouseEvent) => (event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey,
+ repairHiddenTabSelection: noop, clearSelect: noop, hideAllElements: noop, globalClickHideMenu: noop,
+ dragOverScroll: noop, stopScrollAnimation: noop, countBlockWord: (ids: string[]) => selectedCounts.push(ids),
+ };
+ const dragOwner = {
+ element: dragParent, contentElement: dragParent, selectElement: dragOverlay,
+ wysiwyg: {element: dragEditor}, options: {render: {breadcrumb: false}},
+ toolbar: {isMultiSelectMode: absent},
+ hint: {deactivateEmojiPanel: noop, element: document.createElement("div")},
+ };
+ new Function("protyle", "services", `const {${Object.keys(services).join(",")}} = services;\n` + dragSource)
+ .call(Object.assign(dragOwner.wysiwyg, {host: dragHost, owner: dragOwner}), dragOwner, services);
+ const dragErrors: string[] = [];
+ const collectDragError = (event: ErrorEvent) => dragErrors.push(event.message);
+ window.addEventListener("error", collectDragError);
+ await settle();
+ const selectedIDs = () => Array.from(dragEditor.querySelectorAll<HTMLElement>(".protyle-wysiwyg--select"))
+ .map(item => item.dataset.nodeId);
+ for (const side of ["left", "right"]) {
+ for (const direction of [1, -1]) {
+ dragEditor.querySelectorAll(".protyle-wysiwyg--select").forEach(item =>
+ item.classList.remove("protyle-wysiwyg--select"));
+ const rect = dragHost.getBoundingClientRect();
+ const start = {x: Math.round(side === "left" ? rect.left - 32 : rect.right + 32),
+ y: Math.round(rect.top + rect.height / 2)};
+ const inside = {x: Math.round(side === "left" ? rect.left + 24 : rect.right - 24),
+ y: start.y + 30 * direction};
+ check.ok(dragHost.contains(document.elementFromPoint(inside.x, inside.y)));
+ await nativeInput([
+ {type: "mouseMove", ...start},
+ {type: "mouseDown", ...start, button: "left", clickCount: 1},
+ {type: "mouseMove", ...inside, button: "left"},
+ ]);
+ check.deepEqual(dragErrors, []);
+ check.deepEqual(selectedIDs(), [dragList.dataset.nodeId], `${side} ${direction}: selects the whole mind map`);
+ const adjacent = direction === 1 ? dragList.nextElementSibling : dragList.previousElementSibling;
+ const outside = {x: inside.x, y: Math.round(adjacent.getBoundingClientRect().top + 10)};
+ await nativeInput([{type: "mouseMove", ...outside, button: "left"}]);
+ check.deepEqual(new Set(selectedIDs()), new Set([dragList.dataset.nodeId, adjacent.getAttribute("data-node-id")]));
+ await nativeInput([{type: "mouseMove", ...inside, button: "left"}]);
+ check.deepEqual(selectedIDs(), [dragList.dataset.nodeId], "returning into the mind map shrinks the selection");
+ await nativeInput([{type: "mouseUp", ...inside, button: "left", clickCount: 1}]);
+ check.deepEqual(dragErrors, []);
+ check.equal(dragEditor.classList.contains("fn__pointer-none"), false);
+ check.ok(dragOverlay.classList.contains("fn__none"));
+ await nativeInput([{type: "mouseMove", ...outside}]);
+ check.deepEqual(selectedIDs(), [dragList.dataset.nodeId], "moving after release does not extend the selection");
+ check.ok(dragOverlay.classList.contains("fn__none"));
+
+ // 点击空白画布或节点均退出块选中状态,并清理多块选区、端点标记和状态栏计数。
+ adjacent.classList.add("protyle-wysiwyg--select");
+ dragList.classList.add("protyle-wysiwyg--select-mode");
+ dragList.setAttribute("select-start", "true");
+ dragList.setAttribute("select-end", "true");
+ selectedCounts.length = 0;
+ const clickPoint = side === "left" ? inside : centerPoint(dragHost.querySelector(".list-mindmap__node"));
+ await nativeInput([
+ {type: "mouseMove", ...clickPoint},
+ {type: "mouseDown", ...clickPoint, button: "left", clickCount: 1},
+ {type: "mouseUp", ...clickPoint, button: "left", clickCount: 1},
+ ]);
+ check.deepEqual(dragErrors, []);
+ check.deepEqual(selectedIDs(), [], "clicking the mind map clears the outer block selection");
+ check.equal(dragList.classList.contains("protyle-wysiwyg--select-mode"), false);
+ check.equal(dragList.hasAttribute("select-start"), false);
+ check.equal(dragList.hasAttribute("select-end"), false);
+ check.deepEqual(selectedCounts, [[]]);
+ }
+ }
+ // 右键和多选工具栏操作保留选区,内嵌编辑器的工具栏同样不能取消其操作对象。
+ dragList.classList.add("protyle-wysiwyg--select");
+ const selectionPointerDown = (element: Element, button = 0) =>
+ element.dispatchEvent(new PointerEvent("pointerdown", {bubbles: true, button}));
+ selectionPointerDown(dragHost, 2);
+ check.deepEqual(selectedIDs(), [dragList.dataset.nodeId]);
+ dragOwner.toolbar.isMultiSelectMode = () => true;
+ selectionPointerDown(dragHost);
+ check.deepEqual(selectedIDs(), [dragList.dataset.nodeId]);
+ dragOwner.toolbar.isMultiSelectMode = absent;
+ const selectionToolbar = document.createElement("div");
+ selectionToolbar.className = "protyle-toolbar";
+ dragHost.append(selectionToolbar);
+ selectionPointerDown(selectionToolbar);
+ check.deepEqual(selectedIDs(), [dragList.dataset.nodeId]);
+ check.deepEqual(dragErrors, []);
+ window.removeEventListener("error", collectDragError);
+ dragView.destroy();
+ dragParent.remove();
taskStyle.remove();
hostParent.remove();
style.remove();
@@ -1984,6 +2104,35 @@ test("list mindmap mutations preserve block data in the real DOM and Lute", {
`\nclass TaskController {${taskMethods.map(member => member.getText(indexSource)).join("\n")}}`, {
compilerOptions: {target: typescript.ScriptTarget.ES2021},
}).outputText;
+ const wysiwygSource = typescript.createSourceFile("index.ts",
+ readFileSync(path.join(__dirname, "../../wysiwyg/index.ts"), "utf8"), typescript.ScriptTarget.Latest, true);
+ let mouseDownBinding: import("typescript").CallExpression;
+ const findMouseDown = (node: import("typescript").Node) => {
+ if (typescript.isCallExpression(node) && node.expression.getText(wysiwygSource) === "this.element.addEventListener" &&
+ node.arguments[0]?.getText(wysiwygSource) === '"mousedown"') {
+ mouseDownBinding = node;
+ }
+ typescript.forEachChild(node, findMouseDown);
+ };
+ findMouseDown(wysiwygSource);
+ assert.ok(mouseDownBinding);
+ const constructor = controller.members.find(typescript.isConstructorDeclaration);
+ const pointerDownBinding = constructor.body.statements.find(statement =>
+ typescript.isExpressionStatement(statement) && typescript.isCallExpression(statement.expression) &&
+ statement.expression.expression.getText(indexSource) === "this.host.addEventListener" &&
+ statement.expression.arguments[0]?.getText(indexSource) === '"pointerdown"');
+ assert.ok(pointerDownBinding);
+ const dragSource = compile(path.join(__dirname, "../../util/hasClosest.ts")) +
+ compile(path.join(__dirname, "../../wysiwyg/getBlock.ts")) +
+ compile(path.join(__dirname, "../../wysiwyg/blockDragSelect.ts")) +
+ "const {getBlockSelectionModeElement, clearBlockSelectionMode} = (() => {" +
+ compile(path.join(__dirname, "../../wysiwyg/blockSelection.ts")) +
+ "return {getBlockSelectionModeElement, clearBlockSelectionMode};})();\n" +
+ "const {hideElements} = (() => {" + compile(path.join(__dirname, "../../ui/hideElements.ts")) +
+ "return {hideElements};})();\n" +
+ typescript.transpileModule(mouseDownBinding.getText(wysiwygSource) + ";\n" + pointerDownBinding.getText(indexSource), {
+ compilerOptions: {target: typescript.ScriptTarget.ES2021},
+ }).outputText;
const lutePath = path.resolve(__dirname, "../../../../stage/protyle/js/lute/lute.min.js");
const code = `const {app, BrowserWindow, ipcMain} = require("electron");
app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
@@ -2004,7 +2153,7 @@ app.whenReady().then(async () => {
await win.loadURL("data:text/html,<html><body></body></html>");
await win.webContents.executeJavaScript(require("node:fs").readFileSync(${JSON.stringify(lutePath)}, "utf8"));
const result = await win.webContents.executeJavaScript(${JSON.stringify(
- `const __name = value => value; (${browserCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(css)}, ${JSON.stringify(taskSource)}, ${JSON.stringify(taskCSS)})`)});
+ `const __name = value => value; (${browserCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(css)}, ${JSON.stringify(taskSource)}, ${JSON.stringify(taskCSS)}, ${JSON.stringify(dragSource)})`)});
console.log(result);
win.destroy();
app.exit(0);
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 79e11216f9..d9336f8f5b 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -1635,7 +1635,6 @@ export class ListMindmapView {
const measure = createElement("span", "list-mindmap__root-title-measure");
input.rows = 1;
input.value = this.model.metadata.rootTitle || "";
- input.placeholder = this.label("listMindmapPlaceholder");
measure.textContent = input.value;
input.setAttribute("aria-label", this.label("text"));
content.classList.add("list-mindmap__root-title-host");
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 969bce52d4..ed43e4f571 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -2220,11 +2220,11 @@ export class WYSIWYG {
return firstTopBlock ? getDragSelectBlock(getFirstBlock(firstTopBlock)) : false;
};
let lastMoveEvent: MouseEvent;
- const selectScrollEvent = () => lastMoveEvent && documentSelf.onmousemove?.(lastMoveEvent);
+ const selectScrollEvent = () => lastMoveEvent && moveDragSelect(lastMoveEvent);
if (startsFromPadding) {
protyle.contentElement.addEventListener("scroll", selectScrollEvent);
}
- documentSelf.onmousemove = (moveEvent: MouseEvent) => {
+ const moveDragSelect = (moveEvent: MouseEvent) => {
lastMoveEvent = moveEvent;
let moveTarget: boolean | HTMLElement = moveEvent.target as HTMLElement;
// table cell select
@@ -2592,6 +2592,7 @@ export class WYSIWYG {
return;
}
dragSelectFinished = true;
+ documentSelf.removeEventListener("mousemove", moveDragSelect, true);
documentSelf.removeEventListener("mouseup", finishDragSelect, true);
if (documentSelf.onmouseup === finishDragSelect) {
documentSelf.onmouseup = null;
@@ -3009,6 +3010,12 @@ export class WYSIWYG {
}
}
};
+ // 侧边划选在捕获阶段更新,避免脑图等容器拦截移动事件后选区停止更新。
+ if (startsFromPadding) {
+ documentSelf.addEventListener("mousemove", moveDragSelect, true);
+ } else {
+ documentSelf.onmousemove = moveDragSelect;
+ }
// 底部反链包含嵌套编辑器,捕获阶段结束框选,避免内部事件阻断后选区无法清理
documentSelf.onmouseup = finishDragSelect;
documentSelf.addEventListener("mouseup", finishDragSelect, {capture: true, once: true});