Skip to content

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

:art: Support locating the opened document on mobile https://github.com/siyuan-note/siyuan/issues/19094

Daniele197021d214d
diff --git a/app/src/config/tabs/fileTab.ts b/app/src/config/tabs/fileTab.ts
index a67136adc4..c275639cd0 100644
--- a/app/src/config/tabs/fileTab.ts
+++ b/app/src/config/tabs/fileTab.ts
@@ -40,10 +40,10 @@ const genNotebookSavePathHtml = (
<input class="b3-text-field fn__flex-center fn__block" id="${template.id}" value="">` : ""}
</div>`;
-/// #if !MOBILE
const registerFileTreeBehaviorGroup = (tab: SettingTabBuilder) => {
const group = tab.group("behavior", window.siyuan.languages.configGroupBehavior);
+ /// #if !MOBILE
group.switch("fileTree.docIconClickExpand", {
title: window.siyuan.languages.docIconClickExpand,
desc: window.siyuan.languages.docIconClickExpandTip,
@@ -58,10 +58,12 @@ const registerFileTreeBehaviorGroup = (tab: SettingTabBuilder) => {
getAllModels().files.forEach((files) => files.updateDocActions());
}),
});
+ /// #endif
group.switch("fileTree.alwaysSelectOpenedFile", {
title: window.siyuan.languages.selectOpen,
desc: window.siyuan.languages.fileTree2,
});
+ /// #if !MOBILE
group.switch("fileTree.openFilesUseCurrentTab", {
title: window.siyuan.languages.fileTree7,
desc: window.siyuan.languages.fileTree8,
@@ -70,8 +72,8 @@ const registerFileTreeBehaviorGroup = (tab: SettingTabBuilder) => {
title: window.siyuan.languages.noSplitScreenWhenOpenTab,
desc: window.siyuan.languages.noSplitScreenWhenOpenTabTip,
});
+ /// #endif
};
-/// #endif
const registerTabStartupGroup = (tab: SettingTabBuilder) => {
const group = tab.group("tabStartup", window.siyuan.languages.tabStartup);
@@ -331,9 +333,7 @@ const registerFileOthersGroup = (tab: SettingTabBuilder) => {
};
export const registerFileTab = (tab: SettingTabBuilder) => {
- /// #if !MOBILE
registerFileTreeBehaviorGroup(tab);
- /// #endif
registerTabStartupGroup(tab);
registerFileNewDocumentGroup(tab);
registerFileManagementGroup(tab);
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index 1084bd0ea9..2d6919d86b 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -55,6 +55,7 @@ import {
type IDocSortModeChanged,
updateFileTreeSortMode
} from "../../util/fileTreeSort";
+import {MobileOpenedFileSelection} from "./mobileOpenedFileSelection";
export class MobileFiles extends Model {
public element: HTMLElement;
@@ -64,6 +65,7 @@ export class MobileFiles extends Model {
private docSortModeRefreshTimeout: number;
private docSortModeChanges = new Map<string, IDocSortModeChanged>();
private movedExpandedDocIDs = new Set<string>();
+ private openedFileSelection = new MobileOpenedFileSelection();
private touchDragState: {
selectedElement: HTMLElement;
startX: number;
@@ -1330,6 +1332,26 @@ export class MobileFiles extends Model {
}
}
+ public async selectOpenedFile(notebookId: string, filePath: string) {
+ const currentElement = this.element.querySelector(
+ `ul[data-url="${notebookId}"] li[data-path="${filePath}"]`
+ );
+ if (currentElement?.classList.contains("b3-list-item--focus")) {
+ this.openedFileSelection.cancel();
+ return;
+ }
+ const target = await this.openedFileSelection.resolve(
+ () => this.selectItem(notebookId, filePath, undefined, true, false),
+ () => {
+ const protyle = window.siyuan.mobile.editor?.protyle;
+ return window.siyuan.config.fileTree.alwaysSelectOpenedFile &&
+ !protyle?.element.classList.contains("fn__none") &&
+ protyle?.notebookId === notebookId && protyle.path === filePath;
+ }
+ );
+ this.setCurrent(target);
+ }
+
public getLeaf(liElement: Element, notebookId: string, focusUpdate = false) {
const toggleElement = liElement.querySelector(".b3-list-item__arrow");
if (cancelFileTreeCollapse(liElement)) {
diff --git a/app/src/mobile/dock/mobileOpenedFileSelection.test.ts b/app/src/mobile/dock/mobileOpenedFileSelection.test.ts
new file mode 100644
index 0000000000..ea41c031be
--- /dev/null
+++ b/app/src/mobile/dock/mobileOpenedFileSelection.test.ts
@@ -0,0 +1,41 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {MobileOpenedFileSelection} from "./mobileOpenedFileSelection";
+
+describe("mobile opened file selection", () => {
+ it("only resolves the latest selection", async () => {
+ const selection = new MobileOpenedFileSelection();
+ let resolveFirst!: (value: string) => void;
+ const firstValue = new Promise<string>((resolve) => {
+ resolveFirst = resolve;
+ });
+
+ const firstRequest = selection.resolve(() => firstValue, () => true);
+ const secondResult = await selection.resolve(() => Promise.resolve("second"), () => true);
+ resolveFirst("first");
+
+ assert.equal(await firstRequest, undefined);
+ assert.equal(secondResult, "second");
+ });
+
+ it("ignores a selection that is no longer current", async () => {
+ const selection = new MobileOpenedFileSelection();
+ const result = await selection.resolve(() => Promise.resolve("document"), () => false);
+
+ assert.equal(result, undefined);
+ });
+
+ it("cancels a pending selection", async () => {
+ const selection = new MobileOpenedFileSelection();
+ let resolvePending!: (value: string) => void;
+ const pendingValue = new Promise<string>((resolve) => {
+ resolvePending = resolve;
+ });
+ const pendingRequest = selection.resolve(() => pendingValue, () => true);
+
+ selection.cancel();
+ resolvePending("document");
+
+ assert.equal(await pendingRequest, undefined);
+ });
+});
diff --git a/app/src/mobile/dock/mobileOpenedFileSelection.ts b/app/src/mobile/dock/mobileOpenedFileSelection.ts
new file mode 100644
index 0000000000..57f58b20db
--- /dev/null
+++ b/app/src/mobile/dock/mobileOpenedFileSelection.ts
@@ -0,0 +1,16 @@
+export class MobileOpenedFileSelection {
+ private request = 0;
+
+ public cancel() {
+ this.request++;
+ }
+
+ public async resolve<T>(load: () => Promise<T>, isCurrent: () => boolean): Promise<T | undefined> {
+ const request = ++this.request;
+ const result = await load();
+ if (request !== this.request || !isCurrent()) {
+ return;
+ }
+ return result;
+ }
+}
diff --git a/app/src/mobile/editor.ts b/app/src/mobile/editor.ts
index 52525a0840..eef6eefa29 100644
--- a/app/src/mobile/editor.ts
+++ b/app/src/mobile/editor.ts
@@ -77,6 +77,9 @@ export const loadMobileFileById = (app: App, id: string, action: TProtyleAction[
stickyRow(item, protyle.contentElement, "top");
});
});
+ if (window.siyuan.config.fileTree.alwaysSelectOpenedFile && protyle.path) {
+ void window.siyuan.mobile.docks.file?.selectOpenedFile(protyle.notebookId, protyle.path);
+ }
afterOpen?.(protyle);
};
const fail = (invalid = false) => {