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: Prevent duplicate blocks during dynamic loading https://github.com/siyuan-note/siyuan/issues/18459
Danield35696dc12b6
diff --git a/app/src/protyle/scroll/dynamicLoadState.test.ts b/app/src/protyle/scroll/dynamicLoadState.test.tsnew file mode 100644index 0000000000..62c32fafeb--- /dev/null+++ b/app/src/protyle/scroll/dynamicLoadState.test.ts@@ -0,0 +1,48 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {DynamicLoadState} from "./dynamicLoadState";++describe("DynamicLoadState", () => {+ it("allows only one active request", () => {+ const state = new DynamicLoadState();+ const request = state.begin("root", "last", 2);++ assert.ok(request);+ assert.equal(state.begin("root", "last", 2), undefined);+ assert.equal(state.begin("root", "first", 1), undefined);+ });++ it("validates the current document and boundary", () => {+ const state = new DynamicLoadState();+ const request = state.begin("root", "last", 2);++ assert.ok(request);+ assert.equal(state.isCurrent(request, "root", "last"), true);+ assert.equal(state.isCurrent(request, "other-root", "last"), false);+ assert.equal(state.isCurrent(request, "root", "other-last"), false);+ });++ it("releases the state only for the active request", () => {+ const state = new DynamicLoadState();+ const first = state.begin("root", "last", 2);++ assert.ok(first);+ assert.equal(state.finish(first), true);+ const second = state.begin("root", "next", 2);+ assert.ok(second);+ assert.equal(state.finish(first), false);+ assert.equal(state.isCurrent(second, "root", "next"), true);+ });++ it("invalidates stale responses and allows retrying", () => {+ const state = new DynamicLoadState();+ const stale = state.begin("root", "last", 2);++ assert.ok(stale);+ assert.equal(state.invalidate(), stale);+ const retry = state.begin("root", "last", 2);+ assert.ok(retry);+ assert.equal(state.isCurrent(stale, "root", "last"), false);+ assert.equal(state.isCurrent(retry, "root", "last"), true);+ });+});diff --git a/app/src/protyle/scroll/dynamicLoadState.ts b/app/src/protyle/scroll/dynamicLoadState.tsnew file mode 100644index 0000000000..5ec3466cb5--- /dev/null+++ b/app/src/protyle/scroll/dynamicLoadState.ts@@ -0,0 +1,44 @@+export type TDynamicLoadMode = 1 | 2;++export interface IDynamicLoadRequest {+ readonly token: number;+ readonly rootID: string;+ readonly anchorID: string;+ readonly mode: TDynamicLoadMode;+}++export class DynamicLoadState {+ private activeRequest?: IDynamicLoadRequest;+ private token = 0;++ public begin(rootID: string, anchorID: string, mode: TDynamicLoadMode) {+ if (this.activeRequest) {+ return undefined;+ }+ this.activeRequest = {+ token: ++this.token,+ rootID,+ anchorID,+ mode,+ };+ return this.activeRequest;+ }++ public isCurrent(request: IDynamicLoadRequest, rootID?: string, anchorID?: string | null) {+ return this.activeRequest === request && request.rootID === rootID && request.anchorID === anchorID;+ }++ public finish(request: IDynamicLoadRequest) {+ if (this.activeRequest !== request) {+ return false;+ }+ this.activeRequest = undefined;+ return true;+ }++ public invalidate() {+ const request = this.activeRequest;+ this.activeRequest = undefined;+ return request;+ }+}diff --git a/app/src/protyle/scroll/event.ts b/app/src/protyle/scroll/event.tsindex a388ad9581..22fe786006 100644--- a/app/src/protyle/scroll/event.ts+++ b/app/src/protyle/scroll/event.ts@@ -1,12 +1,9 @@import {Constants} from "../../constants";import {hideElements} from "../ui/hideElements";-import {fetchPost} from "../../util/fetch";-import {onGet} from "../util/onGet";import {isMobile} from "../../util/functions";import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest";import {stickyRow} from "../render/av/row";import {trimAVRowsSync} from "../render/av/virtualScroll";-import {isEncryptedBox} from "../../util/pathName";let getIndexTimeout: number;const avScrollPending = new WeakSet<HTMLElement>();@@ -93,26 +90,17 @@ export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {if (element.scrollTop < element.clientHeight &&firstId && firstElement.getAttribute("data-eof") !== "1") {// 禁用滚动时会产生抖动 https://ld246.com/article/1666717094418- protyle.contentElement.style.width = (protyle.contentElement.offsetWidth) + "px";- protyle.contentElement.style.overflow = "hidden";- protyle.wysiwyg.element.setAttribute("data-top", element.scrollTop.toString());- const getDocParam: IObject = {- id: firstId,- mode: 1,- size: window.siyuan.config.editor.dynamicLoadBlocks,- };- if (isEncryptedBox(protyle.notebookId)) {- getDocParam.notebook = protyle.notebookId;- }- fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {+ const clearLoadingStyle = () => {protyle.contentElement.style.overflow = "";protyle.contentElement.style.width = "";- onGet({- data: getResponse,- protyle,- action: [Constants.CB_GET_BEFORE, Constants.CB_GET_UNCHANGEID],- });- });+ };+ if (protyle.scroll.loadDynamic(protyle, 1, {+ beforeApply: clearLoadingStyle,+ onFinish: clearLoadingStyle,+ })) {+ protyle.contentElement.style.width = (protyle.contentElement.offsetWidth) + "px";+ protyle.contentElement.style.overflow = "hidden";+ }}} else if ((element.scrollTop > element.scrollHeight - element.clientHeight * 1.8) &&lastElement && lastId && lastElement.getAttribute("data-eof") !== "2") {@@ -121,22 +109,7 @@ export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {element.scrollTop = protyle.scroll.lastScrollTop;return;}- protyle.wysiwyg.element.setAttribute("data-top", element.scrollTop.toString());- const getDocParam: IObject = {- id: lastId,- mode: 2,- size: window.siyuan.config.editor.dynamicLoadBlocks,- };- if (isEncryptedBox(protyle.notebookId)) {- getDocParam.notebook = protyle.notebookId;- }- fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {- onGet({- data: getResponse,- protyle,- action: [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID],- });- });+ protyle.scroll.loadDynamic(protyle, 2);}protyle.scroll.lastScrollTop = Math.max(element.scrollTop, 0);}, {diff --git a/app/src/protyle/scroll/index.ts b/app/src/protyle/scroll/index.tsindex 6d63a00e12..ca260bb7fb 100644--- a/app/src/protyle/scroll/index.ts+++ b/app/src/protyle/scroll/index.ts@@ -7,11 +7,19 @@ import {goEnd, goHome} from "../wysiwyg/commonHotkey";import {showTooltip} from "../../dialog/tooltip";import {isEncryptedBox} from "../../util/pathName";import {updateScrollVisibility} from "./visibility";+import {+ DynamicLoadState,+ type IDynamicLoadRequest,+ type TDynamicLoadMode+} from "./dynamicLoadState";export class Scroll {public element: HTMLElement;private parentElement: HTMLElement;private inputElement: HTMLInputElement;+ private dynamicLoadState = new DynamicLoadState();+ private dynamicLoadAbortController?: AbortController;+ private dynamicLoadFinish?: () => void;public lastScrollTop: number;public keepLazyLoad: boolean; // 保持加载内容@@ -62,6 +70,83 @@ export class Scroll {}, {passive: true});}+ public loadDynamic(protyle: IProtyle, mode: TDynamicLoadMode, options?: {+ beforeApply?: () => void,+ onFinish?: () => void,+ }) {+ const anchorElement = mode === 1 ?+ protyle.wysiwyg.element.firstElementChild : protyle.wysiwyg.element.lastElementChild;+ const anchorID = anchorElement?.getAttribute("data-node-id");+ const rootID = protyle.block.rootID;+ const eof = anchorElement?.getAttribute("data-eof");+ if (!anchorID || !rootID || eof === (mode === 1 ? "1" : "2")) {+ return false;+ }+ // 同一编辑器的动态加载需要串行,避免相同边界响应被重复追加 https://github.com/siyuan-note/siyuan/issues/18459+ const request = this.dynamicLoadState.begin(rootID, anchorID, mode);+ if (!request) {+ return false;+ }++ const abortController = new AbortController();+ this.dynamicLoadAbortController = abortController;+ this.dynamicLoadFinish = options?.onFinish;+ protyle.wysiwyg.element.setAttribute("data-top", protyle.contentElement.scrollTop.toString());+ const getDocParam: IObject = {+ id: anchorID,+ mode,+ size: window.siyuan.config.editor.dynamicLoadBlocks,+ };+ if (isEncryptedBox(protyle.notebookId)) {+ getDocParam.notebook = protyle.notebookId;+ }+ void fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {+ const currentAnchor = mode === 1 ?+ protyle.wysiwyg.element.firstElementChild : protyle.wysiwyg.element.lastElementChild;+ const currentAnchorID = currentAnchor?.getAttribute("data-node-id");+ if (!protyle.element.isConnected ||+ !this.dynamicLoadState.isCurrent(request, protyle.block.rootID, currentAnchorID)) {+ return;+ }+ options?.beforeApply?.();+ onGet({+ data: getResponse,+ protyle,+ action: [+ mode === 1 ? Constants.CB_GET_BEFORE : Constants.CB_GET_APPEND,+ Constants.CB_GET_UNCHANGEID+ ],+ });+ }, undefined, undefined, abortController.signal).finally(() => {+ this.finishDynamicLoad(request, protyle);+ });+ return true;+ }++ public invalidateDynamicLoad(protyle: IProtyle) {+ if (!this.dynamicLoadState.invalidate()) {+ return;+ }+ const abortController = this.dynamicLoadAbortController;+ const onFinish = this.dynamicLoadFinish;+ this.dynamicLoadAbortController = undefined;+ this.dynamicLoadFinish = undefined;+ protyle.wysiwyg.element.removeAttribute("data-top");+ onFinish?.();+ abortController?.abort();+ }++ private finishDynamicLoad(request: IDynamicLoadRequest, protyle: IProtyle) {+ if (!this.dynamicLoadState.finish(request)) {+ return;+ }+ const onFinish = this.dynamicLoadFinish;+ this.dynamicLoadAbortController = undefined;+ this.dynamicLoadFinish = undefined;+ protyle.wysiwyg.element.removeAttribute("data-top");+ onFinish?.();+ }+private setIndex(protyle: IProtyle) {if (protyle.wysiwyg.element.getAttribute("data-top")) {return;diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.tsindex 1266cf982e..ab5a3d4d84 100644--- a/app/src/protyle/util/onGet.ts+++ b/app/src/protyle/util/onGet.ts@@ -46,6 +46,10 @@ export const onGet = (options: {if (!options.action) {options.action = [];}+ if (!options.action.includes(Constants.CB_GET_APPEND) &&+ !options.action.includes(Constants.CB_GET_BEFORE)) {+ options.protyle.scroll?.invalidateDynamicLoad(options.protyle);+ }options.protyle.wysiwyg.element.removeAttribute("data-top");if (options.data.code === 1) {// 其他报错@@ -365,36 +369,12 @@ const setHTML = (options: {protyle.contentElement.scrollHeight > 0 && // 没有激活的页签 https://github.com/siyuan-note/siyuan/issues/5255!options.action.includes(Constants.CB_GET_FOCUSFIRST) && // 防止 eof 为true https://github.com/siyuan-note/siyuan/issues/5291protyle.contentElement.scrollHeight <= protyle.contentElement.clientHeight) {- const getDocParam: IObject = {- id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),- mode: 2,- size: window.siyuan.config.editor.dynamicLoadBlocks,- };- if (isEncryptedBox(protyle.notebookId)) {- getDocParam.notebook = protyle.notebookId;- }- fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {- onGet({data: getResponse, protyle, action: [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID]});- });+ protyle.scroll.loadDynamic(protyle, 2);}// 动态滚动条拖拽到最后几个块时需多加载一点块 https://github.com/siyuan-note/siyuan/issues/16906if (options.action.includes(Constants.CB_GET_FOCUSFIRST) &&protyle.wysiwyg.element.getBoundingClientRect().top > protyle.breadcrumb.element.getBoundingClientRect().bottom) {- const getDocParam: IObject = {- id: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id"),- mode: 1,- size: window.siyuan.config.editor.dynamicLoadBlocks,- };- if (isEncryptedBox(protyle.notebookId)) {- getDocParam.notebook = protyle.notebookId;- }- fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {- onGet({- data: getResponse,- protyle,- action: [Constants.CB_GET_BEFORE, Constants.CB_GET_UNCHANGEID],- });- });+ protyle.scroll.loadDynamic(protyle, 1);}if (options.scrollAttr && !protyle.scroll.element.classList.contains("fn__none") && !protyle.element.classList.contains("fn__none")) {// 使用动态滚动条定位到最后一个块,重启后无法触发滚动事件,需要再次更新 indexdiff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex ca434592b7..8988d63f4f 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -87,7 +87,6 @@ import {popSearch} from "../../mobile/menu/search";import {copyPlainText, encodeBase64, isInIOS, isMac, isOnlyMeta, readClipboard} from "../util/compatibility";import {MenuItem} from "../../menus/Menu";import {fetchPost, fetchSyncPost} from "../../util/fetch";-import {onGet} from "../util/onGet";import {clearTableCell,deleteTableColumns,@@ -3380,55 +3379,21 @@ export class WYSIWYG {/// #endif});- let preventGetTopHTML = false;this.element.addEventListener("mousewheel", (event: WheelEvent) => {hideTooltip();// https://ld246.com/article/1648865235549- // 不能使用上一版本的 timeStamp,否则一直滚动将导致间隔不够 https://ld246.com/article/1662852664926- if (!preventGetTopHTML && !protyle.scroll.element.classList.contains("fn__none")) {+ if (!protyle.scroll.element.classList.contains("fn__none")) {const firstElement = protyle.wysiwyg.element.firstElementChild;const lastElement = protyle.wysiwyg.element.lastElementChild;const firstId = firstElement?.getAttribute("data-node-id");const lastId = lastElement?.getAttribute("data-node-id");if (event.deltaY < 0 && firstElement && firstId && firstElement.getAttribute("data-eof") !== "1" &&(protyle.contentElement.clientHeight === protyle.contentElement.scrollHeight || protyle.contentElement.scrollTop === 0)) {- const getDocParam: IObject = {- id: firstId,- mode: 1,- size: window.siyuan.config.editor.dynamicLoadBlocks,- };- if (isEncryptedBox(protyle.notebookId)) {- getDocParam.notebook = protyle.notebookId;- }- fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {- preventGetTopHTML = false;- onGet({- data: getResponse,- protyle,- action: [Constants.CB_GET_BEFORE, Constants.CB_GET_UNCHANGEID],- });- });- preventGetTopHTML = true;+ protyle.scroll.loadDynamic(protyle, 1);} else if (event.deltaY > 0 && lastElement && lastId && lastElement.getAttribute("data-eof") !== "2" &&(protyle.contentElement.clientHeight === protyle.contentElement.scrollHeight ||protyle.contentElement.clientHeight + Math.ceil(protyle.contentElement.scrollTop) >= protyle.contentElement.scrollHeight)) {- const getDocParam: IObject = {- id: lastId,- mode: 2,- size: window.siyuan.config.editor.dynamicLoadBlocks,- };- if (isEncryptedBox(protyle.notebookId)) {- getDocParam.notebook = protyle.notebookId;- }- fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {- preventGetTopHTML = false;- onGet({- data: getResponse,- protyle,- action: [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID],- });- });- preventGetTopHTML = true;+ protyle.scroll.loadDynamic(protyle, 2);}}if (event.deltaX === 0) {diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.tsindex 1f5c39071d..498b5ffa81 100644--- a/app/src/protyle/wysiwyg/remove.ts+++ b/app/src/protyle/wysiwyg/remove.ts@@ -39,10 +39,8 @@ import {hasClosestBlock, hasClosestByClassName, hasClosestByTag, isInEmbedBlock}/// #if !MOBILEimport {getAllModels} from "../../layout/getAll";/// #endif-import {fetchPost, fetchSyncPost} from "../../util/fetch";-import {onGet} from "../util/onGet";+import {fetchSyncPost} from "../../util/fetch";import {setFold} from "../util/blockFold";-import {isEncryptedBox} from "../../util/pathName";import {highlightRender} from "../render/highlightRender";import * as dayjs from "dayjs";import {mergeSameInlineElement} from "../toolbar/util";@@ -726,21 +724,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang!protyle.scroll.element.classList.contains("fn__none") &&protyle.contentElement.scrollHeight - protyle.contentElement.scrollTop < protyle.contentElement.clientHeight * 2) {- const getDocParam: IObject = {- id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),- mode: 2,- size: window.siyuan.config.editor.dynamicLoadBlocks,- };- if (isEncryptedBox(protyle.notebookId)) {- getDocParam.notebook = protyle.notebookId;- }- fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {- onGet({- data: getResponse,- protyle,- action: [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID],- });- });+ protyle.scroll.loadDynamic(protyle, 2);}}, Constants.TIMEOUT_COUNT);// 需等待滚动阻塞、后台处理完成。否则会加载已删除的内容return true;diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex 455ee812c5..a4120d063a 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -17,7 +17,7 @@ import {processRender} from "../util/processCode";import {highlightRender} from "../render/highlightRender";import {hasClosestBlock, hasClosestByAttribute, hasTopClosestByAttribute, isInEmbedBlock} from "../util/hasClosest";import {zoomOut} from "../../menus/protyle";-import {disabledProtyle, enableProtyle, onGet} from "../util/onGet";+import {disabledProtyle, enableProtyle} from "../util/onGet";/// #if !MOBILEimport {getAllModels} from "../../layout/getAll";/// #endif@@ -31,7 +31,6 @@ import {isPaidUser, needSubscribe} from "../../util/needSubscribe";import {resize} from "../util/resize";import {scrollCenter} from "../../util/highlightById";import {setFold} from "../util/blockFold";-import {isEncryptedBox} from "../../util/pathName";import {queueTransaction} from "../util/transactionQueue";import {cleanHeadingNumberHTML,@@ -1985,21 +1984,7 @@ const processFold = (operation: IOperation, protyle: IProtyle) => {!protyle.scroll.element.classList.contains("fn__none") &&protyle.contentElement.scrollHeight - protyle.contentElement.scrollTop < protyle.contentElement.clientHeight * 2 // https://github.com/siyuan-note/siyuan/issues/7785) {- const getDocParam: IObject = {- id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),- mode: 2,- size: window.siyuan.config.editor.dynamicLoadBlocks,- };- if (isEncryptedBox(protyle.notebookId)) {- getDocParam.notebook = protyle.notebookId;- }- fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {- onGet({- data: getResponse,- protyle,- action: [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID],- });- });+ protyle.scroll.loadDynamic(protyle, 2);}return;}