Skip to content

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

:art: Improve the mobile landscape layout https://github.com/siyuan-note/siyuan/issues/18919

Daniele2f677829d16
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index b3eda18b27..21557cf267 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -26,6 +26,24 @@ body.mobile-keyboard--open {
.protyle-breadcrumb {
-webkit-user-select: none;
user-select: none;
+
+ &__space--mobile-title {
+ display: flex;
+ align-items: center;
+ min-width: 0;
+ height: 42px;
+ padding: 0;
+ text-align: left;
+
+ .toolbar__title {
+ min-width: 0;
+ }
+
+ .toolbar__button {
+ width: 40px;
+ height: 40px;
+ }
+ }
}
.protyle-preview__title {
@@ -394,6 +412,24 @@ body.mobile-keyboard--open {
margin-bottom: -48px;
}
+body.mobile-topbar--merged {
+ .mobile-topbar {
+ height: 0;
+ min-height: 0;
+ padding: 0;
+ overflow: hidden;
+ border-bottom: 0;
+ transform: none;
+ visibility: hidden;
+ }
+
+ &.mobile-chrome--hidden #editor,
+ &.mobile-chrome--transitioning #editor {
+ margin-bottom: 0;
+ transform: none;
+ }
+}
+
.mobile-keyboard--open .mobile-bottom-bar {
opacity: 0;
pointer-events: none;
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index d57a1c1220..7dd279fdbc 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -55,6 +55,7 @@ import {initTouchDragBridge} from "../util/touchDragBridge";
import {appearanceConfigApi} from "../config/tabs/appearanceRuntime";
import {openByMobile} from "../editor/openLink";
import {initHarmonyTextSelectionMenu} from "../util/harmonyTextSelectionMenu";
+import {updateMobileTopBarLayout} from "./util/mobileTopBar";
class App {
public plugins: import("../plugin").Plugin[] = [];
@@ -167,6 +168,7 @@ class App {
// 判断手机横竖屏状态
window.matchMedia("(orientation:portrait)").addEventListener("change", () => {
updateCardHV();
+ updateMobileTopBarLayout();
activeBlur();
});
fetchPost("/api/system/getConf", {}, async (confResponse) => {
diff --git a/app/src/mobile/util/mobileBars.ts b/app/src/mobile/util/mobileBars.ts
index af76c74544..89e1960bc6 100644
--- a/app/src/mobile/util/mobileBars.ts
+++ b/app/src/mobile/util/mobileBars.ts
@@ -17,6 +17,7 @@ let programmaticScrollTimeout = 0;
let barsTransitionTimeout = 0;
let barsTransitionFrame = 0;
let barsTransitionEndHandler: ((event: TransitionEvent) => void) | undefined;
+let barsTransitionElement: HTMLElement | undefined;
let initialized = false;
const hasTextSelection = () => {
@@ -48,10 +49,11 @@ const clearBarsTransitionWatch = () => {
document.body.classList.remove("mobile-chrome--transitioning");
barsTransitionTimeout = 0;
barsTransitionFrame = 0;
- if (barsTransitionEndHandler) {
- document.getElementById("mobileTopBar")?.removeEventListener("transitionend", barsTransitionEndHandler);
- barsTransitionEndHandler = undefined;
+ if (barsTransitionEndHandler && barsTransitionElement) {
+ barsTransitionElement.removeEventListener("transitionend", barsTransitionEndHandler);
}
+ barsTransitionEndHandler = undefined;
+ barsTransitionElement = undefined;
};
const finishBarsTransition = () => {
@@ -74,19 +76,20 @@ const startBarsTransition = () => {
active: true,
scrollTop: scrollElement?.scrollTop,
});
- const topbarElement = document.getElementById("mobileTopBar");
- if (!topbarElement || window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
+ barsTransitionElement = document.body.classList.contains("mobile-topbar--merged") ?
+ document.getElementById("mobileBottomBar") : document.getElementById("mobileTopBar");
+ if (!barsTransitionElement || window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
barsTransitionFrame = requestAnimationFrame(() => {
barsTransitionFrame = requestAnimationFrame(finishBarsTransition);
});
return;
}
barsTransitionEndHandler = (event: TransitionEvent) => {
- if (event.target === topbarElement && event.propertyName === "transform") {
+ if (event.target === barsTransitionElement && event.propertyName === "transform") {
finishBarsTransition();
}
};
- topbarElement.addEventListener("transitionend", barsTransitionEndHandler);
+ barsTransitionElement.addEventListener("transitionend", barsTransitionEndHandler);
barsTransitionTimeout = window.setTimeout(finishBarsTransition, MOBILE_BARS_TRANSITION_FALLBACK);
};
diff --git a/app/src/mobile/util/mobileTopBar.test.ts b/app/src/mobile/util/mobileTopBar.test.ts
new file mode 100644
index 0000000000..647bf6c9b7
--- /dev/null
+++ b/app/src/mobile/util/mobileTopBar.test.ts
@@ -0,0 +1,110 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {updateMobileTopBarLayout} from "./mobileTopBar";
+
+class TestClassList {
+ private classes = new Set<string>();
+
+ public add(value: string) {
+ this.classes.add(value);
+ }
+
+ public remove(value: string) {
+ this.classes.delete(value);
+ }
+
+ public contains(value: string) {
+ return this.classes.has(value);
+ }
+
+ public toggle(value: string, force?: boolean) {
+ const enabled = force ?? !this.classes.has(value);
+ if (enabled) {
+ this.classes.add(value);
+ } else {
+ this.classes.delete(value);
+ }
+ return enabled;
+ }
+}
+
+class TestElement {
+ public classList = new TestClassList();
+ public parentElement?: TestElement;
+ public children: TestElement[] = [];
+ public breadcrumbSpace?: TestElement;
+
+ public appendChild(element: TestElement) {
+ if (element.parentElement) {
+ element.parentElement.children = element.parentElement.children.filter((item) => item !== element);
+ }
+ element.parentElement = this;
+ this.children.push(element);
+ }
+
+ public querySelector(selector: string) {
+ return selector === ".protyle-breadcrumb__space" ? this.breadcrumbSpace : undefined;
+ }
+}
+
+describe("mobile top bar layout", () => {
+ it("moves title controls into the landscape breadcrumb and restores them in portrait", () => {
+ const originalDocument = globalThis.document;
+ const originalWindow = globalThis.window;
+ const bodyElement = new TestElement();
+ const topBarElement = new TestElement();
+ const editorElement = new TestElement();
+ const breadcrumbSpace = new TestElement();
+ const toolbarName = new TestElement();
+ const toolbarNameReadonly = new TestElement();
+ const toolbarSync = new TestElement();
+ const elements = new Map<string, TestElement>([
+ ["mobileTopBar", topBarElement],
+ ["editor", editorElement],
+ ["toolbarName", toolbarName],
+ ["toolbarNameReadonly", toolbarNameReadonly],
+ ["toolbarSync", toolbarSync],
+ ]);
+ editorElement.breadcrumbSpace = breadcrumbSpace;
+ topBarElement.appendChild(toolbarName);
+ topBarElement.appendChild(toolbarNameReadonly);
+ topBarElement.appendChild(toolbarSync);
+ let landscape = true;
+
+ Object.defineProperty(globalThis, "document", {
+ configurable: true,
+ value: {
+ body: bodyElement,
+ getElementById: (id: string) => elements.get(id),
+ },
+ });
+ Object.defineProperty(globalThis, "window", {
+ configurable: true,
+ value: {
+ matchMedia: () => ({matches: landscape}),
+ },
+ });
+
+ try {
+ updateMobileTopBarLayout();
+ assert.equal(bodyElement.classList.contains("mobile-topbar--merged"), true);
+ assert.equal(breadcrumbSpace.classList.contains("protyle-breadcrumb__space--mobile-title"), true);
+ assert.deepEqual(breadcrumbSpace.children, [toolbarName, toolbarNameReadonly, toolbarSync]);
+
+ landscape = false;
+ updateMobileTopBarLayout();
+ assert.equal(bodyElement.classList.contains("mobile-topbar--merged"), false);
+ assert.equal(breadcrumbSpace.classList.contains("protyle-breadcrumb__space--mobile-title"), false);
+ assert.deepEqual(topBarElement.children, [toolbarName, toolbarNameReadonly, toolbarSync]);
+
+ landscape = true;
+ editorElement.classList.add("fn__none");
+ updateMobileTopBarLayout();
+ assert.equal(bodyElement.classList.contains("mobile-topbar--merged"), false);
+ assert.deepEqual(topBarElement.children, [toolbarName, toolbarNameReadonly, toolbarSync]);
+ } finally {
+ Object.defineProperty(globalThis, "document", {configurable: true, value: originalDocument});
+ Object.defineProperty(globalThis, "window", {configurable: true, value: originalWindow});
+ }
+ });
+});
diff --git a/app/src/mobile/util/mobileTopBar.ts b/app/src/mobile/util/mobileTopBar.ts
new file mode 100644
index 0000000000..182e76750f
--- /dev/null
+++ b/app/src/mobile/util/mobileTopBar.ts
@@ -0,0 +1,44 @@
+const MOBILE_TOP_BAR_ELEMENT_IDS = ["toolbarName", "toolbarNameReadonly", "toolbarSync"] as const;
+
+let topBarElements: HTMLElement[];
+let mergedBreadcrumbSpace: HTMLElement | undefined;
+
+const getTopBarElements = () => {
+ if (!topBarElements) {
+ topBarElements = MOBILE_TOP_BAR_ELEMENT_IDS
+ .map((id) => document.getElementById(id))
+ .filter((element): element is HTMLElement => Boolean(element));
+ }
+ return topBarElements;
+};
+
+export const updateMobileTopBarLayout = () => {
+ const topBarElement = document.getElementById("mobileTopBar");
+ const editorElement = document.getElementById("editor");
+ if (!topBarElement || !editorElement) {
+ return;
+ }
+
+ const breadcrumbSpace = editorElement.querySelector<HTMLElement>(".protyle-breadcrumb__space");
+ const merged = window.matchMedia("(orientation: landscape)").matches &&
+ !editorElement.classList.contains("fn__none") && Boolean(breadcrumbSpace);
+ const targetElement = merged ? breadcrumbSpace : topBarElement;
+ if (!targetElement) {
+ return;
+ }
+
+ if (mergedBreadcrumbSpace && mergedBreadcrumbSpace !== targetElement) {
+ mergedBreadcrumbSpace.classList.remove("protyle-breadcrumb__space--mobile-title");
+ mergedBreadcrumbSpace = undefined;
+ }
+ getTopBarElements().forEach((element) => {
+ if (element.parentElement !== targetElement) {
+ targetElement.appendChild(element);
+ }
+ });
+ if (merged && breadcrumbSpace) {
+ breadcrumbSpace.classList.add("protyle-breadcrumb__space--mobile-title");
+ mergedBreadcrumbSpace = breadcrumbSpace;
+ }
+ document.body.classList.toggle("mobile-topbar--merged", merged);
+};
diff --git a/app/src/mobile/util/setEmpty.ts b/app/src/mobile/util/setEmpty.ts
index 239ad7c60c..0be26b5b9d 100644
--- a/app/src/mobile/util/setEmpty.ts
+++ b/app/src/mobile/util/setEmpty.ts
@@ -7,6 +7,7 @@ import {openHistory} from "../../history/history";
import type {App} from "../../index";
import {setTitle} from "../../util/processTitle";
import {clearMobileBarsScroll} from "./mobileBars";
+import {updateMobileTopBarLayout} from "./mobileTopBar";
export const setEmpty = (app: App) => {
setTitle("", true);
@@ -15,6 +16,7 @@ export const setEmpty = (app: App) => {
document.getElementById("toolbarName").classList.add("fn__hidden");
document.getElementById("toolbarNameReadonly").classList.add("fn__hidden");
document.getElementById("editor").classList.add("fn__none");
+ updateMobileTopBarLayout();
const emptyElement = document.getElementById("empty");
emptyElement.classList.remove("fn__none");
if (emptyElement.innerHTML !== "") {
@@ -85,4 +87,5 @@ export const setEditor = () => {
document.getElementById("toolbarNameReadonly").classList.remove("fn__hidden");
document.getElementById("editor").classList.remove("fn__none");
document.getElementById("empty").classList.add("fn__none");
+ updateMobileTopBarLayout();
};