Skip to content

Commit f321d52

Browse files
committed
fix: filters mess up the live preview
1 parent b071e75 commit f321d52

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/filters/liquid.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,8 @@ export default function(editor: Editor): Filter[] {
901901
output: field => field,
902902
apply: (arr, options) => {
903903
const count = parseInt(options.count as string || '1')
904-
return (arr as unknown[])
904+
console.log('=========>', {arr, options})
905+
return ((arr || []) as unknown[])
905906
.sort(() => 0.5 - Math.random())
906907
.slice(0, count)
907908
},

src/model/expressionEvaluator.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,21 @@ export function evaluatePropertyToken(
7676
let value = prevObj ? (prevObj as Record<string, unknown>)[token.fieldId] : null
7777

7878
// Handle preview index if resolvePreviewIndex is true
79+
// Don't resolve if next token is a filter - filters need to operate on full arrays
7980
if (context.resolvePreviewIndex) {
80-
value = handlePreviewIndex(value, token)
81+
const nextToken = remaining[0]
82+
if (!nextToken || nextToken.type !== 'filter') {
83+
value = handlePreviewIndex(value, token)
84+
}
8185
}
8286

83-
// For non-final tokens, always handle preview index regardless of resolvePreviewIndex
87+
// For non-final tokens, handle preview index only if next token is not a filter
88+
// Filters need to operate on full arrays, not individual items
8489
if (remaining.length > 0 && !context.resolvePreviewIndex) {
85-
value = handlePreviewIndex(value, token)
90+
const nextToken = remaining[0]
91+
if (nextToken.type !== 'filter') {
92+
value = handlePreviewIndex(value, token)
93+
}
8694
}
8795

8896
// Special handling for items state
@@ -223,8 +231,9 @@ export function evaluateFilterToken(
223231
return null
224232
}
225233

226-
// Always handle preview index if resolvePreviewIndex is true, or if there are more tokens
227-
if (context.resolvePreviewIndex || remaining.length > 0) {
234+
// Don't resolve before filters - they need arrays
235+
const nextIsFilter = remaining.length > 0 && remaining[0]?.type === 'filter'
236+
if (!nextIsFilter && (context.resolvePreviewIndex || remaining.length > 0)) {
228237
value = handlePreviewIndex(value, token)
229238
}
230239

0 commit comments

Comments
 (0)