Skip to content

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

:bug: Preserve list boundaries when dropping complete lists https://github.com/siyuan-note/siyuan/issues/20817

Daniel66925c739527
diff --git a/app/src/protyle/util/dragDocument.test.ts b/app/src/protyle/util/dragDocument.test.ts
index ccc92e029e..128cd05ec9 100644
--- a/app/src/protyle/util/dragDocument.test.ts
+++ b/app/src/protyle/util/dragDocument.test.ts
@@ -440,15 +440,31 @@ describe("getTopListDragTarget", () => {
describe("shouldKeepListBlockDragTarget", () => {
it("keeps both complete lists when forming a horizontal super block", () => {
- assert.equal(shouldKeepListBlockDragTarget("nodelist", true, false), true);
+ for (const side of ["left", "right"]) {
+ assert.equal(shouldKeepListBlockDragTarget("nodelist", [`dragover__${side}`], false), true);
+ }
});
it("allows a list block to expand into list items for a regular list drop", () => {
- assert.equal(shouldKeepListBlockDragTarget("nodelist", false, false), false);
+ for (const position of ["top--sibling", "bottom--sibling", "bottom--child"]) {
+ assert.equal(shouldKeepListBlockDragTarget("nodelist", [`dragover__${position}`], false), false);
+ }
});
it("keeps a list block when reordering columns", () => {
- assert.equal(shouldKeepListBlockDragTarget("nodelist", false, true), true);
+ assert.equal(shouldKeepListBlockDragTarget("nodelist", [], true), true);
+ });
+
+ it("keeps complete lists at upper and lower block boundaries", () => {
+ for (const side of ["top", "bottom"]) {
+ assert.equal(shouldKeepListBlockDragTarget("nodelist", ["list", `dragover__${side}`], false), true);
+ }
+ });
+
+ it("preserves list item source semantics at list boundaries", () => {
+ for (const side of ["top", "bottom", "left", "right"]) {
+ assert.equal(shouldKeepListBlockDragTarget("nodelistitem", [`dragover__${side}`], false), false);
+ }
});
});
diff --git a/app/src/protyle/util/dragDocument.ts b/app/src/protyle/util/dragDocument.ts
index 4e391b738f..1cc22f6ce7 100644
--- a/app/src/protyle/util/dragDocument.ts
+++ b/app/src/protyle/util/dragDocument.ts
@@ -194,9 +194,10 @@ export const getTopListDragTarget = (targetElement: Element) => {
return topList;
};
-export const shouldKeepListBlockDragTarget = (sourceType: string, isHorizontalDrop: boolean,
+export const shouldKeepListBlockDragTarget = (sourceType: string, targetClasses: string[],
isColumnSuperBlockChild: boolean) =>
- sourceType === "nodelist" && (isHorizontalDrop || isColumnSuperBlockChild);
+ sourceType === "nodelist" && (isColumnSuperBlockChild ||
+ targetClasses.some(className => ["dragover__top", "dragover__bottom", "dragover__left", "dragover__right"].includes(className)));
export const replaceDragUndoOperation = <T>(operations: T[], operation: T, replacements: T[]) => {
const index = operations.indexOf(operation);
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 6c26b1b8e1..31b344803c 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -1986,12 +1986,10 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
}
}
- // 列表块横向拖拽或在横排超级块内重排时,目标列表本身是列单元,需保留完整列表。
+ // 完整列表的边界指示表示块间插入;列表项的同级、子级指示才表示合并到列表内。
const isColSbChildList = targetElement.parentElement?.getAttribute("data-type") === "NodeSuperBlock" &&
targetElement.parentElement?.getAttribute("data-sb-layout") === "col";
- const isHorizontalDrop = targetClass.includes("dragover__left") ||
- targetClass.includes("dragover__right");
- const keepListBlockTarget = shouldKeepListBlockDragTarget(gutterTypes[0], isHorizontalDrop,
+ const keepListBlockTarget = shouldKeepListBlockDragTarget(gutterTypes[0], targetClass,
isColSbChildList);
if (isListItemSource && targetElement.classList.contains("list") &&
!keepListBlockTarget) {
diff --git a/app/tests/editorCommonEvent.test.js b/app/tests/editorCommonEvent.test.js
new file mode 100644
index 0000000000..d80dcb0424
--- /dev/null
+++ b/app/tests/editorCommonEvent.test.js
@@ -0,0 +1,231 @@
+const assert = require("node:assert/strict");
+const {readFileSync, mkdtempSync, rmSync} = require("node:fs");
+const path = require("node:path");
+const os = require("node:os");
+
+const sources = () => {
+ const ts = require("typescript");
+ const read = file => ts.createSourceFile(file, readFileSync(path.join(__dirname, "../src", file), "utf8"),
+ ts.ScriptTarget.Latest, true);
+ const compile = code => ts.transpileModule(code.replace(/export const /g, "const "), {
+ compilerOptions: {target: ts.ScriptTarget.ES2021},
+ }).outputText;
+ const extract = (file, names) => {
+ const source = read(file);
+ const statements = source.statements.filter(statement => ts.isVariableStatement(statement) &&
+ statement.declarationList.declarations.some(declaration => names.includes(declaration.name.getText(source))));
+ assert.equal(statements.length, names.length);
+ return compile(statements.map(statement => statement.getText(source)).join("\n"));
+ };
+ const source = read("protyle/util/editorCommonEvent.ts");
+ const dropBranches = [];
+ const visit = node => {
+ if (ts.isIfStatement(node) && node.expression.getText(source) === "sourceElements.length > 0") {
+ dropBranches.push(node.thenStatement.getText(source));
+ }
+ ts.forEachChild(node, visit);
+ };
+ visit(source);
+ assert.equal(dropBranches.length, 1);
+ return {
+ drop: compile(`async function drop(protyle, sourceElements, targetElement, targetClass, gutterTypes, event, isCopyDrag) {
+ const editorElement = protyle.wysiwyg.element;
+ let dragoverElement;
+ ${dropBranches[0]}
+ }`),
+ actions: [
+ extract("protyle/util/editorCommonEvent.ts", ["getTargetListItem", "getDragSourceParentID",
+ "getDragSourceNextID", "moveTo", "dragSame"]),
+ extract("protyle/util/dragDocument.ts", ["isSameDragEditor", "isDragTargetInSource", "isSameSiblingMove",
+ "getSameSuperBlockEdgeTarget", "shouldKeepListBlockDragTarget"]),
+ extract("protyle/util/foldHeadingMove.ts", ["isFoldedHeading", "getHeadingLevel", "shouldUnfoldMovedHeading"]),
+ extract("protyle/wysiwyg/getBlock.ts", ["getParentBlock", "getPreviousBlockSibling", "getNextBlockSibling",
+ "getTopAloneElement", "getSbChildBlockCount"]),
+ ].join("\n"),
+ };
+};
+
+const cases = async source => {
+ const check = require("node:assert/strict");
+ const lute = window.Lute.New();
+ lute.SetKramdownIAL(true);
+ lute.SetProtyleWYSIWYG(true);
+ const root = document.createElement("div");
+ root.className = "protyle-wysiwyg";
+ document.body.append(root);
+ const protyle = {lute, wysiwyg: {element: root}, block: {rootID: "doc"}};
+ let transaction;
+ const api = new Function("transaction", "focusBlock", source.actions +
+ "; return {dragSame, getTargetListItem, shouldKeepListBlockDragTarget, getSameSuperBlockEdgeTarget};")(
+ (_protyle, forward, backward) => { transaction = {forward, backward}; }, () => {});
+ let route;
+ let pending;
+ let execute = false;
+ const drop = new Function("dragSame", "dragSb", "getTargetListItem", "shouldKeepListBlockDragTarget",
+ "getSameSuperBlockEdgeTarget", source.drop + "; return drop;")(
+ (...args) => {
+ route = {kind: "same", sources: [...args[1]], target: args[2], bottom: args[3], copy: args[4]};
+ if (execute) {
+ pending = api.dragSame(...args);
+ return pending;
+ }
+ }, (...args) => { route = {kind: "super", sources: [...args[1]], target: args[2]}; },
+ api.getTargetListItem, api.shouldKeepListBlockDragTarget, api.getSameSuperBlockEdgeTarget);
+ const makeList = markdown => {
+ const template = document.createElement("template");
+ template.innerHTML = lute.Md2BlockDOM(markdown);
+ return template.content.firstElementChild;
+ };
+ const setup = (markdown, placement) => {
+ root.replaceChildren();
+ const target = makeList(markdown);
+ const moving = makeList(markdown);
+ moving.setAttribute("name", "source-list");
+ moving.setAttribute("custom-test", "preserved");
+ target.setAttribute("name", "target-list");
+ const gap = document.createElement("div");
+ gap.dataset.nodeId = "gap";
+ gap.dataset.type = "NodeParagraph";
+ if (placement === "end") {
+ root.append(moving, gap, target);
+ } else if (placement === "middle") {
+ root.append(gap, target, moving);
+ } else {
+ root.append(target, moving);
+ }
+ route = undefined;
+ transaction = undefined;
+ pending = undefined;
+ return {target, moving};
+ };
+ const run = async (moving, target, indicator, copy = false) => {
+ await drop(protyle, [moving], target, [indicator],
+ [moving.dataset.type.toLowerCase()], {ctrlKey: copy}, copy);
+ await pending;
+ };
+ // 执行真实 drop 分支及移动事务,验证首尾和中间落点的列表边界、属性、编号与撤销重做。
+ for (const markdown of ["1. One\n 1. Nested\n2. Two", "- One\n - Nested\n- Two", "- [ ] One\n- [x] Two"]) {
+ for (const placement of ["start", "middle", "end"]) {
+ const {target, moving} = setup(markdown, placement);
+ const before = root.innerHTML;
+ const movingHTML = moving.outerHTML;
+ const targetHTML = target.outerHTML;
+ execute = false;
+ const indicator = placement === "end" ? "dragover__bottom" : "dragover__top";
+ await run(moving, target, indicator);
+ check.deepEqual(route.sources, [moving], "a block boundary must retain the source list");
+ check.equal(route.target, target, "a block boundary must retain the target list");
+ execute = true;
+ await run(moving, target, indicator);
+ check.equal(moving.outerHTML, movingHTML);
+ check.equal(target.outerHTML, targetHTML);
+ check.equal(root.querySelectorAll(":scope > .list").length, 2);
+ check.equal(placement === "end" ? target.nextElementSibling : target.previousElementSibling, moving);
+ check.equal(transaction.forward.length, 1);
+ check.equal(transaction.forward[0].id, moving.dataset.nodeId);
+ check.equal(transaction.forward[0].action, "move");
+ const after = root.innerHTML;
+ const replay = operations => operations.forEach(operation => {
+ check.equal(operation.action, "move");
+ const find = id => id === "doc" ? root : root.querySelector(`[data-node-id="${id}"]`);
+ const node = find(operation.id);
+ if (operation.nextID) {
+ find(operation.nextID).before(node);
+ } else if (operation.previousID) {
+ find(operation.previousID).after(node);
+ } else {
+ find(operation.parentID).prepend(node);
+ }
+ });
+ for (let repeat = 0; repeat < 2; repeat++) {
+ replay(transaction.backward);
+ check.equal(root.innerHTML, before);
+ replay(transaction.forward);
+ check.equal(root.innerHTML, after);
+ }
+ }
+ }
+ execute = false;
+ for (const copy of [false, true]) {
+ for (const indicator of ["dragover__top", "dragover__bottom"]) {
+ const {target, moving} = setup("- One\n- Two", "start");
+ await run(moving, target, indicator, copy);
+ check.deepEqual(route.sources, [moving]);
+ check.equal(route.target, target);
+ check.equal(route.copy, copy);
+ }
+ }
+ // 列表项落点仍展开源列表,保留原有同级合并与子级嵌套语义。
+ for (const indicator of ["dragover__top--sibling", "dragover__bottom--sibling", "dragover__bottom--child"]) {
+ const {target, moving} = setup("- One\n- Two", "start");
+ const targetItem = target.querySelector(".li");
+ await run(moving, targetItem, indicator);
+ check.deepEqual(route.sources, Array.from(moving.querySelectorAll(":scope > .li")));
+ check.equal(route.target, indicator.endsWith("--child") ? targetItem.querySelector(".p") : targetItem);
+ }
+ for (const indicator of ["dragover__top", "dragover__bottom"]) {
+ const {target, moving} = setup("- One\n- Two", "start");
+ const sourceItem = moving.querySelector(".li");
+ await run(sourceItem, target, indicator);
+ check.deepEqual(route.sources, [sourceItem]);
+ const items = target.querySelectorAll(":scope > .li");
+ check.equal(route.target, items[indicator === "dragover__top" ? 0 : items.length - 1]);
+ }
+ for (const indicator of ["dragover__left", "dragover__right"]) {
+ const {target, moving} = setup("- One\n- Two", "start");
+ await run(moving, target, indicator);
+ check.equal(route.kind, "super");
+ check.deepEqual(route.sources, [moving]);
+ check.equal(route.target, target);
+ }
+ root.remove();
+ return "List drop cases passed";
+};
+
+const run = async () => {
+ const {app, BrowserWindow} = require("electron");
+ app.setPath("userData", process.argv[2]);
+ app.commandLine.appendSwitch("disable-gpu");
+ await app.whenReady();
+ const win = new BrowserWindow({show: false, webPreferences: {
+ nodeIntegration: true, contextIsolation: false, offscreen: true,
+ }});
+ let code = 0;
+ try {
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ await win.webContents.executeJavaScript(readFileSync(path.join(__dirname,
+ "../stage/protyle/js/lute/lute.min.js"), "utf8"));
+ const result = await win.webContents.executeJavaScript(`(async () => { try {
+ return await (${cases.toString()})(${JSON.stringify(sources())});
+ } catch (error) { return error.stack; } })()`);
+ assert.equal(result, "List drop cases passed");
+ console.log(result);
+ } catch (error) {
+ console.error(error);
+ code = 1;
+ } finally {
+ win.destroy();
+ app.exit(code);
+ }
+};
+
+if (process.versions.electron && process.type === "browser") {
+ run().catch(error => { console.error(error); require("electron").app.exit(1); });
+} else {
+ require("node:test").test("editor list drops preserve block boundaries, list item routing, and move undo/redo", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 45000,
+ }, async () => {
+ const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-list-drop-test-"));
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(
+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 40000});
+ assert.match(stdout, /List drop cases passed/);
+ } finally {
+ assert.ok(path.resolve(profile).startsWith(path.resolve(os.tmpdir()) + path.sep));
+ rmSync(profile, {recursive: true, force: true});
+ }
+ });
+}