Skip to content

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

:bug: Improve opening database results in global search https://github.com/siyuan-note/siyuan/issues/18846

Daniel5448ade0f629
diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts
index 0a718d4b6c..09093d2ece 100644
--- a/app/src/editor/util.ts
+++ b/app/src/editor/util.ts
@@ -49,6 +49,7 @@ const isSameCustomTab = (type: string, data: any, options: IOpenFileOptions) =>
export const openFileById = async (options: {
app: App,
id: string,
+ notebookId?: string,
position?: string,
mode?: TEditorMode,
action?: TProtyleAction[]
@@ -60,7 +61,7 @@ export const openFileById = async (options: {
afterOpen?: (model: Model) => void,
scrollPosition?: ScrollLogicalPosition
}) => {
- const response = await fetchSyncPost("/api/block/getBlockInfo", {id: options.id});
+ const response = await fetchSyncPost("/api/block/getBlockInfo", {id: options.id, notebook: options.notebookId});
if (response.code === -1) {
return;
}
@@ -77,6 +78,7 @@ export const openFileById = async (options: {
rootIcon: response.data.rootIcon,
rootID: response.data.rootID,
id: options.id,
+ notebookId: options.notebookId,
position: options.position,
mode: options.mode,
action: options.action,
@@ -354,6 +356,7 @@ const getUnInitTab = (options: IOpenFileOptions) => {
if (initObj.instance === "Editor" &&
(initObj.rootId === options.rootID || initObj.blockId === options.rootID)) {
initObj.blockId = options.id;
+ initObj.notebookId = options.notebookId;
initObj.mode = options.mode;
if (options.zoomIn) {
initObj.action = [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS];
@@ -571,6 +574,7 @@ const newTab = (options: IOpenFileOptions) => {
tab,
blockId: options.id,
rootId: options.rootID,
+ notebookId: options.notebookId,
action: [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS],
scrollPosition: options.scrollPosition,
});
@@ -580,6 +584,7 @@ const newTab = (options: IOpenFileOptions) => {
tab,
blockId: options.id,
rootId: options.rootID,
+ notebookId: options.notebookId,
mode: options.mode,
action: options.action,
scrollPosition: options.scrollPosition,
diff --git a/app/src/protyle/render/av/openDatabaseItem.ts b/app/src/protyle/render/av/openDatabaseItem.ts
new file mode 100644
index 0000000000..40dd4b1016
--- /dev/null
+++ b/app/src/protyle/render/av/openDatabaseItem.ts
@@ -0,0 +1,51 @@
+import {Constants} from "../../../constants";
+/// #if !MOBILE
+import {openFileById} from "../../../editor/util";
+/// #endif
+import {openMobileFileById} from "../../../mobile/editor";
+import {activateQueuedAVLocate, queueAVLocateRequest} from "./locate";
+
+import type {App} from "../../../index";
+
+export interface IDatabaseItemOpenData {
+ databaseBlockID: string;
+ notebookID?: string;
+ viewID?: string;
+ groupID?: string;
+ itemID: string;
+}
+
+export const openDatabaseItem = async (app: App, data: IDatabaseItemOpenData, options?: {
+ position?: string;
+}) => {
+ if (!data.databaseBlockID || !data.itemID) {
+ return false;
+ }
+ queueAVLocateRequest(data.databaseBlockID, {
+ itemID: data.itemID,
+ viewID: data.viewID,
+ groupID: data.groupID,
+ });
+ const action = [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] as TProtyleAction[];
+ /// #if MOBILE
+ openMobileFileById(app, data.databaseBlockID, action, undefined, data.notebookID,
+ (protyle) => activateQueuedAVLocate(protyle, data.databaseBlockID));
+ return true;
+ /// #else
+ const opened = await openFileById({
+ app,
+ id: data.databaseBlockID,
+ notebookId: data.notebookID,
+ position: options?.position,
+ action,
+ zoomIn: false,
+ afterOpen(model) {
+ const protyle = (model as { editor?: { protyle?: IProtyle } })?.editor?.protyle;
+ if (protyle) {
+ activateQueuedAVLocate(protyle, data.databaseBlockID);
+ }
+ },
+ });
+ return Boolean(opened);
+ /// #endif
+};
diff --git a/app/src/search/util.ts b/app/src/search/util.ts
index 0a79223509..abe760b655 100644
--- a/app/src/search/util.ts
+++ b/app/src/search/util.ts
@@ -49,7 +49,7 @@ import {highlightById} from "../util/highlightById";
import {getSelectionOffset} from "../protyle/util/selection";
import {electronUndo} from "../protyle/undo";
import {getContenteditableElement} from "../protyle/wysiwyg/getBlock";
-import {IDatabaseRowOpenData, openDatabaseRowByData} from "../protyle/render/av/openDatabaseRow";
+import {IDatabaseItemOpenData, openDatabaseItem} from "../protyle/render/av/openDatabaseItem";
import {scheduleSearchRequest} from "./request";
export const openGlobalSearch = (app: App, text: string, replace: boolean, searchData?: Config.IUILayoutTabSearchConfig) => {
@@ -975,7 +975,7 @@ export const openSearchEditor = async (options: {
keywords: options.keywords,
});
if (response.code === 0 && response.data) {
- const opened = await openDatabaseRowByData(options.protyle, response.data as IDatabaseRowOpenData, {
+ const opened = await openDatabaseItem(options.protyle.app, response.data as IDatabaseItemOpenData, {
position: options.openPosition,
});
if (opened) {
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index 30a31365f7..02239bb776 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -836,6 +836,7 @@ interface IOpenFileOptions {
rootIcon?: string, // 文档图标
id?: string, // file 必填
rootID?: string, // file 必填
+ notebookId?: string,
position?: string, // file 或者 asset,打开位置
page?: number | string, // asset
mode?: TEditorMode // file
diff --git a/app/src/util/uri.ts b/app/src/util/uri.ts
index fb4dd83b9d..2fd60c7d30 100644
--- a/app/src/util/uri.ts
+++ b/app/src/util/uri.ts
@@ -13,7 +13,7 @@ import {isValidBazaarPackageName} from "./bazaarPackage";
import {isBazaarAvailable, isBazaarPackageTypeAvailable} from "./bazaarAvailability";
import type {App} from "../index";
-import {activateQueuedAVLocate, queueAVLocateRequest} from "../protyle/render/av/locate";
+import {openDatabaseItem} from "../protyle/render/av/openDatabaseItem";
const bazaarTypes = new Set<TBazaarType>(["plugins", "themes", "icons", "templates", "widgets"]);
@@ -21,35 +21,30 @@ const processSiYuanUriBlocks = (app: App, uriObj: URL): boolean => {
const blockInfo = parseSiYuanUriInfo(uriObj);
if (blockInfo != null) {
const {id, focus} = blockInfo;
- if (blockInfo.avItemID) {
- queueAVLocateRequest(id, {
- itemID: blockInfo.avItemID,
- viewID: blockInfo.avViewID,
- groupID: blockInfo.avGroupID,
- });
- }
window.siyuan.editorIsFullscreen = blockInfo.fullscreen;
fetchPost("/api/block/checkBlockExist", { id }, existResponse => {
if (existResponse.data) {
checkFold(id, (zoomIn) => {
+ if (blockInfo.avItemID) {
+ void openDatabaseItem(app, {
+ databaseBlockID: id,
+ itemID: blockInfo.avItemID,
+ viewID: blockInfo.avViewID,
+ groupID: blockInfo.avGroupID,
+ });
+ return;
+ }
/// #if !MOBILE
openFileById({
app,
id,
- action: blockInfo.avItemID ? [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] :
- ((zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]),
- zoomIn: blockInfo.avItemID ? false : zoomIn || focus,
- afterOpen: (model) => {
- const protyle = (model as { editor?: { protyle?: IProtyle } })?.editor?.protyle;
- if (protyle) {
- activateQueuedAVLocate(protyle, id);
- }
- },
+ action: (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] :
+ [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],
+ zoomIn: zoomIn || focus,
});
/// #else
- openMobileFileById(app, id, blockInfo.avItemID ? [Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL] :
- ((zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]),
- undefined, undefined, blockInfo.avItemID ? (protyle) => activateQueuedAVLocate(protyle, id) : undefined);
+ openMobileFileById(app, id, (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] :
+ [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
/// #endif
});
/// #if !BROWSER
diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go
index b96343a56a..5fa471d1e0 100644
--- a/kernel/model/attribute_view_render.go
+++ b/kernel/model/attribute_view_render.go
@@ -52,6 +52,8 @@ type AttributeViewSearchTarget struct {
AvID string `json:"avID"`
DatabaseBlockID string `json:"databaseBlockID"`
NotebookID string `json:"notebookID"`
+ ViewID string `json:"viewID,omitempty"`
+ GroupID string `json:"groupID,omitempty"`
ItemID string `json:"itemID"`
ValueID string `json:"valueID"`
MatchedValueID string `json:"matchedValueID"`
@@ -67,6 +69,11 @@ type attributeViewSearchMatch struct {
keyID string
}
+type attributeViewSearchItem struct {
+ itemID string
+ groupID string
+}
+
func GetAttributeViewSearchTarget(blockID string, keywords []string) (ret *AttributeViewSearchTarget) {
waitForSyncingStorages()
@@ -96,38 +103,40 @@ func GetAttributeViewSearchTarget(blockID string, keywords []string) (ret *Attri
return
}
- var orderedItemIDs []string
+ var orderedItems []attributeViewSearchItem
+ viewID := ""
blockValues := attrView.GetBlockKeyValues()
pageSize := len(matches) + 1
if nil != blockValues && len(blockValues.Values) >= pageSize {
pageSize = len(blockValues.Values) + 1
}
viewable, renderErr := renderAttributeView(attrView, blockID, "", "", "", 1, pageSize, nil, false, false, nil, "")
- if nil == renderErr {
- orderedItemIDs = appendAttributeViewSearchItemIDs(orderedItemIDs, viewable, false)
- orderedItemIDs = appendAttributeViewSearchItemIDs(orderedItemIDs, viewable, true)
- } else {
+ if nil == renderErr && nil != viewable {
+ viewID = viewable.GetID()
+ orderedItems = appendAttributeViewSearchItems(orderedItems, viewable, false)
+ orderedItems = appendAttributeViewSearchItems(orderedItems, viewable, true)
+ } else if nil != renderErr {
logging.LogWarnf("render attribute view [%s] for search target failed: %s", attrView.ID, renderErr)
}
if nil != blockValues {
for _, value := range blockValues.Values {
if nil != value {
- orderedItemIDs = append(orderedItemIDs, value.BlockID)
+ orderedItems = append(orderedItems, attributeViewSearchItem{itemID: value.BlockID})
}
}
}
visited := map[string]bool{}
- for _, itemID := range orderedItemIDs {
- if visited[itemID] {
+ for _, item := range orderedItems {
+ if visited[item.itemID] {
continue
}
- visited[itemID] = true
- match := matches[itemID]
+ visited[item.itemID] = true
+ match := matches[item.itemID]
if nil == match {
continue
}
- blockValue := attrView.GetBlockValue(itemID)
+ blockValue := attrView.GetBlockValue(item.itemID)
if nil == blockValue || nil == blockValue.Block {
continue
}
@@ -135,7 +144,9 @@ func GetAttributeViewSearchTarget(blockID string, keywords []string) (ret *Attri
AvID: attrView.ID,
DatabaseBlockID: blockID,
NotebookID: tree.Box,
- ItemID: itemID,
+ ViewID: viewID,
+ GroupID: item.groupID,
+ ItemID: item.itemID,
ValueID: blockValue.ID,
MatchedValueID: match.valueID,
MatchedKeyID: match.keyID,
@@ -184,9 +195,9 @@ func getAttributeViewSearchMatches(attrView *av.AttributeView, keywords []string
return
}
-func appendAttributeViewSearchItemIDs(itemIDs []string, viewable av.Viewable, hiddenGroups bool) []string {
+func appendAttributeViewSearchItems(items []attributeViewSearchItem, viewable av.Viewable, hiddenGroups bool) []attributeViewSearchItem {
if nil == viewable {
- return itemIDs
+ return items
}
baseInstance := getAttributeViewBaseInstance(viewable)
if nil != baseInstance && 0 < len(baseInstance.Groups) {
@@ -196,21 +207,21 @@ func appendAttributeViewSearchItemIDs(itemIDs []string, viewable av.Viewable, hi
}
if collection, ok := group.(av.Collection); ok {
for _, item := range collection.GetItems() {
- itemIDs = append(itemIDs, item.GetID())
+ items = append(items, attributeViewSearchItem{itemID: item.GetID(), groupID: group.GetID()})
}
}
}
- return itemIDs
+ return items
}
if hiddenGroups {
- return itemIDs
+ return items
}
if collection, ok := viewable.(av.Collection); ok {
for _, item := range collection.GetItems() {
- itemIDs = append(itemIDs, item.GetID())
+ items = append(items, attributeViewSearchItem{itemID: item.GetID()})
}
}
- return itemIDs
+ return items
}
func GetAttributeViewItemStatuses(blockID, avID, viewID, query string, itemIDs []string) (ret map[string]string, err error) {
@@ -238,14 +249,14 @@ func getAttributeViewItemStatuses(attrView *av.AttributeView, viewable av.Viewab
}
}
}
- for _, itemID := range appendAttributeViewSearchItemIDs(nil, viewable, true) {
- if requested[itemID] && "filtered" == ret[itemID] {
- ret[itemID] = "groupHidden"
+ for _, item := range appendAttributeViewSearchItems(nil, viewable, true) {
+ if requested[item.itemID] && "filtered" == ret[item.itemID] {
+ ret[item.itemID] = "groupHidden"
}
}
- for _, itemID := range appendAttributeViewSearchItemIDs(nil, viewable, false) {
- if requested[itemID] {
- ret[itemID] = "visible"
+ for _, item := range appendAttributeViewSearchItems(nil, viewable, false) {
+ if requested[item.itemID] {
+ ret[item.itemID] = "visible"
}
}
return
diff --git a/kernel/model/attribute_view_render_target_test.go b/kernel/model/attribute_view_render_target_test.go
index 6f84c04779..103ab0c412 100644
--- a/kernel/model/attribute_view_render_target_test.go
+++ b/kernel/model/attribute_view_render_target_test.go
@@ -86,13 +86,13 @@ func TestGetAttributeViewSearchMatches(t *testing.T) {
}
}
-func TestAppendAttributeViewSearchItemIDs(t *testing.T) {
+func TestAppendAttributeViewSearchItems(t *testing.T) {
hiddenGroup := &av.Table{
- BaseInstance: &av.BaseInstance{GroupHidden: 2},
+ BaseInstance: &av.BaseInstance{ID: "20260726000000-grouphid", GroupHidden: 2},
Rows: []*av.TableRow{{ID: "20260726000000-hiddena"}},
}
visibleGroup := &av.Table{
- BaseInstance: &av.BaseInstance{},
+ BaseInstance: &av.BaseInstance{ID: "20260726000000-groupvis"},
Rows: []*av.TableRow{
{ID: "20260726000000-visiblea"},
{ID: "20260726000000-visibleb"},
@@ -100,17 +100,30 @@ func TestAppendAttributeViewSearchItemIDs(t *testing.T) {
}
viewable := &av.Table{BaseInstance: &av.BaseInstance{Groups: []av.Viewable{hiddenGroup, visibleGroup}}}
- itemIDs := appendAttributeViewSearchItemIDs(nil, viewable, false)
- itemIDs = appendAttributeViewSearchItemIDs(itemIDs, viewable, true)
- expected := []string{"20260726000000-visiblea", "20260726000000-visibleb", "20260726000000-hiddena"}
- if len(expected) != len(itemIDs) {
- t.Fatalf("unexpected item IDs: %+v", itemIDs)
+ items := appendAttributeViewSearchItems(nil, viewable, false)
+ items = appendAttributeViewSearchItems(items, viewable, true)
+ expected := []attributeViewSearchItem{
+ {itemID: "20260726000000-visiblea", groupID: "20260726000000-groupvis"},
+ {itemID: "20260726000000-visibleb", groupID: "20260726000000-groupvis"},
+ {itemID: "20260726000000-hiddena", groupID: "20260726000000-grouphid"},
+ }
+ if len(expected) != len(items) {
+ t.Fatalf("unexpected items: %+v", items)
}
- for i, itemID := range itemIDs {
- if expected[i] != itemID {
- t.Fatalf("unexpected item IDs: %+v", itemIDs)
+ for i, item := range items {
+ if expected[i] != item {
+ t.Fatalf("unexpected items: %+v", items)
}
}
+
+ ungrouped := &av.Table{
+ BaseInstance: &av.BaseInstance{ID: "20260726000000-viewaaaa"},
+ Rows: []*av.TableRow{{ID: "20260726000000-itemaaaa"}},
+ }
+ items = appendAttributeViewSearchItems(nil, ungrouped, false)
+ if 1 != len(items) || "20260726000000-itemaaaa" != items[0].itemID || "" != items[0].groupID {
+ t.Fatalf("unexpected ungrouped items: %+v", items)
+ }
}
func TestGetAttributeViewItemStatuses(t *testing.T) {