An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Refine dynamic document loading https://github.com/siyuan-note/siyuan/issues/12696
Daniel03434e92300a
diff --git a/app/src/config/tabs/editorRuntime.ts b/app/src/config/tabs/editorRuntime.tsindex f470ffe845..b73d7225c7 100644--- a/app/src/config/tabs/editorRuntime.ts+++ b/app/src/config/tabs/editorRuntime.ts@@ -6,7 +6,7 @@ import {resize} from "../../protyle/util/resize";import {createConfigNamespaceApi} from "../util/namespaceApi";const applyEditorConfig = (data: Config.IEditor) => {- const refreshKeepLazyLoad = window.siyuan.config.editor.keepLazyLoad !== data.keepLazyLoad;+ const refreshKeepLoadedContent = window.siyuan.config.editor.keepLoadedContent !== data.keepLoadedContent;const refreshDatabaseRowLayout = window.siyuan.config.editor.fullWidth !== data.fullWidth;const refreshHeadingNumbers = window.siyuan.config.editor.headingNumber !== data.headingNumber ||window.siyuan.config.editor.headingNumberFormat !== data.headingNumberFormat;@@ -25,8 +25,8 @@ const applyEditorConfig = (data: Config.IEditor) => {}getAllEditor().forEach((editorItem) => {const protyle = editorItem.protyle;- if (refreshKeepLazyLoad) {- protyle.scroll.keepLazyLoad = data.keepLazyLoad;+ if (refreshKeepLoadedContent) {+ protyle.scroll.keepLoadedContent = data.keepLoadedContent;}protyle.databaseAttributePanel?.updateDisplayConfig();reloadProtyle(protyle, false);diff --git a/app/src/config/tabs/editorTab.ts b/app/src/config/tabs/editorTab.tsindex e7c5cb5906..f8ccc7165b 100644--- a/app/src/config/tabs/editorTab.ts+++ b/app/src/config/tabs/editorTab.ts@@ -54,7 +54,7 @@ const registerEditorBehaviorGroup = (tab: SettingTabBuilder) => {title: window.siyuan.languages.pasteURLAutoConvert,desc: window.siyuan.languages.pasteURLAutoConvertTip,});- group.switch("editor.keepLazyLoad", {+ group.switch("editor.keepLoadedContent", {title: window.siyuan.languages.keepLazyLoad,desc: window.siyuan.languages.keepLazyLoadTip,});diff --git a/app/src/protyle/breadcrumb/index.ts b/app/src/protyle/breadcrumb/index.tsindex 7c0492838d..29221eb7ab 100644--- a/app/src/protyle/breadcrumb/index.ts+++ b/app/src/protyle/breadcrumb/index.ts@@ -801,10 +801,10 @@ ${padHTML}window.siyuan.menus.menu.append(new MenuItem({id: "keepLazyLoad",icon: "iconKeepContent",- current: protyle.scroll.keepLazyLoad,+ current: protyle.scroll.keepLoadedContent,label: window.siyuan.languages.keepLazyLoad,click: () => {- protyle.scroll.keepLazyLoad = !protyle.scroll.keepLazyLoad;+ protyle.scroll.keepLoadedContent = !protyle.scroll.keepLoadedContent;}}).element);}diff --git a/app/src/protyle/scroll/index.ts b/app/src/protyle/scroll/index.tsindex 8a2b7349a1..7b5e24f194 100644--- a/app/src/protyle/scroll/index.ts+++ b/app/src/protyle/scroll/index.ts@@ -8,8 +8,8 @@ import {showTooltip} from "../../dialog/tooltip";import {isEncryptedBox} from "../../util/pathName";import {updateScrollVisibility} from "./visibility";import {hideMessage, showMessage} from "../../dialog/message";-import {isDocumentBoundaryLoaded} from "../util/documentRange";-import {loadUntilDocumentBoundary} from "./loadAll";+import {isDocumentBoundaryLoaded, markDocumentBoundaryLoaded} from "../util/documentRange";+import {isDocumentBlockCountCovered, loadUntilDocumentBoundary} from "./loadAll";import {DynamicLoadState,type IDynamicLoadRequest,@@ -25,7 +25,7 @@ export class Scroll {private dynamicLoadFinish?: (success: boolean) => void;private loadingAll = false;public lastScrollTop: number;- public keepLazyLoad: boolean; // 保持加载内容+ public keepLoadedContent: boolean; // 保持加载内容constructor(protyle: IProtyle) {this.parentElement = document.createElement("div");@@ -42,7 +42,7 @@ export class Scroll {this.element = this.parentElement.querySelector(".protyle-scroll__bar");this.element.classList.add("fn__none");- this.keepLazyLoad = window.siyuan.config.editor.keepLazyLoad;+ this.keepLoadedContent = window.siyuan.config.editor.keepLoadedContent;this.lastScrollTop = 0;this.inputElement = this.element.firstElementChild as HTMLInputElement;this.inputElement.addEventListener("input", () => {@@ -162,7 +162,7 @@ export class Scroll {}public shouldKeepLoadedContent() {- return this.keepLazyLoad || this.loadingAll;+ return this.keepLoadedContent || this.loadingAll;}public invalidateDynamicLoad(protyle: IProtyle) {@@ -200,7 +200,14 @@ export class Scroll {return boundaryElement?.getAttribute("data-node-id");},load: () => new Promise<boolean>((resolve) => {- if (!this.loadDynamic(protyle, mode, {size, onFinish: resolve})) {+ const requestSize = Math.max(size, protyle.block.blockCount || 0);+ const onFinish = (success: boolean) => {+ if (success && isDocumentBlockCountCovered(requestSize, protyle.block.blockCount)) {+ markDocumentBoundaryLoaded(protyle.wysiwyg.element, position);+ }+ resolve(success);+ };+ if (!this.loadDynamic(protyle, mode, {size: requestSize, onFinish})) {resolve(false);}}),diff --git a/app/src/protyle/scroll/loadAll.test.ts b/app/src/protyle/scroll/loadAll.test.tsindex 4ade83fe54..03cd75b811 100644--- a/app/src/protyle/scroll/loadAll.test.ts+++ b/app/src/protyle/scroll/loadAll.test.ts@@ -1,6 +1,18 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {loadUntilDocumentBoundary} from "./loadAll";+import {isDocumentBlockCountCovered, loadUntilDocumentBoundary} from "./loadAll";++describe("isDocumentBlockCountCovered", () => {+ it("covers an unchanged or smaller document", () => {+ assert.equal(isDocumentBlockCountCovered(192, 192), true);+ assert.equal(isDocumentBlockCountCovered(192, 128), true);+ });++ it("does not cover a document that grew while loading", () => {+ assert.equal(isDocumentBlockCountCovered(192, 193), false);+ assert.equal(isDocumentBlockCountCovered(192), false);+ });+});describe("loadUntilDocumentBoundary", () => {it("does not request content when the boundary is already loaded", async () => {@@ -38,6 +50,25 @@ describe("loadUntilDocumentBoundary", () => {assert.equal(index, 2);});+ it("finishes after one request marks the boundary", async () => {+ let boundaryLoaded = false;+ let requests = 0;++ const loaded = await loadUntilDocumentBoundary({+ isCurrent: () => true,+ isBoundaryLoaded: () => boundaryLoaded,+ getBoundaryID: () => "last",+ load: async () => {+ requests++;+ boundaryLoaded = true;+ return true;+ },+ });++ assert.equal(loaded, true);+ assert.equal(requests, 1);+ });+it("stops when loading fails", async () => {const loaded = await loadUntilDocumentBoundary({isCurrent: () => true,diff --git a/app/src/protyle/scroll/loadAll.ts b/app/src/protyle/scroll/loadAll.tsindex b30acf4c7a..4c8488e013 100644--- a/app/src/protyle/scroll/loadAll.ts+++ b/app/src/protyle/scroll/loadAll.ts@@ -1,3 +1,6 @@+export const isDocumentBlockCountCovered = (requestSize: number, blockCount?: number) =>+ typeof blockCount === "number" && requestSize >= blockCount;+export const loadUntilDocumentBoundary = async (options: {isCurrent: () => boolean,isBoundaryLoaded: () => boolean,diff --git a/app/src/protyle/util/documentRange.test.ts b/app/src/protyle/util/documentRange.test.tsindex 1411f5e27e..91c8206df9 100644--- a/app/src/protyle/util/documentRange.test.ts+++ b/app/src/protyle/util/documentRange.test.ts@@ -1,6 +1,11 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {hasUnloadedDocumentBlocks, isDocumentBoundaryLoaded, updateDocumentBottomEof} from "./documentRange";+import {+ hasUnloadedDocumentBlocks,+ isDocumentBoundaryLoaded,+ markDocumentBoundaryLoaded,+ updateDocumentBottomEof+} from "./documentRange";const createWysiwygElement = (options: {firstElementEof?: string,@@ -9,14 +14,22 @@ const createWysiwygElement = (options: {initialBottomEof?: boolean,} = {}) => {let bottomEof = options.initialBottomEof || false;- const createElement = (eof?: string, index?: string) => ({- getAttribute: (name: string) => {- if (name === "data-eof") {- return eof || null;- }- return name === "data-node-index" ? index || null : null;- },- });+ const createElement = (eof?: string, index?: string) => {+ let currentEof = eof;+ return {+ getAttribute: (name: string) => {+ if (name === "data-eof") {+ return currentEof || null;+ }+ return name === "data-node-index" ? index || null : null;+ },+ setAttribute: (name: string, value: string) => {+ if (name === "data-eof") {+ currentEof = value;+ }+ },+ };+ };return {element: {firstElementChild: options.firstElementEof || options.firstElementIndex ?@@ -106,3 +119,22 @@ describe("isDocumentBoundaryLoaded", () => {assert.equal(isDocumentBoundaryLoaded(wysiwyg.element, "after"), true);});});++describe("markDocumentBoundaryLoaded", () => {+ it("marks the beginning without changing the document end", () => {+ const wysiwyg = createWysiwygElement({firstElementIndex: "10"});++ markDocumentBoundaryLoaded(wysiwyg.element, "before");++ assert.equal(isDocumentBoundaryLoaded(wysiwyg.element, "before"), true);+ assert.equal(isDocumentBoundaryLoaded(wysiwyg.element, "after"), false);+ });++ it("marks the last block and updates the document end", () => {+ const wysiwyg = createWysiwygElement({lastElementEof: "0"});++ markDocumentBoundaryLoaded(wysiwyg.element, "after");++ assert.equal(isDocumentBoundaryLoaded(wysiwyg.element, "after"), true);+ });+});diff --git a/app/src/protyle/util/documentRange.ts b/app/src/protyle/util/documentRange.tsindex ef5f74b0b4..1a3dcfe8cf 100644--- a/app/src/protyle/util/documentRange.ts+++ b/app/src/protyle/util/documentRange.ts@@ -17,6 +17,15 @@ export const isDocumentBoundaryLoaded = (wysiwygElement: HTMLElement, position:firstElement?.getAttribute("data-node-index") === "0";};+export const markDocumentBoundaryLoaded = (wysiwygElement: HTMLElement, position: "before" | "after") => {+ const boundaryElement = position === "before" ?+ wysiwygElement.firstElementChild : wysiwygElement.lastElementChild;+ boundaryElement?.setAttribute("data-eof", position === "before" ? "1" : "2");+ if (position === "after") {+ updateDocumentBottomEof(wysiwygElement);+ }+};+export const hasUnloadedDocumentBlocks = (wysiwygElement: HTMLElement, dynamicLoad: boolean) => {if (!dynamicLoad) {return false;diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 066131c13a..37da75a090 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -378,7 +378,7 @@ export class WYSIWYG {const ids: string[] = [];const hasSelectClassElement = this.element.querySelector(".protyle-wysiwyg--select");if (!hasSelectClassElement && protyle.scroll && !protyle.scroll.element.classList.contains("fn__none") &&- !protyle.scroll.keepLazyLoad &&+ !protyle.scroll.keepLoadedContent &&(startElement.getBoundingClientRect().top < -protyle.contentElement.clientHeight * 2 ||endElement.getBoundingClientRect().bottom > protyle.contentElement.clientHeight * 2)) {showMessage(window.siyuan.languages.crossKeepLazyLoad);diff --git a/app/src/types/config.d.ts b/app/src/types/config.d.tsindex 9a9d78573e..84bdc583c2 100644--- a/app/src/types/config.d.ts+++ b/app/src/types/config.d.ts@@ -585,7 +585,7 @@ declare namespace Config {/*** Whether to retain dynamically loaded content blocks*/- keepLazyLoad: boolean;+ keepLoadedContent: boolean;/*** The number of blocks loaded each time they are dynamically loaded*/diff --git a/kernel/conf/editor.go b/kernel/conf/editor.goindex dc0569cfc3..81e448ee85 100644--- a/kernel/conf/editor.go+++ b/kernel/conf/editor.go@@ -53,7 +53,7 @@ type Editor struct {ListItemDotNumberClickFocus bool `json:"listItemDotNumberClickFocus"` // 单击列表项标记聚焦FloatWindowMode int `json:"floatWindowMode"` // 浮窗触发模式,0:光标悬停,1:按住 Ctrl 悬停,2:不触发浮窗FloatWindowDelay *int `json:"floatWindowDelay"` // 浮窗悬停触发延迟,单位:毫秒,默认 620,nil 表示未设置- KeepLazyLoad bool `json:"keepLazyLoad"` // 是否保持动态加载的内容+ KeepLoadedContent bool `json:"keepLoadedContent"` // 是否保持动态加载的内容DynamicLoadBlocks int `json:"dynamicLoadBlocks"` // 块动态数,下限 48Justify bool `json:"justify"` // 是否两端对齐RTL bool `json:"rtl"` // 是否从右到左显示@@ -107,7 +107,7 @@ func NewEditor() *Editor {ListItemDotNumberClickFocus: true,FloatWindowMode: 0,FloatWindowDelay: new(620),- KeepLazyLoad: false,+ KeepLoadedContent: false,DynamicLoadBlocks: 192,Justify: false,RTL: false,