Skip to content

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

:sparkles: Temporarily expand focused list items https://github.com/siyuan-note/siyuan/issues/8956

Danielf91cf6b9ed59
diff --git a/app/src/protyle/util/blockFold.ts b/app/src/protyle/util/blockFold.ts
index fb7f7053fe..945652b000 100644
--- a/app/src/protyle/util/blockFold.ts
+++ b/app/src/protyle/util/blockFold.ts
@@ -8,6 +8,7 @@ import {clearSelect} from "./clear";
import {removeFoldHeading} from "./heading";
import {getSbChildBlockCount, getTopAloneElement} from "../wysiwyg/getBlock";
import {fetchSyncPost} from "../../util/fetch";
+import {stopFocusFold} from "./focusFold";
import {
getViewFoldOccurrenceID,
hasViewFoldContext,
@@ -63,6 +64,9 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
return getEmptyFoldResult();
}
const hasFold = nodeElement.getAttribute("fold") === "1";
+ if (typeof isOpen !== "boolean") {
+ stopFocusFold(protyle, nodeElement.getAttribute("data-node-id"));
+ }
if (hasViewFoldContext(protyle)) {
if ((hasFold && typeof isOpen === "boolean" && !isOpen) ||
(!hasFold && typeof isOpen === "boolean" && isOpen)) {
diff --git a/app/src/protyle/util/focusFold.test.ts b/app/src/protyle/util/focusFold.test.ts
new file mode 100644
index 0000000000..e6d3434443
--- /dev/null
+++ b/app/src/protyle/util/focusFold.test.ts
@@ -0,0 +1,139 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {applyFocusFold, stopFocusFold, updateFocusFoldSource} from "./focusFold";
+
+class FoldElement {
+ attrs: Record<string, string>;
+
+ constructor(id: string, type = "NodeListItem", fold = "1") {
+ this.attrs = {"data-node-id": id, "data-type": type, fold};
+ }
+
+ getAttribute(name: string) {
+ return this.attrs[name] ?? null;
+ }
+
+ hasAttribute(name: string) {
+ return name in this.attrs;
+ }
+
+ setAttribute(name: string, value: string) {
+ this.attrs[name] = value;
+ }
+
+ removeAttribute(name: string) {
+ delete this.attrs[name];
+ }
+}
+
+const editor = (elements: FoldElement[]) => ({
+ block: {id: "parent", rootID: "doc", showAll: true},
+ options: {},
+ wysiwyg: {
+ element: {
+ querySelector: (selector: string) => elements.find(element =>
+ selector === `[data-node-id="${element.attrs["data-node-id"]}"][data-type="${element.attrs["data-type"]}"]`),
+ },
+ },
+} as unknown as IProtyle);
+
+describe("focused list folding", () => {
+ it("temporarily unfolds only the focused item and restores it on exit", () => {
+ const parent = new FoldElement("parent");
+ const child = new FoldElement("child");
+ const protyle = editor([parent, child]);
+ applyFocusFold(protyle);
+ assert.equal(parent.getAttribute("fold"), null);
+ assert.equal(parent.getAttribute("data-view-fold-source"), "1");
+ assert.equal(child.getAttribute("fold"), "1");
+ protyle.block.showAll = false;
+ applyFocusFold(protyle);
+ assert.equal(parent.getAttribute("fold"), "1");
+ assert.equal(parent.hasAttribute("data-view-fold-source"), false);
+ });
+
+ it("restores each level when navigating into a child and back", () => {
+ const parent = new FoldElement("parent");
+ const child = new FoldElement("child");
+ const protyle = editor([parent, child]);
+ applyFocusFold(protyle);
+ protyle.block.id = "child";
+ applyFocusFold(protyle);
+ assert.equal(parent.getAttribute("fold"), "1");
+ assert.equal(child.getAttribute("fold"), null);
+ protyle.block.id = "parent";
+ applyFocusFold(protyle);
+ assert.equal(child.getAttribute("fold"), "1");
+ assert.equal(parent.getAttribute("fold"), null);
+ });
+
+ it("keeps the automatic override when content is replaced", () => {
+ const elements = [new FoldElement("parent")];
+ const protyle = editor(elements);
+ applyFocusFold(protyle);
+ elements[0] = new FoldElement("parent");
+ applyFocusFold(protyle);
+ assert.equal(elements[0].getAttribute("fold"), null);
+ assert.equal(elements[0].getAttribute("data-view-fold-source"), "1");
+ });
+
+ it("honors manual folding across refreshes until focus is entered again", () => {
+ const elements = [new FoldElement("parent")];
+ const protyle = editor(elements);
+ applyFocusFold(protyle);
+ stopFocusFold(protyle, "parent");
+ elements[0].setAttribute("fold", "1");
+ assert.equal(elements[0].hasAttribute("data-view-fold-source"), false);
+ elements[0] = new FoldElement("parent");
+ applyFocusFold(protyle);
+ assert.equal(elements[0].getAttribute("fold"), "1");
+ protyle.block.showAll = false;
+ applyFocusFold(protyle);
+ protyle.block.showAll = true;
+ applyFocusFold(protyle);
+ assert.equal(elements[0].getAttribute("fold"), null);
+ });
+
+ it("preserves manual child folding and isolates editor instances", () => {
+ const parent = new FoldElement("parent");
+ const otherParent = new FoldElement("parent");
+ const protyle = editor([parent]);
+ const other = editor([otherParent]);
+ applyFocusFold(protyle);
+ stopFocusFold(protyle, "child");
+ applyFocusFold(protyle);
+ assert.equal(parent.getAttribute("fold"), null);
+ assert.equal(otherParent.getAttribute("fold"), "1");
+ applyFocusFold(other);
+ stopFocusFold(protyle, "parent");
+ parent.setAttribute("fold", "1");
+ applyFocusFold(other);
+ assert.equal(otherParent.getAttribute("fold"), null);
+ });
+
+ it("restores the latest source state after a remote fold change", () => {
+ const parent = new FoldElement("parent");
+ const protyle = editor([parent]);
+ applyFocusFold(protyle);
+ updateFocusFoldSource(protyle, "parent", false);
+ applyFocusFold(protyle);
+ protyle.block.showAll = false;
+ applyFocusFold(protyle);
+ assert.equal(parent.getAttribute("fold"), null);
+ });
+
+ it("does not unfold headings, ordinary document views, or backlink views", () => {
+ const heading = new FoldElement("parent", "NodeHeading");
+ applyFocusFold(editor([heading]));
+ assert.equal(heading.getAttribute("fold"), "1");
+ const parent = new FoldElement("parent");
+ const protyle = editor([parent]);
+ protyle.block.showAll = false;
+ applyFocusFold(protyle);
+ assert.equal(parent.getAttribute("fold"), "1");
+ protyle.block.showAll = true;
+ protyle.options.backlinkData = {} as IProtyle["options"]["backlinkData"];
+ applyFocusFold(protyle);
+ assert.equal(parent.getAttribute("fold"), "1");
+ });
+});
diff --git a/app/src/protyle/util/focusFold.ts b/app/src/protyle/util/focusFold.ts
new file mode 100644
index 0000000000..0ad27aac74
--- /dev/null
+++ b/app/src/protyle/util/focusFold.ts
@@ -0,0 +1,68 @@
+const SOURCE_FOLD = "data-view-fold-source";
+
+interface IFocusFoldState {
+ id: string,
+ rootID: string,
+ manual: boolean,
+ element?: Element,
+}
+
+const states = new WeakMap<IProtyle, IFocusFoldState>();
+
+const restoreElement = (element?: Element) => {
+ const source = element?.getAttribute(SOURCE_FOLD);
+ if (source === "1") {
+ element.setAttribute("fold", "1");
+ } else if (source === "0") {
+ element.removeAttribute("fold");
+ }
+ element?.removeAttribute(SOURCE_FOLD);
+};
+
+export const applyFocusFold = (protyle: IProtyle) => {
+ let state = states.get(protyle);
+ const {id, rootID, showAll} = protyle.block;
+ const focused = showAll && id !== rootID && !protyle.options.backlinkData && !protyle.lite;
+ if (state && (!focused || state.id !== id || state.rootID !== rootID)) {
+ restoreElement(state.element);
+ states.delete(protyle);
+ state = undefined;
+ }
+ if (!focused || state?.manual) {
+ return;
+ }
+ const element = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"][data-type="NodeListItem"]`);
+ if (!element) {
+ return;
+ }
+ if (!state) {
+ state = {id, rootID, manual: false};
+ states.set(protyle, state);
+ }
+ if (state.element !== element) {
+ restoreElement(state.element);
+ state.element = element;
+ }
+ // 复用视图折叠的源属性标记,编辑内容序列化时仍保留文档中的折叠状态。
+ if (!element.hasAttribute(SOURCE_FOLD)) {
+ element.setAttribute(SOURCE_FOLD, element.getAttribute("fold") === "1" ? "1" : "0");
+ }
+ element.removeAttribute("fold");
+};
+
+export const stopFocusFold = (protyle: IProtyle, id: string) => {
+ const state = states.get(protyle);
+ if (!state || state.id !== id) {
+ return;
+ }
+ // 主动折叠从当前可见状态开始,后续刷新不再自动展开,直到重新进入聚焦。
+ state.manual = true;
+ state.element?.removeAttribute(SOURCE_FOLD);
+};
+
+export const updateFocusFoldSource = (protyle: IProtyle, id: string, folded: boolean) => {
+ const state = states.get(protyle);
+ if (state?.id === id && !state.manual) {
+ state.element?.setAttribute(SOURCE_FOLD, folded ? "1" : "0");
+ }
+};
diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.ts
index a0f803e696..64ed0ecdfb 100644
--- a/app/src/protyle/util/onGet.ts
+++ b/app/src/protyle/util/onGet.ts
@@ -39,6 +39,7 @@ import {forEachPluginSubscriber} from "../../plugin/EventBusCore";
import {disposeCustomBlocksInElement, setCustomBlockRootReady} from "../../plugin/customBlockRender";
import {invalidateTrackedRanges, invalidateTrackedRangesInElement} from "./trackedRange";
import {areProtylePluginExtensionsEnabled} from "../runtimeCapabilities";
+import {applyFocusFold} from "./focusFold";
/// #if MOBILE
import {updateMobileTitleReadonly} from "./setEditMode";
/// #endif
@@ -332,6 +333,7 @@ const setHTML = (options: {
}
}
+ applyFocusFold(protyle);
if (options.eof) {
const eofElement = options.action.includes(Constants.CB_GET_BEFORE) ?
protyle.wysiwyg.element.firstElementChild : protyle.wysiwyg.element.lastElementChild;
diff --git a/app/src/protyle/util/viewFold.ts b/app/src/protyle/util/viewFold.ts
index 65d224d04b..b3b5b15c19 100644
--- a/app/src/protyle/util/viewFold.ts
+++ b/app/src/protyle/util/viewFold.ts
@@ -9,6 +9,7 @@ import {
clearViewFoldOccurrenceRuntimeState,
} from "./viewFoldRuntimeState";
import {normalizeHTMLAssetIFrameBlockDOM} from "../../asset/html";
+import {applyFocusFold, stopFocusFold, updateFocusFoldSource} from "./focusFold";
const VIEW_FOLD_SOURCE = "data-view-fold-source";
const VIEW_FOLD_VALUE = "data-view-fold";
@@ -369,6 +370,7 @@ export const setViewFoldTransient = async (protyle: IProtyle, element: Element,
};
export const applyViewFoldStates = async (protyle: IProtyle, scope?: ParentNode) => {
+ applyFocusFold(protyle);
const context = viewFoldContexts.get(protyle);
if (!context) {
return;
@@ -520,6 +522,11 @@ const getFoldValue = (operation: IOperation) => {
};
export const prepareViewFoldTransaction = (protyle: IProtyle, doOperations: IOperation[], undoOperations?: IOperation[]) => {
+ doOperations.forEach(operation => {
+ if (typeof getFoldValue(operation) === "boolean") {
+ stopFocusFold(protyle, operation.id);
+ }
+ });
if (!hasViewFoldContext(protyle)) {
return {
doOperations: sanitizeOperationHTML(doOperations),
@@ -533,6 +540,10 @@ export const prepareViewFoldTransaction = (protyle: IProtyle, doOperations: IOpe
};
export const handleViewFoldSourceOperation = (protyle: IProtyle, operation: IOperation) => {
+ const focusFold = getFoldValue(operation);
+ if (typeof focusFold === "boolean") {
+ updateFocusFoldSource(protyle, operation.id, focusFold);
+ }
if (!hasViewFoldContext(protyle) || !operation.id) {
return false;
}