Skip to content

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

:bug: Preserve position when exiting focus on folded search results https://github.com/siyuan-note/siyuan/issues/19653

Danielfc8fdabce0e5
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts
index 975454bb70..9fb207026e 100644
--- a/app/src/menus/protyle.ts
+++ b/app/src/menus/protyle.ts
@@ -1081,6 +1081,15 @@ export const zoomOut = (options: {
getDocParam.notebook = options.protyle.notebookId;
}
fetchPost("/api/filetree/getDoc", getDocParam, async (getResponse) => {
+ // 退出聚焦时先确定可见的折叠祖先,使滚动和延迟恢复光标使用同一目标。
+ if (options.focusId && options.id === options.protyle.block.rootID) {
+ const unfoldResponse = await fetchSyncPost("/api/block/getUnfoldedParentID", {id: options.focusId});
+ if (unfoldResponse.code === 0 && unfoldResponse.data.parentID &&
+ unfoldResponse.data.parentID !== options.focusId) {
+ options.focusId = unfoldResponse.data.parentID;
+ options.focusPosition = undefined;
+ }
+ }
const action: TProtyleAction[] = [Constants.CB_GET_HTML];
if (!options.isPushBack) {
action.push(Constants.CB_GET_UNUNDO);
diff --git a/app/src/menus/zoomOut.test.ts b/app/src/menus/zoomOut.test.ts
new file mode 100644
index 0000000000..d6f561191b
--- /dev/null
+++ b/app/src/menus/zoomOut.test.ts
@@ -0,0 +1,76 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isVariableStatement, ScriptTarget, transpileModule} from "typescript";
+import {getZoomFocusScrollAttr, shouldFocusAfterZoom} from "../protyle/util/focusRestore";
+
+const source = createSourceFile("protyle.ts", readFileSync("src/menus/protyle.ts", "utf8"), ScriptTarget.ES2021, true);
+const declaration = source.statements.find(statement => isVariableStatement(statement) &&
+ statement.declarationList.declarations.some(item => item.name.getText(source) === "zoomOut"));
+const compiled = transpileModule(declaration.getText(source).replace(/^export /, "") + "\nglobalThis.zoom = zoomOut;", {
+ compilerOptions: {target: ScriptTarget.ES2021},
+}).outputText;
+
+const exitFocus = async (parentID: string, code = 0) => {
+ const renders: any[] = [];
+ let response: Promise<void>;
+ const element = {
+ getBoundingClientRect: () => ({height: 20}),
+ classList: {contains: () => false},
+ };
+ const context: any = {
+ window: {siyuan: {config: {editor: {dynamicLoadBlocks: 64}}}},
+ Constants: {CB_GET_HTML: "html", CB_GET_FOCUS: "focus"},
+ isPhablet: () => false,
+ isEncryptedBox: () => false,
+ hasClosestByClassName: (): undefined => undefined,
+ fetchPost: (_url: string, _params: unknown, callback: (data: unknown) => Promise<void>) => {
+ response = callback({});
+ },
+ fetchSyncPost: async () => ({code, data: {parentID}}),
+ onGet: (options: unknown) => renders.push(options),
+ getZoomFocusScrollAttr,
+ shouldFocusAfterZoom,
+ getFirstBlock: (target: unknown) => target,
+ focusBlock: () => {},
+ focusByOffset: () => {},
+ };
+ runInNewContext(compiled, context);
+ context.zoom({
+ protyle: {
+ options: {},
+ block: {rootID: "root"},
+ wysiwyg: {element: {querySelector: () => element}},
+ },
+ id: "root",
+ focusId: "nested-block",
+ focusPosition: {start: 2, end: 2},
+ });
+ await response;
+ return renders[0];
+};
+
+test("exiting a folded search result restores focus to the visible ancestor before rendering", async () => {
+ const render = await exitFocus("folded-list-item");
+ assert.equal(render.scrollAttr.focusId, "folded-list-item");
+ assert.equal(render.scrollAttr.focusStart, undefined);
+ assert.equal(render.scrollAttr.focusEnd, undefined);
+ assert.equal(render.focusAfterZoom, true);
+ assert.ok(render.action.includes("focus"));
+});
+
+test("exiting a visible block preserves its cursor offsets", async () => {
+ const render = await exitFocus("nested-block");
+ assert.equal(render.scrollAttr.focusId, "nested-block");
+ assert.equal(render.scrollAttr.focusStart, 2);
+ assert.equal(render.scrollAttr.focusEnd, 2);
+});
+
+test("an unavailable ancestor does not discard the original focus target", async () => {
+ for (const [parentID, code] of [["", 0], ["", -1]] as const) {
+ const render = await exitFocus(parentID, code);
+ assert.equal(render.scrollAttr.focusId, "nested-block");
+ assert.equal(render.scrollAttr.focusStart, 2);
+ }
+});