Skip to content

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

:bug: Keep mobile panels open after removing documents https://github.com/siyuan-note/siyuan/issues/20091

Daniel7241dfa5db04
diff --git a/app/src/mobile/editor.ts b/app/src/mobile/editor.ts
index 29db7de3b9..81d558ad77 100644
--- a/app/src/mobile/editor.ts
+++ b/app/src/mobile/editor.ts
@@ -69,7 +69,7 @@ export const loadMobileFileById = (app: App, id: string, action: TProtyleAction[
afterOpen?: (protyle: IProtyle) => void, forceReload = false,
isValid: () => boolean = () => true, signal?: AbortSignal,
scrollAttr?: IScrollAttr, updateRecent = true,
- onFailure?: (invalid?: boolean) => void) => {
+ onFailure?: (invalid?: boolean) => void, keepPanels = false) => {
let completed = false;
let titleHidden = false;
const complete = (protyle: IProtyle) => {
@@ -145,7 +145,9 @@ export const loadMobileFileById = (app: App, id: string, action: TProtyleAction[
protyle.block.id = protyle.block.rootID;
protyle.wysiwyg.element.setAttribute("data-doc-type", "NodeDocument");
}
- closePanel();
+ if (!keepPanels) {
+ closePanel();
+ }
// 更新文档浏览时间
const rootID = protyle.block.rootID;
if (updateRecent) {
@@ -316,7 +318,9 @@ export const loadMobileFileById = (app: App, id: string, action: TProtyleAction[
return;
}
}
- closePanel();
+ if (!keepPanels) {
+ closePanel();
+ }
}, undefined, undefined, signal).then(() => {
if (!blockInfoHandled) {
fail();
diff --git a/app/src/mobile/tabs/MobileTabs.startup.test.ts b/app/src/mobile/tabs/MobileTabs.startup.test.ts
index 29fca863ec..4e2b40f466 100644
--- a/app/src/mobile/tabs/MobileTabs.startup.test.ts
+++ b/app/src/mobile/tabs/MobileTabs.startup.test.ts
@@ -39,6 +39,7 @@ const setup = (storage: Record<string, any> = {}, options: {
const writes: Array<{key: string, value: any}> = [];
const opened: string[] = [];
const loads: string[] = [];
+ const keptPanels: boolean[] = [];
const folds: Array<(zoomIn: boolean) => void> = [];
let empty = 0;
let sequence = 0;
@@ -51,7 +52,7 @@ const setup = (storage: Record<string, any> = {}, options: {
}
const fetchPost = (url: string, data: any, callback?: (response: any) => void) => {
if (url === "/api/block/getBlockInfo") {
- callback({code: options.failOpen ? 1 : 0, data: {rootID: data.id, box: "box"}});
+ callback({code: options.failOpen ? 1 : 0, data: {rootID: data.id, box: data.notebook || "box"}});
} else {
requests.push({url, data, callback});
}
@@ -73,9 +74,12 @@ const setup = (storage: Record<string, any> = {}, options: {
({code: 0, data: Object.fromEntries(data.ids.map(id => [id, !options.missing?.includes(id)]))})},
"../util/setEmpty": {setEmpty}, "../util/closePanel": {closeModel: () => {}},
"../editor": {updateRecentDocSwitchTime: () => {}, loadMobileFileById: (_app: unknown, id: string,
- action: string[], _position: unknown, _box: string, callback: (protyle: unknown) => void) => {
+ action: string[], _position: unknown, _box: string, callback: (protyle: unknown) => void,
+ _forceReload: boolean, _isValid: unknown, _signal: unknown, _scroll: unknown, _updateRecent: boolean,
+ _onFailure: unknown, keepPanels = false) => {
loads.push(id);
- const protyle = {block: {rootID: id, id, action, showAll: true}, notebookId: "box", path: `/${id}.sy`};
+ keptPanels.push(keepPanels);
+ const protyle = {block: {rootID: id, id, action, showAll: true}, notebookId: _box, path: `/${id}.sy`};
window.siyuan.mobile.editor = {protyle};
callback(protyle);
}},
@@ -110,7 +114,7 @@ const setup = (storage: Record<string, any> = {}, options: {
request.callback = undefined;
callback(response);
};
- return {window, requests, writes, opened, loads, folds, api, respond, buttons,
+ return {window, requests, writes, opened, loads, keptPanels, folds, api, respond, buttons,
start: (isStart = true) => startup({}, isStart), get tabs(): any { return window.siyuan.mobile.tabs; },
get empty() { return empty; }};
};
@@ -140,6 +144,41 @@ test("closing every tab or the final tab persists empty state without clearing u
}
});
+test("removing the active document restores another tab without closing panels", async () => {
+ for (const removeNotebook of [false, true]) {
+ const state = setup({mobileTabs: saved([tab("a", entry("a", "removed")), tab("b")])});
+ await state.start();
+ await flush();
+ assert.equal(state.keptPanels.at(-1), false);
+ if (removeNotebook) {
+ state.tabs.removeNotebook("removed");
+ } else {
+ state.tabs.removeRoots(["a"]);
+ }
+ await flush();
+ assert.equal(state.loads.at(-1), "b");
+ assert.equal(state.keptPanels.at(-1), true);
+ assert.equal(state.tabs.state.activeTabID, "b");
+ await state.tabs.open("c");
+ assert.equal(state.keptPanels.at(-1), false);
+ }
+});
+
+test("removing an inactive or final document does not open another document", async () => {
+ const state = setup({mobileTabs: saved([tab("a"), tab("b")])});
+ await state.start();
+ await flush();
+ const loaded = state.loads.length;
+ state.tabs.removeRoots(["b"]);
+ await flush();
+ assert.equal(state.loads.length, loaded);
+ state.tabs.removeRoots(["a"]);
+ await flush();
+ assert.equal(state.loads.length, loaded);
+ assert.equal(state.tabs.state.tabs.length, 0);
+ assert.ok(state.empty > 0);
+});
+
test("saved blank, sanitized encrypted, malformed and deleted tabs never resurrect an unrelated old document", async () => {
for (const [tabs, options] of [
[[{...tab("blank"), current: undefined}], {}],
diff --git a/app/src/mobile/tabs/MobileTabs.ts b/app/src/mobile/tabs/MobileTabs.ts
index d4798ddb8b..5622978844 100644
--- a/app/src/mobile/tabs/MobileTabs.ts
+++ b/app/src/mobile/tabs/MobileTabs.ts
@@ -60,6 +60,7 @@ type OpenOptions = {
tabID?: string;
recentPreviousRootID?: string;
recordActivation?: boolean;
+ keepPanels?: boolean;
};
type MobileTabOpenResult = "success" | "cancelled" | "invalid" | "failed";
@@ -459,7 +460,7 @@ export class MobileTabs {
options.afterOpen?.(protyle);
}, options.forceReload, () => epoch === this.navigationEpoch, undefined, loadScroll, false, (invalid) => {
finish(abortController.signal.aborted ? "cancelled" : (invalid ? "invalid" : "failed"));
- });
+ }, options.keepPanels);
});
}
@@ -472,7 +473,7 @@ export class MobileTabs {
return !this.hasStoredTabs && this.navigationEpoch === 0;
}
- async restore(): Promise<boolean> {
+ async restore(keepPanels = false): Promise<boolean> {
const tab = this.activeTab;
if (!tab) {
setEmpty(this.app);
@@ -492,13 +493,14 @@ export class MobileTabs {
replace: true,
scroll: tab.current.scroll,
tabID: tab.id,
+ keepPanels,
});
if (result === "invalid") {
this.state.tabs = this.state.tabs.filter((item) => item.id !== tab.id);
this.state.activeTabID = [...this.state.tabs].sort((a, b) => b.activeAt - a.activeAt)[0]?.id;
this.persist();
this.updateCounter();
- return this.restore();
+ return this.restore(keepPanels);
}
if (result === "failed") {
setEmpty(this.app);
@@ -782,7 +784,7 @@ export class MobileTabs {
this.cancelNavigation();
this.state.activeTabID = [...this.state.tabs].sort((a, b) => b.activeAt - a.activeAt)[0]?.id;
if (this.state.activeTabID) {
- void this.restore();
+ void this.restore(true);
} else {
setEmpty(this.app);
}