Skip to content

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

:bug: Refresh document tree sorting after renaming documents https://github.com/siyuan-note/siyuan/issues/19176

Daniel056b1648880f
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index bb0a761cf2..2f5dacc87e 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -1605,11 +1605,17 @@ data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}"
public onRename(data: { path: string, title: string, box: string }) {
const fileItemElement = this.element.querySelector(`ul[data-url="${data.box}"] li[data-path="${data.path}"]`);
- if (!fileItemElement) {
- return;
+ if (fileItemElement) {
+ fileItemElement.setAttribute("data-name", data.title);
+ fileItemElement.querySelector(".b3-list-item__text").innerHTML = escapeHtml(data.title);
+ }
+ const parentPath = pathPosix().dirname(data.path);
+ const listPath = parentPath === "/" ? "/" : `${parentPath}.sy`;
+ const parentElement = this.element.querySelector(`ul[data-url="${data.box}"] li[data-path="${listPath}"]`);
+ const listElement = parentElement?.nextElementSibling;
+ if (listElement?.tagName === "UL" && !isCustomFileTreeList(listElement)) {
+ this.onFiletreeSortChanged({notebook: data.box, parentPath});
}
- fileItemElement.setAttribute("data-name", data.title);
- fileItemElement.querySelector(".b3-list-item__text").innerHTML = escapeHtml(data.title);
}
public onFiletreeSortChanged(data: { notebook: string, parentPath: string }) {
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index 2c36c52b79..e6ed458bc4 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -1125,11 +1125,17 @@ export class MobileFiles extends Model {
public onRename(data: { path: string, title: string, box: string }) {
const fileItemElement = this.element.querySelector(`ul[data-url="${data.box}"] li[data-path="${data.path}"]`);
- if (!fileItemElement) {
- return;
+ if (fileItemElement) {
+ fileItemElement.setAttribute("data-name", data.title);
+ fileItemElement.querySelector(".b3-list-item__text").innerHTML = escapeHtml(data.title);
+ }
+ const parentPath = pathPosix().dirname(data.path);
+ const listPath = parentPath === "/" ? "/" : `${parentPath}.sy`;
+ const parentElement = this.element.querySelector(`ul[data-url="${data.box}"] li[data-path="${listPath}"]`);
+ const listElement = parentElement?.nextElementSibling;
+ if (listElement?.tagName === "UL" && !isCustomFileTreeList(listElement)) {
+ this.onFiletreeSortChanged({notebook: data.box, parentPath});
}
- fileItemElement.setAttribute("data-name", data.title);
- fileItemElement.querySelector(".b3-list-item__text").innerHTML = escapeHtml(data.title);
}
public onFiletreeSortChanged(data: { notebook: string, parentPath: string }) {
diff --git a/app/src/util/fileTreeRename.test.ts b/app/src/util/fileTreeRename.test.ts
new file mode 100644
index 0000000000..7d6331dd01
--- /dev/null
+++ b/app/src/util/fileTreeRename.test.ts
@@ -0,0 +1,69 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {posix} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+import {isCustomFileTreeList} from "./fileTreeSort";
+
+for (const sourcePath of ["src/layout/dock/Files.ts", "src/mobile/dock/MobileFiles.ts"]) {
+ const source = createSourceFile(sourcePath, readFileSync(sourcePath, "utf8"), ScriptTarget.ES2021, true);
+ const declaration = source.statements.find(isClassDeclaration);
+ const methods = declaration.members.filter(member =>
+ ["onRename", "onFiletreeSortChanged"].includes(member.name?.getText(source))
+ ).map(member => member.getText(source)).join("\n");
+ const compiled = transpileModule(`class Handler {${methods}} exports.Handler = Handler;`, {
+ compilerOptions: {target: ScriptTarget.ES2021},
+ }).outputText;
+
+ for (const parentPath of ["/", "/parent/child"]) {
+ for (const mode of [0, 3, 4, 6]) {
+ for (const visible of [true, false]) {
+ test(`${sourcePath}: rename refreshes mode ${mode}, parent ${parentPath}, visible ${visible}`, () => {
+ const docPath = posix.join(parentPath, "document.sy");
+ const listPath = parentPath === "/" ? "/" : `${parentPath}.sy`;
+ const list = {tagName: "UL", getAttribute: () => String(mode)};
+ const parent = {nextElementSibling: list};
+ const title = {innerHTML: "old"};
+ let name = "old";
+ const file = {setAttribute: (_key: string, value: string) => name = value, querySelector: () => title};
+ const requests: any[] = [];
+ const rendered: any[] = [];
+ const response = {files: [{id: "document"}], path: listPath};
+ const exports: any = {};
+ runInNewContext(compiled, {
+ exports,
+ pathPosix: () => posix,
+ escapeHtml: (value: string) => value.replace(/</g, "&lt;"),
+ isCustomFileTreeList,
+ Constants: {SIYUAN_APPID: "app"},
+ fetchPost: (_url: string, data: any, callback: (data: any) => void) => {
+ requests.push(data);
+ callback({data: response});
+ },
+ });
+ const handler = new exports.Handler();
+ handler.element = {querySelector: (selector: string) => {
+ if (selector === 'ul[data-url="notebook"]') {
+ return {querySelector: () => parent};
+ }
+ if (selector === `ul[data-url="notebook"] li[data-path="${listPath}"]`) {
+ return parent;
+ }
+ return visible ? file : null;
+ }};
+ handler.onLsHTML = (data: any) => rendered.push(data);
+ handler.onRename({box: "notebook", path: docPath, title: "<new"});
+ assert.equal(requests.length, mode === 6 ? 0 : 1);
+ if (mode !== 6) {
+ assert.equal(requests[0].notebook, "notebook");
+ assert.equal(requests[0].path, listPath);
+ assert.equal(rendered[0], response);
+ }
+ assert.equal(name, visible ? "<new" : "old");
+ assert.equal(title.innerHTML, visible ? "&lt;new" : "old");
+ });
+ }
+ }
+ }
+}