@grapesjs/studio-sdk-plugins 1.0.22 → 1.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- const b = "app.grapesjs.com", I = "app-stage.grapesjs.com", S = [
2
- b,
3
- I,
1
+ const C = "app.grapesjs.com", w = "app-stage.grapesjs.com", b = [
2
+ C,
3
+ w,
4
4
  "localhost",
5
5
  "127.0.0.1",
6
6
  ".local-credentialless.webcontainer.io",
@@ -9,83 +9,83 @@ const b = "app.grapesjs.com", I = "app-stage.grapesjs.com", S = [
9
9
  // For stackblitz.com demos
10
10
  "-sandpack.codesandbox.io"
11
11
  // For Sandpack demos
12
- ], v = "license:check:start", w = "license:check:end", P = () => typeof window < "u", k = ({ isDev: i, isStage: t }) => `${i ? "" : `https://${t ? I : b}`}/api`, T = () => {
13
- const i = P() && window.location.hostname;
14
- return !!i && (S.includes(i) || S.some((t) => i.endsWith(t)));
12
+ ], P = "license:check:start", L = "license:check:end", v = () => typeof window < "u", O = ({ isDev: o, isStage: t }) => `${o ? "" : `https://${t ? w : C}`}/api`, k = () => {
13
+ const o = v() && window.location.hostname;
14
+ return !!o && (b.includes(o) || b.some((t) => o.endsWith(t)));
15
15
  };
16
- async function M({
17
- path: i,
16
+ async function T({
17
+ path: o,
18
18
  baseApiUrl: t,
19
19
  method: e = "GET",
20
- headers: r = {},
20
+ headers: n = {},
21
21
  params: s,
22
- body: n
22
+ body: r
23
23
  }) {
24
- const o = `${t || k({ isDev: !1, isStage: !1 })}${i}`, l = {
24
+ const a = `${t || O({ isDev: !1, isStage: !1 })}${o}`, c = {
25
25
  method: e,
26
26
  headers: {
27
27
  "Content-Type": "application/json",
28
- ...r
28
+ ...n
29
29
  }
30
30
  };
31
- n && (l.body = JSON.stringify(n));
32
- const p = s ? new URLSearchParams(s).toString() : "", c = p ? `?${p}` : "", h = await fetch(`${o}${c}`, l);
31
+ r && (c.body = JSON.stringify(r));
32
+ const p = s ? new URLSearchParams(s).toString() : "", l = p ? `?${p}` : "", h = await fetch(`${a}${l}`, c);
33
33
  if (!h.ok)
34
34
  throw new Error(`HTTP error! status: ${h.status}`);
35
35
  return h.json();
36
36
  }
37
- var d = /* @__PURE__ */ ((i) => (i.free = "free", i.startup = "startup", i.business = "business", i.enterprise = "enterprise", i))(d || {});
38
- const $ = {
39
- [d.free]: 0,
40
- [d.startup]: 10,
41
- [d.business]: 20,
42
- [d.enterprise]: 30
37
+ var g = /* @__PURE__ */ ((o) => (o.free = "free", o.startup = "startup", o.business = "business", o.enterprise = "enterprise", o))(g || {});
38
+ const I = {
39
+ [g.free]: 0,
40
+ [g.startup]: 10,
41
+ [g.business]: 20,
42
+ [g.enterprise]: 30
43
43
  };
44
- function N(i) {
45
- const t = i;
46
- return t.init = (e) => (r) => i(r, e), t;
44
+ function N(o) {
45
+ const t = o;
46
+ return t.init = (e) => (n) => o(n, e), t;
47
47
  }
48
- const O = (i) => /* @__PURE__ */ N(i);
49
- async function _({
50
- editor: i,
48
+ const M = (o) => /* @__PURE__ */ N(o);
49
+ async function B({
50
+ editor: o,
51
51
  plan: t,
52
52
  pluginName: e,
53
- licenseKey: r,
53
+ licenseKey: n,
54
54
  cleanup: s
55
55
  }) {
56
- let n = "", a = !1;
57
- const o = T(), l = (c) => {
58
- console.warn("Cleanup plugin:", e, "Reason:", c), s();
59
- }, p = (c = {}) => {
60
- var E;
61
- const { error: h, sdkLicense: f } = c, u = (E = c.plan) == null ? void 0 : E.category;
62
- if (!(f || c.license) || h)
63
- l(h || "Invalid license");
64
- else if (u) {
65
- const C = $[t], L = $[u];
66
- C > L && l({ pluginRequiredPlan: t, licensePlan: u });
56
+ let r = "", i = !1;
57
+ const a = k(), c = (l) => {
58
+ console.warn("Cleanup plugin:", e, "Reason:", l), s();
59
+ }, p = (l = {}) => {
60
+ var u;
61
+ const { error: h, sdkLicense: d } = l, f = (u = l.plan) == null ? void 0 : u.category;
62
+ if (!(d || l.license) || h)
63
+ c(h || "Invalid license");
64
+ else if (f) {
65
+ const m = I[t], S = I[f];
66
+ m > S && c({ pluginRequiredPlan: t, licensePlan: f });
67
67
  }
68
68
  };
69
- i.on(v, (c) => {
70
- n = c == null ? void 0 : c.baseApiUrl, a = !0;
71
- }), i.on(w, (c) => {
72
- p(c);
69
+ o.on(P, (l) => {
70
+ r = l == null ? void 0 : l.baseApiUrl, i = !0;
71
+ }), o.on(L, (l) => {
72
+ p(l);
73
73
  }), setTimeout(async () => {
74
- if (!a) {
75
- if (o) return;
76
- if (r) {
77
- const c = await B({ licenseKey: r, pluginName: e, baseApiUrl: n });
78
- c && p(c);
74
+ if (!i) {
75
+ if (a) return;
76
+ if (n) {
77
+ const l = await _({ licenseKey: n, pluginName: e, baseApiUrl: r });
78
+ l && p(l);
79
79
  } else
80
- l("The `licenseKey` option not provided");
80
+ c("The `licenseKey` option not provided");
81
81
  }
82
82
  }, 2e3);
83
83
  }
84
- async function B(i) {
85
- const { licenseKey: t, pluginName: e, baseApiUrl: r } = i;
84
+ async function _(o) {
85
+ const { licenseKey: t, pluginName: e, baseApiUrl: n } = o;
86
86
  try {
87
- return (await M({
88
- baseApiUrl: r,
87
+ return (await T({
88
+ baseApiUrl: n,
89
89
  path: `/sdk/${t || "na"}`,
90
90
  method: "POST",
91
91
  params: {
@@ -97,7 +97,7 @@ async function B(i) {
97
97
  return console.error("Error during SDK license check:", s), !1;
98
98
  }
99
99
  }
100
- var g = /* @__PURE__ */ ((i) => (i.variable = "data-variable", i.condition = "data-condition", i.conditionTrue = "data-condition-true-content", i.conditionFalse = "data-condition-false-content", i.collection = "data-collection", i.collectionItem = "data-collection-item", i))(g || {});
100
+ var y = /* @__PURE__ */ ((o) => (o.variable = "data-variable", o.condition = "data-condition", o.conditionTrue = "data-condition-true-content", o.conditionFalse = "data-condition-false-content", o.collection = "data-collection", o.collectionItem = "data-collection-item", o))(y || {});
101
101
  class W {
102
102
  constructor(t) {
103
103
  this.strategy = t, this.requiredHelpers = /* @__PURE__ */ new Set(), this.helperLibrary = {};
@@ -127,131 +127,132 @@ class W {
127
127
  this.requiredHelpers.clear();
128
128
  }
129
129
  }
130
- function j(i) {
131
- return typeof i == "object" && i.type === g.variable;
130
+ function j(o) {
131
+ return typeof o == "object" && o.type === y.variable;
132
132
  }
133
- function m({
134
- component: i,
133
+ function x({
134
+ component: o,
135
135
  value: t,
136
136
  getCustomPath: e
137
137
  }) {
138
138
  if (j(t) && !t.collectionId && e) {
139
- const r = t.path ?? "", s = r.split(".");
139
+ const n = t.path ?? "", s = n.split(".");
140
140
  return {
141
141
  ...t,
142
- path: e({ component: i, path: r, paths: s })
142
+ path: e({ component: o, path: n, paths: s })
143
143
  };
144
144
  }
145
145
  return t;
146
146
  }
147
- const A = (i) => {
148
- y(i, g.variable, {
147
+ const A = (o) => {
148
+ $(o, y.variable, {
149
149
  toHTML(t) {
150
- var a;
151
- const e = this.tagName, r = this.__attrToString(t), s = (a = this.getDataResolver) == null ? void 0 : a.call(this), n = m({
150
+ var i;
151
+ const e = this.tagName, n = this.__attrToString(t), s = (i = this.getDataResolver) == null ? void 0 : i.call(this), r = x({
152
152
  component: this,
153
153
  value: s,
154
154
  getCustomPath: t == null ? void 0 : t.getCustomPath
155
155
  });
156
- return `<${e}${r ? ` ${r}` : ""}>${t.exporter.generateVariable(n)}</${e}>`;
156
+ return `<${e}${n ? ` ${n}` : ""}>${t.exporter.generateVariable(r)}</${e}>`;
157
157
  }
158
- }), y(i, g.condition, {
158
+ }), $(o, y.condition, {
159
159
  toHTML(t) {
160
- var p, c;
161
- const e = t.exporter, r = t == null ? void 0 : t.getCustomPath, s = this.components(), [n, a] = (s == null ? void 0 : s.models) ?? [], o = (p = this.getDataResolver) == null ? void 0 : p.call(this), l = ((c = this.resolveCondition) == null ? void 0 : c.call(this, o == null ? void 0 : o.condition, r)) ?? {};
162
- return `
163
- ${e.generateConditionalStart(l)}
164
- ${(n == null ? void 0 : n.toHTML(t)) ?? ""}
165
- ${e.generateConditionElse()}
166
- ${(a == null ? void 0 : a.toHTML(t)) ?? ""}
167
- ${e.generateConditionalEnd()}
160
+ var l, h;
161
+ const e = t.exporter, n = t == null ? void 0 : t.getCustomPath, s = this.components(), [r, i] = (s == null ? void 0 : s.models) ?? [], a = (l = this.getDataResolver) == null ? void 0 : l.call(this), c = ((h = this.resolveCondition) == null ? void 0 : h.call(this, a == null ? void 0 : a.condition, n)) ?? {};
162
+ let p = "";
163
+ return i && (p = `
164
+ ${e.generateConditionElse()}
165
+ ${i.toHTML(t) ?? ""}`), `
166
+ ${e.generateConditionalStart(c)}
167
+ ${(r == null ? void 0 : r.toHTML(t)) ?? ""}${p}
168
+ ${e.generateConditionalEnd()}
168
169
  `;
169
170
  },
170
171
  resolveCondition(t, e) {
171
- var s, n;
172
+ var s, r;
172
173
  if (!t) return {};
173
174
  if (t.operator === "and" || t.operator === "or")
174
175
  return {
175
176
  operator: t.operator,
176
- statements: ((n = (s = t.statements) == null ? void 0 : s.map) == null ? void 0 : n.call(s, (a) => {
177
- var o;
178
- return ((o = this.resolveCondition) == null ? void 0 : o.call(this, a, e)) ?? {};
177
+ statements: ((r = (s = t.statements) == null ? void 0 : s.map) == null ? void 0 : r.call(s, (i) => {
178
+ var a;
179
+ return ((a = this.resolveCondition) == null ? void 0 : a.call(this, i, e)) ?? {};
179
180
  })) ?? []
180
181
  };
181
- const r = { component: this, getCustomPath: e };
182
+ const n = { component: this, getCustomPath: e };
182
183
  return {
183
184
  ...t,
184
- left: m({ ...r, value: t.left }),
185
- right: m({ ...r, value: t.right })
185
+ left: x({ ...n, value: t.left }),
186
+ right: x({ ...n, value: t.right })
186
187
  };
187
188
  }
188
- }), y(i, g.collection, {
189
+ }), $(o, y.collection, {
189
190
  toHTML(t) {
190
- const e = t.exporter, r = this.tagName, s = this.__attrToString(t), n = this.getDataResolver(), a = {
191
- ...n,
192
- dataSource: m({
191
+ const e = t.exporter, n = this.tagName, s = this.__attrToString(t), r = this.getDataResolver(), i = {
192
+ ...r,
193
+ dataSource: x({
193
194
  component: this,
194
- value: n == null ? void 0 : n.dataSource,
195
+ value: r == null ? void 0 : r.dataSource,
195
196
  getCustomPath: t == null ? void 0 : t.getCustomPath
196
197
  })
197
- }, o = this.components().at(0);
198
+ }, a = this.components().at(0);
198
199
  return `
199
- <${r}${s ? ` ${s}` : ""}>${e.generateCollectionStart(a)}
200
- ${(o == null ? void 0 : o.toHTML(t)) ?? ""}
201
- ${e.generateCollectionEnd()}</${r}>
200
+ <${n}${s ? ` ${s}` : ""}>${e.generateCollectionStart(i)}
201
+ ${(a == null ? void 0 : a.toHTML(t)) ?? ""}
202
+ ${e.generateCollectionEnd()}</${n}>
202
203
  `;
203
204
  }
204
205
  });
205
- }, y = (i, t, e) => {
206
- const r = i.Components.getType(t);
207
- if (!r) {
206
+ }, $ = (o, t, e) => {
207
+ const n = o.Components.getType(t);
208
+ if (!n) {
208
209
  console.warn(`Component type ${t} not found`);
209
210
  return;
210
211
  }
211
- const { model: s } = r;
212
- i.Components.addType(t, {
212
+ const { model: s } = n;
213
+ o.Components.addType(t, {
213
214
  model: {
214
215
  ...e,
215
- toHTML(n = {}) {
216
- var o;
217
- const { exporter: a } = n;
218
- return a ? a.export ? a.export({ component: this }) : ((o = e.toHTML) == null ? void 0 : o.call(this, n)) ?? "" : s.prototype.toHTML.call(this, n) ?? "";
216
+ toHTML(r = {}) {
217
+ var a;
218
+ const { exporter: i } = r;
219
+ return i ? i.export ? i.export({ component: this }) : ((a = e.toHTML) == null ? void 0 : a.call(this, r)) ?? "" : s.prototype.toHTML.call(this, r) ?? "";
219
220
  }
220
221
  }
221
222
  });
222
- }, J = (i) => {
223
- const t = i;
223
+ }, J = (o) => {
224
+ const t = o;
224
225
  return t.logicalOperator === "and" || t.logicalOperator === "or";
225
- }, H = (i, t) => (i.config.optsHtml = {
226
- ...i.config.optsHtml,
226
+ }, F = (o, t) => (o.config.optsHtml = {
227
+ ...o.config.optsHtml,
227
228
  exporter: new W(t)
228
- }, A(i), () => {
229
- i.config.optsHtml.exporter = void 0;
230
- }), U = (i, t) => {
229
+ }, A(o), () => {
230
+ o.config.optsHtml.exporter = void 0;
231
+ }), H = (o, t) => {
231
232
  const e = (s) => {
232
- const { input: n } = s, { contains: a } = t;
233
- (!a || n.includes(a)) && (s.input = t.importer.import(n));
234
- }, r = i.Parser.events.htmlBefore;
235
- return i.on(r, e), () => {
236
- i.off(r, e);
233
+ const { input: r } = s, { contains: i } = t;
234
+ (!i || r.includes(i)) && (s.input = t.importer.import(r));
235
+ }, n = o.Parser.events.htmlBefore;
236
+ return o.on(n, e), () => {
237
+ o.off(n, e);
237
238
  };
238
239
  };
239
- class D {
240
+ class U {
240
241
  getVariableSyntax(t) {
241
- const { defaultValue: e } = t, r = this.getFullPath(t), s = e ? ` || \`${e}\`` : "";
242
- return `<%= ${r}${s} %>`;
242
+ const { defaultValue: e } = t, n = this.getFullPath(t), s = e ? ` || \`${e}\`` : "";
243
+ return `<%= ${n}${s} %>`;
243
244
  }
244
245
  getCollectionStartSyntax(t) {
245
- const { collectionId: e, dataSource: r, startIndex: s, endIndex: n } = t, a = (r == null ? void 0 : r.path) || e || "", o = this._sanitizeVariableName(e);
246
- let l = "";
247
- if (s !== void 0 || n !== void 0) {
248
- const p = s ?? 0, c = n !== void 0 ? n : "";
249
- l = `.slice(${p}${c !== "" ? `, ${c}` : ""})`;
246
+ const { collectionId: e, dataSource: n, startIndex: s, endIndex: r } = t, i = (n == null ? void 0 : n.path) || e || "", a = this._sanitizeVariableName(e);
247
+ let c = "";
248
+ if (s !== void 0 || r !== void 0) {
249
+ const p = s ?? 0, l = r !== void 0 ? r : "";
250
+ c = `.slice(${p}${l !== "" ? `, ${l}` : ""})`;
250
251
  }
251
- return `<% ${a}${l}.forEach(function(currentItem, index, arr) {
252
+ return `<% ${i}${c}.forEach(function(currentItem, index, arr) {
252
253
  const startIndex = parseInt(${s ?? 0}, 10);
253
254
  const endIndex = startIndex + arr.length;
254
- const ${o}={ currentItem, currentIndex: startIndex + index, remainingItems: endIndex - (startIndex + index)} %>`;
255
+ const ${a}={ currentItem, currentIndex: startIndex + index, remainingItems: endIndex - (startIndex + index)} %>`;
255
256
  }
256
257
  _sanitizeVariableName(t) {
257
258
  return t.replace(/[^a-zA-Z0-9_$]/g, "_");
@@ -268,36 +269,37 @@ const ${o}={ currentItem, currentIndex: startIndex + index, remainingItems: endI
268
269
  getConditionalEndSyntax() {
269
270
  return "<% } %>";
270
271
  }
271
- getFullPath({ collectionId: t, path: e, variableType: r }) {
272
- return (t ? [this._sanitizeVariableName(t), r, e].filter(Boolean).join(".") : e) || "";
272
+ getFullPath({ collectionId: t, path: e, variableType: n }) {
273
+ return (t ? [this._sanitizeVariableName(t), n, e].filter(Boolean).join(".") : e) || "";
273
274
  }
274
275
  _parseCondition(t) {
275
276
  if (!t) return "";
276
277
  if (J(t)) {
277
- const e = t.statements.map((r) => this._parseCondition(r));
278
- return e.length > 1 ? `(${e.join(`) ${t.logicalOperator} (`)})` : e[0];
278
+ const e = t.statements.map((r) => this._parseCondition(r)), n = D[t.logicalOperator];
279
+ let s = e.length > 1 ? `(${e.join(`)${n}(`)})` : e[0];
280
+ return t.logicalOperator === "xor" && (s = `!!(${s})`), s;
279
281
  } else {
280
- const e = this._parseValue(t.left), r = this._parseValue(t.right);
282
+ const e = this._parseValue(t.left), n = this._parseValue(t.right);
281
283
  switch (t.operator) {
282
284
  case "equals":
283
- return `${e} === ${r}`;
285
+ return `${e} === ${n}`;
284
286
  case ">":
285
287
  case "<":
286
288
  case ">=":
287
289
  case "<=":
288
290
  case "=":
289
291
  case "!=":
290
- return `${e} ${t.operator} ${r}`;
292
+ return `${e} ${t.operator} ${n}`;
291
293
  case "contains":
292
- return `${e}?.includes(${r})`;
294
+ return `${e}?.includes(${n})`;
293
295
  case "startsWith":
294
- return `${e}?.startsWith(${r})`;
296
+ return `${e}?.startsWith(${n})`;
295
297
  case "endsWith":
296
- return `${e}?.endsWith(${r})`;
298
+ return `${e}?.endsWith(${n})`;
297
299
  case "equalsIgnoreCase":
298
- return `${e}?.toLowerCase() === ${r}?.toLowerCase()`;
300
+ return `${e}?.toLowerCase() === ${n}?.toLowerCase()`;
299
301
  case "trimEquals":
300
- return `${e}?.trim() === ${r}?.trim()`;
302
+ return `${e}?.trim() === ${n}?.trim()`;
301
303
  case "isTruthy":
302
304
  return `!!${e}`;
303
305
  case "isFalsy":
@@ -319,7 +321,7 @@ const ${o}={ currentItem, currentIndex: startIndex + index, remainingItems: endI
319
321
  case "isBoolean":
320
322
  return `typeof ${e} === 'boolean'`;
321
323
  default:
322
- return `${e} ${t.operator} ${r}`;
324
+ return `${e} ${t.operator} ${n}`;
323
325
  }
324
326
  }
325
327
  }
@@ -327,7 +329,11 @@ const ${o}={ currentItem, currentIndex: startIndex + index, remainingItems: endI
327
329
  return typeof t == "object" ? this.getFullPath(t) : typeof t == "string" ? `'${t.replace(/'/g, "\\'")}'` : String(t);
328
330
  }
329
331
  }
330
- const V = ["===", "==", "!==", "!=", "<=", ">=", "<", ">", "&&", "||"], q = {
332
+ const D = {
333
+ and: "&&",
334
+ or: "||",
335
+ xor: "^"
336
+ }, V = ["===", "==", "!==", "!=", "<=", ">=", "<", ">", "&&", "||"], q = {
331
337
  "===": "=",
332
338
  "==": "=",
333
339
  "!==": "!=",
@@ -339,7 +345,7 @@ const V = ["===", "==", "!==", "!=", "<=", ">=", "<", ">", "&&", "||"], q = {
339
345
  "&&": "&&",
340
346
  "||": "||"
341
347
  };
342
- class F {
348
+ class G {
343
349
  constructor() {
344
350
  this.messages = [], this.collectionStack = [];
345
351
  }
@@ -354,34 +360,37 @@ class F {
354
360
  }
355
361
  parseEjsTemplate(t) {
356
362
  const e = [];
357
- let r = 0;
358
- for (; r < t.length; ) {
359
- const s = t.indexOf("<%", r);
363
+ let n = 0;
364
+ for (; n < t.length; ) {
365
+ const s = t.indexOf("<%", n);
360
366
  if (s === -1) {
361
- r < t.length && e.push(this.createAppendLiteral(t.slice(r)));
367
+ n < t.length && e.push(this.createAppendLiteral(t.slice(n)));
362
368
  break;
363
369
  }
364
- s > r && e.push(this.createAppendLiteral(t.slice(r, s)));
365
- const n = t.indexOf("%>", s);
366
- if (n === -1)
370
+ s > n && e.push(this.createAppendLiteral(t.slice(n, s)));
371
+ const r = t.indexOf("%>", s);
372
+ if (r === -1)
367
373
  throw new Error("Unclosed EJS tag");
368
- const a = t.slice(s + 2, n).trim();
369
- if (r = n + 2, a.startsWith("=") || a.startsWith("-")) {
370
- const o = a.slice(1).trim();
371
- e.push(this.createAppendExpression(o));
374
+ const i = t.slice(s + 2, r).trim();
375
+ if (n = r + 2, i.startsWith("=") || i.startsWith("-")) {
376
+ const a = i.slice(1).trim();
377
+ e.push(this.createAppendExpression(a));
372
378
  } else {
373
- if (a.startsWith("#"))
379
+ if (i.startsWith("#"))
374
380
  continue;
375
- if (a.startsWith("if")) {
376
- const o = this.parseIfStatement(a, t, r);
377
- e.push(o), r = o.endPos || r;
378
- } else if (a.startsWith("for")) {
379
- const o = this.parseForStatement(a, t, r);
380
- e.push(o), r = o.endPos || r;
381
+ if (i.startsWith("if")) {
382
+ const a = this.parseIfStatement(i, t, n);
383
+ e.push(a), n = a.endPos || n;
384
+ } else if (i.startsWith("for")) {
385
+ const a = this.parseForStatement(i, t, n);
386
+ e.push(a), n = a.endPos || n;
381
387
  } else {
382
- if (a.startsWith("}") || a.startsWith("} else"))
383
- break;
384
- this.addMessage("info", `Unsupported scriptlet content: ${a}`);
388
+ const a = i.match(/([\w][\w.-]*[\w])\s*\.\s*forEach\s*\(\s*(\w+)\s*=>\s*\{?/);
389
+ if (a) {
390
+ const c = a[1], p = a[2], l = this.parseForEachStatement(c, p, t, n);
391
+ e.push(l), n = l.endPos || n;
392
+ } else
393
+ this.addMessage("info", `Unsupported scriptlet content: ${i}`);
385
394
  }
386
395
  }
387
396
  }
@@ -410,60 +419,57 @@ class F {
410
419
  }
411
420
  };
412
421
  }
413
- findNextElse(t, e) {
414
- const r = /<%\s*\}\s*else(?:\s+if\s*\([^)]*\))?\s*\{\s*%>/g;
415
- r.lastIndex = e;
416
- const s = r.exec(t);
417
- return s ? {
418
- start: s.index,
419
- end: t.indexOf("%>", s.index) + 2
420
- // End of <% } else { %>
421
- } : null;
422
- }
423
- parseIfStatement(t, e, r) {
422
+ parseIfStatement(t, e, n) {
424
423
  const s = t.match(/if\s*\((.*)\)\s*\{?/);
425
424
  if (!s)
426
425
  throw new Error("Invalid if statement syntax");
427
- const n = this.parseJsExpression(s[1]), { body: a, endPos: o } = this.parseTemplateBlock(e, r);
428
- let l = null, p = o;
429
- const c = this.findNextElse(e, o);
430
- if (c) {
431
- const h = e.slice(c.start, c.end);
432
- if (/else\s+if/.test(h)) {
433
- const f = c.end, u = this.parseIfStatement(h, e, f);
434
- l = {
435
- type: "IfStatement",
436
- test: u.test,
437
- consequent: u.consequent,
438
- alternate: u.alternate
439
- }, p = u.endPos;
440
- } else {
441
- const f = c.end, { body: u, endPos: x } = this.parseTemplateBlock(e, f);
442
- l = {
443
- type: "BlockStatement",
444
- body: u
445
- }, p = x;
426
+ const r = this.parseJsExpression(s[1]), { body: i, endPos: a } = this.parseTemplateBlock(e, n);
427
+ let c = null, p = a;
428
+ const l = e.indexOf("<%", p);
429
+ if (l === p) {
430
+ const h = e.indexOf("%>", l);
431
+ if (h !== -1) {
432
+ const d = e.slice(l + 2, h).trim(), f = h + 2 - l;
433
+ if (d.startsWith("} else if")) {
434
+ p = l + f;
435
+ const E = d.substring(d.indexOf("if")).trim(), u = this.parseIfStatement(E, e, p);
436
+ c = {
437
+ type: "IfStatement",
438
+ test: u.test,
439
+ consequent: u.consequent,
440
+ alternate: u.alternate
441
+ }, p = u.endPos;
442
+ } else if (d.startsWith("} else")) {
443
+ p = l + f;
444
+ const { body: E, endPos: u } = this.parseTemplateBlock(e, p);
445
+ c = {
446
+ type: "BlockStatement",
447
+ body: E
448
+ }, p = u;
449
+ const m = e.indexOf("<%", p), S = e.indexOf("%>", m);
450
+ m === p && e.slice(m + 2, S).trim() === "}" && (p = S + 2);
451
+ } else d === "}" && (p = l + f);
446
452
  }
447
453
  }
448
454
  return {
449
455
  type: "IfStatement",
450
- test: n,
456
+ test: r,
451
457
  consequent: {
452
458
  type: "BlockStatement",
453
- body: a
459
+ body: i
454
460
  },
455
- alternate: l,
461
+ alternate: c,
456
462
  endPos: p
457
463
  };
458
464
  }
459
- parseForStatement(t, e, r) {
465
+ parseForStatement(t, e, n) {
460
466
  const s = t.match(/for\s*\((.*)\)\s*\{?/);
461
467
  if (!s)
462
468
  throw new Error("Invalid for statement syntax");
463
- const n = s[1].split("of");
464
- if (n.length !== 2)
469
+ const r = s[1].split("of");
470
+ if (r.length !== 2)
465
471
  throw new Error("Only for...of loops are supported");
466
- const a = n[0].trim().replace(/^let\s+|^const\s+|^var\s+/, ""), o = n[1].trim(), { body: l, endPos: p } = this.parseTemplateBlock(e, r);
472
+ const i = r[0].trim().replace(/^let\s+|^const\s+|^var\s+/, ""), a = r[1].trim(), { body: c, endPos: p } = this.parseTemplateBlock(e, n);
467
473
  return {
468
474
  type: "ForStatement",
469
475
  init: {
@@ -472,8 +478,8 @@ class F {
472
478
  declarations: [
473
479
  {
474
480
  type: "VariableDeclarator",
475
- id: { type: "Identifier", name: a },
476
- init: this.parseJsExpression(o)
481
+ id: { type: "Identifier", name: i },
482
+ init: this.parseJsExpression(a)
477
483
  }
478
484
  ]
479
485
  },
@@ -481,42 +487,72 @@ class F {
481
487
  update: null,
482
488
  body: {
483
489
  type: "BlockStatement",
484
- body: l
490
+ body: c
485
491
  },
486
- endPos: p
492
+ // Consume the closing '}' tag for the for loop
493
+ endPos: (() => {
494
+ const l = e.indexOf("<%", p), h = e.indexOf("%>", l);
495
+ return l === p && e.slice(l + 2, h).trim() === "}" ? h + 2 : p;
496
+ })()
497
+ };
498
+ }
499
+ parseForEachStatement(t, e, n, s) {
500
+ const { body: r, endPos: i } = this.parseTemplateBlock(n, s);
501
+ return {
502
+ type: "ForEachStatement",
503
+ collection: this.parseJsExpression(t),
504
+ itemIdentifier: { type: "Identifier", name: e },
505
+ body: {
506
+ type: "BlockStatement",
507
+ body: r
508
+ },
509
+ endPos: (() => {
510
+ const a = n.indexOf("<%", i), c = n.indexOf("%>", a);
511
+ return a === i && n.slice(a + 2, c).trim() === "}" ? c + 2 : i;
512
+ })()
487
513
  };
488
514
  }
489
515
  parseTemplateBlock(t, e) {
490
- const r = [];
491
- let s = e, n = 1;
492
- for (; s < t.length && n > 0; ) {
493
- const a = t.indexOf("<%", s);
494
- if (a === -1) {
495
- s < t.length && r.push(this.createAppendLiteral(t.slice(s)));
516
+ const n = [];
517
+ let s = e;
518
+ for (; s < t.length; ) {
519
+ const r = t.indexOf("<%", s);
520
+ if (r === -1) {
521
+ s < t.length && n.push(this.createAppendLiteral(t.slice(s))), s = t.length;
496
522
  break;
497
523
  }
498
- a > s && r.push(this.createAppendLiteral(t.slice(s, a)));
499
- const o = t.indexOf("%>", a);
500
- if (o === -1)
501
- throw new Error("Unclosed EJS tag");
502
- const l = t.slice(a + 2, o).trim();
503
- if (s = o + 2, l === "}") {
504
- if (n--, n === 0)
505
- break;
506
- } else (l.startsWith("if") || l.startsWith("for")) && n++;
507
- if (l.startsWith("=") || l.startsWith("-")) {
508
- const p = l.slice(1).trim();
509
- r.push(this.createAppendExpression(p));
524
+ r > s && n.push(this.createAppendLiteral(t.slice(s, r)));
525
+ const i = t.indexOf("%>", r);
526
+ if (i === -1)
527
+ throw new Error("Unclosed EJS tag within block");
528
+ const a = t.slice(r + 2, i).trim();
529
+ if (a === "}" || a.startsWith("} else")) {
530
+ s = r;
531
+ break;
532
+ }
533
+ if (s = i + 2, a.startsWith("=") || a.startsWith("-")) {
534
+ const c = a.slice(1).trim();
535
+ n.push(this.createAppendExpression(c));
510
536
  } else {
511
- if (l.startsWith("#"))
537
+ if (a.startsWith("#"))
512
538
  continue;
513
- if (l.startsWith("} else")) {
514
- s = a;
515
- break;
539
+ if (a.startsWith("if")) {
540
+ const c = this.parseIfStatement(a, t, s);
541
+ n.push(c), s = c.endPos || s;
542
+ } else if (a.startsWith("for")) {
543
+ const c = this.parseForStatement(a, t, s);
544
+ n.push(c), s = c.endPos || s;
545
+ } else {
546
+ const c = a.match(/([\w][\w.-]*[\w])\s*\.\s*forEach\s*\(\s*(\w+)\s*=>\s*\{?/);
547
+ if (c) {
548
+ const p = c[1], l = c[2], h = this.parseForEachStatement(p, l, t, s);
549
+ n.push(h), s = h.endPos || s;
550
+ } else
551
+ this.addMessage("info", `Unsupported scriptlet content within block: ${a}`);
516
552
  }
517
553
  }
518
554
  }
519
- return { body: r, endPos: s };
555
+ return { body: n, endPos: s };
520
556
  }
521
557
  parseJsExpression(t) {
522
558
  if (/^(['"]).*\1$/.test(t))
@@ -540,33 +576,34 @@ class F {
540
576
  value: t === "true"
541
577
  };
542
578
  for (const s of V) {
543
- const n = t.indexOf(s);
544
- if (n > 0)
579
+ const r = t.lastIndexOf(s);
580
+ if (r > 0 && t[r - 1] !== "!" && t[r - 1] !== "=" && t[r - 1] !== "<" && t[r - 1] !== ">")
545
581
  return {
546
582
  type: s === "&&" || s === "||" ? "LogicalExpression" : "BinaryExpression",
547
583
  operator: q[s],
548
- left: this.parseJsExpression(t.slice(0, n).trim()),
549
- right: this.parseJsExpression(t.slice(n + s.length).trim())
584
+ left: this.parseJsExpression(t.slice(0, r).trim()),
585
+ right: this.parseJsExpression(t.slice(r + s.length).trim())
550
586
  };
551
587
  }
552
- const e = t.indexOf("."), r = t.indexOf("[");
553
- if (e > 0 || r > 0) {
554
- const s = e > 0 && (r === -1 || e < r) ? e : r;
555
- let n, a, o = !1;
588
+ const e = t.indexOf("."), n = t.indexOf("[");
589
+ if (e > 0 && (n === -1 || e < n) || n > 0 && (e === -1 || n < e)) {
590
+ const s = e > 0 && (n === -1 || e < n) ? e : n;
591
+ let r, i, a = !1;
556
592
  if (s === e)
557
- n = t.slice(0, e), a = this.parseJsExpression(t.slice(e + 1));
593
+ r = t.slice(0, e), i = { type: "Identifier", name: t.slice(e + 1) }, a = !1;
558
594
  else {
559
- n = t.slice(0, r);
560
- const l = t.indexOf("]", r);
561
- if (l === -1)
595
+ r = t.slice(0, n);
596
+ const c = t.indexOf("]", n);
597
+ if (c === -1)
562
598
  throw new Error("Unclosed bracket in expression");
563
- a = this.parseJsExpression(t.slice(r + 1, l)), o = !0;
599
+ const p = t.slice(n + 1, c);
600
+ i = /^(['"]).*\1$/.test(p) || /^-?\d+(\.\d+)?$/.test(p) ? this.parseJsExpression(p) : { type: "Identifier", name: p }, a = !0;
564
601
  }
565
602
  return {
566
603
  type: "MemberExpression",
567
- object: this.parseJsExpression(n),
568
- property: a,
569
- computed: o
604
+ object: this.parseJsExpression(r),
605
+ property: i,
606
+ computed: a
570
607
  };
571
608
  }
572
609
  return {
@@ -586,6 +623,8 @@ class F {
586
623
  return this.handleIfStatement(t);
587
624
  case "ForStatement":
588
625
  return this.handleForStatement(t);
626
+ case "ForEachStatement":
627
+ return this.handleForEachStatement(t);
589
628
  case "BlockStatement":
590
629
  return this.handleBlockStatement(t);
591
630
  case "Literal":
@@ -606,8 +645,8 @@ class F {
606
645
  }
607
646
  handleProgram(t) {
608
647
  let e = "";
609
- for (const r of t.body)
610
- e += this.astToGrapesJS(r);
648
+ for (const n of t.body)
649
+ e += this.astToGrapesJS(n);
611
650
  return e;
612
651
  }
613
652
  handleVariableDeclaration(t) {
@@ -617,11 +656,11 @@ class F {
617
656
  return t.expression.type === "CallExpression" && t.expression.callee.type === "Identifier" && t.expression.callee.name === "__append" && t.expression.arguments.length > 0 ? this.astToGrapesJS(t.expression.arguments[0]) : "";
618
657
  }
619
658
  handleIfStatement(t) {
620
- const e = this.getConditionExpression(t.test), r = this.astToGrapesJS(t.consequent);
659
+ const e = this.getConditionExpression(t.test), n = this.astToGrapesJS(t.consequent);
621
660
  let s = "";
622
661
  return t.alternate && (s = this.astToGrapesJS(t.alternate)), `
623
662
  <data-condition data-gjs-data-resolver='${JSON.stringify({ condition: e })}'>
624
- <data-condition-true-content>${r}</data-condition-true-content>
663
+ <data-condition-true-content>${n}</data-condition-true-content>
625
664
  <data-condition-false-content>${s}</data-condition-false-content>
626
665
  </data-condition>
627
666
  `;
@@ -633,30 +672,44 @@ class F {
633
672
  const e = t.init.declarations[0];
634
673
  if (e.id.type !== "Identifier")
635
674
  return this.addMessage("warning", "Unsupported for loop variable declaration id"), "";
636
- let r = "";
675
+ let n = "";
637
676
  if (e.init)
638
- r = this.getExpressionString(e.init);
677
+ n = this.getExpressionString(e.init);
639
678
  else
640
679
  return this.addMessage("warning", "For loop collection is undefined"), "";
641
- r.startsWith("data.") || (r = `data.${r}`);
642
- const s = `collection-${Math.random().toString(36).substring(2, 10)}`;
680
+ const s = e.id.name, r = s;
643
681
  this.collectionStack.push(s);
644
- const n = this.astToGrapesJS(t.body);
682
+ const i = this.astToGrapesJS(t.body);
683
+ return this.collectionStack.pop(), `
684
+ <data-collection data-gjs-data-resolver='${JSON.stringify({
685
+ dataSource: { type: "data-variable", path: n },
686
+ collectionId: r
687
+ })}'>
688
+ <data-collection-item>
689
+ ${i}
690
+ </data-collection-item>
691
+ </data-collection>
692
+ `;
693
+ }
694
+ handleForEachStatement(t) {
695
+ const e = this.getExpressionString(t.collection), n = t.itemIdentifier.name, s = n;
696
+ this.collectionStack.push(n);
697
+ const r = this.astToGrapesJS(t.body);
645
698
  return this.collectionStack.pop(), `
646
699
  <data-collection data-gjs-data-resolver='${JSON.stringify({
647
- dataSource: { type: "data-variable", path: r },
700
+ dataSource: { type: "data-variable", path: e },
648
701
  collectionId: s
649
702
  })}'>
650
703
  <data-collection-item>
651
- ${n}
704
+ ${r}
652
705
  </data-collection-item>
653
706
  </data-collection>
654
707
  `;
655
708
  }
656
709
  handleBlockStatement(t) {
657
710
  let e = "";
658
- for (const r of t.body)
659
- e += this.astToGrapesJS(r);
711
+ for (const n of t.body)
712
+ e += this.astToGrapesJS(n);
660
713
  return e;
661
714
  }
662
715
  handleLiteral(t) {
@@ -664,34 +717,30 @@ class F {
664
717
  }
665
718
  handleIdentifier(t) {
666
719
  let e = { path: t.name };
667
- if (this.collectionStack.length > 0 && t.name === "item") {
668
- const r = this.collectionStack[this.collectionStack.length - 1];
669
- e = {
670
- path: t.name,
671
- collectionId: r,
720
+ if (this.collectionStack.length > 0) {
721
+ const n = this.collectionStack[this.collectionStack.length - 1];
722
+ t.name === n && (e = {
723
+ collectionId: n,
672
724
  variableType: "currentItem"
673
- };
725
+ });
674
726
  }
675
727
  return `<data-variable data-gjs-data-resolver='${JSON.stringify(e)}'></data-variable>`;
676
728
  }
677
729
  handleMemberExpression(t, e = {}) {
678
730
  let s = { path: this.getExpressionString(t), ...e };
679
731
  if (this.collectionStack.length > 0) {
680
- let n = t;
732
+ const r = this.collectionStack[this.collectionStack.length - 1];
733
+ let i = t;
681
734
  const a = [];
682
- let o = !1;
683
- for (; n.type === "MemberExpression"; )
684
- if (n.property.type === "Identifier" ? a.unshift(n.property.name) : n.property.type === "Literal" && n.computed && a.unshift(`[${n.property.value}]`), n = n.object, n.type === "Identifier" && n.name === "item") {
685
- o = !0;
686
- break;
687
- }
688
- if (o && n.type === "Identifier" && n.name === "item") {
689
- const l = this.collectionStack[this.collectionStack.length - 1], p = a.join(".").replace(/\.\[/g, "[");
735
+ for (; i.type === "MemberExpression"; )
736
+ i.property.type === "Identifier" && !i.computed ? a.unshift(i.property.name) : i.property.type === "Literal" && i.computed ? a.unshift(String(i.property.value)) : a.unshift(this.getExpressionString(i.property)), i = i.object;
737
+ if (i.type === "Identifier" && i.name === r) {
738
+ const c = a.join(".");
690
739
  s = {
691
- ...s,
692
- collectionId: l,
740
+ ...e,
741
+ collectionId: r,
693
742
  variableType: "currentItem",
694
- path: p
743
+ path: c
695
744
  };
696
745
  }
697
746
  }
@@ -708,8 +757,8 @@ class F {
708
757
  handleCallExpression(t) {
709
758
  var e;
710
759
  if (t.callee.type === "Identifier" && t.callee.name === "include") {
711
- const r = ((e = t.arguments[0]) == null ? void 0 : e.type) === "Literal" ? String(t.arguments[0].value) : "";
712
- return this.addMessage("info", `Partial includes are not fully supported: ${r}`), "";
760
+ const n = ((e = t.arguments[0]) == null ? void 0 : e.type) === "Literal" ? String(t.arguments[0].value) : "";
761
+ return this.addMessage("info", `Partial includes are not fully supported: ${n}`), "";
713
762
  }
714
763
  return "";
715
764
  }
@@ -725,20 +774,20 @@ class F {
725
774
  ]
726
775
  };
727
776
  if (t.type === "MemberExpression") {
728
- let e = this.getExpressionString(t), r = "isTruthy";
729
- return e.startsWith("!!") ? (r = "isTruthy", e = e.slice(2)) : e.startsWith("!") && (r = "isFalsy", e = e.slice(1)), {
777
+ let e = this.getExpressionString(t), n = "isTruthy";
778
+ return e.startsWith("!!") ? (n = "isTruthy", e = e.slice(2)) : e.startsWith("!") && (n = "isFalsy", e = e.slice(1)), {
730
779
  logicalOperator: "and",
731
780
  statements: [
732
781
  {
733
782
  left: { type: "data-variable", path: e },
734
- operator: r
783
+ operator: n
735
784
  }
736
785
  ]
737
786
  };
738
787
  }
739
788
  if (t.type === "BinaryExpression" || t.type === "LogicalExpression") {
740
- const e = this.getConditionExpression(t.left), r = this.getConditionExpression(t.right);
741
- return `${e} ${t.operator} ${r}`;
789
+ const e = this.getConditionExpression(t.left), n = this.getConditionExpression(t.right);
790
+ return `${e} ${t.operator} ${n}`;
742
791
  }
743
792
  return t.type === "Literal" ? typeof t.value == "string" ? `'${t.value}'` : String(t.value ?? "") : this.getExpressionString(t);
744
793
  }
@@ -749,16 +798,16 @@ class F {
749
798
  case "Literal":
750
799
  return String(t.value ?? "");
751
800
  case "MemberExpression": {
752
- const e = this.getExpressionString(t.object), r = this.getExpressionString(t.property);
753
- return t.computed ? `${e}[${r}]` : `${e}.${r}`;
801
+ const e = this.getExpressionString(t.object), n = this.getExpressionString(t.property);
802
+ return t.computed ? `${e}[${n}]` : `${e}.${n}`;
754
803
  }
755
804
  case "BinaryExpression": {
756
- const e = this.getExpressionString(t.left), r = this.getExpressionString(t.right);
757
- return `${e} ${t.operator} ${r}`;
805
+ const e = this.getExpressionString(t.left), n = this.getExpressionString(t.right);
806
+ return `${e} ${t.operator} ${n}`;
758
807
  }
759
808
  case "LogicalExpression": {
760
- const e = this.getExpressionString(t.left), r = this.getExpressionString(t.right);
761
- return `${e} ${t.operator} ${r}`;
809
+ const e = this.getExpressionString(t.left), n = this.getExpressionString(t.right);
810
+ return `${e} ${t.operator} ${n}`;
762
811
  }
763
812
  default:
764
813
  return this.addMessage("warning", `Unsupported expression type for getExpressionString: ${t.type}`), "";
@@ -771,21 +820,21 @@ class F {
771
820
  });
772
821
  }
773
822
  }
774
- const G = "dataSourceEjs", R = d.startup, K = function(i, t = {}) {
775
- const e = H(i, new D()), r = U(i, {
823
+ const R = "dataSourceEjs", K = g.startup, z = function(o, t = {}) {
824
+ const e = F(o, new U()), n = H(o, {
776
825
  contains: "<%",
777
- importer: new F()
826
+ importer: new G()
778
827
  });
779
- _({
780
- editor: i,
828
+ B({
829
+ editor: o,
781
830
  licenseKey: t.licenseKey,
782
- plan: R,
783
- pluginName: G,
831
+ plan: K,
832
+ pluginName: R,
784
833
  cleanup: () => {
785
- e(), r();
834
+ e(), n();
786
835
  }
787
836
  });
788
- }, z = O(K);
837
+ }, Z = M(z);
789
838
  export {
790
- z as default
839
+ Z as default
791
840
  };