Skip to content

Commit 4c95902

Browse files
committed
dynamic preview of attributes
1 parent c45c931 commit 4c95902

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/view/canvas.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
* @jest-environment jsdom
33
*/
44

5-
import { Component } from 'grapesjs'
65
import { onRender, isComponentVisible } from './canvas'
7-
import fs from 'fs'
8-
import path from 'path'
96
import { DataTree } from '../model/DataTree'
107
import { BinariOperator, Expression, UnariOperator } from '../types'
118

src/view/canvas.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ export function isComponentVisible(
9090

9191
// For unary operators, only condition1 is needed
9292
switch (conditionOperator) {
93-
case UnariOperator.TRUTHY:
93+
case UnariOperator.TRUTHY:
9494
return !!condition1Value
95-
case UnariOperator.FALSY:
95+
case UnariOperator.FALSY:
9696
return !condition1Value
97-
case UnariOperator.EMPTY_ARR:
97+
case UnariOperator.EMPTY_ARR:
9898
return Array.isArray(condition1Value) && condition1Value.length === 0
99-
case UnariOperator.NOT_EMPTY_ARR:
99+
case UnariOperator.NOT_EMPTY_ARR:
100100
return Array.isArray(condition1Value) && condition1Value.length > 0
101-
default:
101+
default:
102102
}
103103

104104
// For binary operators, we need condition2
@@ -110,26 +110,25 @@ export function isComponentVisible(
110110

111111
// Apply binary operator
112112
switch (conditionOperator) {
113-
case BinariOperator.EQUAL:
113+
case BinariOperator.EQUAL:
114114
return condition1Value == condition2Value
115-
case BinariOperator.NOT_EQUAL:
115+
case BinariOperator.NOT_EQUAL:
116116
return condition1Value !== condition2Value
117-
case BinariOperator.GREATER_THAN:
117+
case BinariOperator.GREATER_THAN:
118118
return Number(condition1Value) > Number(condition2Value)
119-
case BinariOperator.LESS_THAN:
119+
case BinariOperator.LESS_THAN:
120120
return Number(condition1Value) < Number(condition2Value)
121-
case BinariOperator.GREATER_THAN_OR_EQUAL:
121+
case BinariOperator.GREATER_THAN_OR_EQUAL:
122122
return Number(condition1Value) >= Number(condition2Value)
123-
case BinariOperator.LESS_THAN_OR_EQUAL:
123+
case BinariOperator.LESS_THAN_OR_EQUAL:
124124
return Number(condition1Value) <= Number(condition2Value)
125-
default:
125+
default:
126126
throw new Error(`Unknown operator ${conditionOperator}`)
127127
}
128128
}
129129

130130
function renderAttributes(
131131
component: Component,
132-
element: HTMLElement,
133132
dataTree: DataTree,
134133
): void {
135134
const privateStates = component.get('privateStates') || []
@@ -144,7 +143,7 @@ function renderAttributes(
144143
try {
145144
const value = dataTree.getValue(state.expression, component, true)
146145
if (value !== null && value !== undefined) {
147-
element.setAttribute(state.label || state.id, String(value))
146+
component.view?.el.setAttribute(state.label || state.id, String(value))
148147
}
149148
} catch (e) {
150149
console.warn(`Error evaluating attribute ${state.id}:`, e)
@@ -198,19 +197,15 @@ export function onRender(comp: Component, dataTree: DataTree, deep = 0) {
198197
const __data = renderLoopData(comp, dataTree)
199198
if (__data) {
200199
if (__data.length === 0) {
201-
// el.remove()
202-
el.setAttribute('data-hidden', '')
203-
el.style.display = 'none'
200+
el.remove()
204201
} else {
205202
// Render each loop iteration
206203
// Render first iteration in the original element
207204
setPreviewIndex(comp, 0)
208-
const visibleParent = isComponentVisible(comp, dataTree)
209-
if(visibleParent) {
205+
if(isComponentVisible(comp, dataTree)) {
210206
comp.components().forEach(c => onRender(c, dataTree, deep+1))
207+
renderAttributes(comp, dataTree)
211208
} else {
212-
// el.setAttribute('data-hidden', '')
213-
// el.style.display = 'none'
214209
el.remove()
215210
}
216211

@@ -222,9 +217,9 @@ export function onRender(comp: Component, dataTree: DataTree, deep = 0) {
222217

223218
// Set preview index for the next iteration and render into original element
224219
setPreviewIndex(comp, idx)
225-
const visible = isComponentVisible(comp, dataTree)
226-
if (visible) {
220+
if (isComponentVisible(comp, dataTree)) {
227221
renderContent(comp, dataTree, deep)
222+
renderAttributes(comp, dataTree)
228223
} else {
229224
// el.setAttribute('data-hidden', '')
230225
// el.style.display = 'none'
@@ -236,6 +231,7 @@ export function onRender(comp: Component, dataTree: DataTree, deep = 0) {
236231
} else {
237232
if(isComponentVisible(comp, dataTree)) {
238233
renderContent(comp, dataTree, deep)
234+
renderAttributes(comp, dataTree)
239235
} else {
240236
el.remove()
241237
}

0 commit comments

Comments
 (0)