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: Stabilize database item link navigation https://github.com/siyuan-note/siyuan/issues/12022
Daniel79de5b1b8195
diff --git a/app/src/index.ts b/app/src/index.tsindex c3fe08beab..f62f2aba16 100644--- a/app/src/index.ts+++ b/app/src/index.ts@@ -15,7 +15,7 @@ import {setNoteBook} from "./util/pathName";import {registerServiceWorker} from "./util/serviceWorker";-import {queueAVLocateRequest} from "./protyle/render/av/locate";+import {activateQueuedAVLocate, queueAVLocateRequest} from "./protyle/render/av/locate";import {openFileById} from "./editor/util";import {activateOnboarding, ensureOnboarding} from "./onboarding";import {@@ -300,8 +300,15 @@ window.openFileByURL = (openURL) => {openFileById({app: siyuanApp,id: blockInfo.id,- action: blockInfo.focus ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],- zoomIn: blockInfo.focus+ action: blockInfo.avItemID ? [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] :+ (blockInfo.focus ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]),+ zoomIn: blockInfo.avItemID ? false : blockInfo.focus,+ afterOpen: (model) => {+ const protyle = (model as { editor?: { protyle?: IProtyle } })?.editor?.protyle;+ if (protyle) {+ activateQueuedAVLocate(protyle, blockInfo.id);+ }+ },});return true;}diff --git a/app/src/layout/util.ts b/app/src/layout/util.tsindex bb95720439..c3b646f83d 100644--- a/app/src/layout/util.ts+++ b/app/src/layout/util.ts@@ -32,7 +32,7 @@ import {newCenterEmptyTab, resizeTabs, setTabPosition} from "./tabUtil";import {setStorageVal} from "../protyle/util/compatibility";import {adjustDockPadding} from "./dock/util";import {setTitle} from "../util/processTitle";-import {queueAVLocateRequest} from "../protyle/render/av/locate";+import {activateQueuedAVLocate, queueAVLocateRequest} from "../protyle/render/av/locate";const isBuiltInCustomModel = (type: string) => {return type === "siyuan-card" || type === "siyuan-database-row";@@ -527,8 +527,15 @@ export const JSONToLayout = (app: App, isStart: boolean) => {openFileById({app,id: info.id,- action: info.focus ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],- zoomIn: info.focus,+ action: info.avItemID ? [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] :+ (info.focus ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]),+ zoomIn: info.avItemID ? false : info.focus,+ afterOpen: (model) => {+ const protyle = (model as { editor?: { protyle?: IProtyle } })?.editor?.protyle;+ if (protyle) {+ activateQueuedAVLocate(protyle, info.id);+ }+ },});} else {let latestTabHeaderElement: HTMLElement;diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.tsindex 05aa645e96..aba6cf93f0 100644--- a/app/src/mobile/index.ts+++ b/app/src/mobile/index.ts@@ -12,7 +12,7 @@ import {Model} from "../layout/Model";import "../assets/scss/mobile.scss";import {Menus} from "../menus";import {addBaseURL, parseSiYuanUriInfo, setNoteBook} from "../util/pathName";-import {queueAVLocateRequest} from "../protyle/render/av/locate";+import {activateQueuedAVLocate, queueAVLocateRequest} from "../protyle/render/av/locate";import {handleTouchEnd, handleTouchMove, handleTouchStart, handleTouchUp} from "./util/touch";import {fetchGet, fetchPost} from "../util/fetch";import {initFramework} from "./util/initFramework";@@ -267,8 +267,11 @@ window.openFileByURL = (openURL) => {groupID: blockInfo.avGroupID,});}- openMobileFileById(siyuanApp, blockInfo.id,- blockInfo.focus ? [Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);+ if (blockInfo.avItemID) {+ activateQueuedAVLocate(window.siyuan.mobile.editor?.protyle, blockInfo.id);+ }+ openMobileFileById(siyuanApp, blockInfo.id, blockInfo.avItemID ? [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] :+ (blockInfo.focus ? [Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]));return true;}return false;diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.tsindex d7ea8849e4..d90d196f43 100644--- a/app/src/mobile/util/initFramework.ts+++ b/app/src/mobile/util/initFramework.ts@@ -25,7 +25,7 @@ import {MobileCustom} from "../dock/MobileCustom";import {Menu} from "../../plugin/Menu";import {showMessage} from "../../dialog/message";import {setTitle} from "../../util/processTitle";-import {queueAVLocateRequest} from "../../protyle/render/av/locate";+import {activateQueuedAVLocate, queueAVLocateRequest} from "../../protyle/render/av/locate";let custom: MobileCustom;const openDockMenu = (app: App) => {@@ -202,8 +202,11 @@ export const initFramework = (app: App, isStart: boolean) => {groupID: info.avGroupID,});}- openMobileFileById(app, info.id,- info.focus ? [Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);+ if (info.avItemID) {+ activateQueuedAVLocate(window.siyuan.mobile.editor?.protyle, info.id);+ }+ openMobileFileById(app, info.id, info.avItemID ? [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] :+ (info.focus ? [Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]));return;}if (openMobileOnboarding(app)) {diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.tsindex 9ac988bf7d..f1d1d574fb 100644--- a/app/src/protyle/render/av/gallery/render.ts+++ b/app/src/protyle/render/av/gallery/render.ts@@ -9,7 +9,7 @@ import {getPageSize} from "../groups";import {renderKanban} from "../kanban/render";import {getBodyVirtualData, initVirtualScroll} from "../virtualScroll";import {getRowHTML, updateHeader} from "../row";-import {finishAVLocate, getAVLocateParams, prepareAVLocate} from "../locate";+import {beginAVRender, finishAVLocate, getAVLocateParams, isCurrentAVRender, prepareAVLocate} from "../locate";interface IIds {groupId: string,@@ -154,8 +154,8 @@ export const afterRenderGallery = (options: ITableOptions) => {if (options.cb) {options.cb(options.data);}- finishAVLocate(options.blockElement, options.protyle, options.data);if (!options.renderAll) {+ finishAVLocate(options.blockElement, options.protyle, options.data);return;}bindAvSearch({@@ -165,6 +165,7 @@ export const afterRenderGallery = (options: ITableOptions) => {onChange: () => updateSearch(options.blockElement, options.protyle),});initVirtualScroll(options);+ finishAVLocate(options.blockElement, options.protyle, options.data);};export const renderGallery = async (options: {@@ -174,6 +175,7 @@ export const renderGallery = async (options: {renderAll: boolean,data?: IAV,}) => {+ const renderToken = beginAVRender(options.blockElement);const searchInputElement = options.blockElement.querySelector('[data-type="av-search"]');const editIds: IIds[] = [];options.blockElement.querySelectorAll(".av__gallery-fields--edit").forEach(item => {@@ -247,6 +249,9 @@ export const renderGallery = async (options: {});data = response.data;}+ if (!isCurrentAVRender(options.blockElement, renderToken)) {+ return;+ }prepareAVLocate(options.blockElement, data, resetData);if (data.viewType === "table") {options.blockElement.setAttribute("data-av-type", data.viewType);diff --git a/app/src/protyle/render/av/kanban/render.ts b/app/src/protyle/render/av/kanban/render.tsindex 26eda4c241..df073cb215 100644--- a/app/src/protyle/render/av/kanban/render.ts+++ b/app/src/protyle/render/av/kanban/render.ts@@ -7,7 +7,7 @@ import {afterRenderGallery, renderGallery} from "../gallery/render";import {escapeHtml} from "../../../../util/escape";import {getRowHTML} from "../row";import {getBodyVirtualData} from "../virtualScroll";-import {getAVLocateParams, prepareAVLocate} from "../locate";+import {beginAVRender, getAVLocateParams, isCurrentAVRender, prepareAVLocate} from "../locate";interface IIds {groupId: string,@@ -71,6 +71,7 @@ export const renderKanban = async (options: {renderAll: boolean,data?: IAV,}) => {+ const renderToken = beginAVRender(options.blockElement);const searchInputElement = options.blockElement.querySelector('[data-type="av-search"]') as HTMLInputElement;const editIds: IIds[] = [];options.blockElement.querySelectorAll(".av__gallery-fields--edit").forEach(item => {@@ -145,6 +146,9 @@ export const renderKanban = async (options: {});data = response.data;}+ if (!isCurrentAVRender(options.blockElement, renderToken)) {+ return;+ }prepareAVLocate(options.blockElement, data, resetData);if (data.viewType === "table") {options.blockElement.setAttribute("data-av-type", "table");diff --git a/app/src/protyle/render/av/locate.ts b/app/src/protyle/render/av/locate.tsindex 2044ed7ee7..ebc549e41d 100644--- a/app/src/protyle/render/av/locate.ts+++ b/app/src/protyle/render/av/locate.ts@@ -9,15 +9,52 @@ export interface IAVLocateRequest {groupID?: string;viewID?: string;select?: boolean;+ highlight?: boolean;previousViewID?: string;messageShown?: boolean;}const locateRequests = new WeakMap<HTMLElement, IAVLocateRequest>();const queuedLocateRequests = new Map<string, IAVLocateRequest>();+const renderTokens = new WeakMap<HTMLElement, symbol>();+const highlightTimers = new WeakMap<HTMLElement, number>();++const highlightTemporarily = (blockElement: HTMLElement, targetElement: HTMLElement, className: string) => {+ window.clearTimeout(highlightTimers.get(blockElement));+ targetElement.classList.add(className);+ const timer = window.setTimeout(() => {+ targetElement.classList.remove(className);+ if (highlightTimers.get(blockElement) === timer) {+ highlightTimers.delete(blockElement);+ }+ }, 1024);+ highlightTimers.set(blockElement, timer);+};++export const beginAVRender = (blockElement: HTMLElement) => {+ const token = Symbol();+ renderTokens.set(blockElement, token);+ return token;+};++export const isCurrentAVRender = (blockElement: HTMLElement, token: symbol) => {+ return renderTokens.get(blockElement) === token;+};export const queueAVLocateRequest = (blockID: string, request: IAVLocateRequest) => {- queuedLocateRequests.set(blockID, request);+ queuedLocateRequests.set(blockID, {...request, select: false, highlight: true});+};++export const activateQueuedAVLocate = (protyle: IProtyle, blockID: string) => {+ const request = queuedLocateRequests.get(blockID);+ const blockElement = protyle?.wysiwyg.element.querySelector(`.av[data-node-id="${blockID}"]`) as HTMLElement;+ if (!request || !blockElement) {+ return false;+ }+ locateRequests.set(blockElement, request);+ blockElement.removeAttribute("data-render");+ import("./render").then(({avRender}) => avRender(blockElement, protyle));+ return true;};export const setAVLocateRequest = (blockElement: HTMLElement, request: IAVLocateRequest) => {@@ -29,7 +66,6 @@ const getAVLocateRequest = (blockElement: HTMLElement) => {if (!request) {request = queuedLocateRequests.get(blockElement.dataset.nodeId);if (request) {- queuedLocateRequests.delete(blockElement.dataset.nodeId);locateRequests.set(blockElement, request);}}@@ -54,7 +90,7 @@ export const prepareAVLocate = (blockElement: HTMLElement, data: IAV, resetData:pageSizes: { [key: string]: string },}) => {const request = getAVLocateRequest(blockElement);- if (!request || !data.target) {+ if (!blockElement.isConnected || !request || !data.target || data.target.itemID !== request.itemID) {return;}if (data.target.status !== "visible") {@@ -97,7 +133,11 @@ export const prepareAVLocate = (blockElement: HTMLElement, data: IAV, resetData:export const finishAVLocate = (blockElement: HTMLElement, protyle: IProtyle, data: IAV) => {const request = getAVLocateRequest(blockElement);- if (!request) {+ if (!request || !data.target || data.target.itemID !== request.itemID) {+ return;+ }+ if (!blockElement.isConnected) {+ locateRequests.delete(blockElement);return;}const currentViewID = blockElement.getAttribute(Constants.CUSTOM_SY_AV_VIEW) || request.previousViewID || data.viewID;@@ -118,6 +158,7 @@ export const finishAVLocate = (blockElement: HTMLElement, protyle: IProtyle, dat}if (data.target?.status !== "visible") {locateRequests.delete(blockElement);+ queuedLocateRequests.delete(blockElement.dataset.nodeId);return;}const groupQuery = data.target.groupID ? `.av__body[data-group-id="${data.target.groupID}"]` : ".av__body";@@ -128,7 +169,14 @@ export const finishAVLocate = (blockElement: HTMLElement, protyle: IProtyle, dat}let targetElement: HTMLElement;if (data.viewType === "table") {- targetElement = bodyElement?.querySelector(`.av__row[data-id="${request.itemID}"] .av__cell[data-dtype="block"]`) as HTMLElement;+ const rowElement = bodyElement?.querySelector(`.av__row[data-id="${request.itemID}"]`) as HTMLElement;+ targetElement = rowElement?.querySelector(".av__cell[data-dtype=\"block\"]") as HTMLElement;+ if (rowElement && request.highlight) {+ protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--hl, .av__row--hl").forEach(item => {+ item.classList.remove("protyle-wysiwyg--hl", "av__row--hl");+ });+ highlightTemporarily(blockElement, rowElement, "av__row--hl");+ }if (targetElement && request.select !== false) {clearSelect(["cell"], blockElement);targetElement.classList.add("av__cell--select");@@ -136,9 +184,13 @@ export const finishAVLocate = (blockElement: HTMLElement, protyle: IProtyle, dat}} else {targetElement = bodyElement?.querySelector(`.av__gallery-item[data-id="${request.itemID}"]`) as HTMLElement;- if (targetElement && request.select !== false) {+ if (targetElement && (request.select !== false || request.highlight)) {blockElement.querySelectorAll(".av__gallery-item--select").forEach(item => item.classList.remove("av__gallery-item--select"));- targetElement.classList.add("av__gallery-item--select");+ if (request.highlight) {+ highlightTemporarily(blockElement, targetElement, "av__gallery-item--select");+ } else {+ targetElement.classList.add("av__gallery-item--select");+ }}}if (!targetElement) {@@ -151,4 +203,5 @@ export const finishAVLocate = (blockElement: HTMLElement, protyle: IProtyle, dattargetElement.scrollIntoView({block: "center", inline: "nearest"});}locateRequests.delete(blockElement);+ queuedLocateRequests.delete(blockElement.dataset.nodeId);};diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.tsindex f9e5026648..81eab60088 100644--- a/app/src/protyle/render/av/render.ts+++ b/app/src/protyle/render/av/render.ts@@ -20,7 +20,7 @@ import {showMessage} from "../../../dialog/message";import {renderKanban} from "./kanban/render";import {bindAvSearch} from "./search";import {getBodyVirtualData, initVirtualScroll} from "./virtualScroll";-import {finishAVLocate, getAVLocateParams, prepareAVLocate, setAVLocateRequest} from "./locate";+import {beginAVRender, finishAVLocate, getAVLocateParams, isCurrentAVRender, prepareAVLocate, setAVLocateRequest} from "./locate";interface IIds {groupId: string,@@ -382,8 +382,8 @@ const afterRenderTable = (options: ITableOptions) => {if (options.cb) {options.cb(options.data);}- finishAVLocate(options.blockElement, options.protyle, options.data);if (!options.renderAll) {+ finishAVLocate(options.blockElement, options.protyle, options.data);return;}bindAvSearch({@@ -393,6 +393,7 @@ const afterRenderTable = (options: ITableOptions) => {onChange: () => updateSearch(options.blockElement, options.protyle),});initVirtualScroll(options);+ finishAVLocate(options.blockElement, options.protyle, options.data);};export const avRender = async (element: Element, protyle: IProtyle, cb?: (data: IAV) => void, renderAll = true, avData?: IAV) => {@@ -414,6 +415,7 @@ export const avRender = async (element: Element, protyle: IProtyle, cb?: (data:if (isMobile() || isInMobileApp()) {e.classList.add("av--touch");}+ const renderToken = beginAVRender(e);if (e.getAttribute("data-av-type") === "gallery") {await renderGallery({blockElement: e, protyle, cb, renderAll});@@ -533,6 +535,9 @@ export const avRender = async (element: Element, protyle: IProtyle, cb?: (data:} else {data = avData;}+ if (!isCurrentAVRender(e, renderToken)) {+ continue;+ }prepareAVLocate(e, data, resetData);if (data.viewType === "gallery") {e.setAttribute("data-av-type", data.viewType);diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex c9e47c3910..1892fe57b2 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -108,7 +108,8 @@ import {updateCalloutType} from "./callout";import {nbsp2space, removeZWJ} from "../util/normalizeText";import {setFold} from "../util/blockFold";import {BlockPanel} from "../../block/Panel";-import {isEncryptedBox} from "../../util/pathName";+import {isEncryptedBox, parseSiYuanUriInfo} from "../../util/pathName";+import {processSiYuanUri} from "../../util/uri";export class WYSIWYG {public lastHTMLs: { [key: string]: string } = {};@@ -3108,6 +3109,14 @@ export class WYSIWYG {}const blockRefElement = hasClosestByAttribute(event.target, "data-type", "block-ref");+ const siyuanURIInfo = aLink.startsWith("siyuan://blocks/") ? parseSiYuanUriInfo(aLink) : undefined;+ if (siyuanURIInfo?.avItemID && (range.toString() === "" || event.shiftKey)) {+ event.stopPropagation();+ event.preventDefault();+ hideElements(["dialog", "toolbar"], protyle);+ processSiYuanUri(protyle.app, aLink);+ return;+ }if (blockRefElement || aLink.startsWith("siyuan://blocks/")) {event.stopPropagation();event.preventDefault();diff --git a/app/src/util/uri.ts b/app/src/util/uri.tsindex 99c2314014..ceb36ce9e3 100644--- a/app/src/util/uri.ts+++ b/app/src/util/uri.ts@@ -12,7 +12,7 @@ import {openMobileFileById} from "../mobile/editor";import {isValidBazaarPackageName} from "./bazaarPackage";import type {App} from "../index";-import {queueAVLocateRequest} from "../protyle/render/av/locate";+import {activateQueuedAVLocate, queueAVLocateRequest} from "../protyle/render/av/locate";const processSiYuanUriBlocks = (app: App, uriObj: URL): boolean => {const blockInfo = parseSiYuanUriInfo(uriObj);@@ -33,11 +33,22 @@ const processSiYuanUriBlocks = (app: App, uriObj: URL): boolean => {openFileById({app,id,- action: (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],- zoomIn: zoomIn || focus+ action: blockInfo.avItemID ? [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] :+ ((zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]),+ zoomIn: blockInfo.avItemID ? false : zoomIn || focus,+ afterOpen: (model) => {+ const protyle = (model as { editor?: { protyle?: IProtyle } })?.editor?.protyle;+ if (protyle) {+ activateQueuedAVLocate(protyle, id);+ }+ },});/// #else- openMobileFileById(app, id, (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);+ if (blockInfo.avItemID) {+ activateQueuedAVLocate(window.siyuan.mobile.editor?.protyle, id);+ }+ openMobileFileById(app, id, blockInfo.avItemID ? [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] :+ ((zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]));/// #endif});/// #if !BROWSER