Skip to content

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

:bug: Preserve folded heading super block structure https://github.com/siyuan-note/siyuan/issues/18218

Daniel45803a8f8d20
diff --git a/app/src/block/util.ts b/app/src/block/util.ts
index 3dc7ffe506..a064ff5de3 100644
--- a/app/src/block/util.ts
+++ b/app/src/block/util.ts
@@ -125,8 +125,8 @@ export const refreshSbResize = (sbElement: Element) => {
// 子块进出超级块后,重新分配所有子块的宽度(按比例均摊 gap),避免 gap 不均或换行
// 仅当超级块中已有子块设置了宽度时才调整(否则保持 CSS 默认等分)
-// 返回被改动的块信息(id + 改前 HTML),供调用方持久化
-export const rebalanceSbWidth = (sbElement: Element): Array<{id: string, oldHTML: string}> => {
+// 返回被改动的块信息(id + 改前 style),供调用方持久化
+export const rebalanceSbWidth = (sbElement: Element): Array<{id: string, oldStyle: string}> => {
if (!sbElement || sbElement.getAttribute("data-sb-layout") !== "col") {
return [];
}
@@ -155,20 +155,20 @@ export const rebalanceSbWidth = (sbElement: Element): Array<{id: string, oldHTML
});
// 归一化到总和 1,使子块填满整个超级块(删除/移入后不留空白)
const totalRatio = ratios.reduce((s, r) => s + r, 0) || 1;
- // 记录改前 HTML 用于持久化
- const changes: Array<{id: string, oldHTML: string}> = [];
+ // 记录改前 style 用于持久化
+ const changes: Array<{id: string, oldStyle: string}> = [];
children.forEach((child, i) => {
- const oldHTML = child.outerHTML;
+ const oldStyle = child.getAttribute("style") || "";
const pct = Math.round((ratios[i] / totalRatio) * 100 * 10) / 10;
child.style.width = `calc(${pct}% - ${gapShare}px)`;
child.style.flex = "none";
- changes.push({id: child.getAttribute("data-node-id"), oldHTML});
+ changes.push({id: child.getAttribute("data-node-id"), oldStyle});
});
return changes;
};
// 刷新超级块的拖拽手柄并重新分配子块宽度,把变更持久化到 do/undo operations
-// 宽度撤销插入到 undoOperations 头部,确保 update undo 先于 move undo 执行(位置恢复后再还原宽度会错位)
+// 宽度撤销插入到 undoOperations 头部,确保 setAttrs undo 先于 move undo 执行(位置恢复后再还原宽度会错位)
// 已脱离 DOM 的超级块会被跳过(cancelSB 可能已将其删除)
export const refreshSbAndPersistWidth = (sbElement: Element,
doOperations: IOperation[], undoOperations: IOperation[]) => {
@@ -180,8 +180,17 @@ export const refreshSbAndPersistWidth = (sbElement: Element,
widthChanges.forEach(change => {
const targetEl = sbElement.querySelector(`[data-node-id="${change.id}"]`);
if (targetEl) {
- doOperations.push({action: "update", id: change.id, data: targetEl.outerHTML});
- undoOperations.splice(0, 0, {action: "update", id: change.id, data: change.oldHTML});
+ // 折叠标题的子块可能未渲染到 DOM,只持久化 style 可避免用不完整 HTML 覆盖块内容。
+ doOperations.push({
+ action: "setAttrs",
+ id: change.id,
+ data: JSON.stringify({style: targetEl.getAttribute("style") || ""})
+ });
+ undoOperations.splice(0, 0, {
+ action: "setAttrs",
+ id: change.id,
+ data: JSON.stringify({style: change.oldStyle})
+ });
}
});
};
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 46ac12340d..6e23074cfc 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -89,9 +89,97 @@ const getTargetListItem = (targetElement: Element, isBottom: boolean) => {
return targetElement.closest(".li") as HTMLElement;
};
+const isFoldedHeading = (element: Element) => {
+ return element.getAttribute("data-type") === "NodeHeading" && element.getAttribute("fold") === "1";
+};
+
+type TDragSourcePosition = {
+ previousID: string,
+ parentID: string
+};
+
+const getDragSourceParentID = async (protyle: IProtyle, element: Element) => {
+ const parentBlock = getParentBlock(element);
+ const parentID = parentBlock?.getAttribute("data-node-id");
+ if (parentID) {
+ return parentID;
+ }
+ if (parentBlock === protyle.wysiwyg.element) {
+ return protyle.block.parentID || protyle.block.rootID;
+ }
+ let sourceRootID = "";
+ /// #if !MOBILE
+ const sourceEditor = getAllEditor().find(editor => editor.protyle.wysiwyg.element === parentBlock);
+ sourceRootID = sourceEditor?.protyle?.block?.rootID || "";
+ /// #endif
+ if (sourceRootID) {
+ return sourceRootID;
+ }
+ const response = await fetchSyncPost("/api/block/getBlockInfo", {
+ id: element.getAttribute("data-node-id")
+ });
+ return response?.data?.rootID || "";
+};
+
+const wrapInRowSB = async (protyle: IProtyle, elements: Element[]) => {
+ const firstElement = elements[0];
+ const sourcePosition: TDragSourcePosition = {
+ previousID: getPreviousBlockSibling(firstElement)?.getAttribute("data-node-id") || "",
+ parentID: await getDragSourceParentID(protyle, firstElement)
+ };
+ const operations = await turnsIntoOneTransaction({
+ protyle,
+ selectsElement: elements,
+ type: "BlocksMergeSuperBlock",
+ level: "row",
+ unfocus: true,
+ getOperations: true,
+ parentID: sourcePosition.parentID
+ });
+ return {
+ element: firstElement.parentElement,
+ doOperations: operations.doOperations,
+ undoOperations: operations.undoOperations,
+ sourcePosition
+ };
+};
+
+const genRowSBElement = (firstChild: Element) => {
+ const sbElement = genSBElement("row");
+ const childElement = firstChild as HTMLElement;
+ if (childElement.style.width) {
+ (sbElement as HTMLElement).style.width = childElement.style.width;
+ (sbElement as HTMLElement).style.flex = childElement.style.flex;
+ }
+ return sbElement;
+};
+
+const clearColumnWidth = (element: Element) => {
+ const htmlElement = element as HTMLElement;
+ if (!htmlElement.style.width) {
+ return false;
+ }
+ htmlElement.style.width = "";
+ htmlElement.style.flex = "";
+ return true;
+};
+
+const clearCopiedColumnWidth = (sbElement: Element, operations: IOperation[]) => {
+ const childElement = Array.from(sbElement.querySelectorAll<HTMLElement>("[data-node-id]")).find(item => item.style.width);
+ if (!childElement || !clearColumnWidth(childElement)) {
+ return;
+ }
+ const insertOperation = operations.find(item => item.action === "insert" &&
+ item.id === childElement.getAttribute("data-node-id"));
+ if (insertOperation) {
+ insertOperation.data = childElement.outerHTML;
+ }
+};
+
// position: afterbegin 为拖拽成超级块; "afterend", "beforebegin" 一般拖拽
const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element,
- isSameEditor: boolean, position: InsertPosition, isCopy: boolean) => {
+ isSameEditor: boolean, position: InsertPosition, isCopy: boolean,
+ sourcePositions = new Map<string, TDragSourcePosition>()) => {
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
const copyFoldHeadingIds: { newId: string, oldId: string }[] = [];
@@ -112,36 +200,15 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
// 不能依赖循环内 getParentBlock(item)(移动后 item 的父已变),否则撤销会移到错误位置。
// 关键:对于文档顶层块,getParentBlock 返回 .protyle-wysiwyg 容器(无 data-node-id),
// 不能用目标 protyle 的 rootID(跨文档拖拽时这是错误的文档),必须用源 DOM 所属文档 rootID。
- const sourcePositions = new Map<string, { previousID: string, parentID: string }>();
- for (const item of sourceElements) {
- const id = item.getAttribute("data-node-id");
- if (id) {
- const parentBlock = getParentBlock(item);
- let srcParentID = parentBlock?.getAttribute("data-node-id");
- if (!srcParentID) {
- // 顶层块:父是 .protyle-wysiwyg 容器(无 data-node-id)。
- let srcRootID = "";
- /// #if !MOBILE
- // 通过 getAllEditor 反查 item 所属的源 protyle,取其 block.rootID。
- const sourceEditor = getAllEditor().find(editor =>
- editor.protyle.wysiwyg.element === parentBlock);
- if (sourceEditor?.protyle?.block?.rootID) {
- srcRootID = sourceEditor.protyle.block.rootID;
- }
- /// #endif
- if (srcRootID) {
- srcParentID = srcRootID;
- } else {
- // 跨窗口/移动端 getAllEditor 找不到源编辑器,用 kernel API 反查块的真实 rootID。
- // 不能 fallback 到目标 protyle 的 rootID(会导致撤销把块移到错误文档)。
- const response = await fetchSyncPost("/api/block/getBlockInfo", {id});
- srcParentID = response?.data?.rootID || "";
- }
+ if (!isCopy) {
+ for (const item of sourceElements) {
+ const id = item.getAttribute("data-node-id");
+ if (id && !sourcePositions.has(id)) {
+ sourcePositions.set(id, {
+ previousID: getPreviousBlockSibling(item)?.getAttribute("data-node-id") || "",
+ parentID: await getDragSourceParentID(protyle, item)
+ });
}
- sourcePositions.set(id, {
- previousID: getPreviousBlockSibling(item)?.getAttribute("data-node-id") || "",
- parentID: srcParentID || "",
- });
}
}
for (let index = sourceElements.length - 1; index >= 0; index--) {
@@ -470,6 +537,31 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
return;
}
}
+ const focusSourceElement = sourceElements[0];
+ const sourceHasFoldHeading = sourceElements.some(isFoldedHeading);
+ const wrapDoOperations: IOperation[] = [];
+ const wrapUndoOperations: IOperation[] = [];
+ const sourcePositions = new Map<string, TDragSourcePosition>();
+ // 先在原标题位置完成纵向分组,避免新外层超级块落入 HeadingChildren 后形成循环父子关系。
+ if (direct === "col" && sourceHasFoldHeading && !isCopy) {
+ const sourceWrap = await wrapInRowSB(protyle, sourceElements);
+ wrapDoOperations.push(...sourceWrap.doOperations);
+ wrapUndoOperations.splice(0, 0, ...sourceWrap.undoOperations);
+ sourceElements = [sourceWrap.element];
+ sourcePositions.set(sourceWrap.element.getAttribute("data-node-id"), sourceWrap.sourcePosition);
+ }
+ if (direct === "col" && isFoldedHeading(targetElement)) {
+ const targetID = targetElement.getAttribute("data-node-id");
+ const targetWrap = await wrapInRowSB(protyle, [targetElement]);
+ wrapDoOperations.push(...targetWrap.doOperations);
+ wrapUndoOperations.splice(0, 0, ...targetWrap.undoOperations);
+ targetElement = targetWrap.element;
+ sourcePositions.forEach(position => {
+ if (position.previousID === targetID) {
+ position.previousID = targetElement.getAttribute("data-node-id");
+ }
+ });
+ }
const undoOperations: IOperation[] = [];
const targetMoveUndo: IOperation = {
action: "move",
@@ -482,19 +574,35 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
};
const sbElement = genSBElement(direct);
targetElement.parentElement.replaceChild(sbElement, targetElement);
- const doOperations: IOperation[] = [{
+ const doOperations: IOperation[] = [...wrapDoOperations, {
action: "insert",
data: sbElement.outerHTML,
id: sbElement.getAttribute("data-node-id"),
- nextID: getNextBlockSibling(sbElement)?.getAttribute("data-node-id"),
- previousID: getPreviousBlockSibling(sbElement)?.getAttribute("data-node-id"),
- parentID: getParentBlock(sbElement)?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID
+ // 目标块稍后才会移入新超级块,先用它作为同级锚点可避免外层超级块误插入源折叠标题的纵向分组。
+ nextID: targetElement.getAttribute("data-node-id"),
+ parentID: targetMoveUndo.parentID
}];
// 临时插入,防止后面计算错误,最终再移动矫正
sbElement.lastElementChild.before(targetElement);
- const moveToResult = await moveTo(protyle, sourceElements, sbElement, isSameEditor, "afterbegin", isCopy);
+ // 复制折叠标题时原块不移动,可直接在目标外层超级块中创建纵向分组。
+ let sourceRowElement: Element;
+ if (direct === "col" && sourceHasFoldHeading && isCopy) {
+ sourceRowElement = genRowSBElement(sourceElements[0]);
+ sbElement.lastElementChild.before(sourceRowElement);
+ doOperations.push({
+ action: "insert",
+ data: sourceRowElement.outerHTML,
+ id: sourceRowElement.getAttribute("data-node-id"),
+ parentID: sbElement.getAttribute("data-node-id")
+ });
+ }
+ const moveToResult = await moveTo(protyle, sourceElements, sourceRowElement || sbElement,
+ isSameEditor, "afterbegin", isCopy, sourcePositions);
+ if (sourceRowElement && isCopy) {
+ clearCopiedColumnWidth(sourceRowElement, moveToResult.doOperations);
+ }
doOperations.push(...moveToResult.doOperations);
- undoOperations.push(...moveToResult.undoOperations);
+ const sourceUndoOperations = [...moveToResult.undoOperations];
const newSourceParentElement = moveToResult.newSourceElements;
// 横向超级块A内两个元素拖拽成纵向超级块B,取消超级块A会导致 targetElement 被删除,需先移动再删除 https://github.com/siyuan-note/siyuan/issues/16292
let removeIndex = doOperations.length;
@@ -509,39 +617,46 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
}
});
+ const sourcePreviousID = (sourceRowElement || newSourceParentElement[0]).getAttribute("data-node-id");
+ const targetOperations: IOperation[] = [];
if (isBottom) {
- // 拖拽到超级块 col 下方, 其他块右侧
+ // 拖拽到超级块 col 下方,其他块右侧
sbElement.insertAdjacentElement("afterbegin", targetElement);
- doOperations.splice(removeIndex, 0, {
+ targetOperations.push({
action: "move",
id: targetElement.getAttribute("data-node-id"),
parentID: sbElement.getAttribute("data-node-id")
});
} else {
sbElement.lastElementChild.insertAdjacentElement("beforebegin", targetElement);
- doOperations.splice(removeIndex, 0, {
+ targetOperations.push({
action: "move",
id: targetElement.getAttribute("data-node-id"),
- previousID: newSourceParentElement[0].getAttribute("data-node-id"),
+ previousID: sourcePreviousID
});
}
- undoOperations.push(targetMoveUndo);
+ doOperations.splice(removeIndex, 0, ...targetOperations);
+ const targetUndoOperations = [targetMoveUndo];
+ // 目标原本紧跟源块时,需要先恢复作为 previousID 的源块,再恢复目标块。
+ let targetUndoIndex = 0;
+ if (!isCopy && targetMoveUndo.previousID) {
+ const previousSourceUndoIndex = sourceUndoOperations.findIndex(item =>
+ ["insert", "move"].includes(item.action) && item.id === targetMoveUndo.previousID);
+ if (previousSourceUndoIndex > -1) {
+ targetUndoIndex = previousSourceUndoIndex + 1;
+ }
+ }
+ undoOperations.push(
+ ...sourceUndoOperations.slice(0, targetUndoIndex),
+ ...targetUndoOperations,
+ ...sourceUndoOperations.slice(targetUndoIndex)
+ );
undoOperations.push({
action: "delete",
id: sbElement.getAttribute("data-node-id"),
});
- const foldElements: Element[] = [];
- newSourceParentElement.forEach(item => {
- const nextBlockElement = getNextBlockSibling(item);
- if (item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1" &&
- nextBlockElement && (
- nextBlockElement.getAttribute("data-type") !== "NodeHeading" ||
- (nextBlockElement.getAttribute("data-subtype") || "") > item.getAttribute("data-subtype")
- )) {
- foldElements.push(item);
- }
- });
- if ((newSourceParentElement.length > 1 || foldElements.length > 0) && direct === "col") {
+ undoOperations.push(...wrapUndoOperations);
+ if (!sourceRowElement && newSourceParentElement.length > 1 && direct === "col") {
const mergeOperations = await turnsIntoOneTransaction({
protyle,
selectsElement: newSourceParentElement.reverse(),
@@ -553,19 +668,14 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
doOperations.push(...mergeOperations.doOperations);
undoOperations.splice(0, 0, ...mergeOperations.undoOperations);
}
- foldElements.forEach(item => {
- const foldOperations = setFold(protyle, item, true, false, false, true);
- doOperations.push(...foldOperations.doOperations);
- undoOperations.splice(0, 0, ...foldOperations.undoOperations);
- });
refreshSbResize(sbElement);
originSbSet.forEach(sb => {
refreshSbAndPersistWidth(sb, doOperations, undoOperations);
});
// 跨文档移动为可逆条目:全局撤销栈按 rootID 分栈联动,撤销时经 mutatedRootIDs 判定弹确认
transaction(protyle, doOperations, undoOperations);
- if (document.contains(sourceElements[0])) {
- focusBlock(sourceElements[0]);
+ if (document.contains(focusSourceElement)) {
+ focusBlock(focusSourceElement);
} else {
focusBlock(targetElement);
}
@@ -573,6 +683,7 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean, isCopy: boolean) => {
const isSameEditor = isSameDragEditor(protyle.wysiwyg.element, sourceElements[0]);
+ const focusSourceElement = sourceElements[0];
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
// 移动前记录源块所在的超级块,移动后刷新其手柄(移出后需重建)
@@ -584,17 +695,69 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
}
});
- const moveToResult = await moveTo(protyle, sourceElements, targetElement, isSameEditor, isBottom ? "afterend" : "beforebegin", isCopy);
+ const sourceHasFoldHeading = sourceElements.some(isFoldedHeading);
+ const targetParentElement = targetElement.parentElement;
+ const isColumnDrop = targetParentElement.classList.contains("sb") &&
+ targetParentElement.getAttribute("data-sb-layout") === "col";
+ const wrapUndoOperations: IOperation[] = [];
+ const sourcePositions = new Map<string, TDragSourcePosition>();
+ let sourceRowElement: Element;
+ if (sourceHasFoldHeading && isColumnDrop && !isCopy) {
+ const sourceWrap = await wrapInRowSB(protyle, sourceElements);
+ doOperations.push(...sourceWrap.doOperations);
+ wrapUndoOperations.splice(0, 0, ...sourceWrap.undoOperations);
+ sourceElements = [sourceWrap.element];
+ sourcePositions.set(sourceWrap.element.getAttribute("data-node-id"), sourceWrap.sourcePosition);
+ }
+ if (isColumnDrop && isFoldedHeading(targetElement)) {
+ const targetID = targetElement.getAttribute("data-node-id");
+ const targetWrap = await wrapInRowSB(protyle, [targetElement]);
+ doOperations.push(...targetWrap.doOperations);
+ wrapUndoOperations.splice(0, 0, ...targetWrap.undoOperations);
+ targetElement = targetWrap.element;
+ sourcePositions.forEach(position => {
+ if (position.previousID === targetID) {
+ position.previousID = targetElement.getAttribute("data-node-id");
+ }
+ });
+ }
+ if (sourceHasFoldHeading && isColumnDrop && isCopy) {
+ // 复制折叠标题时原块不移动,可直接在目标横向超级块中创建纵向分组。
+ sourceRowElement = genRowSBElement(sourceElements[0]);
+ const sourceRowData = sourceRowElement.outerHTML;
+ targetElement.insertAdjacentElement(isBottom ? "afterend" : "beforebegin", sourceRowElement);
+ doOperations.push({
+ action: "insert",
+ data: sourceRowData,
+ id: sourceRowElement.getAttribute("data-node-id"),
+ nextID: isBottom ? undefined : targetElement.getAttribute("data-node-id"),
+ previousID: isBottom ? targetElement.getAttribute("data-node-id") : undefined,
+ parentID: targetParentElement.getAttribute("data-node-id")
+ });
+ }
+ const moveToResult = await moveTo(protyle, sourceElements, sourceRowElement || targetElement, isSameEditor,
+ sourceRowElement ? "afterbegin" : (isBottom ? "afterend" : "beforebegin"), isCopy, sourcePositions);
+ if (sourceRowElement && isCopy) {
+ clearCopiedColumnWidth(sourceRowElement, moveToResult.doOperations);
+ }
doOperations.push(...moveToResult.doOperations);
- undoOperations.push(...moveToResult.undoOperations);
+ const sourceUndoOperations = [...moveToResult.undoOperations];
+ undoOperations.push(...sourceUndoOperations);
+ if (sourceRowElement) {
+ undoOperations.push({
+ action: "delete",
+ id: sourceRowElement.getAttribute("data-node-id")
+ });
+ }
+ undoOperations.push(...wrapUndoOperations);
const newSourceParentElement = moveToResult.newSourceElements;
let foldData;
const previousBlockElement = getPreviousBlockSibling(targetElement);
- if (isBottom &&
+ if (!isColumnDrop && isBottom &&
targetElement.getAttribute("data-type") === "NodeHeading" &&
targetElement.getAttribute("fold") === "1") {
foldData = setFold(protyle, targetElement, true, false, false, true);
- } else if (!isBottom &&
+ } else if (!isColumnDrop && !isBottom &&
previousBlockElement?.getAttribute("data-type") === "NodeHeading" &&
previousBlockElement.getAttribute("fold") === "1") {
foldData = setFold(protyle, previousBlockElement, true, false, false, true);
@@ -631,24 +794,11 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
});
});
}
- let hasFoldHeading = false;
- newSourceParentElement.forEach(item => {
- if (item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") {
- hasFoldHeading = true;
- const nextBlockElement = getNextBlockSibling(item);
- if (nextBlockElement && (
- nextBlockElement.getAttribute("data-type") !== "NodeHeading" ||
- nextBlockElement.getAttribute("data-subtype") > item.getAttribute("data-subtype")
- )) {
- const foldOperations = setFold(protyle, item, true, false, false, true);
- doOperations.push(...foldOperations.doOperations);
- // 不折叠,否则无法撤销 undoOperations.push(...foldOperations.undoOperations);
- }
- return true;
- }
- });
// 移入/移出超级块后刷新拖拽手柄并重新分配宽度(如 A 拖到超级块内 B 前面,需在 A、B 间补手柄)
const dragSbSet = new Set<Element>(originSbSet);
+ if (isColumnDrop) {
+ dragSbSet.add(targetParentElement);
+ }
[newSourceParentElement[0], targetElement].forEach(el => {
const sb = el?.closest('[data-type="NodeSuperBlock"]');
if (sb) {
@@ -658,7 +808,7 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
dragSbSet.forEach(sb => {
refreshSbAndPersistWidth(sb, doOperations, undoOperations);
});
- if ((newSourceParentElement.length > 1 || hasFoldHeading) &&
+ if (!sourceRowElement && newSourceParentElement.length > 1 &&
newSourceParentElement[0].parentElement.classList.contains("sb") &&
newSourceParentElement[0].parentElement.getAttribute("data-sb-layout") === "col") {
// 合并到同一个 transaction,避免新超级块 id 在第二个 transaction 中找不到
@@ -675,8 +825,8 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
}
// 跨文档移动为可逆条目:全局撤销栈按 rootID 分栈联动,撤销时经 mutatedRootIDs 判定弹确认
transaction(protyle, doOperations, undoOperations);
- if (document.contains(sourceElements[0])) {
- focusBlock(sourceElements[0]);
+ if (document.contains(focusSourceElement)) {
+ focusBlock(focusSourceElement);
} else {
focusBlock(targetElement);
}
diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts
index b356ae4e04..4d9b5c04fc 100644
--- a/app/src/protyle/wysiwyg/remove.ts
+++ b/app/src/protyle/wysiwyg/remove.ts
@@ -451,8 +451,16 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
widthChanges.forEach(change => {
const targetEl = topParentElement.querySelector(`[data-node-id="${change.id}"]`);
if (targetEl) {
- deletes.push({action: "update", id: change.id, data: targetEl.outerHTML});
- inserts.push({action: "update", id: change.id, data: change.oldHTML});
+ deletes.push({
+ action: "setAttrs",
+ id: change.id,
+ data: JSON.stringify({style: targetEl.getAttribute("style") || ""})
+ });
+ inserts.push({
+ action: "setAttrs",
+ id: change.id,
+ data: JSON.stringify({style: change.oldStyle})
+ });
}
});
}
@@ -862,8 +870,16 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
widthChanges.forEach(change => {
const targetEl = parentElement.querySelector(`[data-node-id="${change.id}"]`);
if (targetEl) {
- doOperations.push({action: "update", id: change.id, data: targetEl.outerHTML});
- undoOperations.push({action: "update", id: change.id, data: change.oldHTML});
+ doOperations.push({
+ action: "setAttrs",
+ id: change.id,
+ data: JSON.stringify({style: targetEl.getAttribute("style") || ""})
+ });
+ undoOperations.push({
+ action: "setAttrs",
+ id: change.id,
+ data: JSON.stringify({style: change.oldStyle})
+ });
}
});
}
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index df792373d2..c1a22f6641 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -77,16 +77,27 @@ const removeTopElement = (updateElement: Element, protyle: IProtyle) => {
}
};
-const syncFoldAttr = (element: Element, operation: IOperation) => {
+const syncFoldAndStyleAttrs = (element: Element, operation: IOperation) => {
const attrs = JSON.parse(operation.data);
- if (!Object.prototype.hasOwnProperty.call(attrs, "fold")) {
+ const hasFold = Object.prototype.hasOwnProperty.call(attrs, "fold");
+ const hasStyle = Object.prototype.hasOwnProperty.call(attrs, "style");
+ if (!hasFold && !hasStyle) {
return;
}
element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(item => {
- if (attrs.fold === "1") {
- item.setAttribute("fold", "1");
- } else {
- item.removeAttribute("fold");
+ if (hasFold) {
+ if (attrs.fold === "1") {
+ item.setAttribute("fold", "1");
+ } else {
+ item.removeAttribute("fold");
+ }
+ }
+ if (hasStyle) {
+ if (attrs.style) {
+ item.setAttribute("style", attrs.style);
+ } else {
+ item.removeAttribute("style");
+ }
}
});
};
@@ -394,7 +405,7 @@ const promiseTransaction = (options: {
return;
}
if (operation.action === "setAttrs") {
- syncFoldAttr(protyle.wysiwyg.element, operation);
+ syncFoldAndStyleAttrs(protyle.wysiwyg.element, operation);
const gutterFoldElement = protyle.gutter.element.querySelector('[data-type="fold"]');
if (gutterFoldElement) {
gutterFoldElement.removeAttribute("disabled");
@@ -593,7 +604,7 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd
updateElements.push(item);
});
if (operation.action === "setAttrs") {
- syncFoldAttr(protyle.wysiwyg.element, operation);
+ syncFoldAndStyleAttrs(protyle.wysiwyg.element, operation);
return;
}
if (operation.action === "unfoldHeading") {
@@ -1190,8 +1201,10 @@ export const turnsIntoOneTransaction = async (options: {
level?: TTurnIntoOneSub,
unfocus?: boolean,
getOperations?: boolean,
+ parentID?: string,
}) => {
let parentElement: Element;
+ let firstChildOldStyle: string;
const id = Lute.NewNodeID();
if (options.type === "BlocksMergeSuperBlock") {
parentElement = genSBElement(options.level, id);
@@ -1199,6 +1212,7 @@ export const turnsIntoOneTransaction = async (options: {
// https://github.com/siyuan-note/siyuan/issues/9521
const firstChild = options.selectsElement[0] as HTMLElement;
if (firstChild.style.width) {
+ firstChildOldStyle = firstChild.getAttribute("style") || "";
(parentElement as HTMLElement).style.width = firstChild.style.width;
(parentElement as HTMLElement).style.flex = firstChild.style.flex;
firstChild.style.width = "";
@@ -1243,7 +1257,7 @@ export const turnsIntoOneTransaction = async (options: {
parentElement.innerHTML = html + '<div class="protyle-attr" contenteditable="false"></div>';
}
const previousId = options.selectsElement[0].getAttribute("data-node-id");
- const parentId = getEmbedChildOperationParentID(options.selectsElement[0]) ||
+ const parentId = options.parentID || getEmbedChildOperationParentID(options.selectsElement[0]) ||
getParentBlock(options.selectsElement[0]).getAttribute("data-node-id") || options.protyle.block.parentID;
const doOperations: IOperation[] = [{
action: "insert",
@@ -1308,6 +1322,19 @@ export const turnsIntoOneTransaction = async (options: {
blockRender(options.protyle, item);
}
});
+ if (firstChildOldStyle !== undefined) {
+ const firstChild = options.selectsElement[0];
+ doOperations.push({
+ action: "setAttrs",
+ id: firstChild.getAttribute("data-node-id"),
+ data: JSON.stringify({style: firstChild.getAttribute("style") || ""})
+ });
+ undoOperations.splice(undoOperations.length - 1, 0, {
+ action: "setAttrs",
+ id: firstChild.getAttribute("data-node-id"),
+ data: JSON.stringify({style: firstChildOldStyle})
+ });
+ }
// 子块移入完成后刷新超级块拖拽手柄
if (parentElement.classList.contains("sb")) {
refreshSbs(parentElement);
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 1bcd63598f..005b159b49 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -696,9 +696,18 @@ func (tx *Transaction) doMove(operation *Operation) (ret *TxErr) {
}
func isMovingFoldHeadingIntoSelf(targetNode *ast.Node, headingChildren []*ast.Node) bool {
+ headingChildIDs := map[string]struct{}{}
for _, headingChild := range headingChildren {
- if headingChild.ID == targetNode.ID {
- // 不能将折叠标题移动到自己下方节点的前或后 https://github.com/siyuan-note/siyuan/issues/7163
+ headingChildIDs[headingChild.ID] = struct{}{}
+ }
+ visited := map[*ast.Node]struct{}{}
+ for target := targetNode; nil != target; target = target.Parent {
+ if _, ok := visited[target]; ok {
+ return true
+ }
+ visited[target] = struct{}{}
+ if _, ok := headingChildIDs[target.ID]; ok {
+ // 不能将折叠标题移动到自己下方节点的前后或内部 https://github.com/siyuan-note/siyuan/issues/7163
return true
}
}
diff --git a/kernel/model/transaction_fold_test.go b/kernel/model/transaction_fold_test.go
new file mode 100644
index 0000000000..b59467b2e5
--- /dev/null
+++ b/kernel/model/transaction_fold_test.go
@@ -0,0 +1,87 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestMovingFoldHeadingIntoDescendantContainer(t *testing.T) {
+ headingChild := &ast.Node{Type: ast.NodeSuperBlock, ID: "heading-child"}
+ container := &ast.Node{Type: ast.NodeSuperBlock, ID: "container"}
+ target := &ast.Node{Type: ast.NodeParagraph, ID: "target"}
+ headingChild.AppendChild(container)
+ container.AppendChild(target)
+
+ if !isMovingFoldHeadingIntoSelf(target, []*ast.Node{headingChild}) {
+ t.Fatal("moving a folded heading into a descendant container should be rejected")
+ }
+
+ unrelated := &ast.Node{Type: ast.NodeParagraph, ID: "unrelated"}
+ if isMovingFoldHeadingIntoSelf(unrelated, []*ast.Node{headingChild}) {
+ t.Fatal("moving a folded heading next to an unrelated node should be allowed")
+ }
+}
+
+func TestInsertOuterSuperBlockBeforeTargetWrapper(t *testing.T) {
+ const (
+ sourceID = "20260726000000-source0"
+ targetID = "20260726000000-target0"
+ outerID = "20260726000000-outer00"
+ )
+ luteEngine := util.NewLute()
+ tree := luteEngine.BlockDOM2Tree(
+ `<div data-node-id="` + sourceID + `" data-type="NodeSuperBlock" class="sb" data-sb-layout="row">` +
+ `<div class="protyle-attr" contenteditable="false"></div></div>` +
+ `<div data-node-id="` + targetID + `" data-type="NodeSuperBlock" class="sb" data-sb-layout="row">` +
+ `<div class="protyle-attr" contenteditable="false"></div></div>`)
+ tx := &Transaction{
+ luteEngine: luteEngine,
+ nodes: map[string]*ast.Node{},
+ }
+ err := tx.doInsert0(&Operation{
+ Action: "insert",
+ ID: outerID,
+ NextID: targetID,
+ Data: `<div data-node-id="` + outerID + `" data-type="NodeSuperBlock" class="sb" data-sb-layout="col">` +
+ `<div class="protyle-attr" contenteditable="false"></div></div>`,
+ }, tree)
+ if nil != err {
+ t.Fatal(err)
+ }
+
+ source := treenode.GetNodeInTree(tree, sourceID)
+ target := treenode.GetNodeInTree(tree, targetID)
+ outer := treenode.GetNodeInTree(tree, outerID)
+ if nil == source || nil == target || nil == outer {
+ t.Fatal("expected source, target, and outer super blocks")
+ }
+ nextSourceBlock := source.Next
+ for nil != nextSourceBlock && ast.NodeKramdownBlockIAL == nextSourceBlock.Type {
+ nextSourceBlock = nextSourceBlock.Next
+ }
+ if outer.Parent != target.Parent || outer.Next != target || nextSourceBlock != outer {
+ t.Fatal("outer super block should be inserted as the target wrapper's sibling")
+ }
+ if isMovingParentIntoChild(source, outer) {
+ t.Fatal("outer super block should not be inserted into the source wrapper")
+ }
+}