Skip to content

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

:bug: Prevent unintended document loading while scrolling https://github.com/siyuan-note/siyuan/issues/18383

Daniel408ce582dc00
diff --git a/app/src/protyle/scroll/event.ts b/app/src/protyle/scroll/event.ts
index 44bf01a6c3..a388ad9581 100644
--- a/app/src/protyle/scroll/event.ts
+++ b/app/src/protyle/scroll/event.ts
@@ -81,19 +81,23 @@ export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
!protyle.wysiwyg.element.firstElementChild) {
return;
}
+ 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 (protyle.scroll.lastScrollTop > element.scrollTop) {
if (element.scrollTop === 0) {
// 使用鼠标拖拽滚动条中无法准确获取 scrollTop,在此忽略
return;
}
if (element.scrollTop < element.clientHeight &&
- protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") !== "1") {
+ 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: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id"),
+ id: firstId,
mode: 1,
size: window.siyuan.config.editor.dynamicLoadBlocks,
};
@@ -111,8 +115,7 @@ export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
});
}
} else if ((element.scrollTop > element.scrollHeight - element.clientHeight * 1.8) &&
- protyle.wysiwyg.element.lastElementChild &&
- protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "2") {
+ lastElement && lastId && lastElement.getAttribute("data-eof") !== "2") {
if (protyle.scroll.lastScrollTop > 768 && element.scrollTop > protyle.scroll.lastScrollTop * 2) {
// 使用鼠标拖拽滚动条时导致加载需进行矫正
element.scrollTop = protyle.scroll.lastScrollTop;
@@ -120,7 +123,7 @@ export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
}
protyle.wysiwyg.element.setAttribute("data-top", element.scrollTop.toString());
const getDocParam: IObject = {
- id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
+ id: lastId,
mode: 2,
size: window.siyuan.config.editor.dynamicLoadBlocks,
};
diff --git a/app/src/protyle/scroll/index.ts b/app/src/protyle/scroll/index.ts
index 42d9d28e0f..6d63a00e12 100644
--- a/app/src/protyle/scroll/index.ts
+++ b/app/src/protyle/scroll/index.ts
@@ -6,6 +6,7 @@ import {hasClosestByClassName} from "../util/hasClosest";
import {goEnd, goHome} from "../wysiwyg/commonHotkey";
import {showTooltip} from "../../dialog/tooltip";
import {isEncryptedBox} from "../../util/pathName";
+import {updateScrollVisibility} from "./visibility";
export class Scroll {
public element: HTMLElement;
@@ -28,6 +29,7 @@ export class Scroll {
</div>`;
this.element = this.parentElement.querySelector(".protyle-scroll__bar");
+ this.element.classList.add("fn__none");
this.keepLazyLoad = false;
this.lastScrollTop = 0;
this.inputElement = this.element.firstElementChild as HTMLInputElement;
@@ -111,6 +113,6 @@ export class Scroll {
}
const visible = protyle.options.render.scroll && !protyle.block.showAll && protyle.block.scroll &&
!protyle.contentElement.classList.contains("fn__none");
- this.parentElement.classList.toggle("fn__none", !visible);
+ updateScrollVisibility(this.parentElement, this.element, visible);
}
}
diff --git a/app/src/protyle/scroll/visibility.test.ts b/app/src/protyle/scroll/visibility.test.ts
new file mode 100644
index 0000000000..8f7304c90d
--- /dev/null
+++ b/app/src/protyle/scroll/visibility.test.ts
@@ -0,0 +1,44 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {updateScrollVisibility} from "./visibility";
+
+const createElement = () => {
+ const classes = new Set<string>();
+ return {
+ element: {
+ classList: {
+ toggle: (name: string, force: boolean) => {
+ if (force) {
+ classes.add(name);
+ } else {
+ classes.delete(name);
+ }
+ },
+ },
+ } as unknown as HTMLElement,
+ hasClass: (name: string) => classes.has(name),
+ };
+};
+
+describe("updateScrollVisibility", () => {
+ it("keeps the container and bar hidden together", () => {
+ const parent = createElement();
+ const bar = createElement();
+
+ updateScrollVisibility(parent.element, bar.element, false);
+
+ assert.equal(parent.hasClass("fn__none"), true);
+ assert.equal(bar.hasClass("fn__none"), true);
+ });
+
+ it("shows the container and bar together", () => {
+ const parent = createElement();
+ const bar = createElement();
+ updateScrollVisibility(parent.element, bar.element, false);
+
+ updateScrollVisibility(parent.element, bar.element, true);
+
+ assert.equal(parent.hasClass("fn__none"), false);
+ assert.equal(bar.hasClass("fn__none"), false);
+ });
+});
diff --git a/app/src/protyle/scroll/visibility.ts b/app/src/protyle/scroll/visibility.ts
new file mode 100644
index 0000000000..df011d89a3
--- /dev/null
+++ b/app/src/protyle/scroll/visibility.ts
@@ -0,0 +1,4 @@
+export const updateScrollVisibility = (parentElement: HTMLElement, barElement: HTMLElement, visible: boolean) => {
+ parentElement.classList.toggle("fn__none", !visible);
+ barElement.classList.toggle("fn__none", !visible);
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index f38e027974..cac1acf728 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -2688,10 +2688,14 @@ export class WYSIWYG {
// https://ld246.com/article/1648865235549
// 不能使用上一版本的 timeStamp,否则一直滚动将导致间隔不够 https://ld246.com/article/1662852664926
if (!preventGetTopHTML && !protyle.scroll.element.classList.contains("fn__none")) {
- if (event.deltaY < 0 && protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") !== "1" &&
+ 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: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id"),
+ id: firstId,
mode: 1,
size: window.siyuan.config.editor.dynamicLoadBlocks,
};
@@ -2707,11 +2711,11 @@ export class WYSIWYG {
});
});
preventGetTopHTML = true;
- } else if (event.deltaY > 0 && protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "2" &&
+ } 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: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
+ id: lastId,
mode: 2,
size: window.siyuan.config.editor.dynamicLoadBlocks,
};