Skip to content

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

:bug: Remember mobile sidebar selections after first opening https://github.com/siyuan-note/siyuan/issues/19704

Danielac9183f37f1e
diff --git a/app/guide/20210808180117-6v0mkxr/20240113110040-7sgw8kl.sy b/app/guide/20210808180117-6v0mkxr/20240113110040-7sgw8kl.sy
index a818104f2d..5cf6da9afe 100644
--- a/app/guide/20210808180117-6v0mkxr/20240113110040-7sgw8kl.sy
+++ b/app/guide/20210808180117-6v0mkxr/20240113110040-7sgw8kl.sy
@@ -7,7 +7,7 @@
"id": "20240113110040-7sgw8kl",
"title": "Mobile App",
"type": "doc",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
@@ -16,7 +16,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20240113110114-xav9o1n",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
@@ -62,12 +62,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260921090531-lx0v0bv",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
"Type": "NodeText",
- "Data": "Swipe right in the editor to open the left sidebar, or swipe left to open the right sidebar. Each gesture opens the first visible feature on that side. Change the feature order in "
+ "Data": "Swipe right in the editor to open the left sidebar, or swipe left to open the right sidebar. After starting the app, each sidebar initially opens its first visible feature. Subsequent openings restore the last used feature on that side. Change the feature order in "
},
{
"Type": "NodeTextMark",
diff --git a/app/guide/20210808180117-czj9bvb/20240113102857-c63dmo5.sy b/app/guide/20210808180117-czj9bvb/20240113102857-c63dmo5.sy
index 53892c2c5c..eabb5c4d93 100644
--- a/app/guide/20210808180117-czj9bvb/20240113102857-c63dmo5.sy
+++ b/app/guide/20210808180117-czj9bvb/20240113102857-c63dmo5.sy
@@ -7,7 +7,7 @@
"id": "20240113102857-c63dmo5",
"title": "移动端 App",
"type": "doc",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
@@ -16,7 +16,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20240113102857-aof1w7f",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
@@ -62,12 +62,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260921090531-bcyyegw",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
"Type": "NodeText",
- "Data": "在编辑器中向右滑动可打开左侧栏,向左滑动可打开右侧栏,每次滑动都会打开该侧首个可见功能。可在 "
+ "Data": "在编辑器中向右滑动可打开左侧栏,向左滑动可打开右侧栏。应用启动后,首次打开侧栏时显示该侧首个可见功能,之后再次打开会恢复该侧上次使用的功能。可在 "
},
{
"Type": "NodeTextMark",
diff --git a/app/guide/20211226090932-5lcq56f/20240113110500-dz2ae4n.sy b/app/guide/20211226090932-5lcq56f/20240113110500-dz2ae4n.sy
index 40c5480aa2..a41f82305d 100644
--- a/app/guide/20211226090932-5lcq56f/20240113110500-dz2ae4n.sy
+++ b/app/guide/20211226090932-5lcq56f/20240113110500-dz2ae4n.sy
@@ -7,7 +7,7 @@
"id": "20240113110500-dz2ae4n",
"title": "行動端 App",
"type": "doc",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
@@ -16,7 +16,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20240113110513-8s7tp6d",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
@@ -62,12 +62,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260921090531-u48uhj6",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
"Type": "NodeText",
- "Data": "在編輯器中向右滑動可開啟左側欄,向左滑動可開啟右側欄,每次滑動都會開啟該側第一個可見功能。可在 "
+ "Data": "在編輯器中向右滑動可開啟左側欄,向左滑動可開啟右側欄。應用程式啟動後,首次開啟側欄時顯示該側第一個可見功能,之後再次開啟會恢復該側上次使用的功能。可在 "
},
{
"Type": "NodeTextMark",
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-e6z5okf.sy b/app/guide/20240530133126-axarxgx/20240530101000-e6z5okf.sy
index f334f6dfbc..e72edf6f15 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-e6z5okf.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-e6z5okf.sy
@@ -8,7 +8,7 @@
"id": "20240530101000-e6z5okf",
"title": "モバイルアプリ",
"type": "doc",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
@@ -18,7 +18,7 @@
"Properties": {
"ID": "20240530101000-peznsnc",
"id": "20240530101000-2nn9stf",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
@@ -46,12 +46,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260921090531-0l8vdyb",
- "updated": "20260921090531"
+ "updated": "20260921210136"
},
"Children": [
{
"Type": "NodeText",
- "Data": "エディターで右にスワイプすると左サイドバー、左にスワイプすると右サイドバーが開きます。スワイプするたびに、その側で表示されている先頭の機能が開きます。機能の順序は "
+ "Data": "エディターで右にスワイプすると左サイドバー、左にスワイプすると右サイドバーが開きます。アプリ起動後に各サイドバーを初めて開くと、表示されている先頭の機能が開きます。以降は、その側で最後に使った機能が開きます。機能の順序は "
},
{
"Type": "NodeTextMark",
diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.ts
index 1fa3043bb2..f369ac1c28 100644
--- a/app/src/mobile/util/initFramework.ts
+++ b/app/src/mobile/util/initFramework.ts
@@ -49,6 +49,9 @@ import {
import {exitSiYuan} from "../../dialog/processSystem";
import {enterDocumentFromTitle} from "../../protyle/header/titleEnter";
+// 侧栏首次使用前随布局选择默认功能,使用后保留当前页签。
+const activatedSidePanels = new WeakSet<HTMLElement>();
+
const getDockTabElement = (type: string) => {
return document.querySelector(`[data-type="${CSS.escape(`sidebar-${type}-tab`)}"]`) as HTMLElement;
};
@@ -63,6 +66,9 @@ const getDockIdFromTabElement = (element: HTMLElement) => {
};
const getActiveDockId = (sidePanelElement: HTMLElement) => {
+ if (!activatedSidePanels.has(sidePanelElement)) {
+ return;
+ }
const activeElement = sidePanelElement.firstElementChild.querySelector<HTMLElement>(
"[data-type$='-tab'].toolbar__icon--active");
return activeElement ? getDockIdFromTabElement(activeElement) : undefined;
@@ -277,6 +283,7 @@ const initSidePanelTabs = (app: App, sidePanelElement: HTMLElement) => {
if (!type) {
return;
}
+ activatedSidePanels.add(sidePanelElement);
if (svgElement.classList.contains("toolbar__icon--active")) {
if (isProgrammatic) {
updateDock(app, type, getDockContentElement(type));
diff --git a/app/src/mobile/util/mobileSidePanelLayout.test.ts b/app/src/mobile/util/mobileSidePanelLayout.test.ts
index 23ab965caf..9a1113c9eb 100644
--- a/app/src/mobile/util/mobileSidePanelLayout.test.ts
+++ b/app/src/mobile/util/mobileSidePanelLayout.test.ts
@@ -5,7 +5,7 @@ import {runInNewContext} from "node:vm";
import {ModuleKind, transpileModule} from "typescript";
import {createDefaultMobileSidePanelConfig, MOBILE_SIDE_PANEL_DOCK_IDS, normalizeMobileSidePanelConfig} from "./mobileSidePanelConfig";
-test("side panel rendering hides docks, selects a visible fallback and closes an empty panel", () => {
+test("side panel layout selects initial defaults, remembers activated tabs and falls back when hidden", () => {
const element = (type: string) => {
const classes = new Set<string>();
return {
@@ -22,9 +22,12 @@ test("side panel rendering hides docks, selects a visible fallback and closes an
type DockElement = ReturnType<typeof element>;
const panel = () => {
const children: DockElement[] = [];
+ let onClick: (event: {detail: string}) => void;
return {
style: {transform: ""},
firstElementChild: {
+ addEventListener: (_type: string, callback: typeof onClick) => onClick = callback,
+ dispatchEvent: (event: {detail: string}) => onClick(event),
firstElementChild: {append: (item: DockElement) => {
const index = children.indexOf(item);
if (index >= 0) {
@@ -33,6 +36,7 @@ test("side panel rendering hides docks, selects a visible fallback and closes an
children.push(item);
}},
querySelector: () => children.find(item => item.classList.contains("toolbar__icon--active")),
+ querySelectorAll: () => children,
},
lastElementChild: {append: (): void => undefined},
};
@@ -41,14 +45,19 @@ test("side panel rendering hides docks, selects a visible fallback and closes an
const right = panel();
let closed = 0;
let aiDisabled = false;
- const moduleExports = {} as {renderMobileSidePanelLayout: (app: unknown, config: unknown) => void};
+ const moduleExports = {} as {
+ renderMobileSidePanelLayout: (app: unknown, config: unknown) => void;
+ initSidePanelTabs: (app: unknown, element: unknown) => void;
+ };
const compiled = transpileModule(readFileSync("src/mobile/util/initFramework.ts", "utf8"), {
compilerOptions: {module: ModuleKind.CommonJS},
}).outputText;
- runInNewContext(compiled, {
+ runInNewContext(compiled + "\nexports.initSidePanelTabs = initSidePanelTabs;", {
exports: moduleExports,
CSS: {escape: (value: string) => value},
- window: {siyuan: {config: {readonly: false}, isPublish: false}},
+ window: {siyuan: {config: {readonly: false}, isPublish: false, mobile: {docks: {
+ bookmark: {update: () => {}}, backlink: {update: () => {}},
+ }}}},
document: {
getElementById: (id: string) => id === "sidebar" ? left : right,
querySelectorAll: (): DockElement[] => [],
@@ -89,4 +98,28 @@ test("side panel rendering hides docks, selects a visible fallback and closes an
aiDisabled = false;
render([]);
assert.equal(tabs.get("agent").classList.contains("fn__none"), false);
+
+ moduleExports.initSidePanelTabs({}, left);
+ moduleExports.initSidePanelTabs({}, right);
+ const config = {...createDefaultMobileSidePanelConfig(),
+ left: ["tag", "bookmark", "file", "inbox"], right: ["agent", "outline", "backlink"], hidden: ["agent"],
+ };
+ moduleExports.renderMobileSidePanelLayout({}, config);
+ assert.equal(tabs.get("tag").classList.contains("toolbar__icon--active"), true);
+ assert.equal(tabs.get("outline").classList.contains("toolbar__icon--active"), true);
+
+ // 首次打开前变更布局时,仍按最新顺序选择首个可见功能。
+ config.left = ["file", "tag", "bookmark", "inbox"];
+ moduleExports.renderMobileSidePanelLayout({}, config);
+ assert.equal(tabs.get("file").classList.contains("toolbar__icon--active"), true);
+ left.firstElementChild.dispatchEvent({detail: "bookmark"});
+ right.firstElementChild.dispatchEvent({detail: "backlink"});
+ moduleExports.renderMobileSidePanelLayout({}, config);
+ assert.equal(tabs.get("bookmark").classList.contains("toolbar__icon--active"), true);
+ assert.equal(tabs.get("backlink").classList.contains("toolbar__icon--active"), true);
+
+ config.hidden = ["agent", "bookmark", "backlink"];
+ moduleExports.renderMobileSidePanelLayout({}, config);
+ assert.equal(tabs.get("file").classList.contains("toolbar__icon--active"), true);
+ assert.equal(tabs.get("outline").classList.contains("toolbar__icon--active"), true);
});
diff --git a/app/src/mobile/util/sidebar.test.ts b/app/src/mobile/util/sidebar.test.ts
index 22e4ac8648..67e4e4fb2e 100644
--- a/app/src/mobile/util/sidebar.test.ts
+++ b/app/src/mobile/util/sidebar.test.ts
@@ -22,7 +22,7 @@ test("sidebar opening restores the active dock, falls back to visible docks and
let cellEditorsClosed = 0;
const moduleExports: {
openSidebar?: (side: string) => void;
- popSidebar?: (side: string, render: boolean, preferFirst: boolean) => void;
+ popSidebar?: (side: string) => void;
} = {};
const source = readFileSync(resolve(process.cwd(), "src/mobile/util/sidebar.ts"), "utf8");
const code = transpileModule(source, {compilerOptions: {module: ModuleKind.CommonJS}}).outputText;
@@ -62,18 +62,18 @@ test("sidebar opening restores the active dock, falls back to visible docks and
assert.equal(rendered.length, 2);
tabs = [tab("tag", false, true), tab("bookmark", false), tab("file", true)];
- moduleExports.popSidebar("left", true, true);
- assert.equal(rendered.at(-1), "bookmark");
- const plugin = tab("plugin-custom", false);
+ moduleExports.popSidebar("left");
+ assert.equal(rendered.at(-1), "file");
+ const plugin = tab("plugin-custom", true);
Object.assign(plugin.dataset, {mobilePluginDockTab: "custom-plugin-tab"});
- tabs = [plugin, tab("file", true)];
- moduleExports.popSidebar("left", true, true);
- assert.equal(rendered.at(-1), "custom-plugin-tab");
tabs = [tab("file", false), plugin];
- moduleExports.popSidebar("left", true, true);
+ moduleExports.popSidebar("left");
+ assert.equal(rendered.at(-1), "custom-plugin-tab");
+ tabs = [tab("file", false), tab("bookmark", false)];
+ moduleExports.popSidebar("left");
assert.equal(rendered.at(-1), "file");
tabs = [];
- moduleExports.popSidebar("left", true, true);
+ moduleExports.popSidebar("left");
assert.equal(rendered.length, 5);
assert.equal(left.style.transform, "");
});
diff --git a/app/src/mobile/util/sidebar.ts b/app/src/mobile/util/sidebar.ts
index 6d8acfb44f..75777a994a 100644
--- a/app/src/mobile/util/sidebar.ts
+++ b/app/src/mobile/util/sidebar.ts
@@ -7,14 +7,14 @@ export const getSidebarElement = (side: MobileSidebarSide) => {
return document.getElementById(side === "left" ? "sidebar" : "sidebarRight");
};
-export const getSidebarDock = (sidebarElement: HTMLElement | null, preferFirst = false) => {
+export const getSidebarDock = (sidebarElement: HTMLElement | null) => {
if (!sidebarElement) {
return;
}
const toolbarElement = sidebarElement.querySelector(".toolbar--border");
const tabElements = Array.from(toolbarElement?.querySelectorAll<HTMLElement>("[data-type]") || []);
- const activeElement = (!preferFirst && tabElements.find(item =>
- item.classList.contains("toolbar__icon--active") && !item.classList.contains("fn__none"))) ||
+ const activeElement = tabElements.find(item =>
+ item.classList.contains("toolbar__icon--active") && !item.classList.contains("fn__none")) ||
tabElements.find(item => !item.classList.contains("fn__none"));
const type = activeElement?.dataset.mobilePluginDockTab ||
activeElement?.dataset.type?.replace(/^sidebar-/, "").replace(/-tab$/, "");
@@ -37,7 +37,7 @@ export const switchToNextSidebarTab = (side: MobileSidebarSide) => {
nextTab.scrollIntoView({block: "nearest", inline: "nearest"});
};
-export const popSidebar = (side: MobileSidebarSide, render = true, preferFirst = false) => {
+export const popSidebar = (side: MobileSidebarSide, render = true) => {
activeBlur();
const sidebarElement = getSidebarElement(side);
if (!sidebarElement) {
@@ -45,7 +45,7 @@ export const popSidebar = (side: MobileSidebarSide, render = true, preferFirst =
}
let dock: ReturnType<typeof getSidebarDock>;
if (render) {
- dock = getSidebarDock(sidebarElement, preferFirst);
+ dock = getSidebarDock(sidebarElement);
if (!dock) {
sidebarElement.style.removeProperty("transform");
closePanel();
diff --git a/app/src/mobile/util/sidebarSwipe.test.ts b/app/src/mobile/util/sidebarSwipe.test.ts
index 365c0e25ac..ee553e04a8 100644
--- a/app/src/mobile/util/sidebarSwipe.test.ts
+++ b/app/src/mobile/util/sidebarSwipe.test.ts
@@ -89,9 +89,9 @@ const createTouchHarness = (side: "left" | "right", options: {
"./sidebar": {
getSidebarElement: () => panel,
getSidebarDock: () => ({}),
- popSidebar: (value: string, _render: boolean, preferFirst: boolean) => {
+ popSidebar: (value: string, render = true) => {
panel.style.transform = "translateX(0px)";
- actions.push(preferFirst ? `first:${value}` : "restore");
+ actions.push(render ? `open:${value}` : "restore");
},
switchToNextSidebarTab: (value: string) => actions.push(`next:${value}`),
},
@@ -138,13 +138,13 @@ const createTouchHarness = (side: "left" | "right", options: {
for (const side of ["left", "right"] as const) {
const nextX = side === "left" ? 240 : 120;
const closeX = side === "left" ? 120 : 240;
- test(`${side} sidebar opens its first visible tab only after a committed swipe`, () => {
+ test(`${side} sidebar restores its selected tab only after a committed swipe`, () => {
const harness = createTouchHarness(side, {closed: true});
harness.start();
harness.move(nextX);
assert.deepEqual(harness.actions, []);
harness.end(nextX);
- assert.deepEqual(harness.actions, [`first:${side}`]);
+ assert.deepEqual(harness.actions, [`open:${side}`]);
const disabled = createTouchHarness(side, {closed: true, disabled: true});
disabled.start();
disabled.move(nextX);
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index 5cbf8617e5..b23b66cdb8 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -297,7 +297,7 @@ export const handleTouchEnd = (event: TouchEvent) => {
return;
}
- popSidebar(getOpeningSidebar(firstDirection), true, true);
+ popSidebar(getOpeningSidebar(firstDirection));
};
export const handleTouchCancel = () => {