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: Restore cursor when focusing table and database blocks https://github.com/siyuan-note/siyuan/issues/18809
Danielfb203ec8b7a1
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.tsindex d200a86777..18a6e3a998 100644--- a/app/src/menus/protyle.ts+++ b/app/src/menus/protyle.ts@@ -74,6 +74,7 @@ import {TABLE_DEFAULT_COLUMN_WIDTH,} from "../protyle/util/tableColumnWidth";import {getParentDocumentID} from "../protyle/util/parentDocument";+import {shouldFocusAfterZoom} from "../protyle/util/focusRestore";const renderAssetList = (element: Element, k: string, position: IPosition, exts: string[] = []) => {fetchPost("/api/search/searchAsset", {@@ -1001,7 +1002,7 @@ export const zoomOut = (options: {}const focusElement = options.protyle.wysiwyg.element.querySelector(`[data-node-id="${options.focusId || options.id}"]`);if (focusElement) {- focusBlock(focusElement);+ focusBlock(focusElement, undefined, true, true);focusElement.scrollIntoView();return;}@@ -1030,7 +1031,13 @@ export const zoomOut = (options: {if (options.id !== options.protyle.block.rootID) {action.push(Constants.CB_GET_ALL);}- if (options.focusId) {+ const focusAfterZoom = shouldFocusAfterZoom({+ focusId: options.focusId,+ id: options.id,+ rootID: options.protyle.block.rootID,+ isPushBack: options.isPushBack,+ });+ if (focusAfterZoom) {action.push(Constants.CB_GET_FOCUS);}onGet({@@ -1044,6 +1051,7 @@ export const zoomOut = (options: {scrollPosition: options.focusId ? "start" : undefined,afterCB: options.callback,dataDocType: options.dataDocType,+ focusAfterZoom,});// https://github.com/siyuan-note/siyuan/issues/4874if (options.focusId) {@@ -1065,7 +1073,7 @@ export const zoomOut = (options: {} else {showElement = getFirstBlock(showElement);}- focusBlock(showElement);+ focusBlock(showElement, undefined, true, true);} else if (!options.focusId) {const getDocParam: IObject = {id: options.protyle.block.rootID,@@ -1080,6 +1088,7 @@ export const zoomOut = (options: {protyle: options.protyle,action: options.isPushBack ? [Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_UNUNDO],dataDocType: options.dataDocType,+ focusAfterZoom: true,});});return;@@ -1102,6 +1111,7 @@ export const zoomOut = (options: {focusId: options.focusId},dataDocType: options.dataDocType,+ focusAfterZoom: true,});});return;diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.tsindex 0994f27be0..7174f2d9f3 100644--- a/app/src/protyle/render/av/gallery/render.ts+++ b/app/src/protyle/render/av/gallery/render.ts@@ -2,6 +2,7 @@ import {hasClosestBlock, hasClosestByClassName} from "../../../util/hasClosest";import {Constants} from "../../../../constants";import {fetchSyncPost} from "../../../../util/fetch";import {focusBlock} from "../../../util/selection";+import {getPendingBlockFocusMode} from "../../../util/focusRestore";import {avRender, genTabHeaderHTML, getGroupTitleHTML, updateSearch} from "../render";import {bindAvSearch} from "../search";import {processRender} from "../../../util/processCode";@@ -117,8 +118,9 @@ export const afterRenderGallery = (options: ITableOptions) => {if (typeof options.resetData.oldOffset === "number") {options.protyle.contentElement.scrollTop = options.resetData.oldOffset;}- if (options.blockElement.getAttribute("data-need-focus") === "true") {- focusBlock(options.blockElement);+ const pendingFocusMode = getPendingBlockFocusMode(options.blockElement.getAttribute("data-need-focus"));+ if (pendingFocusMode) {+ focusBlock(options.blockElement, undefined, true, pendingFocusMode === "zoom");options.blockElement.removeAttribute("data-need-focus");}options.blockElement.setAttribute("data-render", "true");diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.tsindex 806c0e4e6e..6cfa9118b9 100644--- a/app/src/protyle/render/av/render.ts+++ b/app/src/protyle/render/av/render.ts@@ -4,6 +4,7 @@ import {Constants} from "../../../constants";import {addDragFill, cellScrollIntoView, popTextCell} from "./cell";import {unicode2Emoji} from "../../../emoji";import {focusBlock} from "../../util/selection";+import {getPendingBlockFocusMode} from "../../util/focusRestore";import {hasClosestBlock, hasClosestByClassName} from "../../util/hasClosest";import {getRowHTML, stickyRow, updateAVSelectionStatus, updateHeader} from "./row";import {getCalcValue} from "./calc";@@ -412,8 +413,9 @@ const afterRenderTable = (options: ITableOptions) => {options.resetData.dragFillId = undefined;options.resetData.activeIds = [];}- if (options.blockElement.getAttribute("data-need-focus") === "true") {- focusBlock(options.blockElement);+ const pendingFocusMode = getPendingBlockFocusMode(options.blockElement.getAttribute("data-need-focus"));+ if (pendingFocusMode) {+ focusBlock(options.blockElement, undefined, true, pendingFocusMode === "zoom");options.blockElement.removeAttribute("data-need-focus");}options.blockElement.setAttribute("data-render", "true");diff --git a/app/src/protyle/util/focusRestore.test.ts b/app/src/protyle/util/focusRestore.test.tsnew file mode 100644index 0000000000..d0dcbab24d--- /dev/null+++ b/app/src/protyle/util/focusRestore.test.ts@@ -0,0 +1,78 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {getPendingBlockFocusMode, hasFocusOffsets, shouldFocusAfterZoom} from "./focusRestore";++describe("shouldFocusAfterZoom", () => {+ it("focuses a block entered through regular navigation", () => {+ assert.equal(shouldFocusAfterZoom({+ id: "block-id",+ rootID: "root-id",+ isPushBack: true,+ }), true);+ });++ it("leaves back and forward navigation to its focus callback", () => {+ assert.equal(shouldFocusAfterZoom({+ id: "block-id",+ rootID: "root-id",+ isPushBack: false,+ }), false);+ });++ it("focuses an explicit target when exiting focus", () => {+ assert.equal(shouldFocusAfterZoom({+ focusId: "block-id",+ id: "root-id",+ rootID: "root-id",+ isPushBack: false,+ }), true);+ });++ it("does not move focus for a regular root document reload", () => {+ assert.equal(shouldFocusAfterZoom({+ id: "root-id",+ rootID: "root-id",+ isPushBack: true,+ }), false);+ });+});++describe("hasFocusOffsets", () => {+ it("accepts zero offsets", () => {+ assert.equal(hasFocusOffsets({+ rootId: "root-id",+ focusId: "block-id",+ focusStart: 0,+ focusEnd: 0,+ }), true);+ });++ it("rejects a focus target without offsets", () => {+ assert.equal(hasFocusOffsets({+ rootId: "root-id",+ focusId: "block-id",+ }), false);+ });++ it("rejects offsets without a focus target", () => {+ assert.equal(hasFocusOffsets({+ rootId: "root-id",+ focusStart: 0,+ focusEnd: 0,+ }), false);+ });+});++describe("getPendingBlockFocusMode", () => {+ it("preserves the zoom focus strategy through asynchronous rendering", () => {+ assert.equal(getPendingBlockFocusMode("zoom"), "zoom");+ });++ it("keeps the existing default focus strategy", () => {+ assert.equal(getPendingBlockFocusMode("true"), "default");+ });++ it("ignores unrelated attribute values", () => {+ assert.equal(getPendingBlockFocusMode("false"), undefined);+ });+});diff --git a/app/src/protyle/util/focusRestore.ts b/app/src/protyle/util/focusRestore.tsnew file mode 100644index 0000000000..92240d86e2--- /dev/null+++ b/app/src/protyle/util/focusRestore.ts@@ -0,0 +1,18 @@+export const shouldFocusAfterZoom = (options: {+ focusId?: string,+ id: string,+ rootID: string,+ isPushBack: boolean,+}) => Boolean(options.focusId || (options.isPushBack && options.id !== options.rootID));++export const hasFocusOffsets = (scrollAttr?: IScrollAttr) => Boolean(scrollAttr?.focusId) &&+ typeof scrollAttr.focusStart === "number" && typeof scrollAttr.focusEnd === "number";++export const getPendingBlockFocusMode = (value: string | null): "default" | "zoom" | undefined => {+ if (value === "zoom") {+ return "zoom";+ }+ if (value === "true") {+ return "default";+ }+};diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.tsindex dc296a3ac6..480e6586cd 100644--- a/app/src/protyle/util/onGet.ts+++ b/app/src/protyle/util/onGet.ts@@ -31,6 +31,7 @@ import {disabledWYSIWYG} from "./disabledWYSIWYG";import {getEmbeddedDocInfoResponse} from "./docInfo";import {updateWidgetCacheVersion} from "./widgetCache";import {normalizeHTMLAssetIFrameSources} from "../../asset/html";+import {hasFocusOffsets} from "./focusRestore";/// #if MOBILEimport {updateMobileTitleReadonly} from "./setEditMode";/// #endif@@ -45,6 +46,7 @@ export const onGet = (options: {afterCB?: () => void,dataDocType?: string,isValid?: () => boolean,+ focusAfterZoom?: boolean,}) => {if (options.isValid && !options.isValid()) {return;@@ -135,7 +137,8 @@ export const onGet = (options: {isSyncing: options.data.data.isSyncing,refreshHeadingNumbers,afterCB: options.afterCB,- scrollPosition: options.scrollPosition+ scrollPosition: options.scrollPosition,+ focusAfterZoom: options.focusAfterZoom}, options.protyle);removeLoading(options.protyle);return;@@ -152,7 +155,8 @@ export const onGet = (options: {isSyncing: options.data.data.isSyncing,refreshHeadingNumbers,afterCB: options.afterCB,- scrollPosition: options.scrollPosition+ scrollPosition: options.scrollPosition,+ focusAfterZoom: options.focusAfterZoom}, options.protyle);removeLoading(options.protyle);return;@@ -182,7 +186,8 @@ export const onGet = (options: {isSyncing: options.data.data.isSyncing,refreshHeadingNumbers,afterCB: options.afterCB,- scrollPosition: options.scrollPosition+ scrollPosition: options.scrollPosition,+ focusAfterZoom: options.focusAfterZoom}, options.protyle);removeLoading(options.protyle);};@@ -211,7 +216,8 @@ const setHTML = (options: {scrollAttr?: IScrollAttr,scrollPosition?: ScrollLogicalPosition,refreshHeadingNumbers?: boolean,- afterCB?: () => void+ afterCB?: () => void,+ focusAfterZoom?: boolean,}, protyle: IProtyle) => {if (protyle.contentElement.classList.contains("fn__none") && protyle.wysiwyg.element.innerHTML !== "") {return;@@ -359,7 +365,7 @@ const setHTML = (options: {}}- focusElementById(protyle, options.action, options.scrollAttr, options.scrollPosition);+ focusElementById(protyle, options.action, options.scrollAttr, options.scrollPosition, options.focusAfterZoom);if (options.action.includes(Constants.CB_GET_SETID) ||(protyle.model && !protyle.block.showAll)) {@@ -530,7 +536,8 @@ export const enableProtyle = (protyle: IProtyle) => {hideTooltip();};-const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScrollAttr, scrollPosition?: ScrollLogicalPosition) => {+const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScrollAttr,+ scrollPosition?: ScrollLogicalPosition, focusAfterZoom = false) => {let focusElement: Element;if (scrollAttr && scrollAttr.focusId) {focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`);@@ -559,10 +566,11 @@ const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScrif (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_FOCUSFIRST)) {setTimeout(() => {let range: Range;- if (scrollAttr && scrollAttr.focusId) {+ if (hasFocusOffsets(scrollAttr)) {range = focusByOffset(focusElement, scrollAttr.focusStart, scrollAttr.focusEnd) as Range;} else {- range = focusBlock(focusElement, undefined, !action.includes(Constants.CB_GET_OUTLINE)) as Range;+ range = focusBlock(focusElement, undefined, !action.includes(Constants.CB_GET_OUTLINE),+ focusAfterZoom) as Range;}/// #if !MOBILEif (!action.includes(Constants.CB_GET_UNUNDO)) {diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.tsindex 2777637a65..56eaeaf10b 100644--- a/app/src/protyle/util/selection.ts+++ b/app/src/protyle/util/selection.ts@@ -1095,7 +1095,8 @@ export const focusByRange = (range: Range) => {selection.addRange(range);};-export const focusBlock = (element: Element, parentElement?: HTMLElement, toStart = true): false | Range => {+export const focusBlock = (element: Element, parentElement?: HTMLElement, toStart = true,+ focusAVTitle = false): false | Range => {if (!element) {return false;}@@ -1136,13 +1137,20 @@ export const focusBlock = (element: Element, parentElement?: HTMLElement, toStarsetRange = true;} else if (type === "NodeAttributeView") {/// #if !MOBILE- const cursorElement = element.querySelector(".av__cursor");- if (cursorElement) {- range.setStart(cursorElement.firstChild, 0);+ const titleElement = focusAVTitle && element.querySelector(".av__title:not(.fn__none)");+ if (titleElement) {+ range.selectNodeContents(titleElement);+ range.collapse(toStart);setRange = true;} else {- element.setAttribute("data-need-focus", "true");- return false;+ const cursorElement = element.querySelector(".av__cursor");+ if (cursorElement) {+ range.setStart(cursorElement.firstChild, 0);+ setRange = true;+ } else {+ element.setAttribute("data-need-focus", focusAVTitle ? "zoom" : "true");+ return false;+ }}/// #elsereturn false;