Skip to content

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

:bug: Fix database search result focus https://github.com/siyuan-note/siyuan/issues/18630

Daniel1f603b912253
diff --git a/app/src/protyle/render/av/relation.ts b/app/src/protyle/render/av/relation.ts
index 6387985512..6f464dd0b2 100644
--- a/app/src/protyle/render/av/relation.ts
+++ b/app/src/protyle/render/av/relation.ts
@@ -18,6 +18,7 @@ import {writeText} from "../../util/compatibility";
import {Constants} from "../../../constants";
import {openDatabaseRowByData} from "./openDatabaseRow";
import {getAVColumnTextMeasurer, getAVTableFitWidths} from "./columnWidth";
+import {getSearchAVFocus} from "./searchAVFocus";
interface IAVItem {
avID: string;
@@ -56,15 +57,11 @@ const genSearchList = (element: Element, keyword: string, options: IOpenSearchAV
}, (response) => {
let html = "";
const results = response.data.results as Array<IAVItem & { children: IAVItem[] }>;
- const hasMatchedView = showViews && results.some((item) => item.children?.some((subItem) => subItem.matched));
- let focusAssigned = false;
+ const focus = getSearchAVFocus(results, keyword);
results.forEach((item, index) => {
const hasChildren = item.children && item.children.length > 0 && showViews;
const expandChildren = hasChildren && item.children.some((subItem) => subItem.matched);
- const focusParent = !hasMatchedView && index === 0;
- if (focusParent) {
- focusAssigned = true;
- }
+ const focusParent = focus?.resultIndex === index && focus.viewIndex === undefined;
html += `<div class="b3-list-item b3-list-item--narrow${focusParent ? " b3-list-item--focus" : ""}" data-av-id="${item.avID}" data-block-id="${item.blockID}">
<span class="b3-list-item__toggle b3-list-item__toggle--hl${showViews ? "" : " fn__none"}" style="height:auto;align-self: stretch;margin: 4px 0;">
<svg class="b3-list-item__arrow${expandChildren ? " b3-list-item__arrow--open" : ""}">${hasChildren ? '<use xlink:href="#iconRight"></use>' : ""}</svg>
@@ -80,12 +77,9 @@ const genSearchList = (element: Element, keyword: string, options: IOpenSearchAV
</div>`;
if (hasChildren) {
html += `<div class="${expandChildren ? "" : "fn__none"}">`;
- item.children.forEach((subItem) => {
+ item.children.forEach((subItem, viewIndex) => {
const viewDefaultName = getViewName(subItem.viewLayout);
- const focusView = !focusAssigned && Boolean(subItem.matched);
- if (focusView) {
- focusAssigned = true;
- }
+ const focusView = focus?.resultIndex === index && focus.viewIndex === viewIndex;
html += `<div style="padding-left: 48px;" class="b3-list-item b3-list-item--narrow${focusView ? " b3-list-item--focus" : ""}" data-av-id="${subItem.avID}" data-view-id="${subItem.viewID}" data-block-id="${subItem.blockID}">
<span class="b3-list-item__text">${escapeHtml(subItem.viewName)}</span>
<span class="b3-list-item__meta">${viewDefaultName}</span>
diff --git a/app/src/protyle/render/av/searchAVFocus.test.ts b/app/src/protyle/render/av/searchAVFocus.test.ts
new file mode 100644
index 0000000000..af3f14c492
--- /dev/null
+++ b/app/src/protyle/render/av/searchAVFocus.test.ts
@@ -0,0 +1,28 @@
+import * as assert from "node:assert/strict";
+import {describe, it} from "node:test";
+import {getSearchAVFocus} from "./searchAVFocus";
+
+describe("database search focus", () => {
+ it("prefers an earlier matching database over a later matching view", () => {
+ assert.deepEqual(getSearchAVFocus([{
+ matched: true,
+ children: [{matched: false}],
+ }, {
+ matched: false,
+ children: [{matched: false}, {matched: true}],
+ }], "天空"), {resultIndex: 0});
+ });
+
+ it("focuses a matching view when its database name does not match", () => {
+ assert.deepEqual(getSearchAVFocus([{
+ matched: false,
+ children: [{matched: false}, {matched: true}],
+ }], "天空"), {resultIndex: 0, viewIndex: 1});
+ });
+
+ it("uses the first database for an empty search", () => {
+ assert.deepEqual(getSearchAVFocus([{
+ children: [{matched: true}],
+ }], ""), {resultIndex: 0});
+ });
+});
diff --git a/app/src/protyle/render/av/searchAVFocus.ts b/app/src/protyle/render/av/searchAVFocus.ts
new file mode 100644
index 0000000000..05ebc7b94e
--- /dev/null
+++ b/app/src/protyle/render/av/searchAVFocus.ts
@@ -0,0 +1,29 @@
+interface ISearchAVMatchItem {
+ matched?: boolean;
+ children?: ISearchAVMatchItem[];
+}
+
+interface ISearchAVFocus {
+ resultIndex: number;
+ viewIndex?: number;
+}
+
+export const getSearchAVFocus = (results: ISearchAVMatchItem[], keyword: string): ISearchAVFocus | undefined => {
+ if (results.length === 0) {
+ return;
+ }
+ if (keyword.trim() === "") {
+ return {resultIndex: 0};
+ }
+ for (let resultIndex = 0; resultIndex < results.length; resultIndex++) {
+ const item = results[resultIndex];
+ if (item.matched) {
+ return {resultIndex};
+ }
+ const viewIndex = item.children?.findIndex((view) => view.matched) ?? -1;
+ if (viewIndex > -1) {
+ return {resultIndex, viewIndex};
+ }
+ }
+ return {resultIndex: 0};
+};
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index c0094abfe7..6e885adade 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -2819,6 +2819,7 @@ type AvSearchTempResult struct {
AvName string
AvUpdated int64
Score float64
+ Matched bool
SearchInfo *av.AttributeViewSearchInfo
MatchedViewIDs map[string]bool
}
@@ -3113,6 +3114,7 @@ func SearchAttributeViewWithOptions(options SearchAttributeViewOptions) (ret []*
if hit || len(matchedViewIDs) > 0 {
a.AvName = name
a.Score = score
+ a.Matched = hit
a.SearchInfo = searchInfo
a.MatchedViewIDs = matchedViewIDs
avSearchTmpResults = append(avSearchTmpResults, a)
@@ -3184,6 +3186,7 @@ func SearchAttributeViewWithOptions(options SearchAttributeViewOptions) (ret []*
AvName: tmpResult.AvName,
BlockID: node.ID,
HPath: hPath,
+ Matched: tmpResult.Matched,
}
ret = append(ret, parent)