Skip to content

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

:bug: Preserve block drop positions before focused headings https://github.com/siyuan-note/siyuan/issues/19903

Daniel482d66634daa
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index afe9da840e..503f1e99e9 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -284,6 +284,19 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
.filter((id): id is string => !!id));
const targetId = targetElement.classList.contains("tab-item-content") ?
targetElement.parentElement.getAttribute("data-node-id") : targetElement.getAttribute("data-node-id");
+ let targetPreviousID: string;
+ // 聚焦视图首块的前一个同级块可能未渲染,必须从文档树取得真实位置。
+ if (position === "beforebegin" && !getPreviousBlockSibling(targetElement) &&
+ getParentBlock(targetElement) === protyle.wysiwyg.element && protyle.block.showAll === false) {
+ const response = await fetchSyncPost("/api/block/getBlockRelevantIDs", {
+ id: targetId,
+ notebook: protyle.notebookId,
+ });
+ if (response.code !== 0) {
+ throw new Error(response.msg);
+ }
+ targetPreviousID = response.data.previousID;
+ }
const newSourceElements: Element[] = [];
let tempTargetElement = targetElement;
let isSameLi = true;
@@ -360,7 +373,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
action: "insert",
data: newListElement.outerHTML,
id: newListId,
- previousID: position === "afterbegin" ? null : (position === "afterend" ? targetId : getPreviousBlockSibling(tempTargetElement)?.getAttribute("data-node-id")),
+ previousID: position === "afterbegin" ? null : (position === "afterend" ? targetId : getPreviousBlockSibling(tempTargetElement)?.getAttribute("data-node-id") || targetPreviousID),
parentID: position === "afterbegin" ? targetId : (getParentBlock(tempTargetElement)?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID),
});
undoOperations.push({
@@ -438,7 +451,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
action: "insert",
id: copyNewId,
data: copyElement.outerHTML,
- previousID: position === "afterbegin" ? null : (position === "afterend" ? targetId : getPreviousBlockSibling(copyElement)?.getAttribute("data-node-id")), // 不能使用常量,移动后会被修改
+ previousID: position === "afterbegin" ? null : (position === "afterend" ? targetId : getPreviousBlockSibling(copyElement)?.getAttribute("data-node-id") || targetPreviousID), // 不能使用常量,移动后会被修改
parentID: position === "afterbegin" ? targetId : (getParentBlock(copyElement)?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID),
});
newSourceElements.push(copyElement);
@@ -510,7 +523,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
doOperations.push({
action: "move",
id,
- previousID: position === "afterbegin" ? null : (position === "afterend" ? targetId : getPreviousBlockSibling(item)?.getAttribute("data-node-id")), // 不能使用常量,移动后会被修改
+ previousID: position === "afterbegin" ? null : (position === "afterend" ? targetId : getPreviousBlockSibling(item)?.getAttribute("data-node-id") || targetPreviousID), // 不能使用常量,移动后会被修改
parentID: position === "afterbegin" ? targetId : (getParentBlock(item)?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID),
context: {moveGroupID},
});
diff --git a/app/tests/tabsDrag.test.js b/app/tests/tabsDrag.test.js
index 69733229df..43fdd7d978 100644
--- a/app/tests/tabsDrag.test.js
+++ b/app/tests/tabsDrag.test.js
@@ -28,6 +28,7 @@ const sources = () => {
}).outputText;
})(),
extract("protyle/wysiwyg/tabsRemoval.ts", ["repairActiveTab"]),
+ extract("protyle/util/tabsCopy.ts", ["preserveTabTask", "remapTabsDOMIDs"]),
extract("protyle/render/tabsState.ts", ["adjacentTabID", "resolveTabID", "tabKeyboardTarget"]),
extract("protyle/render/tabsAttributes.ts", ["clearTabsAttributes", "renderTabsAttributes"]),
extract("util/escape.ts", ["escapeHtml"]),
@@ -60,9 +61,15 @@ const cases = async (source) => {
const constants = {ZWSP: "\u200b", SIYUAN_DROP_BLOCK: "application/siyuan-block", SIYUAN_DROP_GUTTER: "application/siyuan-gutter"};
const protyle = {lute, wysiwyg: {element: root}, notebookId: "notebook", block: {rootID: "doc"}};
window.siyuan = {config: {system: {workspaceDir: "workspace"}}};
- const {moveTo, bindTabsDrag, isDraggingTabs, syncBlockAttrs, tabsRender, destroyTabsRender} = new Function("Constants", "genEmptyElement", "root", "protyle",
+ const fetchSyncPost = async (url, data) => {
+ check.equal(url, "/api/block/getBlockRelevantIDs");
+ check.equal(data.id, root.firstElementChild.dataset.nodeId);
+ check.equal(data.notebook, "notebook");
+ return {code: 0, data: {previousID: "outside-focus", parentID: "doc"}};
+ };
+ const {moveTo, bindTabsDrag, isDraggingTabs, syncBlockAttrs, tabsRender, destroyTabsRender} = new Function("Constants", "genEmptyElement", "root", "protyle", "fetchSyncPost", "remapListMindmapIDs",
source + "; return {moveTo, bindTabsDrag, isDraggingTabs, syncBlockAttrs, tabsRender, destroyTabsRender};")(
- constants, genEmptyElement, root, protyle);
+ constants, genEmptyElement, root, protyle, fetchSyncPost, () => {});
const list = document.createElement("div");
const button = document.createElement("button");
button.className = "tabs-tab";
@@ -168,6 +175,21 @@ const cases = async (source) => {
check.equal(layoutTabs.firstElementChild, header);
check.equal(layoutTabs.querySelectorAll(":scope > .tabs-header").length, 1);
destroyTabsRender(root);
+ for (const copy of [false, true]) {
+ root.innerHTML = lute.Md2BlockDOM("## Focus\n\nFirst\n\nSecond");
+ protyle.block.showAll = false;
+ const heading = root.firstElementChild;
+ const moving = Array.from(root.children).slice(1);
+ const focusedMove = await moveTo(protyle, moving, heading, true, "beforebegin", copy);
+ const placements = focusedMove.doOperations.filter(op => op.action === (copy ? "insert" : "move"));
+ check.equal(placements.length, 2);
+ check.equal(placements[0].previousID, "outside-focus");
+ check.equal(placements[1].previousID, "outside-focus");
+ check.equal(root.children[2], heading);
+ if (!copy) {
+ check.equal(focusedMove.undoOperations[0].previousID, heading.dataset.nodeId);
+ }
+ }
root.remove();
return "Tabs drag cases passed";
};