Skip to content

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

:art: Improve import notifications https://github.com/siyuan-note/siyuan/issues/19090

Daniela8c1b9aa0f7a
diff --git a/app/src/index.ts b/app/src/index.ts
index 9c4ed10ea2..f893ffa7dc 100644
--- a/app/src/index.ts
+++ b/app/src/index.ts
@@ -246,6 +246,13 @@ export class App {
}
break;
}
+ case "docsImported": {
+ const fileDock = getDockByType("file");
+ if (fileDock) {
+ (fileDock.data.file as Files).onDocsImported(data.data);
+ }
+ break;
+ }
case "docSortModeChanged": {
const fileDock = getDockByType("file");
if (fileDock) {
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index 6187eaccc9..272e79d7f8 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -1547,6 +1547,15 @@ data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}"
});
}
+ public onDocsImported(data: { notebook: string, parentPath: string, rootIDs: string[] }) {
+ const rootID = data.rootIDs?.[0];
+ if (!rootID) {
+ return;
+ }
+ const importedPath = data.parentPath === "/" ? `/${rootID}.sy` : `${data.parentPath}/${rootID}.sy`;
+ this.updateItemArrow(data.notebook, importedPath);
+ }
+
public onDocSortModeChanged(data: IDocSortModeChanged) {
updateFileTreeSortMode(data, this.element);
this.docSortModeChanges.set(`${data.scope}:${data.box}:${data.id}:${data.path}`, data);
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index b273ee3beb..1084bd0ea9 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -1125,6 +1125,15 @@ export class MobileFiles extends Model {
});
}
+ public onDocsImported(data: { notebook: string, parentPath: string, rootIDs: string[] }) {
+ const rootID = data.rootIDs?.[0];
+ if (!rootID) {
+ return;
+ }
+ const importedPath = data.parentPath === "/" ? `/${rootID}.sy` : `${data.parentPath}/${rootID}.sy`;
+ this.updateItemArrow(data.notebook, importedPath);
+ }
+
public onDocSortModeChanged(data: IDocSortModeChanged) {
updateFileTreeSortMode(data, this.element);
this.docSortModeChanges.set(`${data.scope}:${data.box}:${data.id}:${data.path}`, data);
diff --git a/app/src/mobile/util/onMessage.ts b/app/src/mobile/util/onMessage.ts
index c15b3bcb75..c375dce107 100644
--- a/app/src/mobile/util/onMessage.ts
+++ b/app/src/mobile/util/onMessage.ts
@@ -177,6 +177,9 @@ export const onMessage = (app: App, data: IWebSocketData) => {
case "filetreeSortChanged":
window.siyuan.mobile.docks.file?.onFiletreeSortChanged(data.data);
break;
+ case "docsImported":
+ window.siyuan.mobile.docks.file?.onDocsImported(data.data);
+ break;
case "docSortModeChanged":
window.siyuan.mobile.docks.file?.onDocSortModeChanged(data.data);
break;
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 937851c1e3..23c64bf3ca 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -3197,7 +3197,8 @@ func (box *Box) addSort(previousPath, id string) {
}
}
-func (box *Box) setSort(sortIDVals map[string]int) {
+// setImportSort 写入导入文档的排序值,导入完成后由统一事件通知。
+func (box *Box) setImportSort(sortIDVals map[string]int) {
fileTreeSortLock.Lock()
defer fileTreeSortLock.Unlock()
@@ -3216,8 +3217,6 @@ func (box *Box) setSort(sortIDVals map[string]int) {
if err = writeSortConfMap(confPath, fullSortIDs); err != nil {
return
}
-
- pushFiletreeSortChanged(sortIDVals)
}
func pushFiletreeSortChanged(sortIDs map[string]int) {
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 60d350b582..635ee74f60 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -183,6 +183,40 @@ type importedSYSortDoc struct {
hidden bool
}
+func importedSYRootIDs(importedDocs []*importedSYSortDoc) (ret []string) {
+ for _, doc := range importedDocs {
+ if nil == doc || doc.hidden || "/" != path.Dir(doc.sourcePath) {
+ continue
+ }
+ ret = append(ret, doc.newID)
+ }
+ sort.Strings(ret)
+ return
+}
+
+func importedTreeRootIDs(trees []*parse.Tree, parentPath string) (ret []string) {
+ for _, tree := range trees {
+ if nil == tree || parentPath != path.Dir(tree.Path) {
+ continue
+ }
+ ret = append(ret, tree.ID)
+ }
+ sort.Strings(ret)
+ return
+}
+
+func pushDocsImported(boxID, parentPath, format string, rootIDs []string) {
+ if 1 > len(rootIDs) {
+ return
+ }
+ util.BroadcastByType("main", "docsImported", 0, "", map[string]any{
+ "notebook": boxID,
+ "parentPath": parentPath,
+ "rootIDs": rootIDs,
+ "format": format,
+ })
+}
+
func buildImportedSYSortValues(importedDocs []*importedSYSortDoc, sourceSortIDs map[string]int,
existingRootIDs []string, createDocAtTop bool) map[string]int {
groups := map[string][]*importedSYSortDoc{}
@@ -1014,6 +1048,9 @@ func importSY0(zipPath, boxID, toPath string, createNotebook, autoDetect bool, s
}
IncSync()
+ if !createNotebook {
+ pushDocsImported(boxID, baseTargetPath, "sy", importedSYRootIDs(importedSortDocs))
+ }
task.AppendTask(task.UpdateIDs, util.PushUpdateIDs, blockIDs)
return
@@ -1727,6 +1764,7 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
importTrees = append(importTrees, tree)
}
+ var importedRootIDs []string
if 0 < len(importTrees) {
for id, newID := range moveIDs {
for _, importTree := range importTrees {
@@ -1749,6 +1787,7 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
refreshBoxDocInfoByBoxID(boxID)
util.PushClearProgress()
+ importedRootIDs = importedTreeRootIDs(importTrees, baseTargetPath)
importTrees = []*parse.Tree{}
searchLinks = map[string]string{}
@@ -1787,10 +1826,11 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
sortVal++
}
}
- box.setSort(sortIDVals)
+ box.setImportSort(sortIDVals)
}
IncSync()
+ pushDocsImported(boxID, baseTargetPath, "markdown", importedRootIDs)
debug.FreeOSMemory()
return
}
diff --git a/kernel/model/import_test.go b/kernel/model/import_test.go
index 2bc26ebce7..fe21c12a5c 100644
--- a/kernel/model/import_test.go
+++ b/kernel/model/import_test.go
@@ -30,6 +30,7 @@ import (
"strings"
"testing"
+ "github.com/88250/lute/parse"
"github.com/siyuan-note/siyuan/kernel/util"
"golang.org/x/image/bmp"
)
@@ -115,6 +116,41 @@ func TestBuildImportedSYSortValuesWithoutSourceSort(t *testing.T) {
assertImportedSYSortValues(t, got, map[string]int{newer.newID: 0, older.newID: 1, "existing": 2})
}
+func TestImportedSYRootIDs(t *testing.T) {
+ rootB := &importedSYSortDoc{newID: "root-b", sourcePath: "/root-b.sy"}
+ rootA := &importedSYSortDoc{newID: "root-a", sourcePath: "/root-a.sy"}
+ child := &importedSYSortDoc{newID: "child", sourcePath: "/root-a/child.sy"}
+ hidden := &importedSYSortDoc{newID: "hidden", sourcePath: "/hidden.sy", hidden: true}
+
+ got := importedSYRootIDs([]*importedSYSortDoc{rootB, nil, child, hidden, rootA})
+ assertStringSlice(t, got, []string{"root-a", "root-b"})
+}
+
+func TestImportedTreeRootIDs(t *testing.T) {
+ trees := []*parse.Tree{
+ {ID: "root-b", Path: "/target/root-b.sy"},
+ nil,
+ {ID: "child", Path: "/target/root-b/child.sy"},
+ {ID: "root-a", Path: "/target/root-a.sy"},
+ {ID: "other", Path: "/other.sy"},
+ }
+
+ got := importedTreeRootIDs(trees, "/target")
+ assertStringSlice(t, got, []string{"root-a", "root-b"})
+}
+
+func assertStringSlice(t *testing.T, got, want []string) {
+ t.Helper()
+ if len(got) != len(want) {
+ t.Fatalf("string slice length = %d, want %d: got=%v", len(got), len(want), got)
+ }
+ for i, wantValue := range want {
+ if got[i] != wantValue {
+ t.Fatalf("string slice item %d = %q, want %q: got=%v", i, got[i], wantValue, got)
+ }
+ }
+}
+
func assertImportedSYSortValues(t *testing.T, got, want map[string]int) {
t.Helper()
if len(got) != len(want) {