Skip to content

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

:bug: Clarify invalid document drag targets https://github.com/siyuan-note/siyuan/issues/19548

Daniel2b8165468f6d
diff --git a/app/src/layout/dock/Files.drop.test.ts b/app/src/layout/dock/Files.drop.test.ts
new file mode 100644
index 0000000000..8d926e4e13
--- /dev/null
+++ b/app/src/layout/dock/Files.drop.test.ts
@@ -0,0 +1,61 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import test from "node:test";
+import {runInNewContext} from "node:vm";
+import * as ts from "typescript";
+
+const dropDocuments = (paths: string[], targetPath: string) => {
+ const source = readFileSync(join(__dirname, "Files.ts"), "utf8");
+ const start = source.indexOf("const selectRootElements: HTMLElement[] = [];");
+ const end = source.indexOf("if (newElement.classList.contains(\"dragover__bottom\")", start);
+ assert.ok(start >= 0 && end > start);
+ const messages: string[] = [];
+ const requests: {fromPaths: string[], toPath: string}[] = [];
+ const classes = new Set(["dragover"]);
+ const rows = paths.map((path) => ({
+ dataset: {path},
+ getAttribute: (name: string) => name === "data-path" ? path :
+ name === "data-type" ? "navigation-file" : "notebook",
+ }));
+ const script = ts.transpileModule(`(function () {${source.slice(start, end)}}).call(panel);`, {
+ compilerOptions: {target: ts.ScriptTarget.ES2020},
+ }).outputText;
+ runInNewContext(script, {
+ panel: {element: {querySelectorAll: () => rows}},
+ newElement: {
+ getAttribute: () => targetPath,
+ classList: {
+ contains: (name: string) => classes.has(name),
+ remove: (...names: string[]) => names.forEach((name) => classes.delete(name)),
+ },
+ },
+ toURL: "notebook",
+ toPath: targetPath,
+ window: {siyuan: {languages: {_kernel: {87: "Cannot move to this location"}}}},
+ showMessage: (message: string) => messages.push(message),
+ isMoveTargetAllowed: () => true,
+ fetchPost: (_url: string, body: {fromPaths: string[], toPath: string}) => requests.push(body),
+ });
+ return {messages, requests, classes};
+};
+
+test("dropping a document into itself or descendants reports an invalid target without sending an empty move", () => {
+ for (const target of ["/parent.sy", "/parent/child.sy", "/parent/child/grandchild.sy"]) {
+ for (const sources of [["/parent.sy"], ["/other.sy", "/parent.sy"]]) {
+ const result = dropDocuments(sources, target);
+ assert.deepEqual(result.messages, ["Cannot move to this location"]);
+ assert.equal(result.requests.length, 0);
+ assert.equal(result.classes.size, 0);
+ }
+ }
+});
+
+test("dropping documents into another document retains the selected source paths", () => {
+ const result = dropDocuments(["/parent.sy", "/parent/child.sy", "/other.sy"], "/target.sy");
+ assert.deepEqual(result.messages, []);
+ assert.equal(result.requests.length, 1);
+ assert.deepEqual(Array.from(result.requests[0].fromPaths), ["/parent.sy", "/other.sy"]);
+ assert.equal(result.requests[0].toPath, "/target.sy");
+ assert.equal(result.classes.size, 0);
+});
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index 708955c253..d2dc77a62b 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -858,6 +858,7 @@ export class Files extends Model {
const selectRootElements: HTMLElement[] = [];
const selectFileElements: HTMLElement[] = [];
const fromPaths: string[] = [];
+ let invalidMoveTarget = false;
this.element.querySelectorAll(".b3-list-item--focus").forEach((item: HTMLElement) => {
if (item.getAttribute("data-type") === "navigation-root") {
selectRootElements.push(item);
@@ -871,6 +872,7 @@ export class Files extends Model {
if (!isChild) {
// 禁止父节点移动到子节点 https://github.com/siyuan-note/siyuan/issues/12539
if (newElement.getAttribute("data-path").startsWith(item.dataset.path.replace(".sy", ""))) {
+ invalidMoveTarget = true;
return;
}
selectFileElements.push(item);
@@ -878,6 +880,11 @@ export class Files extends Model {
}
}
});
+ if (invalidMoveTarget) {
+ showMessage(window.siyuan.languages._kernel[87]);
+ newElement.classList.remove("dragover", "dragover__bottom", "dragover__top");
+ return;
+ }
if (newElement.classList.contains("dragover")) {
const sourceNotebookIds = selectFileElements.map((item) =>
item.getAttribute("data-notebook-id") || item.closest("ul[data-url]")?.getAttribute("data-url") || "");
diff --git a/kernel/model/file_tree_reorder.go b/kernel/model/file_tree_reorder.go
index c17c8b06b4..7e705b8549 100644
--- a/kernel/model/file_tree_reorder.go
+++ b/kernel/model/file_tree_reorder.go
@@ -1,6 +1,7 @@
package model
import (
+ "errors"
"fmt"
"maps"
"path"
@@ -153,7 +154,7 @@ func ReorderDocTree(sourceIDs []string, targetID, position string, preview, remo
}
if source.BoxID == target.BoxID && (target.Path == source.Path ||
strings.HasPrefix(target.Path, strings.TrimSuffix(source.Path, ".sy")+"/")) {
- return nil, fmt.Errorf("cannot move document [%s] into itself", id)
+ return nil, errors.New(Conf.Language(87))
}
if byID[id] != nil {
continue
diff --git a/kernel/model/file_tree_reorder_test.go b/kernel/model/file_tree_reorder_test.go
index 0cb743ccd8..49bd7e40a8 100644
--- a/kernel/model/file_tree_reorder_test.go
+++ b/kernel/model/file_tree_reorder_test.go
@@ -5,6 +5,7 @@ import (
"path/filepath"
"reflect"
"strconv"
+ "strings"
"testing"
"github.com/siyuan-note/siyuan/kernel/cache"
@@ -86,6 +87,40 @@ func TestDocTreeReorderRejectsUnreadableSort(t *testing.T) {
}
}
+func TestDocTreeReorderRejectsDescendantTargetsWithLocalizedMessage(t *testing.T) {
+ f := setupFileOperationTest(t)
+ Conf.FileTree.Sort = util.SortModeNameASC
+ childPath := strings.TrimSuffix(f.sourcePath, ".sy") + "/20260718000003-abcdefg.sy"
+ grandchildPath := strings.TrimSuffix(childPath, ".sy") + "/20260718000004-abcdefg.sy"
+ for _, docPath := range []string{childPath, grandchildPath} {
+ tree := treenode.NewTree(f.box.ID, docPath, "/Source/Descendant", "Descendant")
+ if _, err := filesys.WriteTree(tree); err != nil {
+ t.Fatal(err)
+ }
+ treenode.UpsertBlockTree(tree)
+ t.Cleanup(func() {
+ cache.RemoveTreeData(tree.ID)
+ cache.RemoveDocIAL(tree.Path)
+ })
+ }
+ for _, targetPath := range []string{childPath, grandchildPath} {
+ for _, position := range []string{"before", "after"} {
+ for _, preview := range []bool{true, false} {
+ _, err := ReorderDocTree([]string{f.sourceID}, util.GetTreeID(targetPath), position, preview, true)
+ if err == nil || err.Error() != Conf.Language(87) {
+ t.Fatalf("expected localized invalid target error for %s, %s, preview=%t, got %v",
+ targetPath, position, preview, err)
+ }
+ for _, docPath := range []string{f.sourcePath, childPath, grandchildPath} {
+ if !f.box.Exist(docPath) {
+ t.Fatalf("invalid reorder changed document %s", docPath)
+ }
+ }
+ }
+ }
+ }
+}
+
func TestDocTreeSortAllModesRespectTies(t *testing.T) {
for mode := util.SortModeNameASC; mode <= util.SortModeSubDocCountDESC; mode++ {
left := &File{ID: "20260718000001-abcdefg", Name: "Same", Sort: 1}