Skip to content

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

:bug: Stabilize dynamic references and search replacement

Danielf0ca77d90a23
diff --git a/app/src/search/util.ts b/app/src/search/util.ts
index 08d6c842c7..f8d42b820f 100644
--- a/app/src/search/util.ts
+++ b/app/src/search/util.ts
@@ -817,15 +817,23 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, ele
/// #endif
if (isClick) {
clickTimeout = window.setTimeout(() => {
+ const activeElement = document.activeElement;
+ const shouldRestoreFocus = (inputElement: HTMLInputElement) =>
+ !activeElement || activeElement === document.body ||
+ activeElement === inputElement || target.contains(activeElement);
if (searchType === "asset") {
if (!target.classList.contains("b3-list-item--focus")) {
assetsElement.querySelector(".b3-list-item--focus").classList.remove("b3-list-item--focus");
target.classList.add("b3-list-item--focus");
renderPreview(element.querySelector("#searchAssetPreview"), target.dataset.id, searchAssetInputElement.value, window.siyuan.storage[Constants.LOCAL_SEARCHASSET].method);
- searchAssetInputElement.focus();
+ if (shouldRestoreFocus(searchAssetInputElement)) {
+ searchAssetInputElement.focus();
+ }
} else if (target.classList.contains("b3-list-item--focus")) {
renderNextAssetMark(element.querySelector("#searchAssetPreview"));
- searchAssetInputElement.focus();
+ if (shouldRestoreFocus(searchAssetInputElement)) {
+ searchAssetInputElement.focus();
+ }
}
} else {
if (event.altKey) {
@@ -848,14 +856,18 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, ele
config: searchType === "doc" ? config : null,
value: searchType === "doc" ? searchInputElement.value : null,
});
- searchInputElement.focus();
+ if (shouldRestoreFocus(searchInputElement)) {
+ searchInputElement.focus();
+ }
} else if (searchType === "doc" && target.classList.contains("b3-list-item--focus")) {
renderNextSearchMark({
edit,
id: target.getAttribute("data-node-id"),
target,
});
- searchInputElement.focus();
+ if (shouldRestoreFocus(searchInputElement)) {
+ searchInputElement.focus();
+ }
}
}
}, Constants.TIMEOUT_DBLCLICK);
diff --git a/kernel/model/push_reload.go b/kernel/model/push_reload.go
index d2d694bbaa..a5932c2bed 100644
--- a/kernel/model/push_reload.go
+++ b/kernel/model/push_reload.go
@@ -326,7 +326,8 @@ func refreshDynamicRefTexts0(updatedDefNodes map[string]*ast.Node, updatedTrees
var changedNodes []*ast.Node
var refs []*sql.Ref
for _, updateNode := range updatedDefNodes {
- refs, changedNodes = getRefsCacheByDefNode(updateNode)
+ boxID := updatedNodeBoxID(updateNode, updatedTrees)
+ refs, changedNodes = getRefsCacheByDefNode(updateNode, boxID)
for _, ref := range refs {
if refIDs, ok := treeRefNodeIDs[ref.RootID]; !ok {
refIDs = hashset.New()
@@ -395,6 +396,14 @@ func refreshDynamicRefTexts0(updatedDefNodes map[string]*ast.Node, updatedTrees
return
}
+func updatedNodeBoxID(updateNode *ast.Node, updatedTrees map[string]*parse.Tree) string {
+ rootID := treenode.TreeRoot(updateNode).ID
+ if updatedTree := updatedTrees[rootID]; nil != updatedTree {
+ return updatedTree.Box
+ }
+ return updateNode.Box
+}
+
var (
setRefDynamicTextTaskLock sync.Mutex
setRefDynamicTextTaskSequence uint64
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 88b9fdc0d6..296e2f2442 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -2360,9 +2360,9 @@ func (tx *Transaction) writeTree(tree *parse.Tree) {
return
}
-func getRefsCacheByDefNode(updateNode *ast.Node) (ret []*sql.Ref, changedNodes []*ast.Node) {
+func getRefsCacheByDefNode(updateNode *ast.Node, boxID string) (ret []*sql.Ref, changedNodes []*ast.Node) {
changedNodesMap := map[string]*ast.Node{}
- ret = sql.GetRefsCacheByDefIDInBox(updateNode.ID, updateNode.Box)
+ ret = sql.GetRefsCacheByDefIDInBox(updateNode.ID, boxID)
if nil != updateNode.Parent && ast.NodeDocument != updateNode.Parent.Type &&
updateNode.Parent.IsContainerBlock() && updateNode == treenode.FirstLeafBlock(updateNode.Parent) {
// 如果是容器块下第一个叶子块,则需要向上查找引用
@@ -2371,7 +2371,7 @@ func getRefsCacheByDefNode(updateNode *ast.Node) (ret []*sql.Ref, changedNodes [
break
}
- parentRefs := sql.GetRefsCacheByDefIDInBox(parent.ID, updateNode.Box)
+ parentRefs := sql.GetRefsCacheByDefIDInBox(parent.ID, boxID)
if 0 < len(parentRefs) {
ret = append(ret, parentRefs...)
if _, ok := changedNodesMap[parent.ID]; !ok {
@@ -2387,7 +2387,7 @@ func getRefsCacheByDefNode(updateNode *ast.Node) (ret []*sql.Ref, changedNodes [
return ast.WalkContinue
}
- childRefs := sql.GetRefsCacheByDefIDInBox(n.ID, updateNode.Box)
+ childRefs := sql.GetRefsCacheByDefIDInBox(n.ID, boxID)
if 0 < len(childRefs) {
ret = append(ret, childRefs...)
changedNodesMap[n.ID] = n
@@ -2399,7 +2399,7 @@ func getRefsCacheByDefNode(updateNode *ast.Node) (ret []*sql.Ref, changedNodes [
// 如果是折叠标题,则需要向下查找引用
children := treenode.HeadingChildren(updateNode)
for _, child := range children {
- childRefs := sql.GetRefsCacheByDefIDInBox(child.ID, updateNode.Box)
+ childRefs := sql.GetRefsCacheByDefIDInBox(child.ID, boxID)
if 0 < len(childRefs) {
ret = append(ret, childRefs...)
changedNodesMap[child.ID] = child
diff --git a/kernel/model/transaction_ref_cache_test.go b/kernel/model/transaction_ref_cache_test.go
new file mode 100644
index 0000000000..5f14a77723
--- /dev/null
+++ b/kernel/model/transaction_ref_cache_test.go
@@ -0,0 +1,58 @@
+package model
+
+import (
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/sql"
+)
+
+func TestUpdatedNodeBoxIDUsesTreeMetadataForRefCache(t *testing.T) {
+ const (
+ boxID = "20260730000000-box0001"
+ defBlockID = "20260730000001-def0001"
+ refBlockID = "20260730000002-ref0001"
+ refRootID = "20260730000003-root001"
+ defRootID = "20260730000004-root001"
+ )
+
+ refRoot := &ast.Node{Type: ast.NodeDocument, ID: refRootID, Box: boxID}
+ refBlock := &ast.Node{Type: ast.NodeParagraph, ID: refBlockID}
+ refNode := &ast.Node{
+ Type: ast.NodeTextMark,
+ TextMarkType: "block-ref",
+ TextMarkBlockRefID: defBlockID,
+ TextMarkBlockRefSubtype: "d",
+ TextMarkTextContent: "Reference",
+ }
+ refBlock.AppendChild(refNode)
+ refRoot.AppendChild(refBlock)
+ sql.CacheRef(&parse.Tree{
+ Root: refRoot,
+ ID: refRootID,
+ Box: boxID,
+ Path: "/" + refRootID + ".sy",
+ }, refNode)
+
+ defRoot := &ast.Node{Type: ast.NodeDocument, ID: defRootID}
+ defBlock := &ast.Node{Type: ast.NodeParagraph, ID: defBlockID}
+ defRoot.AppendChild(defBlock)
+ updatedTrees := map[string]*parse.Tree{
+ defRootID: {
+ Root: defRoot,
+ ID: defRootID,
+ Box: boxID,
+ Path: "/" + defRootID + ".sy",
+ },
+ }
+ resolvedBoxID := updatedNodeBoxID(defBlock, updatedTrees)
+ if boxID != resolvedBoxID {
+ t.Fatalf("resolved box ID = %q, want %q", resolvedBoxID, boxID)
+ }
+
+ refs, _ := getRefsCacheByDefNode(defBlock, resolvedBoxID)
+ if 1 != len(refs) || refBlockID != refs[0].BlockID || boxID != refs[0].Box {
+ t.Fatalf("unexpected cached refs: %+v", refs)
+ }
+}