Skip to content

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

:bug: Preserve complete lists when creating super blocks https://github.com/siyuan-note/siyuan/issues/18174

Daniele86c2d167b96
diff --git a/app/src/protyle/util/dragDocument.test.ts b/app/src/protyle/util/dragDocument.test.ts
index 70caa44a3d..26a2685892 100644
--- a/app/src/protyle/util/dragDocument.test.ts
+++ b/app/src/protyle/util/dragDocument.test.ts
@@ -2,11 +2,13 @@ import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
getSameSuperBlockEdgeTarget,
+ getTopListDragTarget,
isAttributeViewTitleTarget,
isDragTargetInSource,
isSameDragEditor,
isSameSiblingMove,
replaceDragUndoOperation,
+ shouldKeepListBlockDragTarget,
uniqueDragIds
} from "./dragDocument";
@@ -182,6 +184,38 @@ describe("getSameSuperBlockEdgeTarget", () => {
});
});
+describe("getTopListDragTarget", () => {
+ it("uses the complete list when a list item is hit at the outer edge", () => {
+ const list = createClassElement(["list"]);
+ const listItem = createClassElement(["li"], list);
+
+ assert.equal(getTopListDragTarget(listItem), list);
+ });
+
+ it("uses the outer list when a nested list item is hit", () => {
+ const outerList = createClassElement(["list"]);
+ const parentItem = createClassElement(["li"], outerList);
+ const nestedList = createClassElement(["list"], parentItem);
+ const nestedItem = createClassElement(["li"], nestedList);
+
+ assert.equal(getTopListDragTarget(nestedItem), outerList);
+ });
+});
+
+describe("shouldKeepListBlockDragTarget", () => {
+ it("keeps both complete lists when forming a horizontal super block", () => {
+ assert.equal(shouldKeepListBlockDragTarget("nodelist", true, false), true);
+ });
+
+ it("allows a list block to expand into list items for a regular list drop", () => {
+ assert.equal(shouldKeepListBlockDragTarget("nodelist", false, false), false);
+ });
+
+ it("keeps a list block when reordering columns", () => {
+ assert.equal(shouldKeepListBlockDragTarget("nodelist", false, true), true);
+ });
+});
+
describe("replaceDragUndoOperation", () => {
it("restores a valid container before moving the remaining items back", () => {
const operations = ["move-c", "move-b", "move-a"];
diff --git a/app/src/protyle/util/dragDocument.ts b/app/src/protyle/util/dragDocument.ts
index ebafa7cfc9..11b38731de 100644
--- a/app/src/protyle/util/dragDocument.ts
+++ b/app/src/protyle/util/dragDocument.ts
@@ -68,6 +68,22 @@ export const getSameSuperBlockEdgeTarget = (sourceElements: Element[], targetEle
return childBlocks[isRight ? childBlocks.length - 1 : 0];
};
+export const getTopListDragTarget = (targetElement: Element) => {
+ let topList = targetElement;
+ while (topList.parentElement?.classList.contains("li") ||
+ topList.parentElement?.classList.contains("list")) {
+ topList = topList.parentElement;
+ if (topList.classList.contains("list") && !topList.parentElement?.classList.contains("li")) {
+ break;
+ }
+ }
+ return topList;
+};
+
+export const shouldKeepListBlockDragTarget = (sourceType: string, isHorizontalDrop: boolean,
+ isColumnSuperBlockChild: boolean) =>
+ sourceType === "nodelist" && (isHorizontalDrop || isColumnSuperBlockChild);
+
export const replaceDragUndoOperation = <T>(operations: T[], operation: T, replacements: T[]) => {
const index = operations.indexOf(operation);
if (index < 0) {
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index fb7e42f7f9..f3de83f340 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -55,11 +55,13 @@ import {setFold} from "./blockFold";
import {isEncryptedBox} from "../../util/pathName";
import {
getSameSuperBlockEdgeTarget,
+ getTopListDragTarget,
isAttributeViewTitleTarget,
isDragTargetInSource,
isSameDragEditor,
isSameSiblingMove,
replaceDragUndoOperation,
+ shouldKeepListBlockDragTarget,
uniqueDragIds
} from "./dragDocument";
import {getAVFilteredTipContext, getAVViewID} from "../render/av/filteredTip";
@@ -1766,13 +1768,15 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
}
}
- // 拖拽整个列表块(NodeList)到列表项时,展开为其下的列表项,避免形成 list>list 非法嵌套
- // 但当目标是超级块(col 布局)内的列表块时,列表块本身是超级块的一个列单元,
- // 应走列重排(dragSame)而非展开,否则 targetElement 被改写为 .li 后无法命中列重排分支
+ // 列表块横向拖拽或在横排超级块内重排时,目标列表本身是列单元,需保留完整列表。
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,
+ isColSbChildList);
if (isListItemSource && targetElement.classList.contains("list") &&
- !(gutterTypes[0] === "nodelist" && isColSbChildList)) {
+ !keepListBlockTarget) {
const targetListItem = getTargetListItem(targetElement, isBottom);
if (targetListItem) {
targetElement = targetListItem;
@@ -2671,14 +2675,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
// 列表项目标无论是否命中优化分支都需立即处理,避免拖到列表标记符(.protyle-action)上时提示和插入点缺失
if (liTarget) {
// 向上找顶层列表容器,用于判断整个列表的左右边缘(而非子列表)
- let topList: Element = liTarget as HTMLElement;
- while (topList.parentElement?.classList.contains("li") ||
- topList.parentElement?.classList.contains("list")) {
- topList = topList.parentElement;
- if (topList.classList.contains("list") && !topList.parentElement?.classList.contains("li")) {
- break;
- }
- }
+ const topList = getTopListDragTarget(liTarget as HTMLElement) as HTMLElement;
const topListRect = topList.getBoundingClientRect();
const isLeftEdge = event.clientX < topListRect.left + 32;
const isRightEdge = event.clientX > topListRect.right - 32;
@@ -2693,6 +2690,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
}
// 非列表项源:边缘不进入 applyLiTarget,清空 liTarget 让后续通用分支处理横向超级块
if (isLeftEdge || isRightEdge) {
+ targetElement = topList;
liTarget = null;
} else {
applyLiTarget(liTarget as HTMLElement, event, !isContentBlockSource);