🐛 The word count of the selected block is inaccurate https://github.com/siyuan-note/siyuan/issues/12793

This commit is contained in:
Daniel 2024-10-16 11:44:45 +08:00
parent fd15a31026
commit 27618dd849
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -43,19 +43,17 @@ func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
luteEngine := util.NewLute() luteEngine := util.NewLute()
var boxIDs []string var boxIDs []string
var paths []string var paths []string
blockIDs := map[string]string{} blockIDs := map[string][]string{}
seen := map[string]bool{}
for _, bt := range bts { for _, bt := range bts {
key := bt.BoxID + bt.Path
if !seen[key] {
seen[key] = true
boxIDs = append(boxIDs, bt.BoxID) boxIDs = append(boxIDs, bt.BoxID)
paths = append(paths, bt.Path) paths = append(paths, bt.Path)
blockIDs[bt.RootID] = bt.ID if _, ok := blockIDs[bt.RootID]; !ok {
blockIDs[bt.RootID] = []string{}
} }
blockIDs[bt.RootID] = append(blockIDs[bt.RootID], bt.ID)
} }
trees, errs := batchLoadTrees(boxIDs, paths, luteEngine) trees, errs := BatchLoadTrees(boxIDs, paths, luteEngine)
for i := range trees { for i := range trees {
tree := trees[i] tree := trees[i]
err := errs[i] err := errs[i]
@ -64,8 +62,10 @@ func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
continue continue
} }
id := blockIDs[tree.Root.ID] bIDs := blockIDs[tree.Root.ID]
ret[id] = tree for _, bID := range bIDs {
ret[bID] = tree
}
} }
return return
} }
@ -82,11 +82,18 @@ func LoadTree(boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err erro
return return
} }
func batchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) (ret []*parse.Tree, errs []error) { func BatchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) (ret []*parse.Tree, errs []error) {
var wg sync.WaitGroup var wg sync.WaitGroup
lock := sync.Mutex{} lock := sync.Mutex{}
loaded := map[string]bool{}
for i := range paths { for i := range paths {
if loaded[boxIDs[i]+paths[i]] {
continue
}
loaded[boxIDs[i]+paths[i]] = true
wg.Add(1) wg.Add(1)
go func(i int) { go func(i int) {
defer wg.Done() defer wg.Done()