Skip to content

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

:bug: Avoid refreshing the entire document tree when moving documents https://github.com/siyuan-note/siyuan/issues/18818

Daniel5b2ae5d60b80
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index a802ae17da..00ecaa2bd2 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -55,6 +55,7 @@ import {
FILE_TREE_CHILDREN_SORT_MODE,
FILE_TREE_EFFECTIVE_SORT_MODE,
getFileTreeSortRefreshTargets,
+ getMovedFileTreeSortRefreshTargets,
getResponseEffectiveSortMode,
isCustomFileTreeList,
reorderFileTreeNotebooks,
@@ -71,7 +72,6 @@ export class Files extends Model {
private reloadNotebookInfoTimeout: number;
private docSortModeRefreshTimeout: number;
private docSortModeChanges = new Map<string, IDocSortModeChanged>();
- private docSortModeFullRefresh = false;
private movedExpandedDocIDs = new Set<string>();
constructor(options: { tab: Tab, app: App }) {
@@ -1541,27 +1541,13 @@ data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}"
});
}
- public onDocSortModeChanged(data?: IDocSortModeChanged) {
+ public onDocSortModeChanged(data: IDocSortModeChanged) {
updateFileTreeSortMode(data, this.element);
- if (data) {
- this.docSortModeChanges.set(`${data.scope}:${data.box}:${data.id}:${data.path}`, data);
- } else {
- this.docSortModeFullRefresh = true;
- }
+ this.docSortModeChanges.set(`${data.scope}:${data.box}:${data.id}:${data.path}`, data);
window.clearTimeout(this.docSortModeRefreshTimeout);
this.docSortModeRefreshTimeout = window.setTimeout(() => {
- const fullRefresh = this.docSortModeFullRefresh;
const changes = Array.from(this.docSortModeChanges.values());
- this.docSortModeFullRefresh = false;
this.docSortModeChanges.clear();
- if (fullRefresh) {
- if (changes.some((item) => item.scope === "global")) {
- setNoteBook(() => this.init(false));
- } else {
- this.init(false);
- }
- return;
- }
const refreshLists = () => {
getFileTreeSortRefreshTargets(this.element, changes).forEach((target) => {
const notebookElement = Array.from(this.element.children).find((item) =>
@@ -1606,7 +1592,7 @@ data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}"
private onMove(response: IWebSocketData) {
const moves = response.data.moves as IFileTreeMove[];
- const needsSortModeRefresh = moves.some((move) =>
+ const hasParentChange = moves.some((move) =>
move.fromNotebook !== move.toNotebook ||
pathPosix().dirname(move.fromPath) !== pathPosix().dirname(move.newPath));
const refreshTargets = new Map<string, { element: HTMLElement, notebookId: string }>();
@@ -1700,16 +1686,25 @@ data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}"
});
}
});
- if (needsSortModeRefresh) {
+ if (hasParentChange) {
this.getOpenPaths();
- this.onDocSortModeChanged();
- return;
}
if (response.callback !== Constants.CB_MOVE_NOLIST) {
refreshTargets.forEach((target) => {
this.getLeaf(target.element, target.notebookId, true);
});
}
+ getMovedFileTreeSortRefreshTargets(this.element, moves).forEach((target) => {
+ const notebookElement = Array.from(this.element.children).find((item) =>
+ item.getAttribute("data-url") === target.notebookId
+ );
+ const liElement = Array.from(notebookElement?.querySelectorAll("li[data-path]") || []).find((item) =>
+ item.getAttribute("data-path") === target.path
+ );
+ if (liElement) {
+ this.getLeaf(liElement, target.notebookId, true);
+ }
+ });
}
private restoreMovedExpandedItems(listElement: Element, notebookId: string) {
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index 3c69e80c9f..859d8a88a0 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -41,6 +41,7 @@ import {
FILE_TREE_CHILDREN_SORT_MODE,
FILE_TREE_EFFECTIVE_SORT_MODE,
getFileTreeSortRefreshTargets,
+ getMovedFileTreeSortRefreshTargets,
getResponseEffectiveSortMode,
isCustomFileTreeList,
reorderFileTreeNotebooks,
@@ -55,7 +56,6 @@ export class MobileFiles extends Model {
private reloadNotebookInfoTimeout: number;
private docSortModeRefreshTimeout: number;
private docSortModeChanges = new Map<string, IDocSortModeChanged>();
- private docSortModeFullRefresh = false;
private movedExpandedDocIDs = new Set<string>();
private touchDragState: {
selectedElement: HTMLElement;
@@ -917,7 +917,7 @@ export class MobileFiles extends Model {
private onMove(response: IWebSocketData) {
const moves = response.data.moves as IFileTreeMove[];
- const needsSortModeRefresh = moves.some((move) =>
+ const hasParentChange = moves.some((move) =>
move.fromNotebook !== move.toNotebook ||
pathPosix().dirname(move.fromPath) !== pathPosix().dirname(move.newPath));
const refreshTargets = new Map<string, { element: HTMLElement, notebookId: string }>();
@@ -1011,16 +1011,25 @@ export class MobileFiles extends Model {
});
}
});
- if (needsSortModeRefresh) {
+ if (hasParentChange) {
this.getOpenPaths();
- this.onDocSortModeChanged();
- return;
}
if (response.callback !== Constants.CB_MOVE_NOLIST) {
refreshTargets.forEach((target) => {
this.getLeaf(target.element, target.notebookId, true);
});
}
+ getMovedFileTreeSortRefreshTargets(this.element, moves).forEach((target) => {
+ const notebookElement = Array.from(this.element.children).find((item) =>
+ item.getAttribute("data-url") === target.notebookId
+ );
+ const liElement = Array.from(notebookElement?.querySelectorAll("li[data-path]") || []).find((item) =>
+ item.getAttribute("data-path") === target.path
+ );
+ if (liElement) {
+ this.getLeaf(liElement, target.notebookId, true);
+ }
+ });
}
private restoreMovedExpandedItems(listElement: Element, notebookId: string) {
@@ -1123,27 +1132,13 @@ export class MobileFiles extends Model {
});
}
- public onDocSortModeChanged(data?: IDocSortModeChanged) {
+ public onDocSortModeChanged(data: IDocSortModeChanged) {
updateFileTreeSortMode(data, this.element);
- if (data) {
- this.docSortModeChanges.set(`${data.scope}:${data.box}:${data.id}:${data.path}`, data);
- } else {
- this.docSortModeFullRefresh = true;
- }
+ this.docSortModeChanges.set(`${data.scope}:${data.box}:${data.id}:${data.path}`, data);
window.clearTimeout(this.docSortModeRefreshTimeout);
this.docSortModeRefreshTimeout = window.setTimeout(() => {
- const fullRefresh = this.docSortModeFullRefresh;
const changes = Array.from(this.docSortModeChanges.values());
- this.docSortModeFullRefresh = false;
this.docSortModeChanges.clear();
- if (fullRefresh) {
- if (changes.some((item) => item.scope === "global")) {
- setNoteBook(() => this.init(false));
- } else {
- this.init(false);
- }
- return;
- }
const refreshLists = () => {
getFileTreeSortRefreshTargets(this.element, changes).forEach((target) => {
const notebookElement = Array.from(this.element.children).find((item) =>
diff --git a/app/src/util/fileTreeSort.test.ts b/app/src/util/fileTreeSort.test.ts
index d9d5e496fe..200de37ff9 100644
--- a/app/src/util/fileTreeSort.test.ts
+++ b/app/src/util/fileTreeSort.test.ts
@@ -6,6 +6,7 @@ import {
getConfiguredChildrenSortMode,
getFileTreeSortRefreshTargets,
getFileTreeListSortMode,
+ getMovedFileTreeSortRefreshTargets,
isCustomFileTreeList,
reorderFileTreeNotebooks,
updateFileTreeSortMode
@@ -124,6 +125,7 @@ const fileTreeFixture = () => {
return {
element: treeElement("DIV", {}, [notebook, explicitNotebook]),
notebook,
+ overridden,
parent,
};
};
@@ -170,6 +172,56 @@ test("document sorting refreshes expanded inherited lists up to an override", ()
]);
});
+test("moving a document refreshes only expanded inherited lists in the moved subtree", () => {
+ const fixture = fileTreeFixture();
+ assert.deepEqual(getMovedFileTreeSortRefreshTargets(fixture.element, [{
+ fromNotebook: "notebook",
+ fromPath: "/source/parent.sy",
+ toNotebook: "notebook",
+ toPath: "/",
+ newPath: "/parent.sy",
+ }]), [
+ {notebookId: "notebook", path: "/parent.sy"},
+ {notebookId: "notebook", path: "/parent/inherited.sy"},
+ ]);
+});
+
+test("moving a document within the same parent does not refresh inherited sorting", () => {
+ const fixture = fileTreeFixture();
+ assert.deepEqual(getMovedFileTreeSortRefreshTargets(fixture.element, [{
+ fromNotebook: "notebook",
+ fromPath: "/parent.sy",
+ toNotebook: "notebook",
+ toPath: "/",
+ newPath: "/parent.sy",
+ }]), []);
+});
+
+test("moving a document with an explicit sorting rule does not refresh its subtree", () => {
+ const fixture = fileTreeFixture();
+ assert.deepEqual(getMovedFileTreeSortRefreshTargets(fixture.element, [{
+ fromNotebook: "notebook",
+ fromPath: "/source/overridden.sy",
+ toNotebook: "notebook",
+ toPath: "/parent.sy",
+ newPath: "/parent/overridden.sy",
+ }]), []);
+});
+
+test("moving a document across notebooks refreshes inherited sorting", () => {
+ const fixture = fileTreeFixture();
+ assert.deepEqual(getMovedFileTreeSortRefreshTargets(fixture.element, [{
+ fromNotebook: "source",
+ fromPath: "/parent.sy",
+ toNotebook: "notebook",
+ toPath: "/",
+ newPath: "/parent.sy",
+ }]), [
+ {notebookId: "notebook", path: "/parent.sy"},
+ {notebookId: "notebook", path: "/parent/inherited.sy"},
+ ]);
+});
+
test("global sorting refreshes only notebooks that inherit the panel rule", () => {
const fixture = fileTreeFixture();
assert.deepEqual(getFileTreeSortRefreshTargets(fixture.element, [{
diff --git a/app/src/util/fileTreeSort.ts b/app/src/util/fileTreeSort.ts
index 216952301b..e7b753fc23 100644
--- a/app/src/util/fileTreeSort.ts
+++ b/app/src/util/fileTreeSort.ts
@@ -1,3 +1,5 @@
+import type {IFileTreeMove} from "./fileTreeMove";
+
export const FILE_TREE_EFFECTIVE_SORT_MODE = "data-effective-sort-mode";
export const FILE_TREE_CHILDREN_SORT_MODE = "data-children-sort-mode";
@@ -78,6 +80,32 @@ const addInheritedRefreshTargets = (parentElement: Element, notebookId: string,
});
};
+const getPathParent = (filePath: string) => {
+ const separatorIndex = filePath.lastIndexOf("/");
+ return separatorIndex <= 0 ? "/" : filePath.slice(0, separatorIndex);
+};
+
+export const getMovedFileTreeSortRefreshTargets = (element: Element, moves: IFileTreeMove[]) => {
+ const targets = new Map<string, IFileTreeSortRefreshTarget>();
+ moves.forEach((move) => {
+ if (move.fromNotebook === move.toNotebook &&
+ getPathParent(move.fromPath) === getPathParent(move.newPath)) {
+ return;
+ }
+ const notebookElement = getNotebookElements(element).find((item) =>
+ item.getAttribute("data-url") === move.toNotebook
+ );
+ const documentElement = findElement(notebookElement, (item) =>
+ item.tagName === "LI" && item.getAttribute("data-path") === move.newPath
+ );
+ if (!documentElement || getConfiguredChildrenSortMode(documentElement) !== null) {
+ return;
+ }
+ addInheritedRefreshTargets(documentElement, move.toNotebook, targets);
+ });
+ return Array.from(targets.values());
+};
+
export const getFileTreeSortRefreshTargets = (element: Element, changes: IDocSortModeChanged[]) => {
const targets = new Map<string, IFileTreeSortRefreshTarget>();
changes.forEach((change) => {