Skip to content

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

:bug: Keep mobile orientation stable when the keyboard opens https://github.com/siyuan-note/siyuan/issues/20073

Daniel08296ed2062e
diff --git a/app/src/card/util.ts b/app/src/card/util.ts
index 9a9d7f4870..c395d12dc7 100644
--- a/app/src/card/util.ts
+++ b/app/src/card/util.ts
@@ -1,6 +1,8 @@
+import {isMobileLandscape} from "../mobile/util/orientation";
+
export const updateCardHV = () => {
/// #if MOBILE
- if (window.matchMedia("(orientation:portrait)").matches) {
+ if (!isMobileLandscape()) {
document.querySelectorAll(".card__action .card__icon").forEach(item => {
item.classList.remove("fn__none");
});
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index 442c29534c..5f9f692ca1 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -69,6 +69,7 @@ import {openByMobile} from "../editor/openLink";
import {initHarmonyTextSelectionMenu} from "../util/harmonyTextSelectionMenu";
import {initMobileSelect} from "./util/nativeSelect";
import {updateMobileTopBarLayout} from "./util/mobileTopBar";
+import {bindMobileOrientationChange} from "./util/orientation";
import {showMobileBars} from "./util/mobileBars";
import {initializeEnglishCommandTranslations} from "../command/english";
import {loadLanguages} from "../boot/loadLanguages";
@@ -189,7 +190,7 @@ class App {
window.siyuan.mobile.tabs?.save();
}, false);
// 判断手机横竖屏状态
- window.matchMedia("(orientation:portrait)").addEventListener("change", () => {
+ bindMobileOrientationChange(() => {
updateCardHV();
updateMobileTopBarLayout();
showMobileBars();
diff --git a/app/src/mobile/util/keyboardOrientation.test.ts b/app/src/mobile/util/keyboardOrientation.test.ts
new file mode 100644
index 0000000000..1f5f203b38
--- /dev/null
+++ b/app/src/mobile/util/keyboardOrientation.test.ts
@@ -0,0 +1,77 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, forEachChild, isCallExpression, isVariableDeclaration, Node, ScriptTarget, transpileModule} from "typescript";
+import {isMobileLandscape} from "./orientation";
+import {getKeyboardPanelHeight} from "./keyboardPanelHeight";
+import {shouldHideKeyboardAfterResize} from "./touchSelection";
+
+const source = createSourceFile("keyboardToolbar.ts", readFileSync(join(__dirname, "keyboardToolbar.ts"), "utf8"), ScriptTarget.Latest);
+let resizeSource: string;
+let panelSource: string;
+const visit = (node: Node) => {
+ if (isCallExpression(node) && node.expression.getText(source) === "window.addEventListener" &&
+ node.arguments[0].getText(source) === '"resize"' &&
+ node.arguments[1].getText(source).includes("window.siyuan.mobile.size.isLandscape")) {
+ resizeSource = node.arguments[1].getText(source);
+ }
+ if (isVariableDeclaration(node) && node.name.getText(source) === "updateKeyboardPanelHeight") {
+ panelSource = node.initializer.getText(source);
+ }
+ forEachChild(node, visit);
+};
+visit(source);
+assert.ok(resizeSource && panelSource);
+const compile = (text: string) => transpileModule(`(${text})`, {
+ compilerOptions: {target: ScriptTarget.ES2020},
+}).outputText;
+
+const createBrowser = () => ({
+ screen: {orientation: {type: "portrait-primary"}, width: 800, height: 1200},
+ innerWidth: 800,
+ innerHeight: 1200,
+ siyuan: {mobile: {size: {isLandscape: false, portrait: {height1: 1200, height2: 1200}}}},
+});
+
+test("portrait keyboard resizing updates only the portrait height cache", () => {
+ const browser = createBrowser();
+ const onResize = runInNewContext(compile(resizeSource), {
+ window: browser,
+ isMobileLandscape: () => isMobileLandscape(browser as unknown as typeof window),
+ updateKeyboardToolbarPosition() {}, renderKeyboardToolbar() {},
+ document: {activeElement: {tagName: "DIV", isContentEditable: true}},
+ shouldHideKeyboardAfterResize, hasRecentAndroidTableCellSelectAll: () => false,
+ activeBlur: () => assert.fail("keyboard-only resizing must keep editor focus"), preventRender: false,
+ });
+ for (const height of [450, 1200, 450]) {
+ browser.innerHeight = height;
+ onResize();
+ assert.equal(browser.siyuan.mobile.size.isLandscape, false);
+ assert.equal("landscape" in browser.siyuan.mobile.size, false);
+ assert.equal(browser.siyuan.mobile.size.portrait.height1, 1200);
+ assert.equal(browser.siyuan.mobile.size.portrait.height2, 450);
+ }
+});
+
+test("a keyboard panel survives viewport aspect changes but closes on physical rotation", () => {
+ const browser = createBrowser();
+ let closed = 0;
+ const toolbar = {style: {height: ""}, querySelector: () => ({clientHeight: 48})};
+ const update = runInNewContext(compile(panelSource), {
+ keyboardPanelTop: 400, keyboardPanelLandscape: false, keyboardPanelClosing: false,
+ isMobileLandscape: () => isMobileLandscape(browser as unknown as typeof window),
+ hideKeyboardToolbarUtil: () => closed++,
+ document: {getElementById: () => toolbar},
+ getKeyboardPanelHeight, getKeyboardViewportBottom: () => browser.innerHeight,
+ isInMobileApp: () => false, getCurrentEditor: (): null => null,
+ });
+ browser.innerHeight = 450;
+ update();
+ assert.equal(closed, 0);
+ assert.equal(toolbar.style.height, "50px");
+ browser.screen.orientation.type = "landscape-primary";
+ update();
+ assert.equal(closed, 1);
+});
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 0b8f31ee57..8a936629b6 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -83,6 +83,7 @@ import {applyMobileToolbarEntries} from "./toolbarEntries";
import {getEntryOrder, isEntryVisible} from "../../config/entryVisibility/runtime";
import {MOBILE_TOOLBAR_NAMES, TOOLBAR_ENTRY_ROOT_PATH} from "../../protyle/toolbar/defaults";
import {getKeyboardPanelHeight} from "./keyboardPanelHeight";
+import {isMobileLandscape} from "./orientation";
import {restoreGutterBySelection} from "../../protyle/gutter/restore";
import {mountLiteSlashMenu} from "./liteSlashMenu";
import {getTableCellRichContext} from "../../protyle/util/tableCellRichContext";
@@ -435,7 +436,7 @@ const updateKeyboardPanelHeight = () => {
if (keyboardPanelTop === undefined) {
return;
}
- if (keyboardPanelLandscape !== window.matchMedia("(orientation: landscape)").matches) {
+ if (keyboardPanelLandscape !== isMobileLandscape()) {
keyboardPanelClosing = false;
hideKeyboardToolbarUtil();
return;
@@ -1412,7 +1413,7 @@ export const initKeyboardToolbar = () => {
}
renderKeyboardToolbar();
}, false);
- window.siyuan.mobile.size.isLandscape = window.matchMedia && window.matchMedia("(orientation: landscape)").matches;
+ window.siyuan.mobile.size.isLandscape = isMobileLandscape();
if (window.siyuan.mobile.size.isLandscape) {
window.siyuan.mobile.size.landscape = {
height1: window.innerHeight,
@@ -1428,7 +1429,7 @@ export const initKeyboardToolbar = () => {
window.addEventListener("resize", () => {
updateKeyboardToolbarPosition();
// 获取键盘高度
- window.siyuan.mobile.size.isLandscape = window.matchMedia && window.matchMedia("(orientation: landscape)").matches;
+ window.siyuan.mobile.size.isLandscape = isMobileLandscape();
if (window.siyuan.mobile.size.isLandscape) {
if (!window.siyuan.mobile.size.landscape) {
window.siyuan.mobile.size.landscape = {
diff --git a/app/src/mobile/util/mobileTopBar.test.ts b/app/src/mobile/util/mobileTopBar.test.ts
index 24679d2ad3..643d8545d8 100644
--- a/app/src/mobile/util/mobileTopBar.test.ts
+++ b/app/src/mobile/util/mobileTopBar.test.ts
@@ -75,7 +75,13 @@ describe("mobile top bar layout", () => {
topBarElement.appendChild(toolbarNameReadonly);
topBarElement.appendChild(toolbarSync);
topBarElement.appendChild(toolbarSidebarRight);
- let landscape = true;
+ const screen = {orientation: {type: "landscape-primary"}, width: 1200, height: 800};
+ const browser = {
+ screen,
+ innerWidth: 1200,
+ innerHeight: 800,
+ matchMedia: () => ({matches: browser.innerWidth > browser.innerHeight}),
+ };
Object.defineProperty(globalThis, "document", {
configurable: true,
@@ -86,9 +92,7 @@ describe("mobile top bar layout", () => {
});
Object.defineProperty(globalThis, "window", {
configurable: true,
- value: {
- matchMedia: () => ({matches: landscape}),
- },
+ value: browser,
});
try {
@@ -109,13 +113,27 @@ describe("mobile top bar layout", () => {
assert.equal(replacementBreadcrumbSpace.classList.contains("protyle-breadcrumb__space--mobile-title"), true);
assert.deepEqual(replacementBreadcrumbSpace.children, [toolbarSidebarLeft, toolbarName, toolbarNameReadonly, toolbarSync, toolbarSidebarRight]);
- landscape = false;
+ screen.orientation.type = "portrait-primary";
+ screen.width = 800;
+ screen.height = 1200;
+ browser.innerWidth = 800;
+ browser.innerHeight = 1200;
updateMobileTopBarLayout();
assert.equal(bodyElement.classList.contains("mobile-topbar--merged"), false);
assert.equal(replacementBreadcrumbSpace.classList.contains("protyle-breadcrumb__space--mobile-title"), false);
assert.deepEqual(topBarElement.children, [toolbarSidebarLeft, toolbarName, toolbarNameReadonly, toolbarSync, toolbarSidebarRight]);
- landscape = true;
+ // 软键盘压缩视口后媒体查询会变成横屏,但屏幕方向和标题栏布局保持竖屏。
+ for (const height of [450, 1200, 450]) {
+ browser.innerHeight = height;
+ updateMobileTopBarLayout();
+ assert.equal(bodyElement.classList.contains("mobile-topbar--merged"), false);
+ assert.deepEqual(topBarElement.children, [toolbarSidebarLeft, toolbarName, toolbarNameReadonly, toolbarSync, toolbarSidebarRight]);
+ }
+
+ screen.orientation.type = "landscape-primary";
+ updateMobileTopBarLayout();
+ assert.equal(bodyElement.classList.contains("mobile-topbar--merged"), true);
editorElement.classList.add("fn__none");
updateMobileTopBarLayout();
assert.equal(bodyElement.classList.contains("mobile-topbar--merged"), false);
diff --git a/app/src/mobile/util/mobileTopBar.ts b/app/src/mobile/util/mobileTopBar.ts
index 0e114b2128..e12c9b63ec 100644
--- a/app/src/mobile/util/mobileTopBar.ts
+++ b/app/src/mobile/util/mobileTopBar.ts
@@ -1,3 +1,5 @@
+import {isMobileLandscape} from "./orientation";
+
const MOBILE_TOP_BAR_ELEMENT_IDS = ["toolbarSidebarLeft", "toolbarName", "toolbarNameReadonly", "toolbarSync", "toolbarSidebarRight"] as const;
let topBarElements: HTMLElement[];
@@ -37,7 +39,7 @@ export const updateMobileTopBarLayout = () => {
}
const breadcrumbSpace = editorElement.querySelector<HTMLElement>(".protyle-breadcrumb__space");
- const merged = window.matchMedia("(orientation: landscape)").matches &&
+ const merged = isMobileLandscape() &&
!editorElement.classList.contains("fn__none") && Boolean(breadcrumbSpace);
const targetElement = merged ? breadcrumbSpace : topBarElement;
if (!targetElement) {
diff --git a/app/src/mobile/util/orientation.test.ts b/app/src/mobile/util/orientation.test.ts
new file mode 100644
index 0000000000..daf6aeb659
--- /dev/null
+++ b/app/src/mobile/util/orientation.test.ts
@@ -0,0 +1,119 @@
+import {test} from "node:test";
+import * as assert from "node:assert/strict";
+import {bindMobileOrientationChange, isMobileLandscape} from "./orientation";
+
+const createBrowser = (type = "portrait-primary") => {
+ const orientation = Object.assign(new EventTarget(), {type, angle: 0});
+ const target = Object.assign(new EventTarget(), {
+ screen: {orientation, width: 800, height: 1200},
+ navigator: {platform: "Linux armv8l", userAgent: "Android", maxTouchPoints: 5},
+ orientation: 0,
+ innerWidth: 800,
+ innerHeight: 1200,
+ });
+ return {target, browser: target as unknown as typeof window};
+};
+
+test("screen orientation stays portrait across keyboard and split-pane viewport resizing", () => {
+ const {target, browser} = createBrowser();
+ for (const [width, height] of [[800, 1200], [800, 450], [400, 300], [800, 1200]]) {
+ target.innerWidth = width;
+ target.innerHeight = height;
+ assert.equal(isMobileLandscape(browser), false);
+ }
+ target.screen.orientation.type = "landscape-primary";
+ target.innerWidth = 400;
+ target.innerHeight = 1000;
+ assert.equal(isMobileLandscape(browser), true);
+});
+
+test("orientation type takes precedence on naturally landscape devices", () => {
+ const {target, browser} = createBrowser("landscape-primary");
+ assert.equal(isMobileLandscape(browser), true);
+ target.screen.orientation.type = "portrait-primary";
+ target.screen.orientation.angle = 90;
+ target.orientation = 90;
+ assert.equal(isMobileLandscape(browser), false);
+ target.screen.orientation.type = "landscape-secondary";
+ assert.equal(isMobileLandscape(browser), true);
+ target.screen.orientation.type = "portrait-secondary";
+ assert.equal(isMobileLandscape(browser), false);
+});
+
+test("legacy iOS orientation handles phones and desktop-mode iPads", () => {
+ for (const navigator of [
+ {platform: "iPhone", userAgent: "iPhone", maxTouchPoints: 5},
+ {platform: "MacIntel", userAgent: "Macintosh", maxTouchPoints: 5},
+ ]) {
+ const {target, browser} = createBrowser("");
+ target.navigator = navigator;
+ for (const angle of [0, 90, -90, 180]) {
+ target.orientation = angle;
+ assert.equal(isMobileLandscape(browser), Math.abs(angle) === 90);
+ }
+ }
+});
+
+test("non-iOS fallback uses stable screen dimensions, not legacy angles or viewport heights", () => {
+ const {target, browser} = createBrowser("");
+ Reflect.deleteProperty(target.screen, "orientation");
+ target.orientation = 90;
+ target.innerHeight = 450;
+ assert.equal(isMobileLandscape(browser), false);
+ target.screen.width = 1200;
+ target.screen.height = 800;
+ target.orientation = 0;
+ assert.equal(isMobileLandscape(browser), true);
+});
+
+test("keyboard-only resize does not trigger rotation effects and real rotation is deduplicated", () => {
+ const {target, browser} = createBrowser();
+ let calls = 0;
+ const dispose = bindMobileOrientationChange(() => calls++, browser);
+ for (const height of [450, 1200, 450]) {
+ target.innerHeight = height;
+ target.dispatchEvent(new Event("resize"));
+ }
+ assert.equal(calls, 0);
+ target.screen.orientation.type = "portrait-secondary";
+ target.screen.orientation.dispatchEvent(new Event("change"));
+ assert.equal(calls, 0);
+ target.screen.orientation.type = "landscape-primary";
+ target.screen.orientation.dispatchEvent(new Event("change"));
+ target.dispatchEvent(new Event("orientationchange"));
+ target.dispatchEvent(new Event("resize"));
+ assert.equal(calls, 1);
+ target.screen.orientation.type = "landscape-secondary";
+ target.screen.orientation.dispatchEvent(new Event("change"));
+ assert.equal(calls, 1);
+ target.screen.orientation.type = "portrait-secondary";
+ target.dispatchEvent(new Event("resize"));
+ target.screen.orientation.dispatchEvent(new Event("change"));
+ assert.equal(calls, 2);
+ dispose();
+ target.screen.orientation.type = "landscape-secondary";
+ target.screen.orientation.dispatchEvent(new Event("change"));
+ target.dispatchEvent(new Event("orientationchange"));
+ target.dispatchEvent(new Event("resize"));
+ assert.equal(calls, 2);
+});
+
+test("legacy and screen-dimension fallback rotation events remain supported", () => {
+ const {target, browser} = createBrowser("");
+ Reflect.deleteProperty(target.screen, "orientation");
+ target.navigator.userAgent = "iPad";
+ let calls = 0;
+ const dispose = bindMobileOrientationChange(() => calls++, browser);
+ target.orientation = 90;
+ target.dispatchEvent(new Event("orientationchange"));
+ target.dispatchEvent(new Event("resize"));
+ assert.equal(calls, 1);
+ target.navigator.userAgent = "Android";
+ target.dispatchEvent(new Event("resize"));
+ assert.equal(calls, 2);
+ target.screen.width = 1200;
+ target.screen.height = 800;
+ target.dispatchEvent(new Event("resize"));
+ assert.equal(calls, 3);
+ dispose();
+});
diff --git a/app/src/mobile/util/orientation.ts b/app/src/mobile/util/orientation.ts
new file mode 100644
index 0000000000..2605622f2f
--- /dev/null
+++ b/app/src/mobile/util/orientation.ts
@@ -0,0 +1,38 @@
+import {isIOSPlatform} from "../../protyle/util/browserCompatibility";
+
+export const isMobileLandscape = (target = window) => {
+ const orientationType = target.screen.orientation?.type;
+ if (orientationType?.startsWith("landscape")) {
+ return true;
+ }
+ if (orientationType?.startsWith("portrait")) {
+ return false;
+ }
+ // 旧版 iOS 的角度以竖屏为基准;其他设备的自然方向可能是横屏,不能通用角度判断。
+ if (isIOSPlatform(target.navigator) && typeof target.orientation === "number") {
+ return Math.abs(target.orientation) === 90;
+ }
+ // 使用不受软键盘影响的屏幕尺寸,避免把编辑视口缩短误判为旋转。
+ return target.screen.width > target.screen.height;
+};
+
+export const bindMobileOrientationChange = (onChange: () => void, target = window) => {
+ let landscape = isMobileLandscape(target);
+ const onOrientationChange = () => {
+ const nextLandscape = isMobileLandscape(target);
+ if (nextLandscape === landscape) {
+ return;
+ }
+ landscape = nextLandscape;
+ onChange();
+ };
+ const orientation = target.screen.orientation;
+ orientation?.addEventListener?.("change", onOrientationChange);
+ target.addEventListener("orientationchange", onOrientationChange);
+ target.addEventListener("resize", onOrientationChange);
+ return () => {
+ orientation?.removeEventListener?.("change", onOrientationChange);
+ target.removeEventListener("orientationchange", onOrientationChange);
+ target.removeEventListener("resize", onOrientationChange);
+ };
+};