From e922c402c8a8ca2c88c25222e8c77c3616f344c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Yingyi=20/=20=E9=A2=96=E9=80=B8?=
<49649786+Zuoqiu-Yingyi@users.noreply.github.com>
Date: Sun, 24 Mar 2024 22:16:49 +0800
Subject: [PATCH] :art: Improve block collapse and expansion operations
(#10717)
---
app/src/menus/protyle.ts | 14 ++++++--------
app/src/protyle/gutter/index.ts | 12 ++++++------
kernel/api/block_op.go | 2 +-
3 files changed, 13 insertions(+), 15 deletions(-)
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts
index 33006cb59..1654d9121 100644
--- a/app/src/menus/protyle.ts
+++ b/app/src/menus/protyle.ts
@@ -1870,9 +1870,8 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
if (nodeElement.getAttribute("data-type") === "NodeThematicBreak") {
return -1;
}
- // 0 正常;1 折叠
- let fold = "0";
- if (nodeElement.getAttribute("fold") === "1") {
+ let hasFold = nodeElement.getAttribute("fold") === "1";
+ if (hasFold) {
if (typeof isOpen === "boolean" && !isOpen) {
return -1;
}
@@ -1885,7 +1884,6 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
if (typeof isOpen === "boolean" && isOpen) {
return -1;
}
- fold = "1";
nodeElement.setAttribute("fold", "1");
// 光标在子列表中,再次 focus 段尾的时候不会变 https://ld246.com/article/1647099132461
if (getSelection().rangeCount > 0) {
@@ -1909,7 +1907,7 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
}
const id = nodeElement.getAttribute("data-node-id");
if (nodeElement.getAttribute("data-type") === "NodeHeading") {
- if (fold === "0") {
+ if (hasFold) {
nodeElement.insertAdjacentHTML("beforeend", '

');
transaction(protyle, [{
action: "unfoldHeading",
@@ -1933,14 +1931,14 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
transaction(protyle, [{
action: "setAttrs",
id,
- data: JSON.stringify({fold})
+ data: JSON.stringify({fold: hasFold ? "" : "1"})
}], [{
action: "setAttrs",
id,
- data: JSON.stringify({fold: fold === "0" ? "1" : "0"})
+ data: JSON.stringify({fold: hasFold ? "1" : ""})
}]);
}
// 折叠后,防止滚动条滚动后调用 get 请求 https://github.com/siyuan-note/siyuan/issues/2248
preventScroll(protyle);
- return fold;
+ return !hasFold ? 1 : 0;
};
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index f97b4d478..83a2f96f7 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -178,12 +178,12 @@ export class Gutter {
doOperations.push({
action: "setAttrs",
id: listId,
- data: JSON.stringify({fold: hasFold ? "0" : "1"})
+ data: JSON.stringify({fold: hasFold ? "" : "1"})
});
undoOperations.push({
action: "setAttrs",
id: listId,
- data: JSON.stringify({fold: hasFold ? "1" : "0"})
+ data: JSON.stringify({fold: hasFold ? "1" : ""})
});
}
});
@@ -193,9 +193,9 @@ export class Gutter {
buttonElement.removeAttribute("disabled");
} else {
const foldStatus = setFold(protyle, foldElement);
- if (foldStatus === "1") {
+ if (foldStatus === 1) {
(buttonElement.firstElementChild as HTMLElement).style.transform = "";
- } else if (foldStatus === "0") {
+ } else if (foldStatus === 0) {
(buttonElement.firstElementChild as HTMLElement).style.transform = "rotate(90deg)";
}
}
@@ -297,12 +297,12 @@ export class Gutter {
doOperations.push({
action: "setAttrs",
id: listId,
- data: JSON.stringify({fold: hasFold ? "0" : "1"})
+ data: JSON.stringify({fold: hasFold ? "" : "1"})
});
undoOperations.push({
action: "setAttrs",
id: listId,
- data: JSON.stringify({fold: hasFold ? "1" : "0"})
+ data: JSON.stringify({fold: hasFold ? "1" : ""})
});
}
});
diff --git a/kernel/api/block_op.go b/kernel/api/block_op.go
index fa7515b4c..0839d2129 100644
--- a/kernel/api/block_op.go
+++ b/kernel/api/block_op.go
@@ -125,7 +125,7 @@ func unfoldBlock(c *gin.Context) {
},
}
} else {
- data, _ := gulu.JSON.MarshalJSON(map[string]interface{}{"unfold": "1"})
+ data, _ := gulu.JSON.MarshalJSON(map[string]interface{}{"fold": ""})
transactions = []*model.Transaction{
{
DoOperations: []*model.Operation{