Vanessa 2025-09-16 00:44:03 +08:00
parent a8b5c513ae
commit 4014d1ecdc

View file

@ -169,7 +169,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
let topSourceElement; let topSourceElement;
const doOperations: IOperation[] = []; const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = []; const undoOperations: IOperation[] = [];
const copyFoldHeadingIds: string[] = []; const copyFoldHeadingIds: { newId: string, oldId: string }[] = [];
const targetId = targetElement.getAttribute("data-node-id"); const targetId = targetElement.getAttribute("data-node-id");
let tempTargetElement = targetElement; let tempTargetElement = targetElement;
let ignoreInsert = ""; let ignoreInsert = "";
@ -207,16 +207,19 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
topSourceElement = targetElement; topSourceElement = targetElement;
} }
} }
const copyNewId = Lute.NewNodeID();
if (isCopy && item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") { if (isCopy && item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") {
copyFoldHeadingIds.push(id); copyFoldHeadingIds.push({
newId: copyNewId,
oldId: id
});
} }
let copyId;
let copyElement; let copyElement;
if (isCopy) { if (isCopy) {
copyId = Lute.NewNodeID();
undoOperations.push({ undoOperations.push({
action: "delete", action: "delete",
id: copyId, id: copyNewId,
}); });
} else { } else {
undoOperations.push({ undoOperations.push({
@ -239,7 +242,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
if (isCopy) { if (isCopy) {
item.removeAttribute("data-remove"); item.removeAttribute("data-remove");
copyElement = item.cloneNode(true) as HTMLElement; copyElement = item.cloneNode(true) as HTMLElement;
copyElement.setAttribute("data-node-id", copyId); copyElement.setAttribute("data-node-id", copyNewId);
copyElement.querySelectorAll("[data-node-id]").forEach((e) => { copyElement.querySelectorAll("[data-node-id]").forEach((e) => {
const newId = Lute.NewNodeID(); const newId = Lute.NewNodeID();
e.setAttribute("data-node-id", newId); e.setAttribute("data-node-id", newId);
@ -253,7 +256,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
context: { context: {
ignoreProcess: (!needInset).toString(), ignoreProcess: (!needInset).toString(),
}, },
id: copyId, id: copyNewId,
data: copyElement.outerHTML, data: copyElement.outerHTML,
previousID: position === "afterend" ? targetId : (!needInset ? targetPreviousId : copyElement.previousElementSibling?.getAttribute("data-node-id")), // 不能使用常量,移动后会被修改 previousID: position === "afterend" ? targetId : (!needInset ? targetPreviousId : copyElement.previousElementSibling?.getAttribute("data-node-id")), // 不能使用常量,移动后会被修改
parentID: copyElement.parentElement?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID, parentID: copyElement.parentElement?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID,
@ -287,8 +290,9 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
undoOperations.reverse(); undoOperations.reverse();
for (let j = 0; j < copyFoldHeadingIds.length; j++) { for (let j = 0; j < copyFoldHeadingIds.length; j++) {
const childrenItem = copyFoldHeadingIds[j]; const childrenItem = copyFoldHeadingIds[j];
const responseTransaction = await fetchSyncPost("/api/block/getHeadingInsertTransaction", {id: childrenItem}); const responseTransaction = await fetchSyncPost("/api/block/getHeadingInsertTransaction", {id: childrenItem.oldId});
responseTransaction.data.doOperations.splice(0, 1); responseTransaction.data.doOperations.splice(0, 1);
responseTransaction.data.doOperations[0].previousID = childrenItem.newId;
responseTransaction.data.undoOperations.splice(0, 1); responseTransaction.data.undoOperations.splice(0, 1);
doOperations.push(...responseTransaction.data.doOperations); doOperations.push(...responseTransaction.data.doOperations);
undoOperations.push(...responseTransaction.data.undoOperations); undoOperations.push(...responseTransaction.data.undoOperations);
@ -417,7 +421,7 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
id: newSourceId, id: newSourceId,
}); });
} else { } else {
const foldHeadingIds: { id: string, parentID: string }[] = []; const copyFoldHeadingIds: { newId: string, oldId: string }[] = [];
let afterPreviousID; let afterPreviousID;
sourceElements.reverse().forEach((item, index) => { sourceElements.reverse().forEach((item, index) => {
const id = item.getAttribute("data-node-id"); const id = item.getAttribute("data-node-id");
@ -434,8 +438,7 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
} }
if (item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") { if (item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") {
if (isCopy) { if (isCopy) {
item.removeAttribute("fold"); copyFoldHeadingIds.push({oldId: id, newId: copyId});
foldHeadingIds.push({id, parentID});
} }
hasFoldHeading = true; hasFoldHeading = true;
} }
@ -484,29 +487,17 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
} }
}); });
undoOperations.reverse(); undoOperations.reverse();
for (let j = 0; j < foldHeadingIds.length; j++) { for (let j = 0; j < copyFoldHeadingIds.length; j++) {
const childrenItem = foldHeadingIds[j]; const childrenItem = copyFoldHeadingIds[j];
const headingIds = await fetchSyncPost("/api/block/getHeadingChildrenIDs", {id: childrenItem.id}); const responseTransaction = await fetchSyncPost("/api/block/getHeadingInsertTransaction", {id: childrenItem.oldId});
headingIds.data.reverse().forEach((headingId: string) => { responseTransaction.data.doOperations.splice(0, 1);
undoOperations.push({ responseTransaction.data.doOperations[0].previousID = childrenItem.newId;
action: "move", responseTransaction.data.undoOperations.splice(0, 1);
id: headingId, doOperations.push(...responseTransaction.data.doOperations);
previousID: childrenItem.id, undoOperations.push(...responseTransaction.data.undoOperations);
parentID: childrenItem.parentID,
});
});
if (j === 0) { if (j === 0) {
afterPreviousID = headingIds.data[0]; afterPreviousID = copyFoldHeadingIds[0].newId;
} }
undoOperations.push({
action: "foldHeading",
id: childrenItem.id,
data: "remove"
});
doOperations.push({
action: "unfoldHeading",
id: childrenItem.id,
});
} }
if (isBottom) { if (isBottom) {
sbElement.insertAdjacentElement("afterbegin", targetElement); sbElement.insertAdjacentElement("afterbegin", targetElement);
@ -645,7 +636,7 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
level: "row" level: "row"
}); });
} }
if ((sourceElements.length > 1 || hasFoldHeading)) { if (sourceElements.length > 1 || hasFoldHeading) {
turnsIntoOneTransaction({ turnsIntoOneTransaction({
protyle, protyle,
selectsElement: sourceElements.reverse(), selectsElement: sourceElements.reverse(),