Skip to content

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

feat: implement recursive fold/expand functionality for blocks (#17651)

Shivam sainie3d612ade7cb
diff --git a/app/appearance/langs/en_US.json b/app/appearance/langs/en_US.json
index 5a998c21b0..a51264d22f 100644
--- a/app/appearance/langs/en_US.json
+++ b/app/appearance/langs/en_US.json
@@ -983,7 +983,7 @@
"collapse": "Collapse",
"blockEmbed": "Embed Block",
"rowTip": "Click <span class='ft__on-surface'>Open Menu</span><br>Drag <span class='ft__on-surface'>Sort</span><br>⇧Click <span class='ft__on-surface'>Update attribute</span>",
- "gutterTip": "Click/⌘/ <span class='ft__on-surface'>Open Menu</span><br>⌘Click⌥→ <span class='ft__on-surface'>Focus</span><br>⌥Click⌘↑ <span class='ft__on-surface'>Collapse/Expand</span><br>⇧Click⌥⌘A <span class='ft__on-surface'>Update attribute</span><br>Drag <span class='ft__on-surface'>Move position</span><br>⌃Drag <span class='ft__on-surface'>Repeat</span><br>⌥Drag <span class='ft__on-surface'>Generate block reference</span><br>⇧Drag <span class='ft__on-surface'>Generate embedded block</span>",
+ "gutterTip": "Click/⌘/ <span class='ft__on-surface'>Open Menu</span><br>⌘Click⌥→ <span class='ft__on-surface'>Focus</span><br>⌥Click⌘↑ <span class='ft__on-surface'>Collapse/Expand</span><br>⌃Click⌥⌘↑ <span class='ft__on-surface'>Collapse/Expand recursively</span><br>⇧Click⌥⌘A <span class='ft__on-surface'>Update attribute</span><br>Drag <span class='ft__on-surface'>Move position</span><br>⌃Drag <span class='ft__on-surface'>Repeat</span><br>⌥Drag <span class='ft__on-surface'>Generate block reference</span><br>⇧Drag <span class='ft__on-surface'>Generate embedded block</span>",
"gutterTip2": "Click <span class='ft__on-surface'>Open Menu</span><br>⇧Click <span class='ft__on-surface'>Update attribute</span>",
"linkDistance": "Link distance",
"collideStrength": "Collide strength",
@@ -1034,6 +1034,7 @@
"memo": "Memo",
"name": "Name",
"fold": "Fold/Expand",
+ "foldRecursive": "Fold/Expand recursively",
"zoom": "Zoom",
"invalid": "Invalid",
"conflict": "Conflict",
diff --git a/app/appearance/langs/zh_CHT.json b/app/appearance/langs/zh_CHT.json
index 6080b6a717..88f0992ff3 100644
--- a/app/appearance/langs/zh_CHT.json
+++ b/app/appearance/langs/zh_CHT.json
@@ -981,9 +981,10 @@
"toggleWin": "隱藏/顯示視窗",
"customSort": "自訂排序",
"collapse": "摺疊",
+ "foldRecursive": "遞歸摺疊/展開",
"blockEmbed": "嵌入塊",
"rowTip": "點擊 <span class='ft__on-surface'>打開菜單</span><br>拖拽 <span class='ft__on-surface'>排序</span><br>⇧點擊 <span class='ft__on-surface'>修改屬性</span>",
- "gutterTip": "點擊/⌘/ <span class='ft__on-surface'>打開菜單</span><br>⌘點擊⌥→ <span class='ft__on-surface'>聚焦</span><br>⌥點擊⌘↑ <span class='ft__on-surface'>折疊/展開</span><br>⇧點擊⌥⌘A <span class='ft__on-surface'>修改屬性</span><br>拖拽 <span class='ft__on-surface'>移動位置</span><br>⌃拖拽 <span class='ft__on-surface'>重複</span><br>⌥拖拽 <span class='ft__on-surface'>生成塊引用</span><br>⇧拖拽 <span class='ft__on-surface'>生成嵌入塊</span>",
+ "gutterTip": "點擊/⌘/ <span class='ft__on-surface'>打開菜單</span><br>⌘點擊⌥→ <span class='ft__on-surface'>聚焦</span><br>⌥點擊⌘↑ <span class='ft__on-surface'>折疊/展開</span><br>⌃點擊⌥⌘↑ <span class='ft__on-surface'>遞歸折疊/展開</span><br>⇧點擊⌥⌘A <span class='ft__on-surface'>修改屬性</span><br>拖拽 <span class='ft__on-surface'>移動位置</span><br>⌃拖拽 <span class='ft__on-surface'>重複</span><br>⌥拖拽 <span class='ft__on-surface'>生成塊引用</span><br>⇧拖拽 <span class='ft__on-surface'>生成嵌入塊</span>",
"gutterTip2": "點擊 <span class='ft__on-surface'>打開功能表</span><br>⇧點擊 <span class='ft__on-surface'>修改屬性</span>",
"linkDistance": "連結距離",
"collideStrength": "斥力強度",
diff --git a/app/appearance/langs/zh_CN.json b/app/appearance/langs/zh_CN.json
index b4c7dc1091..bab8c67aa0 100644
--- a/app/appearance/langs/zh_CN.json
+++ b/app/appearance/langs/zh_CN.json
@@ -983,7 +983,7 @@
"collapse": "折叠",
"blockEmbed": "嵌入块",
"rowTip": "点击 <span class='ft__on-surface'>打开菜单</span><br>拖拽 <span class='ft__on-surface'>排序</span><br>⇧点击 <span class='ft__on-surface'>修改属性</span>",
- "gutterTip": "点击/⌘/ <span class='ft__on-surface'>打开菜单</span><br>⌘点击⌥→ <span class='ft__on-surface'>聚焦</span><br>⌥点击⌘↑ <span class='ft__on-surface'>折叠/展开</span><br>⇧点击⌥⌘A <span class='ft__on-surface'>修改属性</span><br>拖拽 <span class='ft__on-surface'>移动位置</span><br>⌃拖拽 <span class='ft__on-surface'>重复</span><br>⌥拖拽 <span class='ft__on-surface'>生成块引用</span><br>⇧拖拽 <span class='ft__on-surface'>生成嵌入块</span>",
+ "gutterTip": "点击/⌘/ <span class='ft__on-surface'>打开菜单</span><br>⌘点击⌥→ <span class='ft__on-surface'>聚焦</span><br>⌥点击⌘↑ <span class='ft__on-surface'>折叠/展开</span><br>⌃点击⌥⌘↑ <span class='ft__on-surface'>递归折叠/展开</span><br>⇧点击⌥⌘A <span class='ft__on-surface'>修改属性</span><br>拖拽 <span class='ft__on-surface'>移动位置</span><br>⌃拖拽 <span class='ft__on-surface'>重复</span><br>⌥拖拽 <span class='ft__on-surface'>生成块引用</span><br>⇧拖拽 <span class='ft__on-surface'>生成嵌入块</span>",
"gutterTip2": "点击 <span class='ft__on-surface'>打开菜单</span><br>⇧点击 <span class='ft__on-surface'>修改属性</span>",
"linkDistance": "链接距离",
"collideStrength": "斥力强度",
@@ -1034,6 +1034,7 @@
"memo": "备注",
"name": "命名",
"fold": "折叠/展开",
+ "foldRecursive": "递归折叠/展开",
"zoom": "缩放",
"invalid": "无效",
"conflict": "冲突",
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts
index c026c3a3f9..13f3319dd8 100644
--- a/app/src/boot/globalEvent/keydown.ts
+++ b/app/src/boot/globalEvent/keydown.ts
@@ -38,8 +38,9 @@ import {unicode2Emoji} from "../../emoji";
import {deleteFiles} from "../../editor/deleteFile";
import {escapeHtml} from "../../util/escape";
import {syncGuide} from "../../sync/syncGuide";
-import {duplicateBlock, getStartEndElement, goEnd, goHome} from "../../protyle/wysiwyg/commonHotkey";
-import {getNextFileLi, getPreviousFileLi} from "../../protyle/wysiwyg/getBlock";
+import {duplicateBlock, foldRecursiveHotkey, getStartEndElement, goEnd, goHome} from "../../protyle/wysiwyg/commonHotkey";
+import {getNextFileLi, getPreviousFileLi, getTopAloneElement} from "../../protyle/wysiwyg/getBlock";
+import {foldBlocksRecursively} from "../../protyle/util/blockFold";
import {Backlink} from "../../layout/dock/Backlink";
/// #if !BROWSER
import {setZoom} from "../../layout/topBar";
@@ -352,6 +353,10 @@ const editKeydown = (app: App, event: KeyboardEvent) => {
return true;
}
+ if (!isFileFocus && !event.repeat && !protyle.disabled && foldRecursiveHotkey(protyle, event, hasClosestBlock(range.startContainer))) {
+ return true;
+ }
+
const target = event.target as HTMLElement;
if (target.tagName !== "TABLE" && ["INPUT", "TEXTAREA"].includes(target.tagName)) {
return false;
diff --git a/app/src/constants.ts b/app/src/constants.ts
index e409ac8ddd..b0b3d44df6 100644
--- a/app/src/constants.ts
+++ b/app/src/constants.ts
@@ -497,6 +497,7 @@ export abstract class Constants {
expandUp: {default: "⌥⇧↑", custom: "⌥⇧↑"},
expand: {default: "⌘↓", custom: "⌘↓"},
collapse: {default: "⌘↑", custom: "⌘↑"},
+ foldRecursive: {default: "⌥⌘↑", custom: "⌥⌘↑"},
insertBottom: {default: "⌥⌘.", custom: "⌥⌘."},
refTab: {default: "⇧⌘.", custom: "⇧⌘."},
openBy: {default: "⌥,", custom: "⌥,"},
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts
index d7feb56845..befcb81c09 100644
--- a/app/src/menus/protyle.ts
+++ b/app/src/menus/protyle.ts
@@ -2567,88 +2567,4 @@ export const setFoldById = (data: {
});
};
-export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolean,
- isRemove?: boolean, addLoading = true, getOperations = false) => {
- if (nodeElement.getAttribute("data-type") === "NodeListItem" && nodeElement.childElementCount < 4 &&
- // 该情况需要强制展开 https://github.com/siyuan-note/siyuan/issues/12327
- !isOpen) {
- // 没有子列表或多个块的列表项不进行折叠
- return {fold: -1};
- }
- if (nodeElement.getAttribute("data-type") === "NodeThematicBreak") {
- return {fold: -1};
- }
- const hasFold = nodeElement.getAttribute("fold") === "1";
- if (hasFold) {
- if (typeof isOpen === "boolean" && !isOpen) {
- return {fold: -1};
- }
- nodeElement.removeAttribute("fold");
- // https://github.com/siyuan-note/siyuan/issues/4411
- nodeElement.querySelectorAll(".protyle-linenumber__rows").forEach((item: HTMLElement) => {
- lineNumberRender(item.parentElement);
- });
- } else {
- if (typeof isOpen === "boolean" && isOpen) {
- return {fold: -1};
- }
- nodeElement.setAttribute("fold", "1");
- // 光标在子列表中,再次 focus 段尾的时候不会变 https://ld246.com/article/1647099132461
- if (getSelection().rangeCount > 0) {
- const range = getSelection().getRangeAt(0);
- const blockElement = hasClosestBlock(range.startContainer);
- if (blockElement && blockElement.getBoundingClientRect().width === 0) {
- // https://github.com/siyuan-note/siyuan/issues/5833
- focusBlock(nodeElement, undefined, false);
- }
- }
- clearSelect(["img", "av"], nodeElement);
- scrollCenter(protyle, 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>');
- }
- doOperations.push({
- action: "unfoldHeading",
- id,
- data: isRemove ? "remove" : undefined,
- });
- undoOperations.push({
- action: "foldHeading",
- id
- });
- } else {
- doOperations.push({
- action: "foldHeading",
- id
- });
- undoOperations.push({
- action: "unfoldHeading",
- id
- });
- removeFoldHeading(nodeElement);
- }
- } else {
- 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 {fold: !hasFold ? 1 : 0, undoOperations, doOperations};
-};
+export {foldBlocksRecursively, setFold} from "../protyle/util/blockFold";
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index e4372c00ca..54e6f61af9 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -7,7 +7,8 @@ import {
isInEmbedBlock
} from "../util/hasClosest";
import {getIconByType} from "../../editor/getIcon";
-import {enterBack, iframeMenu, setFold, tableMenu, videoMenu, zoomOut} from "../../menus/protyle";
+import {enterBack, iframeMenu, tableMenu, videoMenu, zoomOut} from "../../menus/protyle";
+import {foldBlocksRecursively, setFold} from "../util/blockFold";
import {MenuItem} from "../../menus/Menu";
import {copySubMenu, openAttr, openFileAttr, openWechatNotify} from "../../menus/commonMenuItem";
import {
@@ -75,10 +76,12 @@ export class Gutter {
if (isMac()) {
this.gutterTip = window.siyuan.languages.gutterTip.replace("⌥→", updateHotkeyAfterTip(window.siyuan.config.keymap.general.enter.custom, "/"))
.replace("⌘↑", updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.collapse.custom, "/"))
+ .replace("⌥⌘↑", updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.foldRecursive.custom, "/"))
.replace("⌥⌘A", updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.attr.custom, "/"));
} else {
this.gutterTip = window.siyuan.languages.gutterTip.replace("⌥→", updateHotkeyAfterTip(window.siyuan.config.keymap.general.enter.custom, "/"))
.replace("⌘↑", updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.collapse.custom, "/"))
+ .replace("⌥⌘↑", updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.foldRecursive.custom, "/"))
.replace("⌥⌘A", updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.attr.custom, "/"))
.replace(/⌘/g, "Ctrl+").replace(/⌥/g, "Alt+").replace(/⇧/g, "Shift+").replace(/⌃/g, "Ctrl+");
}
@@ -417,6 +420,17 @@ export class Gutter {
}
}
foldElement.classList.remove("protyle-wysiwyg--hl");
+ } else if (event.ctrlKey) {
+ let foldElement: Element;
+ Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${id}"]`)).find(item => {
+ if (!isInEmbedBlock(item) && this.isMatchNode(item)) {
+ foldElement = item;
+ return true;
+ }
+ });
+ if (foldElement) {
+ foldBlocksRecursively(protyle, [foldElement]);
+ }
} else if (event.shiftKey && !protyle.disabled) {
// 不使用 window.siyuan.shiftIsPressed ,否则窗口未激活时按 Shift 点击块标无法打开属性面板 https://github.com/siyuan-note/siyuan/issues/15075
openAttr(protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`), "bookmark", protyle);
@@ -2044,6 +2058,18 @@ export class Gutter {
focusBlock(nodeElement);
}
}).element);
+ if (type === "NodeHeading" || ((type === "NodeListItem" || type === "NodeBlockquote" || type === "NodeSuperBlock") && nodeElement.childElementCount > 3)) {
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "foldRecursive",
+ icon: "iconFoldUnFold",
+ label: window.siyuan.languages.foldRecursive || "Fold/Expand recursively",
+ accelerator: window.siyuan.config.keymap.editor.general.foldRecursive?.custom,
+ click() {
+ foldBlocksRecursively(protyle, [nodeElement]);
+ focusBlock(nodeElement);
+ }
+ }).element);
+ }
if (!protyle.disabled) {
window.siyuan.menus.menu.append(new MenuItem({
id: "attr",
diff --git a/app/src/protyle/util/blockFold.ts b/app/src/protyle/util/blockFold.ts
new file mode 100644
index 0000000000..cc0ce4e050
--- /dev/null
+++ b/app/src/protyle/util/blockFold.ts
@@ -0,0 +1,192 @@
+import {lineNumberRender} from "../render/highlightRender";
+import {transaction} from "../wysiwyg/transaction";
+import {preventScroll} from "../scroll/preventScroll";
+import {hasClosestBlock} from "./hasClosest";
+import {focusBlock} from "./selection";
+import {scrollCenter} from "../../util/highlightById";
+import {clearSelect} from "./clear";
+import {removeFoldHeading} from "./heading";
+
+export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolean,
+ isRemove?: boolean, addLoading = true, getOperations = false) => {
+ if (nodeElement.getAttribute("data-type") === "NodeListItem" && nodeElement.childElementCount < 4 &&
+ // 该情况需要强制展开 https://github.com/siyuan-note/siyuan/issues/12327
+ !isOpen) {
+ // 没有子列表或多个块的列表项不进行折叠
+ return {fold: -1};
+ }
+ if (nodeElement.getAttribute("data-type") === "NodeThematicBreak") {
+ return {fold: -1};
+ }
+ const hasFold = nodeElement.getAttribute("fold") === "1";
+ if (hasFold) {
+ if (typeof isOpen === "boolean" && !isOpen) {
+ return {fold: -1};
+ }
+ nodeElement.removeAttribute("fold");
+ if (!getOperations) {
+ // https://github.com/siyuan-note/siyuan/issues/4411
+ nodeElement.querySelectorAll(".protyle-linenumber__rows").forEach((item: HTMLElement) => {
+ lineNumberRender(item.parentElement);
+ });
+ }
+ } else {
+ if (typeof isOpen === "boolean" && isOpen) {
+ return {fold: -1};
+ }
+ nodeElement.setAttribute("fold", "1");
+ if (!getOperations) {
+ // 光标在子列表中,再次 focus 段尾的时候不会变 https://ld246.com/article/1647099132461
+ if (getSelection().rangeCount > 0) {
+ const range = getSelection().getRangeAt(0);
+ const blockElement = hasClosestBlock(range.startContainer);
+ if (blockElement && blockElement.getBoundingClientRect().width === 0) {
+ // https://github.com/siyuan-note/siyuan/issues/5833
+ focusBlock(nodeElement, undefined, false);
+ }
+ }
+ clearSelect(["img", "av"], nodeElement);
+ scrollCenter(protyle, nodeElement);
+ }
+ }
+ const id = nodeElement.getAttribute("data-node-id");
+ const doOperations: IOperation[] = [];
+ const undoOperations: IOperation[] = [];
+ if (nodeElement.getAttribute("data-type") === "NodeHeading") {
+ if (hasFold) {
+ if (addLoading && !getOperations) {
+ nodeElement.insertAdjacentHTML("beforeend", '<div spin="1" style="text-align: center"><img width="24px" height="24px" src="/stage/loading-pure.svg"></div>');
+ }
+ doOperations.push({
+ action: "unfoldHeading",
+ id,
+ data: isRemove ? "remove" : undefined,
+ });
+ undoOperations.push({
+ action: "foldHeading",
+ id
+ });
+ } else {
+ doOperations.push({
+ action: "foldHeading",
+ id
+ });
+ undoOperations.push({
+ action: "unfoldHeading",
+ id
+ });
+ removeFoldHeading(nodeElement);
+ }
+ } else {
+ 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 {fold: !hasFold ? 1 : 0, undoOperations, doOperations};
+};
+
+export const foldBlocksRecursively = (protyle: IProtyle, nodeElements: Element[]) => {
+ const doOperations: IOperation[] = [];
+ const undoOperations: IOperation[] = [];
+
+ const result: Set<Element> = new Set();
+ const isFoldable = (el: Element) => {
+ const type = el.getAttribute("data-type");
+ return type === "NodeHeading" ||
+ ((type === "NodeListItem" || type === "NodeBlockquote" || type === "NodeSuperBlock") && el.childElementCount > 3);
+ };
+
+ nodeElements.forEach(element => {
+ if (isFoldable(element)) {
+ result.add(element);
+ }
+ element.querySelectorAll("[data-type='NodeHeading'], [data-type='NodeListItem'], [data-type='NodeBlockquote'], [data-type='NodeSuperBlock']").forEach(child => {
+ if (isFoldable(child)) {
+ // Skip headings inside list items to avoid "double dot" and gutter icon conflicts
+ if (child.getAttribute("data-type") === "NodeHeading" && child.parentElement?.getAttribute("data-type") === "NodeListItem") {
+ return;
+ }
+ result.add(child);
+ }
+ });
+ const type = element.getAttribute("data-type");
+ if (type === "NodeHeading") {
+ const nodeH = parseInt(element.getAttribute("data-subtype").substr(1));
+ let nextElement = element.nextElementSibling;
+ while (nextElement) {
+ const currentH = parseInt(nextElement.getAttribute("data-subtype")?.substr(1));
+ if (!nextElement.classList.contains("protyle-attr") && (isNaN(currentH) || currentH > nodeH)) {
+ if (isFoldable(nextElement)) {
+ result.add(nextElement);
+ }
+ nextElement.querySelectorAll("[data-type='NodeHeading'], [data-type='NodeListItem'], [data-type='NodeBlockquote'], [data-type='NodeSuperBlock']").forEach(child => {
+ if (isFoldable(child)) {
+ // Skip headings inside list items to avoid "double dot" and gutter icon conflicts
+ if (child.getAttribute("data-type") === "NodeHeading" && child.parentElement?.getAttribute("data-type") === "NodeListItem") {
+ return;
+ }
+ result.add(child);
+ }
+ });
+ nextElement = nextElement.nextElementSibling;
+ } else {
+ break;
+ }
+ }
+ }
+ });
+
+ const elementsToFold = Array.from(result);
+ if (elementsToFold.length === 0) {
+ return;
+ }
+
+ // Determine target state: if any block is unfolded, we fold all. Otherwise we expand all.
+ const isFoldAll = elementsToFold.some(element => element.getAttribute("fold") !== "1");
+ elementsToFold.sort((a, b) => {
+ const position = a.compareDocumentPosition(b);
+ if (position & Node.DOCUMENT_POSITION_FOLLOWING) {
+ return isFoldAll ? 1 : -1;
+ } else if (position & Node.DOCUMENT_POSITION_PRECEDING) {
+ return isFoldAll ? -1 : 1;
+ }
+ return 0;
+ });
+
+ if (elementsToFold.length === 0) {
+ return;
+ }
+
+ elementsToFold.forEach(element => {
+ const hasFold = element.getAttribute("fold") === "1";
+ if (isFoldAll && hasFold) {
+ return;
+ }
+ if (!isFoldAll && !hasFold) {
+ return;
+ }
+ const ops = setFold(protyle, element, !isFoldAll, false, false, true);
+ if (ops.doOperations && ops.doOperations.length > 0) {
+ doOperations.push(...ops.doOperations);
+ undoOperations.push(...ops.undoOperations);
+ }
+ });
+
+ if (doOperations.length > 0) {
+ transaction(protyle, doOperations, undoOperations);
+ preventScroll(protyle);
+ scrollCenter(protyle, elementsToFold[0]);
+ }
+};
diff --git a/app/src/protyle/wysiwyg/commonHotkey.ts b/app/src/protyle/wysiwyg/commonHotkey.ts
index 549317b80b..d2a92c1627 100644
--- a/app/src/protyle/wysiwyg/commonHotkey.ts
+++ b/app/src/protyle/wysiwyg/commonHotkey.ts
@@ -15,7 +15,8 @@ import {processClonePHElement} from "../render/util";
import {copyTextByType} from "../toolbar/util";
import {hasClosestByTag, hasTopClosestByClassName} from "../util/hasClosest";
import {removeEmbed} from "./removeEmbed";
-import {clearBlockElement} from "../util/clear";
+import {clearBlockElement, clearSelect} from "../util/clear";
+import {foldBlocksRecursively} from "../util/blockFold";
export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElement?: HTMLElement) => {
if (matchHotKey(window.siyuan.config.keymap.editor.general.netImg2LocalAsset.custom, event)) {
@@ -95,6 +96,10 @@ export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElemen
return true;
}
+ if (foldRecursiveHotkey(protyle, event, nodeElement)) {
+ return true;
+ }
+
if (matchHotKey(window.siyuan.config.keymap.editor.general.copyBlockEmbed.custom, event)) {
if (nodeElement) {
const selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
@@ -461,3 +466,30 @@ export const alignImgLeft = (protyle: IProtyle, nodeElement: Element, assetEleme
});
updateTransaction(protyle, id, nodeElement.outerHTML, html);
};
+
+export const foldRecursiveHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElement: HTMLElement) => {
+ if (window.siyuan.config.keymap.editor.general.foldRecursive && matchHotKey(window.siyuan.config.keymap.editor.general.foldRecursive.custom, event) && !event.repeat) {
+ const selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
+ if (selectElements.length > 0) {
+ foldBlocksRecursively(protyle, selectElements);
+ } else if (nodeElement) {
+ const nodeType = nodeElement.getAttribute("data-type");
+ if (nodeElement.parentElement.getAttribute("data-type") === "NodeListItem") {
+ if (nodeElement.parentElement.childElementCount > 3) {
+ foldBlocksRecursively(protyle, [nodeElement.parentElement]);
+ } else {
+ foldBlocksRecursively(protyle, [nodeElement]);
+ }
+ } else if (nodeType === "NodeHeading") {
+ foldBlocksRecursively(protyle, [nodeElement]);
+ } else {
+ foldBlocksRecursively(protyle, [getTopAloneElement(nodeElement)]);
+ }
+ }
+ event.stopPropagation();
+ event.preventDefault();
+ return true;
+ }
+ return false;
+};
+
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index e33a56c463..07872a9d7a 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -53,8 +53,9 @@ import {isLocalPath} from "../../util/pathName";
/// #if !MOBILE
import {openBy, openFileById} from "../../editor/util";
/// #endif
-import {alignImgCenter, alignImgLeft, commonHotkey, downSelect, getStartEndElement, upSelect} from "./commonHotkey";
-import {fileAnnotationRefMenu, inlineMathMenu, linkMenu, refMenu, setFold, tagMenu} from "../../menus/protyle";
+import {alignImgCenter, alignImgLeft, commonHotkey, downSelect, foldRecursiveHotkey, getStartEndElement, upSelect} from "./commonHotkey";
+import {fileAnnotationRefMenu, inlineMathMenu, linkMenu, refMenu, tagMenu} from "../../menus/protyle";
+import {foldBlocksRecursively, setFold} from "../util/blockFold";
import {openAttr} from "../../menus/commonMenuItem";
import {Constants} from "../../constants";
import {fetchPost} from "../../util/fetch";
@@ -330,6 +331,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
return false;
}
+ if (foldRecursiveHotkey(protyle, event, nodeElement)) {
+ return true;
+ }
+
if (matchHotKey(window.siyuan.config.keymap.editor.general.expand.custom, event) && !event.repeat) {
const selectElements = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
if (selectElements.length > 0) {
diff --git a/app/src/types/config.d.ts b/app/src/types/config.d.ts
index 60dee80649..1d16bc702e 100644
--- a/app/src/types/config.d.ts
+++ b/app/src/types/config.d.ts
@@ -950,6 +950,7 @@ declare namespace Config {
attr: IKey;
backlinks: IKey;
collapse: IKey;
+ foldRecursive: IKey;
copyBlockEmbed: IKey;
copyBlockRef: IKey;
copyHPath: IKey;