Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 27e1a6d

Browse files
committed
Merge branch 'filters_improvements'
2 parents 5a6c4b7 + e0c9425 commit 27e1a6d

File tree

7 files changed

+90
-75
lines changed

7 files changed

+90
-75
lines changed

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,35 @@
2626
"@testing-library/jest-dom": "5.16.5",
2727
"@testing-library/react": "13.3.0",
2828
"@types/jest": "28.1.8",
29-
"@types/luxon": "3.0.0",
30-
"@types/node": "18.7.11",
31-
"@types/react": "18.0.17",
29+
"@types/luxon": "3.0.1",
30+
"@types/node": "18.7.14",
31+
"@types/react": "18.0.18",
3232
"@types/react-color": "3.0.6",
3333
"@types/react-csv": "1.1.3",
3434
"@types/react-datepicker": "4.4.2",
3535
"@types/react-dom": "18.0.6",
3636
"@types/react-window": "1.8.5",
37-
"@typescript-eslint/eslint-plugin": "5.34.0",
38-
"@typescript-eslint/parser": "5.34.0",
39-
"eslint": "8.22.0",
37+
"@typescript-eslint/eslint-plugin": "5.36.1",
38+
"@typescript-eslint/parser": "5.36.1",
39+
"eslint": "8.23.0",
4040
"jest": "28.1.3",
4141
"jest-mock-extended": "2.0.7",
4242
"obsidian": "0.15.9",
43-
"rollup": "2.78.1",
43+
"rollup": "2.79.0",
4444
"rollup-plugin-terser": "7.0.2",
4545
"rollup-plugin-typescript2": "0.33.0",
4646
"ts-jest": "28.0.8",
4747
"tslib": "2.4.0",
48-
"typescript": "4.7.4"
48+
"typescript": "4.8.2"
4949
},
5050
"dependencies": {
51-
"@emotion/styled": "11.10.0",
51+
"@emotion/styled": "11.10.4",
5252
"@mui/icons-material": "5.10.3",
5353
"@mui/material": "5.10.3",
5454
"@popperjs/core": "2.11.6",
5555
"@tanstack/match-sorter-utils": "8.1.1",
56-
"@tanstack/react-table": "8.5.11",
57-
"luxon": "3.0.1",
56+
"@tanstack/react-table": "8.5.13",
57+
"luxon": "3.0.3",
5858
"fuse.js": "6.6.2",
5959
"monkey-around": "2.3.0",
6060
"obsidian-dataview": "0.5.43",

src/components/portals/DataviewFiltersPortal.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ const DataviewFiltersPortal = (props: DataviewFiltersProps) => {
123123
return (
124124
<MenuItem
125125
value={key}
126-
key={`MenuItem-OperatorSelector-${key}-${selectorProps.index}`}
126+
key={`MenuItem-OperatorSelector-${value[0]}-${selectorProps.index}`}
127127
>
128-
{value}
128+
{value[1]}
129129
</MenuItem>
130130
);
131131
})}
@@ -161,7 +161,7 @@ const DataviewFiltersPortal = (props: DataviewFiltersProps) => {
161161
const alteredFilterState = { ...filters };
162162
alteredFilterState.conditions.push({
163163
field: possibleColumns[0],
164-
operator: "EQUAL",
164+
operator: OperatorFilter.CONTAINS[0],
165165
value: "",
166166
});
167167
configActions.alterFilters(alteredFilterState);
@@ -183,7 +183,7 @@ const DataviewFiltersPortal = (props: DataviewFiltersProps) => {
183183
...styles.popper,
184184
zIndex: 4,
185185
minWidth: 200,
186-
maxWidth: 450,
186+
maxWidth: 500,
187187
padding: "0.75rem",
188188
background: StyleVariables.BACKGROUND_SECONDARY,
189189
}}
@@ -194,21 +194,24 @@ const DataviewFiltersPortal = (props: DataviewFiltersProps) => {
194194
return (
195195
<Grid
196196
container
197-
rowSpacing={0.5}
197+
rowSpacing={0.25}
198198
columnSpacing={{ xs: 0.25, sm: 0.5, md: 0.75 }}
199199
key={`Grid-container-${index}`}
200200
>
201-
<Grid item xs={3.5} key={`Grid-field-${index}`}>
201+
<Grid item xs="auto" key={`Grid-field-${index}`}>
202202
{existedColumnSelector({
203203
currentCol: field,
204204
index: index,
205205
})}
206206
</Grid>
207-
<Grid item xs={2} key={`Grid-operator-${index}`}>
207+
<Grid item xs="auto" key={`Grid-operator-${index}`}>
208208
{operatorSelector({ currentOp: operator, index: index })}
209209
</Grid>
210210
{/* if value exists, show it */}
211-
{value !== undefined && (
211+
{![
212+
OperatorFilter.IS_EMPTY[0],
213+
OperatorFilter.IS_NOT_EMPTY[0],
214+
].contains(operator) && (
212215
<Grid item xs={3.5} key={`Grid-value-${index}`}>
213216
<ValueFilterComponent
214217
value={value}
@@ -217,16 +220,14 @@ const DataviewFiltersPortal = (props: DataviewFiltersProps) => {
217220
</Grid>
218221
)}
219222
{/* Remove button */}
220-
<Grid item xs={1.5} key={`Grid-remove-${index}`}>
223+
<Grid item xs={0.75} key={`Grid-remove-${index}`}>
221224
<Button
222225
variant="contained"
223226
color="secondary"
224227
size="small"
225228
onClick={deleteConditionHadler(index)}
226229
endIcon={<DeleteIcon />}
227-
>
228-
Delete
229-
</Button>
230+
/>
230231
</Grid>
231232
</Grid>
232233
);

src/components/reducers/DataviewFilters.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import React from "react";
88
export default function DataviewFilters(props: DataviewFiltersProps) {
99
const { table } = props;
1010
const { tableState } = table.options.meta;
11-
const [ddbbConfig, filters, filterActions] = tableState.configState(
12-
(state) => [state.ddbbConfig, state.filters, state.actions]
11+
const [configInfo, filters, filterActions] = tableState.configState(
12+
(state) => [state.info, state.filters, state.actions]
1313
);
1414
const columns = tableState.columns((state) => state.columns);
1515
const dataActions = tableState.data((state) => state.actions);
@@ -18,7 +18,11 @@ export default function DataviewFilters(props: DataviewFiltersProps) {
1818
const alteredFilterState = { ...filters };
1919
alteredFilterState.enabled = !alteredFilterState.enabled;
2020
filterActions.alterFilters(alteredFilterState);
21-
dataActions.dataviewRefresh(columns, ddbbConfig, alteredFilterState);
21+
dataActions.dataviewRefresh(
22+
columns,
23+
configInfo.getLocalSettings(),
24+
alteredFilterState
25+
);
2226
};
2327

2428
return (

src/helpers/Constants.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,17 @@ export const ResizeConfiguration = Object.freeze({
247247
});
248248

249249
export const OperatorFilter = Object.freeze({
250-
EQUAL: '=',
251-
NOT_EQUAL: '!=',
252-
GREATER_THAN: '>',
253-
LESS_THAN: '<',
254-
GREATER_THAN_OR_EQUAL: '>=',
255-
LESS_THAN_OR_EQUAL: '<=',
256-
CONTAINS: 'contains',
257-
STARTS_WITH: 'starts_with',
258-
ENDS_WITH: 'ends_with',
250+
EQUAL: ['EQUAL', '='],
251+
NOT_EQUAL: ['NOT_EQUAL', '!='],
252+
GREATER_THAN: ['GREATER_THAN', '>'],
253+
LESS_THAN: ['LESS_THAN', '<'],
254+
GREATER_THAN_OR_EQUAL: ['GREATER_THAN_OR_EQUAL', '>='],
255+
LESS_THAN_OR_EQUAL: ['LESS_THAN_OR_EQUAL', '<='],
256+
CONTAINS: ['CONTAINS', 'contains'],
257+
STARTS_WITH: ['STARTS_WITH', 'starts with'],
258+
ENDS_WITH: ['ENDS_WITH', 'ends with'],
259+
IS_EMPTY: ['IS_EMPTY', 'is empty'],
260+
IS_NOT_EMPTY: ['IS_NOT_EMPTY', 'is not empty'],
259261
});
260262

261263
export function getOperatorFilterValue(keyToFind: string): string {
@@ -264,7 +266,7 @@ export function getOperatorFilterValue(keyToFind: string): string {
264266
);
265267
// Check if the key was found
266268
if (entry) {
267-
return entry[1];
269+
return entry[1][1];
268270
} else {
269271
return '';
270272
}

src/helpers/QueryHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function inlineRegexInFunctionOf(columnId: string) {
1515
const conditionalInitWithField = `[${wrappererKey}]{0,2}${columnId}[${wrappererKey}]{0,2}[:]{2}`;
1616
const thenRegex = `(^${conditionalInitWithField})(.*$)`;
1717
const elseRegex = `(.*)([(])(${conditionalInitWithField})(.*)([)])(.*$)`;
18-
const finalExpression = `(?:(?=(^${conditionalInitWithField})(${thenRegex})|(${elseRegex}))`;
18+
const finalExpression = `(?:(?=(^${conditionalInitWithField}))(${thenRegex})|(${elseRegex}))`;
1919
LOGGER.debug(`<=> inlineRegexInFunctionOf: ${finalExpression}`);
20-
return new RegExp(`(?:(?=(^${conditionalInitWithField}))(${thenRegex})|(${elseRegex}))`, 'gm');
20+
return new RegExp(`${finalExpression}`, 'gm');
2121
}

src/helpers/VaultManagement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function adapterTFilesToRows(folderPath: string, columns: TableColu
8383
folderFiles = folderFiles.where(p => !p[DatabaseCore.FRONTMATTER_KEY]);
8484
// Config filters asociated with the database
8585
if (filters.enabled && filters.conditions.length > 0) {
86-
folderFiles = folderFiles.where(p => DataviewService.filter(filters.conditions, p));
86+
folderFiles = folderFiles.where(p => DataviewService.filter(filters.conditions, p, ddbbConfig));
8787
}
8888
folderFiles.map((page) => {
8989
const noteInfo = new NoteInfo(page);
@@ -101,7 +101,7 @@ export async function obtainAllPossibleRows(folderPath: string, ddbbConfig: Loca
101101
folderFiles = folderFiles.where(p => !p[DatabaseCore.FRONTMATTER_KEY]);
102102
// Config filters asociated with the database
103103
if (filters.enabled && filters.conditions.length > 0) {
104-
folderFiles = folderFiles.where(p => DataviewService.filter(filters.conditions, p));
104+
folderFiles = folderFiles.where(p => DataviewService.filter(filters.conditions, p, ddbbConfig));
105105
}
106106
folderFiles.map((page) => {
107107
const noteInfo = new NoteInfo(page);

src/services/DataviewService.ts

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,51 @@ class DataviewProxy {
2222
}
2323
}
2424

25-
filter(condition: FilterCondition[], p: Record<string, Literal>): boolean {
25+
filter(condition: FilterCondition[], p: Record<string, Literal>, ddbbConfig: LocalSettings): boolean {
2626
if (!condition || condition.length === 0) return true;
2727
for (const c of condition) {
28-
const value = p[c.field];
29-
if (value !== undefined) {
30-
const wrapped = this.wrapLiteral(value);
31-
switch (getOperatorFilterValue(c.operator)) {
32-
case OperatorFilter.EQUAL:
33-
if (wrapped.value !== c.value) return false;
34-
break;
35-
case OperatorFilter.NOT_EQUAL:
36-
if (wrapped.value === c.value) return false;
37-
break;
38-
case OperatorFilter.GREATER_THAN:
39-
if (wrapped.value <= c.value) return false;
40-
break;
41-
case OperatorFilter.LESS_THAN:
42-
if (wrapped.value >= c.value) return false;
43-
break;
44-
case OperatorFilter.GREATER_THAN_OR_EQUAL:
45-
if (wrapped.value < c.value) return false;
46-
break;
47-
case OperatorFilter.LESS_THAN_OR_EQUAL:
48-
if (wrapped.value > c.value) return false;
49-
break;
50-
case OperatorFilter.CONTAINS:
51-
if (wrapped.type === "string" && !wrapped.value.includes(c.value)) return false;
52-
break;
53-
case OperatorFilter.STARTS_WITH:
54-
if (wrapped.type === "string" && !wrapped.value.startsWith(c.value)) return false;
55-
break;
56-
case OperatorFilter.ENDS_WITH:
57-
if (wrapped.type === "string" && !wrapped.value.endsWith(c.value)) return false;
58-
break;
59-
default:
60-
throw new Error(`Unknown operator ${c.operator}`);
61-
}
28+
const filterableValue = this.parseLiteral(p[c.field], InputType.MARKDOWN, ddbbConfig);
29+
switch (getOperatorFilterValue(c.operator)) {
30+
case OperatorFilter.IS_EMPTY[1]:
31+
if (filterableValue !== '') {
32+
return false;
33+
}
34+
break;
35+
case OperatorFilter.IS_NOT_EMPTY[1]:
36+
if (filterableValue === '') {
37+
return false;
38+
}
39+
break;
40+
case OperatorFilter.EQUAL[1]:
41+
if (filterableValue !== c.value) return false;
42+
break;
43+
case OperatorFilter.NOT_EQUAL[1]:
44+
if (filterableValue === c.value) return false;
45+
break;
46+
case OperatorFilter.GREATER_THAN[1]:
47+
if (filterableValue <= c.value) return false;
48+
break;
49+
case OperatorFilter.LESS_THAN[1]:
50+
if (filterableValue >= c.value) return false;
51+
break;
52+
case OperatorFilter.GREATER_THAN_OR_EQUAL[1]:
53+
if (filterableValue < c.value) return false;
54+
break;
55+
case OperatorFilter.LESS_THAN_OR_EQUAL[1]:
56+
if (filterableValue > c.value) return false;
57+
break;
58+
case OperatorFilter.CONTAINS[1]:
59+
if (!filterableValue.toString().includes(c.value)) return false;
60+
break;
61+
case OperatorFilter.STARTS_WITH[1]:
62+
if (!filterableValue.toString().startsWith(c.value)) return false;
63+
break;
64+
case OperatorFilter.ENDS_WITH[1]:
65+
if (!filterableValue.toString().endsWith(c.value)) return false;
66+
break;
67+
default:
68+
throw new Error(`Unknown operator ${c.operator}`);
69+
6270
}
6371
}
6472
return true;

0 commit comments

Comments
 (0)