This commit is contained in:
Vanessa 2025-10-04 15:34:00 +08:00
parent db31c47760
commit 2b0e8feee2
3 changed files with 43 additions and 80 deletions

View file

@ -2433,18 +2433,19 @@ export const tableMenu = (protyle: IProtyle, nodeElement: Element, cellElement:
return {menus, removeMenus, insertMenus, otherMenus, other2Menus};
};
export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolean, isRemove?: boolean, addLoading = true) => {
export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolean,
isRemove?: boolean, addLoading = true, getOperations = false) => {
if (nodeElement.getAttribute("data-type") === "NodeListItem" && nodeElement.childElementCount < 4) {
// 没有子列表或多个块的列表项不进行折叠
return -1;
return {fold: -1};
}
if (nodeElement.getAttribute("data-type") === "NodeThematicBreak") {
return -1;
return {fold: -1};
}
const hasFold = nodeElement.getAttribute("fold") === "1";
if (hasFold) {
if (typeof isOpen === "boolean" && !isOpen) {
return -1;
return {fold: -1};
}
nodeElement.removeAttribute("fold");
// https://github.com/siyuan-note/siyuan/issues/4411
@ -2453,7 +2454,7 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
});
} else {
if (typeof isOpen === "boolean" && isOpen) {
return -1;
return {fold: -1};
}
nodeElement.setAttribute("fold", "1");
// 光标在子列表中,再次 focus 段尾的时候不会变 https://ld246.com/article/1647099132461
@ -2468,41 +2469,49 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
clearSelect(["img", "av"], nodeElement);
}
const id = nodeElement.getAttribute("data-node-id");
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
if (nodeElement.getAttribute("data-type") === "NodeHeading") {
if (hasFold) {
if (addLoading) {
nodeElement.insertAdjacentHTML("beforeend", '<div spin="1" style="text-align: center"><img width="24px" height="24px" src="/stage/loading-pure.svg"></div>');
}
transaction(protyle, [{
doOperations.push({
action: "unfoldHeading",
id,
data: isRemove ? "remove" : undefined,
}], [{
});
undoOperations.push({
action: "foldHeading",
id
}]);
});
} else {
transaction(protyle, [{
doOperations.push({
action: "foldHeading",
id
}], [{
});
undoOperations.push({
action: "unfoldHeading",
id
}]);
});
removeFoldHeading(nodeElement);
}
} else {
transaction(protyle, [{
doOperations.push({
action: "setAttrs",
id,
data: JSON.stringify({fold: hasFold ? "" : "1"})
}], [{
});
undoOperations.push({
action: "setAttrs",
id,
data: JSON.stringify({fold: hasFold ? "1" : ""})
}]);
});
}
if (!getOperations) {
transaction(protyle, doOperations, undoOperations);
}
// 折叠后,防止滚动条滚动后调用 get 请求 https://github.com/siyuan-note/siyuan/issues/2248
preventScroll(protyle);
return !hasFold ? 1 : 0;
return {fold: !hasFold ? 1 : 0, undoOperations, doOperations};
};