An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve active tabs when restoring document focus https://github.com/siyuan-note/siyuan/issues/17642
Daniela75490729370
diff --git a/app/src/protyle/scroll/saveScroll.ts b/app/src/protyle/scroll/saveScroll.tsindex 8b97f7df44..d20993b060 100644--- a/app/src/protyle/scroll/saveScroll.ts+++ b/app/src/protyle/scroll/saveScroll.ts@@ -8,6 +8,7 @@ import {isSupportCSSHL} from "../render/searchMarkRender";import {isEncryptedBox} from "../../util/pathName";import {getContenteditableElement} from "../wysiwyg/getBlock";import {getSavedScrollRange, getScrollRequestParams} from "./scrollRequest";+import {getSavedTabFocusTarget} from "../util/focusRestore";export const saveScroll = (protyle: IProtyle, getObject = false) => {if (!protyle.wysiwyg.element.firstElementChild || window.siyuan.config.readonly ||@@ -40,10 +41,13 @@ export const saveScroll = (protyle: IProtyle, getObject = false) => {if (range && protyle.wysiwyg.element.contains(range.startContainer)) {const blockElement = hasClosestBlock(range.startContainer);if (blockElement) {- const position = getSelectionOffset(getContenteditableElement(blockElement) || blockElement, undefined, range);- attr.focusId = blockElement.getAttribute("data-node-id");- attr.focusStart = position.start;- attr.focusEnd = position.end;+ const target = getSavedTabFocusTarget(blockElement);+ attr.focusId = target.getAttribute("data-node-id");+ if (target === blockElement) {+ const position = getSelectionOffset(getContenteditableElement(blockElement) || blockElement, undefined, range);+ attr.focusStart = position.start;+ attr.focusEnd = position.end;+ }}}}diff --git a/app/src/protyle/util/focusRestore.test.ts b/app/src/protyle/util/focusRestore.test.tsindex f31a54ea3f..4667487fe0 100644--- a/app/src/protyle/util/focusRestore.test.ts+++ b/app/src/protyle/util/focusRestore.test.ts@@ -2,11 +2,59 @@ import {describe, it} from "node:test";import * as assert from "node:assert/strict";import {getPendingBlockFocusMode,+ getSavedTabFocusTarget,getZoomFocusScrollAttr,hasFocusOffsets,shouldFocusAfterZoom} from "./focusRestore";+class FocusElement {+ children: FocusElement[] = [];+ parentElement: FocusElement;+ classList = {contains: (name: string) => this.kind === name};+ constructor(private kind = "", private attrs: Record<string, string> = {}) {}+ append(child: FocusElement) {+ this.children.push(child);+ child.parentElement = this;+ return child;+ }+ closest(selector: string): FocusElement {+ return this.kind === selector.substring(1) ? this : this.parentElement?.closest(selector);+ }+ getAttribute(name: string) {+ return this.attrs[name];+ }+ asElement() {+ return this as unknown as Element;+ }+}++describe("saved focus respects persisted tab selection", () => {+ it("redirects stale first-page focus before visual tab initialization", () => {+ const tabs = new FocusElement("tabs", {"tabs-active-id": "second"});+ const first = tabs.append(new FocusElement("tab-item", {"data-node-id": "first"}));+ const second = tabs.append(new FocusElement("tab-item", {"data-node-id": "second"}));+ const oldFocus = first.append(new FocusElement());+ const activeFocus = second.append(new FocusElement());+ assert.equal(getSavedTabFocusTarget(oldFocus.asElement()), tabs.asElement());+ assert.equal(getSavedTabFocusTarget(activeFocus.asElement()), activeFocus.asElement());+ });+ it("uses the outermost hidden page in nested tabs", () => {+ const outer = new FocusElement("tabs", {"tabs-active-id": "outer-active"});+ const hidden = outer.append(new FocusElement("tab-item", {"data-node-id": "outer-hidden"}));+ outer.append(new FocusElement("tab-item", {"data-node-id": "outer-active"}));+ const inner = hidden.append(new FocusElement("tabs", {"tabs-active-id": "inner-active"}));+ const first = inner.append(new FocusElement("tab-item", {"data-node-id": "inner-hidden"}));+ inner.append(new FocusElement("tab-item", {"data-node-id": "inner-active"}));+ assert.equal(getSavedTabFocusTarget(first.asElement()), outer.asElement());+ });+ it("leaves ordinary focus and a missing target unchanged", () => {+ const paragraph = new FocusElement();+ assert.equal(getSavedTabFocusTarget(paragraph.asElement()), paragraph.asElement());+ assert.equal(getSavedTabFocusTarget(undefined), undefined);+ });+});+describe("shouldFocusAfterZoom", () => {it("focuses a block entered through regular navigation", () => {assert.equal(shouldFocusAfterZoom({diff --git a/app/src/protyle/util/focusRestore.ts b/app/src/protyle/util/focusRestore.tsindex b1104b9509..e863d6ba62 100644--- a/app/src/protyle/util/focusRestore.ts+++ b/app/src/protyle/util/focusRestore.ts@@ -8,6 +8,23 @@ export const shouldFocusAfterZoom = (options: {export const hasFocusOffsets = (scrollAttr?: IScrollAttr) => Boolean(scrollAttr?.focusId) &&typeof scrollAttr.focusStart === "number" && typeof scrollAttr.focusEnd === "number";+// 恢复光标时以持久化的页签选择为准,过期光标不能重新激活隐藏页。+export const getSavedTabFocusTarget = (element: Element): Element => {+ let target = element;+ for (let item = element?.closest(".tab-item"); item; item = item.parentElement?.closest(".tab-item")) {+ const tabs = item.parentElement;+ if (!tabs?.classList.contains("tabs")) {+ continue;+ }+ const items = Array.from(tabs.children).filter(child => child.classList.contains("tab-item"));+ const active = items.find(child => child.getAttribute("data-node-id") === tabs.getAttribute("tabs-active-id")) || items[0];+ if (active !== item) {+ target = tabs;+ }+ }+ return target;+};+export const getZoomFocusScrollAttr = (rootId: string, focusId?: string,focusPosition?: { start: number, end: number }): IScrollAttr | undefined =>focusId ? {diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.tsindex 0384fba9a6..89f7e3c7b3 100644--- a/app/src/protyle/util/onGet.ts+++ b/app/src/protyle/util/onGet.ts@@ -31,7 +31,7 @@ import {disabledWYSIWYG} from "./disabledWYSIWYG";import {getEmbeddedDocInfoResponse} from "./docInfo";import {updateWidgetCacheVersion} from "./widgetCache";import {normalizeHTMLAssetIFrameSources} from "../../asset/html";-import {hasFocusOffsets} from "./focusRestore";+import {getSavedTabFocusTarget, hasFocusOffsets} from "./focusRestore";import {isIPhone} from "./compatibility";import {forEachPluginSubscriber} from "../../plugin/EventBusCore";import {disposeCustomBlocksInElement, setCustomBlockRootReady} from "../../plugin/customBlockRender";@@ -592,6 +592,11 @@ const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScr} else if (!focusElement || action.includes(Constants.CB_GET_FOCUSFIRST)) {focusElement = protyle.wysiwyg.element.firstElementChild;}+ const hasScrollTop = scrollAttr && typeof scrollAttr.scrollTop === "number";+ const savedFocusElement = focusElement;+ if (hasScrollTop && scrollAttr.focusId && !action.includes(Constants.CB_GET_HL)) {+ focusElement = getSavedTabFocusTarget(focusElement);+ }if (action.includes(Constants.CB_GET_HL)) {preventScroll(protyle); // 搜索页签滚动会导致再次请求bgFade(focusElement);@@ -599,7 +604,7 @@ const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScrif (!suppressFocus && (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_FOCUSFIRST))) {setTimeout(() => {let range: Range;- if (hasFocusOffsets(scrollAttr)) {+ if (savedFocusElement === focusElement && hasFocusOffsets(scrollAttr)) {range = focusByOffset(focusElement, scrollAttr.focusStart, scrollAttr.focusEnd) as Range;} else {range = focusBlock(focusElement, undefined, !action.includes(Constants.CB_GET_OUTLINE),@@ -612,7 +617,6 @@ const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScr/// #endif}, focusElement.getAttribute("data-type") === "NodeCodeBlock" ? Constants.TIMEOUT_TRANSITION : 0);}- const hasScrollTop = scrollAttr && typeof scrollAttr.scrollTop === "number";if (hasScrollTop) {protyle.contentElement.scrollTop = scrollAttr.scrollTop;}diff --git a/docs/TABS.md b/docs/TABS.mdindex 33ae423a8a..385f94ebb3 100644--- a/docs/TABS.md+++ b/docs/TABS.md@@ -80,6 +80,7 @@ Linux 下的说明。- 单击切页,双击或菜单进入标题编辑。标题编辑复用提示块行级格式、选区与输入法规则,移动端提供菜单入口。- 新建页签块默认包含两个页签,各包含一个空段落。标题、正文、排序和布局变更参与正常撤销。- 可写编辑器中的主动切换和搜索、引用定位保存选中项。切换不占用正文撤销栈,不刷新正文修改时间,但会落盘并参与同步。+- 刷新或重启后恢复文档位置时,页签选择优先于过期光标。旧光标位于未选中的页签内时,将焦点移到当前选中页的正文,不复用旧文本偏移;保存文档位置时也不记录隐藏页中的过期光标。主动搜索和块引用定位仍可切换到目标页。- 只读页面、历史预览和导出 HTML 可临时切换。外部状态更新先接收数据,若将隐藏正在编辑的正文,延后界面切换,避免丢失焦点和输入法组合。- 各层嵌套独立保存选择。定位隐藏内容时逐层激活所属页签;单独打开页签项展示标题与全部正文。- 普通文字选区不隐式包含隐藏正文;选中整组块后,复制和删除涵盖全部页签。正文首尾的退格和删除不能自动合并相邻页签。