Skip to content

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

:bug: Keep mobile database headers below breadcrumbs https://github.com/siyuan-note/siyuan/issues/14542

Daniel266b709edd30
diff --git a/app/src/mobile/editor.ts b/app/src/mobile/editor.ts
index 592c49eb71..52525a0840 100644
--- a/app/src/mobile/editor.ts
+++ b/app/src/mobile/editor.ts
@@ -18,6 +18,7 @@ import {isEncryptedBox} from "../util/pathName";
import {bindMobileBarsScroll, pauseMobileBarsScroll} from "./util/mobileBars";
import {forEachPluginSubscriber} from "../plugin/EventBusCore";
import {restoreMobileTopBarLayout, updateMobileTopBarLayout} from "./util/mobileTopBar";
+import {stickyRow} from "../protyle/render/av/row";
export const getCurrentEditor = () => {
return window.siyuan.mobile.popEditor || window.siyuan.mobile.editor;
@@ -70,7 +71,12 @@ export const loadMobileFileById = (app: App, id: string, action: TProtyleAction[
}
completed = true;
updateMobileTopBarLayout();
- bindMobileBarsScroll(protyle.contentElement);
+ bindMobileBarsScroll(protyle.contentElement, () => {
+ // 面包屑位移后同步数据库吸顶位置,避免数据库使用上一帧的面包屑位置
+ protyle.wysiwyg.element.querySelectorAll(".av[data-render='true']").forEach((item: HTMLElement) => {
+ stickyRow(item, protyle.contentElement, "top");
+ });
+ });
afterOpen?.(protyle);
};
const fail = (invalid = false) => {
diff --git a/app/src/mobile/util/mobileBars.test.ts b/app/src/mobile/util/mobileBars.test.ts
new file mode 100644
index 0000000000..f9431f7542
--- /dev/null
+++ b/app/src/mobile/util/mobileBars.test.ts
@@ -0,0 +1,84 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {bindMobileBarsScroll, clearMobileBarsScroll} from "./mobileBars";
+
+class TestClassList {
+ public toggle(): boolean {
+ return false;
+ }
+}
+
+class TestStyle {
+ private values = new Map<string, string>();
+
+ public getPropertyValue(name: string): string {
+ return this.values.get(name) || "";
+ }
+
+ public setProperty(name: string, value: string): void {
+ this.values.set(name, value);
+ }
+}
+
+class TestBreadcrumbElement {
+ public style = new TestStyle();
+}
+
+class TestScrollElement {
+ public scrollTop = 0;
+
+ public addEventListener(): void {
+ // 测试无需触发滚动事件
+ }
+
+ public removeEventListener(): void {
+ // 测试无需触发滚动事件
+ }
+
+ public closest(): undefined {
+ return undefined;
+ }
+}
+
+describe("mobile bars", () => {
+ it("notifies after applying the breadcrumb position", () => {
+ const originalDocument = globalThis.document;
+ const originalCancelAnimationFrame = globalThis.cancelAnimationFrame;
+ const originalGetSelection = globalThis.getSelection;
+ const breadcrumbElement = new TestBreadcrumbElement();
+ const scrollElement = new TestScrollElement();
+ Object.defineProperty(globalThis, "document", {
+ configurable: true,
+ value: {
+ body: {classList: new TestClassList()},
+ getElementById: (): undefined => undefined,
+ querySelector: (): TestBreadcrumbElement => breadcrumbElement,
+ },
+ });
+ Object.defineProperty(globalThis, "cancelAnimationFrame", {
+ configurable: true,
+ value: (): undefined => undefined,
+ });
+ Object.defineProperty(globalThis, "getSelection", {
+ configurable: true,
+ value: (): undefined => undefined,
+ });
+
+ try {
+ let notified = 0;
+ bindMobileBarsScroll(scrollElement as unknown as HTMLElement, () => {
+ assert.equal(breadcrumbElement.style.getPropertyValue("--mobile-bar-translate-y"), "0px");
+ notified++;
+ });
+ assert.equal(notified, 1);
+ } finally {
+ clearMobileBarsScroll();
+ Object.defineProperty(globalThis, "document", {configurable: true, value: originalDocument});
+ Object.defineProperty(globalThis, "cancelAnimationFrame", {
+ configurable: true,
+ value: originalCancelAnimationFrame,
+ });
+ Object.defineProperty(globalThis, "getSelection", {configurable: true, value: originalGetSelection});
+ }
+ });
+});
diff --git a/app/src/mobile/util/mobileBars.ts b/app/src/mobile/util/mobileBars.ts
index cef33245a1..7b9d114797 100644
--- a/app/src/mobile/util/mobileBars.ts
+++ b/app/src/mobile/util/mobileBars.ts
@@ -15,6 +15,7 @@ let selectionObserver: MutationObserver | undefined;
let selectionFrame = 0;
let programmaticScrollTimeout = 0;
let initialized = false;
+let breadcrumbPositionChange: (() => void) | undefined;
const hasTextSelection = () => {
const selection = getSelection();
@@ -49,7 +50,13 @@ const renderMobileBars = () => {
topbarElement.setAttribute("aria-hidden", immersive ? "true" : "false");
}
const breadcrumbElement = document.querySelector<HTMLElement>("#editor > .protyle-breadcrumb");
- breadcrumbElement?.style.setProperty("--mobile-bar-translate-y", `${0 - barsState.readingBarsOffset}px`);
+ const breadcrumbTranslateY = `${0 - barsState.readingBarsOffset}px`;
+ const breadcrumbPositionChanged = breadcrumbElement &&
+ breadcrumbElement.style.getPropertyValue("--mobile-bar-translate-y") !== breadcrumbTranslateY;
+ breadcrumbElement?.style.setProperty("--mobile-bar-translate-y", breadcrumbTranslateY);
+ if (breadcrumbPositionChanged) {
+ breadcrumbPositionChange?.();
+ }
const bottomBarElement = document.getElementById("mobileBottomBar");
if (bottomBarElement) {
bottomBarElement.style.setProperty("--mobile-bar-translate-y", `${progress * 100}%`);
@@ -114,7 +121,8 @@ const onScroll = () => {
});
};
-export const bindMobileBarsScroll = (element: HTMLElement) => {
+export const bindMobileBarsScroll = (element: HTMLElement, onBreadcrumbPositionChange?: () => void) => {
+ breadcrumbPositionChange = onBreadcrumbPositionChange;
if (scrollElement === element) {
resetMobileBars(element.scrollTop);
updateSelectionState();
@@ -150,6 +158,7 @@ export const clearMobileBarsScroll = () => {
selectionObserver?.disconnect();
selectionObserver = undefined;
scrollElement = undefined;
+ breadcrumbPositionChange = undefined;
resetMobileBars(0);
};