Skip to content

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

:bug: Keep the mobile title bar visible while editing titles https://github.com/siyuan-note/siyuan/issues/20018

Daniel3d7dd42a3b54
diff --git a/app/src/mobile/util/mobileBars.test.ts b/app/src/mobile/util/mobileBars.test.ts
index 790b5f3c19..091191bf3e 100644
--- a/app/src/mobile/util/mobileBars.test.ts
+++ b/app/src/mobile/util/mobileBars.test.ts
@@ -1,7 +1,12 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {resolve} from "node:path";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, transpileModule} from "typescript";
import {bindMobileBarsScroll, clearMobileBarsScroll} from "./mobileBars";
import {MOBILE_BARS_CONFIG_KEY} from "./mobileBarsConfig";
+import * as mobileBarsState from "./mobileBarsState";
class TestClassList {
public toggle(): boolean {
@@ -56,6 +61,97 @@ class TestScrollElement {
}
describe("mobile bars", () => {
+ for (const keyboardAlreadyOpen of [false, true]) {
+ it(`keeps the title editable when focused with the keyboard ${keyboardAlreadyOpen ? "open" : "closed"}`, () => {
+ const windowListeners = new Map<string, (event: {detail: boolean}) => void>();
+ const documentListeners = new Map<string, () => void>();
+ const classes = new Set<string>();
+ const topbarElement = new TestBreadcrumbElement();
+ const breadcrumbElement = new TestBreadcrumbElement();
+ const bottomBarElement = new TestBreadcrumbElement();
+ const scrollElement = new TestScrollElement();
+ let frame: FrameRequestCallback;
+ let finishScroll: () => void;
+ const mockDocument = {
+ activeElement: {id: "body"},
+ body: {classList: {toggle: (name: string, active: boolean) => {
+ if (active) {
+ classes.add(name);
+ } else {
+ classes.delete(name);
+ }
+ }}},
+ getElementById: (id: string) => {
+ if (id === "mobileTopBar") {
+ return topbarElement;
+ }
+ return id === "mobileBottomBar" ? bottomBarElement : undefined;
+ },
+ querySelector: () => breadcrumbElement,
+ addEventListener: (name: string, callback: () => void) => documentListeners.set(name, callback),
+ };
+ const moduleExports: Partial<typeof import("./mobileBars")> = {};
+ const source = readFileSync(resolve(process.cwd(), "src/mobile/util/mobileBars.ts"), "utf8");
+ runInNewContext(transpileModule(source, {compilerOptions: {module: ModuleKind.CommonJS}}).outputText, {
+ exports: moduleExports,
+ require: (name: string) => name === "./mobileBarsState" ? mobileBarsState :
+ {isMobileBarsAutoHide: () => true},
+ document: mockDocument,
+ window: {
+ addEventListener: (name: string, callback: (event: {detail: boolean}) => void) =>
+ windowListeners.set(name, callback),
+ setTimeout: (callback: () => void) => {
+ finishScroll = callback;
+ return 1;
+ },
+ },
+ clearTimeout: () => {},
+ requestAnimationFrame: (callback: FrameRequestCallback) => {
+ frame = callback;
+ return 1;
+ },
+ cancelAnimationFrame: () => {},
+ getSelection: (): undefined => undefined,
+ MutationObserver: class {
+ observe(): void {}
+ disconnect(): void {}
+ },
+ });
+ moduleExports.initMobileBars();
+ moduleExports.bindMobileBarsScroll(scrollElement as unknown as HTMLElement);
+ const changeKeyboard = (open: boolean) => windowListeners.get("siyuan-mobile-keyboard-change")({detail: open});
+ const assertTitleVisible = () => {
+ assert.equal(topbarElement.style.getPropertyValue("--mobile-bar-translate-y"),
+ "calc(0 * (var(--mobile-topbar-height) + var(--mobile-breadcrumb-height)))");
+ assert.equal(topbarElement.attributes.has("inert"), false);
+ assert.equal(topbarElement.attributes.get("aria-hidden"), "false");
+ assert.equal(classes.has("mobile-chrome--hidden"), false);
+ };
+ if (keyboardAlreadyOpen) {
+ changeKeyboard(true);
+ assert.equal(topbarElement.attributes.has("inert"), true);
+ }
+ mockDocument.activeElement = {id: "toolbarName"};
+ documentListeners.get("focusin")();
+ assertTitleVisible();
+ changeKeyboard(true);
+ assertTitleVisible();
+ assert.equal(bottomBarElement.attributes.has("inert"), true);
+ finishScroll();
+ scrollElement.scrollTop = 100;
+ scrollElement.onScroll();
+ frame(0);
+ assertTitleVisible();
+ mockDocument.activeElement = {id: "body"};
+ changeKeyboard(false);
+ finishScroll();
+ assertTitleVisible();
+ assert.equal(bottomBarElement.attributes.has("inert"), false);
+ changeKeyboard(true);
+ assert.equal(topbarElement.attributes.has("inert"), true);
+ });
+ }
+
it("notifies after applying the breadcrumb position", () => {
const originalDocument = globalThis.document;
const originalCancelAnimationFrame = globalThis.cancelAnimationFrame;
diff --git a/app/src/mobile/util/mobileBars.ts b/app/src/mobile/util/mobileBars.ts
index f06ba442b6..2e62fdfdd3 100644
--- a/app/src/mobile/util/mobileBars.ts
+++ b/app/src/mobile/util/mobileBars.ts
@@ -36,6 +36,10 @@ export const isMobileBlockSelecting = () => {
const isPanelOpen = () => PANEL_IDS.some((id) => Boolean(document.getElementById(id)?.style.transform));
const renderMobileBars = () => {
+ // 标题输入框位于顶栏内,编辑标题时保持栏位可见和可交互。
+ if (document.activeElement?.id === "toolbarName") {
+ barsState = reduceMobileBarsState(barsState, {type: "set-reading-bars", visible: true});
+ }
const visibility = getMobileBarsVisibility(barsState);
const immersive = !visibility.topbarVisible;
document.body.classList.toggle("mobile-chrome--hidden", immersive);
@@ -216,6 +220,11 @@ export const initMobileBars = () => {
}) as EventListener);
window.addEventListener("resize", () => pauseMobileBarsScroll());
window.visualViewport?.addEventListener("resize", () => pauseMobileBarsScroll());
+ document.addEventListener("focusin", () => {
+ if (document.activeElement?.id === "toolbarName") {
+ showMobileBars();
+ }
+ });
document.addEventListener("selectionchange", onSelectionChange);
panelObserver?.disconnect();