Skip to content

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

:bug: Restore block gutters after heading fold transactions https://github.com/siyuan-note/siyuan/issues/18706

Daniel7cb8378c1943
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 1d95ffeed4..ad8092bf5b 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -37,7 +37,7 @@ import {
import {removeBlock} from "../wysiwyg/remove";
import {focusBlock, focusByRange, getBlockElementsByRange, getEditorRange, selectBlocksByRange} from "../util/selection";
import {hideElements} from "../ui/hideElements";
-import {shouldHideGutterAfterFold} from "../ui/gutterVisibility";
+import {markGutterForFoldRestore} from "../ui/gutterVisibility";
import {highlightRender} from "../render/highlightRender";
import {blockRender} from "../render/blockRender";
import {
@@ -428,7 +428,7 @@ export class Gutter {
(buttonElement.firstElementChild as HTMLElement).style.transform = "rotate(90deg)";
}
}
- if (shouldHideGutterAfterFold(foldStatus)) {
+ if (!markGutterForFoldRestore(this.element, foldElement.getAttribute("data-node-id"), foldStatus)) {
hideElements(["gutter"], protyle);
}
hideElements(["select"], protyle);
diff --git a/app/src/protyle/ui/gutterVisibility.test.ts b/app/src/protyle/ui/gutterVisibility.test.ts
index 8086b2ac47..5089bef0bd 100644
--- a/app/src/protyle/ui/gutterVisibility.test.ts
+++ b/app/src/protyle/ui/gutterVisibility.test.ts
@@ -1,6 +1,11 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {hideGutterElements, shouldHideGutterAfterFold} from "./gutterVisibility";
+import {
+ consumeGutterFoldRestore,
+ hideGutterElements,
+ markGutterForFoldRestore,
+ shouldHideGutterAfterFold
+} from "./gutterVisibility";
const createElement = (content: string) => {
const classes = new Set<string>();
@@ -39,12 +44,43 @@ describe("hideGutterElements", () => {
});
describe("shouldHideGutterAfterFold", () => {
- it("展开块后保留块标", () => {
+ it("展开或折叠块后保留块标", () => {
assert.equal(shouldHideGutterAfterFold(0), false);
+ assert.equal(shouldHideGutterAfterFold(1), false);
});
- it("折叠块或折叠状态未变化时隐藏块标", () => {
- assert.equal(shouldHideGutterAfterFold(1), true);
+ it("折叠状态未变化时隐藏块标", () => {
assert.equal(shouldHideGutterAfterFold(-1), true);
});
});
+
+describe("gutter fold restore", () => {
+ const createGutter = () => {
+ const attributes = new Map<string, string>();
+ return {
+ element: {
+ getAttribute: (name: string) => attributes.get(name) || null,
+ removeAttribute: (name: string) => attributes.delete(name),
+ setAttribute: (name: string, value: string) => attributes.set(name, value),
+ } as unknown as HTMLElement,
+ attributes,
+ };
+ };
+
+ it("记录并消费需要恢复块标的折叠操作", () => {
+ const gutter = createGutter();
+
+ assert.equal(markGutterForFoldRestore(gutter.element, "heading", 1), true);
+ assert.equal(consumeGutterFoldRestore(gutter.element, "heading"), true);
+ assert.equal(consumeGutterFoldRestore(gutter.element, "heading"), false);
+ assert.equal(markGutterForFoldRestore(gutter.element, "heading", 0), true);
+ assert.equal(consumeGutterFoldRestore(gutter.element, "heading"), true);
+ });
+
+ it("不记录未改变状态的折叠操作", () => {
+ const gutter = createGutter();
+
+ assert.equal(markGutterForFoldRestore(gutter.element, "heading", -1), false);
+ assert.equal(gutter.attributes.size, 0);
+ });
+});
diff --git a/app/src/protyle/ui/gutterVisibility.ts b/app/src/protyle/ui/gutterVisibility.ts
index b549d5e1ef..0211dd0582 100644
--- a/app/src/protyle/ui/gutterVisibility.ts
+++ b/app/src/protyle/ui/gutterVisibility.ts
@@ -7,4 +7,23 @@ export const hideGutterElements = (elements: HTMLElement[], hidden: boolean) =>
});
};
-export const shouldHideGutterAfterFold = (foldStatus: number) => foldStatus !== 0;
+export const shouldHideGutterAfterFold = (foldStatus: number) => foldStatus < 0;
+
+const GUTTER_FOLD_RESTORE_ATTRIBUTE = "data-fold-restore-id";
+
+export const markGutterForFoldRestore = (element: HTMLElement, id: string | null, foldStatus: number) => {
+ if (!id || shouldHideGutterAfterFold(foldStatus)) {
+ element.removeAttribute(GUTTER_FOLD_RESTORE_ATTRIBUTE);
+ return false;
+ }
+ element.setAttribute(GUTTER_FOLD_RESTORE_ATTRIBUTE, id);
+ return true;
+};
+
+export const consumeGutterFoldRestore = (element: HTMLElement, id: string) => {
+ if (element.getAttribute(GUTTER_FOLD_RESTORE_ATTRIBUTE) !== id) {
+ return false;
+ }
+ element.removeAttribute(GUTTER_FOLD_RESTORE_ATTRIBUTE);
+ return true;
+};
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 84a3ce3e53..e14b853f5d 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -37,6 +37,7 @@ import {countBlockWord} from "../../layout/status";
import {isPaidUser, needSubscribe} from "../../util/needSubscribe";
import {resize} from "../util/resize";
import {scrollCenter} from "../../util/highlightById";
+import {consumeGutterFoldRestore} from "../ui/gutterVisibility";
import {setFold} from "../util/blockFold";
import {queueTransaction} from "../util/transactionQueue";
import {
@@ -2112,6 +2113,18 @@ export const transaction = (protyle: IProtyle, doOperations: IOperation[], undoO
});
};
+const restoreGutterAfterFold = (protyle: IProtyle, id: string) => {
+ if (!consumeGutterFoldRestore(protyle.gutter.element, id)) {
+ return;
+ }
+ window.requestAnimationFrame(() => {
+ const nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
+ if (nodeElement) {
+ protyle.gutter.render(protyle, nodeElement);
+ }
+ });
+};
+
const processFold = (operation: IOperation, protyle: IProtyle) => {
if (operation.action === "unfoldHeading" || operation.action === "foldHeading") {
const gutterFoldElement = protyle.gutter.element.querySelector('[data-type="fold"]');
@@ -2161,6 +2174,7 @@ const processFold = (operation: IOperation, protyle: IProtyle) => {
protyle.contentElement.scrollTop = scrollTop;
protyle.scroll.lastScrollTop = scrollTop;
}
+ restoreGutterAfterFold(protyle, operation.id);
return;
}
protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(item => {
@@ -2176,11 +2190,14 @@ const processFold = (operation: IOperation, protyle: IProtyle) => {
// 折叠移除子块后,刷新折叠标题所在超级块的拖拽手柄(子块数变化)
refreshSbs(...Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`)));
// 折叠标题后未触发动态加载 https://github.com/siyuan-note/siyuan/issues/4168
- if (protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "2" &&
+ const needsDynamicLoad = protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "2" &&
!protyle.scroll.element.classList.contains("fn__none") &&
- protyle.contentElement.scrollHeight - protyle.contentElement.scrollTop < protyle.contentElement.clientHeight * 2 // https://github.com/siyuan-note/siyuan/issues/7785
- ) {
- protyle.scroll.loadDynamic(protyle, 2);
+ // https://github.com/siyuan-note/siyuan/issues/7785
+ protyle.contentElement.scrollHeight - protyle.contentElement.scrollTop < protyle.contentElement.clientHeight * 2;
+ if (!needsDynamicLoad || !protyle.scroll.loadDynamic(protyle, 2, {
+ onFinish: () => restoreGutterAfterFold(protyle, operation.id),
+ })) {
+ restoreGutterAfterFold(protyle, operation.id);
}
return;
}