Skip to content

Commit 292a4ea

Browse files
committed
feat: preview on canvas
1 parent 7a4b50a commit 292a4ea

File tree

3 files changed

+627
-57
lines changed

3 files changed

+627
-57
lines changed

src/model/DataTree.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class DataTree {
217217
// Special handling for items state - always wrap result in array when resolvePreviewIndex is true
218218
const previewIndex = resolvedExpression[resolvedExpression.length - 1].previewIndex
219219
if (state.storedStateId === 'items' && typeof previewIndex !== 'undefined') {
220-
// @ts-ignore
220+
// @ts-expect-error - Adding runtime property for items state handling
221221
resolvedExpression[0].isItems = true
222222
}
223223
return this.getValue(resolvedExpression.concat(...rest), component, resolvePreviewIndex, prevValues)
@@ -244,9 +244,16 @@ export class DataTree {
244244
// Now get the next value
245245
let value = prevObj ? (prevObj as Record<string, unknown>)[token.fieldId] : null
246246

247-
// value = rest.length > 0 ? typeof token.previewIndex === 'undefined' ? this.handlePreviewIndex(value, token, component) : [this.handlePreviewIndex(value, token, component)] : value
248-
value = rest.length > 0 ? this.handlePreviewIndex(value, token, component) : value
249-
// @ts-ignore
247+
// Always handle preview index if resolvePreviewIndex is true
248+
if (resolvePreviewIndex) {
249+
value = this.handlePreviewIndex(value, token)
250+
}
251+
252+
// For non-final tokens, always handle preview index regardless of resolvePreviewIndex
253+
if (rest.length > 0 && !resolvePreviewIndex) {
254+
value = this.handlePreviewIndex(value, token)
255+
}
256+
// @ts-expect-error - Runtime property check for items handling
250257
if (token.isItems && typeof token.previewIndex !== 'undefined') {
251258
if(rest.length > 0) {
252259
value = [value]
@@ -275,7 +282,10 @@ export class DataTree {
275282
return this.getValue(rest, component, resolvePreviewIndex, prevValues)
276283
}
277284

278-
value = resolvePreviewIndex || rest.length > 0 ? this.handlePreviewIndex(value, token, component) : value
285+
// Always handle preview index if resolvePreviewIndex is true, or if there are more tokens
286+
if (resolvePreviewIndex || rest.length > 0) {
287+
value = this.handlePreviewIndex(value, token)
288+
}
279289

280290
return this.getValue(rest, component, resolvePreviewIndex, value)
281291
}
@@ -288,18 +298,14 @@ export class DataTree {
288298
/**
289299
* Handle preview index for a given value and token
290300
*/
291-
handlePreviewIndex(value: unknown, token: StoredToken, component?: Component): unknown {
292-
console.log('handlePreviewIndex', {value, token})
301+
handlePreviewIndex(value: unknown, token: StoredToken): unknown {
293302
if (typeof token.previewIndex === 'undefined') {
294-
console.log('No preview index defined, returning value as-is')
295303
return value
296304
}
297305

298306
if(Array.isArray(value)) {
299-
console.log(`Extracting item at index ${token.previewIndex} from array of length ${value.length}`)
300307
return value[token.previewIndex]
301308
}
302-
console.log('Value is not an array, returning as-is')
303309
return value
304310
}
305311

0 commit comments

Comments
 (0)