Skip to content

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

:bug: Correct database filter condition count https://github.com/siyuan-note/siyuan/issues/18633

Daniel0c5da7bd38d0
diff --git a/app/src/protyle/render/av/filter.ts b/app/src/protyle/render/av/filter.ts
index d2c05c86c7..354af69f57 100644
--- a/app/src/protyle/render/av/filter.ts
+++ b/app/src/protyle/render/av/filter.ts
@@ -9,6 +9,7 @@ import {unicode2Emoji} from "../../../emoji";
import {fetchPost, fetchSyncPost} from "../../../util/fetch";
import {getFieldsByData} from "./view";
import {Constants} from "../../../constants";
+import {countFilterLeaves} from "./filterTree";
const isExactRelationOperator = (operator: string) =>
operator === "Contains any item" || operator === "Does not contain any item";
@@ -301,8 +302,7 @@ export const getFiltersHTML = (data: IAV) => {
: "and";
html = genNodeHTML(root, "", 0, "", rootCombination);
- const countLeaves = (nodes: IAVFilter[]): number => nodes.reduce((sum, n) => sum + (n.filters ? countLeaves(n.filters) : 1), 0);
- const leafCount = countLeaves(root.filters || []);
+ const leafCount = countFilterLeaves(root.filters || []);
return `<div class="b3-menu__items">
<button class="b3-menu__item" data-type="nobg">
diff --git a/app/src/protyle/render/av/filterTree.test.ts b/app/src/protyle/render/av/filterTree.test.ts
new file mode 100644
index 0000000000..5dab250a4d
--- /dev/null
+++ b/app/src/protyle/render/av/filterTree.test.ts
@@ -0,0 +1,43 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {countFilterLeaves, isFilterGroup} from "./filterTree";
+
+describe("database filter tree helpers", () => {
+ it("recognizes groups whose empty filters are omitted during serialization", () => {
+ const root = {combination: "and"} as IAVFilter;
+
+ assert.equal(isFilterGroup(root), true);
+ assert.equal(countFilterLeaves([root]), 0);
+ });
+
+ it("does not count explicitly empty groups as leaves", () => {
+ assert.equal(countFilterLeaves([{combination: "and", filters: []}]), 0);
+ });
+
+ it("counts ordinary and legacy flat leaves", () => {
+ const filters = [
+ {column: "column-a", operator: "Is empty"},
+ {column: "column-b", operator: "Is not empty"},
+ ] as IAVFilter[];
+
+ assert.equal(countFilterLeaves(filters), 2);
+ });
+
+ it("counts only real leaves in nested groups", () => {
+ const filters = [{
+ combination: "and",
+ filters: [
+ {column: "column-a", operator: "Is empty"},
+ {
+ combination: "or",
+ filters: [
+ {column: "column-b", operator: "Is not empty"},
+ {combination: "and"},
+ ],
+ },
+ ],
+ }] as IAVFilter[];
+
+ assert.equal(countFilterLeaves(filters), 2);
+ });
+});
diff --git a/app/src/protyle/render/av/filterTree.ts b/app/src/protyle/render/av/filterTree.ts
new file mode 100644
index 0000000000..765034e0e0
--- /dev/null
+++ b/app/src/protyle/render/av/filterTree.ts
@@ -0,0 +1,14 @@
+// isFilterGroup 判断筛选节点是否为分组,并兼容序列化时省略空 filters 的分组。
+export const isFilterGroup = (filter: IAVFilter): boolean => {
+ return Boolean(filter.filters || filter.combination);
+};
+
+// countFilterLeaves 递归统计筛选树中的真实叶子数量,分组节点不计入。
+export const countFilterLeaves = (filters: IAVFilter[]): number => {
+ return filters.reduce((count, filter) => {
+ if (isFilterGroup(filter)) {
+ return count + countFilterLeaves(filter.filters || []);
+ }
+ return count + 1;
+ }, 0);
+};
diff --git a/app/src/protyle/render/av/view.ts b/app/src/protyle/render/av/view.ts
index c1df745cbc..b82fd8cd6d 100644
--- a/app/src/protyle/render/av/view.ts
+++ b/app/src/protyle/render/av/view.ts
@@ -10,22 +10,7 @@ import {Constants} from "../../../constants";
import {clearSelect} from "../../util/clear";
import {getAVVisibleViewIDs, setAVVisibleViewIDs} from "./viewVisibility";
import {isNotEditBlock} from "../../wysiwyg/getBlock";
-
-// countFilterLeaves 递归统计过滤节点树中的叶子数量(分组不计入)。
-const countFilterLeaves = (filters: IAVFilter[]): number => {
- let count = 0;
- const walk = (nodes: IAVFilter[]) => {
- nodes.forEach(n => {
- if (n.filters) {
- walk(n.filters);
- } else {
- count++;
- }
- });
- };
- walk(filters);
- return count;
-};
+import {countFilterLeaves} from "./filterTree";
export const setAVBlockVisibleViewIDs = (
protyle: IProtyle,