Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Stabilize bottom backlinks while editing https://github.com/siyuan-note/siyuan/issues/18319

Danieldcb5e612a1e7
diff --git a/app/src/editor/index.ts b/app/src/editor/index.ts
index 0351591062..34f2c5b254 100644
--- a/app/src/editor/index.ts
+++ b/app/src/editor/index.ts
@@ -149,9 +149,7 @@ export class Editor extends Model {
});
this.backlinkMutationObserver.observe(this.editor.protyle.wysiwyg.element, {
attributes: true,
- attributeFilter: ["data-eof"],
- childList: true,
- subtree: true,
+ attributeFilter: ["data-bottom-eof"],
});
this.updateBacklinkVisibility();
}
@@ -180,9 +178,8 @@ export class Editor extends Model {
if (!this.backlinkElement) {
return;
}
- const lastElement = this.editor.protyle.wysiwyg.element.lastElementChild;
const hidden = this.backlinkEmpty || (!this.editor.protyle.block.showAll && this.editor.protyle.block.scroll &&
- lastElement?.getAttribute("data-eof") !== "2");
+ !this.editor.protyle.wysiwyg.element.hasAttribute("data-bottom-eof"));
if (this.backlinkElement.classList.contains("fn__none") !== hidden || forcePadding) {
this.backlinkElement.classList.toggle("fn__none", hidden);
setPadding(this.editor.protyle);
diff --git a/app/src/protyle/util/documentRange.test.ts b/app/src/protyle/util/documentRange.test.ts
new file mode 100644
index 0000000000..e124180eb3
--- /dev/null
+++ b/app/src/protyle/util/documentRange.test.ts
@@ -0,0 +1,38 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {updateDocumentBottomEof} from "./documentRange";
+
+const createWysiwygElement = (lastElementEof?: string, initialBottomEof = false) => {
+ let bottomEof = initialBottomEof;
+ return {
+ element: {
+ lastElementChild: lastElementEof ? {
+ getAttribute: (name: string) => name === "data-eof" ? lastElementEof : null,
+ } : null,
+ toggleAttribute: (name: string, force: boolean) => {
+ if (name === "data-bottom-eof") {
+ bottomEof = force;
+ }
+ },
+ } as unknown as HTMLElement,
+ hasBottomEof: () => bottomEof,
+ };
+};
+
+describe("updateDocumentBottomEof", () => {
+ it("marks the loaded range when its last block is the document end", () => {
+ const wysiwyg = createWysiwygElement("2");
+
+ updateDocumentBottomEof(wysiwyg.element);
+
+ assert.equal(wysiwyg.hasBottomEof(), true);
+ });
+
+ it("clears the state when the loaded range no longer contains the document end", () => {
+ const wysiwyg = createWysiwygElement(undefined, true);
+
+ updateDocumentBottomEof(wysiwyg.element);
+
+ assert.equal(wysiwyg.hasBottomEof(), false);
+ });
+});
diff --git a/app/src/protyle/util/documentRange.ts b/app/src/protyle/util/documentRange.ts
new file mode 100644
index 0000000000..0cac19b97e
--- /dev/null
+++ b/app/src/protyle/util/documentRange.ts
@@ -0,0 +1,6 @@
+export const updateDocumentBottomEof = (wysiwygElement: HTMLElement) => {
+ wysiwygElement.toggleAttribute(
+ "data-bottom-eof",
+ wysiwygElement.lastElementChild?.getAttribute("data-eof") === "2"
+ );
+};
diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.ts
index c28a4cf791..e8e971e24c 100644
--- a/app/src/protyle/util/onGet.ts
+++ b/app/src/protyle/util/onGet.ts
@@ -26,6 +26,7 @@ import {
queueHeadingNumberRefresh,
renderHeadingNumbers
} from "./headingNumber";
+import {updateDocumentBottomEof} from "./documentRange";
export const onGet = (options: {
data: IWebSocketData,
@@ -64,6 +65,7 @@ export const onGet = (options: {
} else {
options.protyle.wysiwyg.element.lastElementChild.setAttribute("data-eof", "2");
}
+ updateDocumentBottomEof(options.protyle.wysiwyg.element);
if (options.data.data.mode !== 4) {
return;
}
@@ -242,6 +244,7 @@ const setHTML = (options: {
protyle.wysiwyg.element.firstElementChild : protyle.wysiwyg.element.lastElementChild;
eofElement?.setAttribute("data-eof", options.action.includes(Constants.CB_GET_BEFORE) ? "1" : "2");
}
+ updateDocumentBottomEof(protyle.wysiwyg.element);
/// #if MOBILE
protyle.wysiwyg.element.querySelectorAll("video, audio").forEach(item => {