Skip to content

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

:bug: Improve mobile block scrollbar https://github.com/siyuan-note/siyuan/issues/19034

Daniele512f37601d8
diff --git a/app/src/assets/scss/protyle/_protyle.scss b/app/src/assets/scss/protyle/_protyle.scss
index 89001e9d4d..95bd3ec4d5 100644
--- a/app/src/assets/scss/protyle/_protyle.scss
+++ b/app/src/assets/scss/protyle/_protyle.scss
@@ -45,13 +45,6 @@
}
}
- @media (hover: none) {
- &__down,
- &__up {
- opacity: 1;
- }
- }
-
&__bar {
position: absolute;
right: calc(4px - var(--b3-dynamicscroll-width) / 2);
@@ -76,6 +69,22 @@
}
}
}
+
+ @media (hover: none) {
+ &__down,
+ &__up {
+ opacity: 1;
+ }
+
+ &__bar {
+ top: calc(50% - 22px);
+
+ .b3-slider {
+ height: 44px;
+ touch-action: none;
+ }
+ }
+ }
}
.protyle-gutters {
diff --git a/app/src/protyle/scroll/event.ts b/app/src/protyle/scroll/event.ts
index 22fe786006..7154e3c77b 100644
--- a/app/src/protyle/scroll/event.ts
+++ b/app/src/protyle/scroll/event.ts
@@ -1,9 +1,10 @@
import {Constants} from "../../constants";
import {hideElements} from "../ui/hideElements";
import {isMobile} from "../../util/functions";
-import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest";
import {stickyRow} from "../render/av/row";
import {trimAVRowsSync} from "../render/av/virtualScroll";
+import {isDocumentBoundaryLoaded} from "../util/documentRange";
+import {getVisibleRootBlockID, isScrolledToBottom} from "./viewport";
let getIndexTimeout: number;
const avScrollPending = new WeakSet<HTMLElement>();
@@ -50,24 +51,30 @@ export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
if (protyle.scroll && !protyle.scroll.element.classList.contains("fn__none")) {
clearTimeout(getIndexTimeout);
getIndexTimeout = window.setTimeout(() => {
- let targetElement = document.elementFromPoint(elementRect.left + elementRect.width / 2, elementRect.top + 10);
- if (targetElement.classList.contains("protyle-wysiwyg")) {
- // 恰好定位到块的中间时
- targetElement = document.elementFromPoint(elementRect.left + elementRect.width / 2, elementRect.top + 20);
+ if (element.scrollTop <= 1 && isDocumentBoundaryLoaded(protyle.wysiwyg.element, "before")) {
+ protyle.scroll.setCurrentIndex(protyle, 1, true);
+ return;
}
- const blockElement = hasClosestBlock(targetElement);
- if (!blockElement) {
- if ((protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") === "1" ||
- // goHome 时 data-eof 不为 1
- protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-index") === "0") &&
- (hasClosestByClassName(targetElement, "protyle-background") || hasClosestByClassName(targetElement, "protyle-title"))) {
- const inputElement = protyle.scroll.element.querySelector(".b3-slider") as HTMLInputElement;
- inputElement.value = "1";
- protyle.scroll.element.setAttribute("aria-label", `Blocks 1/${protyle.block.blockCount}`);
- }
+ if (isScrolledToBottom(element.scrollTop, element.scrollHeight, element.clientHeight) &&
+ isDocumentBoundaryLoaded(protyle.wysiwyg.element, "after")) {
+ protyle.scroll.setCurrentIndex(protyle, protyle.block.blockCount, true);
return;
}
- protyle.scroll.updateIndex(protyle, blockElement.getAttribute("data-node-id"));
+ const visibleBlockID = getVisibleRootBlockID(
+ Array.from(protyle.wysiwyg.element.children).map((item) => {
+ const rect = item.getBoundingClientRect();
+ return {
+ id: item.getAttribute("data-node-id"),
+ top: rect.top,
+ bottom: rect.bottom,
+ };
+ }),
+ elementRect.top + 10,
+ elementRect.bottom,
+ );
+ if (visibleBlockID) {
+ protyle.scroll.updateIndex(protyle, visibleBlockID);
+ }
}, Constants.TIMEOUT_LOAD);
}
diff --git a/app/src/protyle/scroll/index.ts b/app/src/protyle/scroll/index.ts
index 6895012518..bbb5c8c8aa 100644
--- a/app/src/protyle/scroll/index.ts
+++ b/app/src/protyle/scroll/index.ts
@@ -15,6 +15,8 @@ import {
type IDynamicLoadRequest,
type TDynamicLoadMode
} from "./dynamicLoadState";
+import {saveScroll} from "./saveScroll";
+import {getScrollIndexFromPointer} from "./slider";
export class Scroll {
public element: HTMLElement;
@@ -23,6 +25,8 @@ export class Scroll {
private dynamicLoadState = new DynamicLoadState();
private dynamicLoadAbortController?: AbortController;
private dynamicLoadFinish?: (success: boolean) => void;
+ private indexAbortController?: AbortController;
+ private indexRequestID = 0;
private loadingAll = false;
public lastScrollTop: number;
public keepLoadedContent: boolean; // 保持加载内容
@@ -46,15 +50,67 @@ export class Scroll {
this.lastScrollTop = 0;
this.inputElement = this.element.firstElementChild as HTMLInputElement;
this.inputElement.addEventListener("input", () => {
- this.element.setAttribute("aria-label", `Blocks ${this.inputElement.value}/${protyle.block.blockCount}`);
+ this.updateLabel(protyle);
showTooltip(this.element.getAttribute("aria-label"), this.element);
});
- /// #if BROWSER
this.inputElement.addEventListener("change", () => {
this.setIndex(protyle);
});
- this.inputElement.addEventListener("touchend", () => {
+ /// #if MOBILE
+ let activePointerID: number | undefined;
+ const updatePointerIndex = (clientY: number) => {
+ const index = getScrollIndexFromPointer(
+ clientY,
+ this.inputElement.getBoundingClientRect(),
+ parseInt(this.inputElement.min),
+ parseInt(this.inputElement.max),
+ );
+ if (this.inputElement.value !== index.toString()) {
+ this.inputElement.value = index.toString();
+ this.updateLabel(protyle);
+ showTooltip(this.element.getAttribute("aria-label"), this.element);
+ }
+ };
+ this.inputElement.addEventListener("pointerdown", (event) => {
+ if (event.pointerType === "mouse") {
+ return;
+ }
+ activePointerID = event.pointerId;
+ this.inputElement.setPointerCapture(event.pointerId);
+ updatePointerIndex(event.clientY);
+ event.preventDefault();
+ });
+ this.inputElement.addEventListener("pointermove", (event) => {
+ if (activePointerID !== event.pointerId) {
+ return;
+ }
+ updatePointerIndex(event.clientY);
+ event.preventDefault();
+ });
+ const releasePointer = (event: PointerEvent) => {
+ if (activePointerID !== event.pointerId) {
+ return false;
+ }
+ if (this.inputElement.hasPointerCapture(event.pointerId)) {
+ this.inputElement.releasePointerCapture(event.pointerId);
+ }
+ activePointerID = undefined;
+ return true;
+ };
+ const finishPointer = (event: PointerEvent) => {
+ if (activePointerID !== event.pointerId) {
+ return;
+ }
+ updatePointerIndex(event.clientY);
+ releasePointer(event);
this.setIndex(protyle);
+ event.preventDefault();
+ };
+ this.inputElement.addEventListener("pointerup", finishPointer);
+ this.inputElement.addEventListener("pointercancel", (event) => {
+ if (releasePointer(event)) {
+ event.preventDefault();
+ }
});
/// #endif
this.parentElement.addEventListener("click", (event) => {
@@ -218,7 +274,8 @@ export class Scroll {
if (protyle.wysiwyg.element.getAttribute("data-top")) {
return;
}
- protyle.wysiwyg.element.setAttribute("data-top", protyle.wysiwyg.element.scrollTop.toString());
+ this.cancelIndexRequest();
+ protyle.wysiwyg.element.setAttribute("data-top", protyle.contentElement.scrollTop.toString());
protyle.contentElement.style.overflow = "hidden";
const getDocParam: IObject = {
index: parseInt(this.inputElement.value),
@@ -234,10 +291,12 @@ export class Scroll {
data: getResponse,
protyle,
action: [Constants.CB_GET_FOCUSFIRST, Constants.CB_GET_UNCHANGEID],
+ suppressFocus: true,
afterCB: () => {
setTimeout(() => {
protyle.contentElement.style.overflow = "";
}, Constants.TIMEOUT_INPUT); // 需和 onGet 中的 preventScroll 保持一致
+ void saveScroll(protyle);
showTooltip(this.element.getAttribute("aria-label"), this.element);
}
});
@@ -245,16 +304,26 @@ export class Scroll {
}
public updateIndex(protyle: IProtyle, id: string, cb?: (index: number) => void) {
- fetchPost("/api/block/getBlockIndex", {id}, (response) => {
+ const requestID = ++this.indexRequestID;
+ this.indexAbortController?.abort();
+ const abortController = new AbortController();
+ this.indexAbortController = abortController;
+ const rootID = protyle.block.rootID;
+ void fetchPost("/api/block/getBlockIndex", {id}, (response) => {
+ if (requestID !== this.indexRequestID || rootID !== protyle.block.rootID) {
+ return;
+ }
if (!response.data) {
return;
}
- const inputElement = protyle.scroll.element.querySelector(".b3-slider") as HTMLInputElement;
- inputElement.value = response.data;
- protyle.scroll.element.setAttribute("aria-label", `Blocks ${response.data}/${protyle.block.blockCount}`);
+ protyle.scroll.setCurrentIndex(protyle, response.data);
if (cb) {
cb(response.data);
}
+ }, undefined, undefined, abortController.signal).finally(() => {
+ if (this.indexAbortController === abortController) {
+ this.indexAbortController = undefined;
+ }
});
}
@@ -268,4 +337,22 @@ export class Scroll {
const barVisible = containerVisible && !protyle.block.showAll && protyle.block.scroll;
updateScrollVisibility(this.parentElement, this.element, containerVisible, barVisible);
}
+
+ public setCurrentIndex(protyle: IProtyle, index: number, cancelPending = false) {
+ if (cancelPending) {
+ this.cancelIndexRequest();
+ }
+ this.inputElement.value = Math.max(1, Math.min(protyle.block.blockCount, index)).toString();
+ this.updateLabel(protyle);
+ }
+
+ private cancelIndexRequest() {
+ this.indexRequestID++;
+ this.indexAbortController?.abort();
+ this.indexAbortController = undefined;
+ }
+
+ private updateLabel(protyle: IProtyle) {
+ this.element.setAttribute("aria-label", `Blocks ${this.inputElement.value}/${protyle.block.blockCount}`);
+ }
}
diff --git a/app/src/protyle/scroll/slider.test.ts b/app/src/protyle/scroll/slider.test.ts
new file mode 100644
index 0000000000..c068f87f45
--- /dev/null
+++ b/app/src/protyle/scroll/slider.test.ts
@@ -0,0 +1,22 @@
+import {strict as assert} from "node:assert";
+import {describe, it} from "node:test";
+import {getScrollIndexFromPointer} from "./slider";
+
+describe("getScrollIndexFromPointer", () => {
+ const rect = {top: 100, height: 200} as DOMRect;
+
+ it("maps the vertical touch position to a block index", () => {
+ assert.equal(getScrollIndexFromPointer(100, rect, 1, 101), 1);
+ assert.equal(getScrollIndexFromPointer(200, rect, 1, 101), 51);
+ assert.equal(getScrollIndexFromPointer(300, rect, 1, 101), 101);
+ });
+
+ it("clamps positions outside the slider track", () => {
+ assert.equal(getScrollIndexFromPointer(0, rect, 1, 101), 1);
+ assert.equal(getScrollIndexFromPointer(400, rect, 1, 101), 101);
+ });
+
+ it("keeps the only available index", () => {
+ assert.equal(getScrollIndexFromPointer(200, rect, 1, 1), 1);
+ });
+});
diff --git a/app/src/protyle/scroll/slider.ts b/app/src/protyle/scroll/slider.ts
new file mode 100644
index 0000000000..93743a6d89
--- /dev/null
+++ b/app/src/protyle/scroll/slider.ts
@@ -0,0 +1,12 @@
+export const getScrollIndexFromPointer = (
+ clientY: number,
+ rect: Pick<DOMRect, "top" | "height">,
+ min: number,
+ max: number,
+) => {
+ if (max <= min || rect.height <= 0) {
+ return min;
+ }
+ const ratio = Math.min(1, Math.max(0, (clientY - rect.top) / rect.height));
+ return Math.round(min + (max - min) * ratio);
+};
diff --git a/app/src/protyle/scroll/viewport.test.ts b/app/src/protyle/scroll/viewport.test.ts
new file mode 100644
index 0000000000..96ee158064
--- /dev/null
+++ b/app/src/protyle/scroll/viewport.test.ts
@@ -0,0 +1,28 @@
+import {strict as assert} from "node:assert";
+import {describe, it} from "node:test";
+import {getVisibleRootBlockID, isScrolledToBottom} from "./viewport";
+
+describe("getVisibleRootBlockID", () => {
+ const blocks = [
+ {id: "first", top: -100, bottom: 80},
+ {id: "second", top: 88, bottom: 240},
+ {id: "third", top: 248, bottom: 400},
+ ];
+
+ it("returns the first root block intersecting the viewport", () => {
+ assert.equal(getVisibleRootBlockID(blocks, 10, 300), "first");
+ assert.equal(getVisibleRootBlockID(blocks, 90, 300), "second");
+ });
+
+ it("ignores blocks without an id and blocks outside the viewport", () => {
+ assert.equal(getVisibleRootBlockID([{id: null, top: 0, bottom: 100}, ...blocks], 90, 240), "second");
+ assert.equal(getVisibleRootBlockID(blocks, 500, 700), undefined);
+ });
+});
+
+describe("isScrolledToBottom", () => {
+ it("allows fractional pixel differences at the bottom", () => {
+ assert.equal(isScrolledToBottom(699.5, 1000, 300), true);
+ assert.equal(isScrolledToBottom(698, 1000, 300), false);
+ });
+});
diff --git a/app/src/protyle/scroll/viewport.ts b/app/src/protyle/scroll/viewport.ts
new file mode 100644
index 0000000000..5188f410e4
--- /dev/null
+++ b/app/src/protyle/scroll/viewport.ts
@@ -0,0 +1,13 @@
+interface IViewportBlock {
+ id: string | null;
+ top: number;
+ bottom: number;
+}
+
+export const getVisibleRootBlockID = (blocks: IViewportBlock[], viewportTop: number, viewportBottom: number) => {
+ return blocks.find((block) => block.id && block.bottom > viewportTop && block.top < viewportBottom)?.id;
+};
+
+export const isScrolledToBottom = (scrollTop: number, scrollHeight: number, clientHeight: number) => {
+ return scrollHeight - scrollTop - clientHeight <= 1;
+};
diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.ts
index d6ad13b9ee..5121429faa 100644
--- a/app/src/protyle/util/onGet.ts
+++ b/app/src/protyle/util/onGet.ts
@@ -49,6 +49,7 @@ export const onGet = (options: {
dataDocType?: string,
isValid?: () => boolean,
focusAfterZoom?: boolean,
+ suppressFocus?: boolean,
}) => {
if (options.isValid && !options.isValid()) {
return;
@@ -140,7 +141,8 @@ export const onGet = (options: {
refreshHeadingNumbers,
afterCB: options.afterCB,
scrollPosition: options.scrollPosition,
- focusAfterZoom: options.focusAfterZoom
+ focusAfterZoom: options.focusAfterZoom,
+ suppressFocus: options.suppressFocus,
}, options.protyle);
removeLoading(options.protyle);
return;
@@ -158,7 +160,8 @@ export const onGet = (options: {
refreshHeadingNumbers,
afterCB: options.afterCB,
scrollPosition: options.scrollPosition,
- focusAfterZoom: options.focusAfterZoom
+ focusAfterZoom: options.focusAfterZoom,
+ suppressFocus: options.suppressFocus,
}, options.protyle);
removeLoading(options.protyle);
return;
@@ -189,7 +192,8 @@ export const onGet = (options: {
refreshHeadingNumbers,
afterCB: options.afterCB,
scrollPosition: options.scrollPosition,
- focusAfterZoom: options.focusAfterZoom
+ focusAfterZoom: options.focusAfterZoom,
+ suppressFocus: options.suppressFocus,
}, options.protyle);
removeLoading(options.protyle);
};
@@ -220,6 +224,7 @@ const setHTML = (options: {
refreshHeadingNumbers?: boolean,
afterCB?: () => void,
focusAfterZoom?: boolean,
+ suppressFocus?: boolean,
}, protyle: IProtyle) => {
if (protyle.contentElement.classList.contains("fn__none") && protyle.wysiwyg.element.innerHTML !== "") {
return;
@@ -371,7 +376,8 @@ const setHTML = (options: {
}
}
- focusElementById(protyle, options.action, options.scrollAttr, options.scrollPosition, options.focusAfterZoom);
+ focusElementById(protyle, options.action, options.scrollAttr, options.scrollPosition,
+ options.focusAfterZoom, options.suppressFocus);
if (options.action.includes(Constants.CB_GET_SETID) ||
(protyle.model && !protyle.block.showAll)) {
@@ -543,7 +549,7 @@ export const enableProtyle = (protyle: IProtyle) => {
};
const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScrollAttr,
- scrollPosition?: ScrollLogicalPosition, focusAfterZoom = false) => {
+ scrollPosition?: ScrollLogicalPosition, focusAfterZoom = false, suppressFocus = false) => {
let focusElement: Element;
if (scrollAttr && scrollAttr.focusId) {
focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`);
@@ -569,7 +575,7 @@ const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScr
preventScroll(protyle); // 搜索页签滚动会导致再次请求
bgFade(focusElement);
}
- if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_FOCUSFIRST)) {
+ if (!suppressFocus && (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_FOCUSFIRST))) {
setTimeout(() => {
let range: Range;
if (hasFocusOffsets(scrollAttr)) {