Skip to content

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

:bug: Unfold headings when dropping blocks below them https://github.com/siyuan-note/siyuan/issues/19900

Daniel2cbd050ec923
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 4013b9cfc8..afe9da840e 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -981,6 +981,7 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
}
const targetListStart = targetElement.getAttribute("data-type") === "NodeListItem" &&
targetElement.getAttribute("data-subtype") === "o" ? getOrderedListStart(targetElement.parentElement) : undefined;
+ const previousBlockElement = getPreviousBlockSibling(targetElement);
const moveToResult = await moveTo(protyle, sourceElements, sourceRowElement || targetElement, isSameEditor,
sourceRowElement || intoEmptyTab ? "afterbegin" : (isBottom ? "afterend" : "beforebegin"), isCopy, sourcePositions);
if (sourceRowElement && isCopy) {
@@ -997,7 +998,6 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
}
undoOperations.push(...wrapUndoOperations);
const newSourceParentElement = moveToResult.newSourceElements;
- const previousBlockElement = getPreviousBlockSibling(targetElement);
const unfoldHeadingElements = new Set<Element>();
if (!isColumnDrop && isBottom &&
targetElement.getAttribute("data-type") === "NodeHeading" &&
diff --git a/app/src/protyle/util/foldHeadingMove.test.ts b/app/src/protyle/util/foldHeadingMove.test.ts
index d9dc346790..ddd5371238 100644
--- a/app/src/protyle/util/foldHeadingMove.test.ts
+++ b/app/src/protyle/util/foldHeadingMove.test.ts
@@ -1,5 +1,8 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {transpileModule, ScriptTarget} from "typescript";
import {isFoldedHeading, shouldUnfoldMovedHeading} from "./foldHeadingMove";
class TestBlockElement {
@@ -42,3 +45,54 @@ describe("folded heading moves", () => {
assert.equal(shouldUnfoldMovedHeading(foldedHeading), false);
});
});
+
+it("unfolds the heading preceding a top drop before moved blocks replace that neighbor", async () => {
+ const source = readFileSync(join(__dirname, "editorCommonEvent.ts"), "utf8");
+ const body = source.slice(source.indexOf("const dragSame ="), source.indexOf("export const dropEvent ="));
+ for (const isBottom of [false, true]) {
+ const parent = {children: [] as Element[], classList: {contains: () => false}};
+ const block = (id: string, type: string, folded = false) => ({
+ getAttribute: (name: string) => ({"data-node-id": id, "data-type": type, fold: folded ? "1" : null}[name]),
+ hasAttribute: (name: string) => name === "data-node-id",
+ classList: {contains: () => false},
+ closest: (): Element => null,
+ parentElement: parent,
+ }) as unknown as Element;
+ const heading = block("heading", "NodeHeading", true);
+ const target = block("target", "NodeHeading");
+ const moving = block("moving", "NodeParagraph");
+ parent.children = [heading, target, moving];
+ let submitted: {doOperations: IOperation[], undoOperations: IOperation[]};
+ const dependencies = {
+ isDragTargetInSource: () => false,
+ isSameSiblingMove: () => false,
+ isSameDragEditor: () => true,
+ isFoldedHeading,
+ shouldUnfoldMovedHeading,
+ getPreviousBlockSibling: (element: Element) => parent.children[parent.children.indexOf(element) - 1],
+ getNextBlockSibling: (element: Element) => parent.children[parent.children.indexOf(element) + 1],
+ moveTo: async () => {
+ parent.children = [heading, moving, target];
+ return {newSourceElements: [moving], doOperations: [{action: "move", id: "moving", previousID: "heading"}],
+ undoOperations: [{action: "move", id: "moving", previousID: "target"}]};
+ },
+ setFold: (_protyle: IProtyle, element: Element) => {
+ assert.equal(element, heading);
+ return {doOperations: [{action: "unfoldHeading", id: "heading"}],
+ undoOperations: [{action: "foldHeading", id: "heading"}]};
+ },
+ transaction: (_protyle: IProtyle, doOperations: IOperation[], undoOperations: IOperation[]) => {
+ submitted = {doOperations, undoOperations};
+ },
+ document: {contains: () => true},
+ focusBlock: (): void => undefined,
+ };
+ const dragSame = new Function(...Object.keys(dependencies), transpileModule(body, {
+ compilerOptions: {target: ScriptTarget.ES2021},
+ }).outputText + "\nreturn dragSame;")(...Object.values(dependencies));
+ await dragSame({wysiwyg: {element: parent}}, [moving], isBottom ? heading : target, isBottom, false);
+ assert.deepEqual(submitted.doOperations.map(operation => operation.action), ["move", "unfoldHeading"]);
+ assert.deepEqual(submitted.undoOperations.map(operation => operation.action), ["move", "foldHeading"]);
+ assert.equal(submitted.doOperations[1].context.focusId, "moving");
+ }
+});