@chaibuilder/sdk 3.0.28 → 3.1.0

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,685 +0,0 @@
1
- import { getRegisteredChaiBlock as Q } from "@chaibuilder/runtime";
2
- import { memoize as Y, get as d, cloneDeep as R, forEach as D, keys as H, isString as B, startsWith as T, isArray as I, isEmpty as b, includes as k, flatten as tt, flatMapDeep as et, set as w, has as L, filter as V, find as y, some as at, compact as st, map as nt, trim as rt, capitalize as ot, last as lt } from "lodash-es";
3
- import { twMerge as ct } from "tailwind-merge";
4
- import { g as pt, c as S } from "./common-functions-BGzDsf1z.js";
5
- import { parse as ut, stringify as C } from "himalaya";
6
- const x = "#styles:", It = "__ADD_BLOCK_INTERNAL_ROOT", Dt = "@", dt = "#", K = (e) => {
7
- if (e = e.replace(x, ""), !e) return { baseClasses: "", classes: "" };
8
- const n = e.split(/,(?![^\[]*\])/);
9
- if (n.length === 1)
10
- return { baseClasses: "", classes: n[0].trim() };
11
- const [t, ...a] = n;
12
- return {
13
- baseClasses: t.trim(),
14
- classes: a.join(",").trim().replace(/ +(?= )/g, "")
15
- };
16
- };
17
- import.meta.vitest && describe("getSplitChaiClasses", () => {
18
- it("should return the base classes and the classes", () => {
19
- const e = {
20
- ",styles-2": { baseClasses: "", classes: "styles-2" },
21
- "styles-1,styles-2": { baseClasses: "styles-1", classes: "styles-2" },
22
- ",bg-[linear-gradient(-10deg,black,transparent_100%)]": {
23
- baseClasses: "",
24
- classes: "bg-[linear-gradient(-10deg,black,transparent_100%)]"
25
- },
26
- "styles-1,bg-[linear-gradient(-10deg,black,transparent_100%)]": {
27
- baseClasses: "styles-1",
28
- classes: "bg-[linear-gradient(-10deg,black,transparent_100%)]"
29
- },
30
- someclass: {
31
- baseClasses: "",
32
- classes: "someclass"
33
- },
34
- ",bg-red-500 text-center font-bold": {
35
- baseClasses: "",
36
- classes: "bg-red-500 text-center font-bold"
37
- },
38
- "bg-[linear-gradient(-10deg,black,transparent_100%)], bg-[linear-gradient(-10deg,black,transparent_100%)]": {
39
- baseClasses: "bg-[linear-gradient(-10deg,black,transparent_100%)]",
40
- classes: "bg-[linear-gradient(-10deg,black,transparent_100%)]"
41
- }
42
- };
43
- for (let n in e) {
44
- const t = K(`${x}${n}`);
45
- expect(t).toEqual(e[n]);
46
- }
47
- });
48
- });
49
- function Ct(e, n, t) {
50
- const a = d(t, "i18nProps", []);
51
- if (b(n) || b(a)) return e;
52
- const s = R(e);
53
- return D(H(s), (o) => {
54
- if (k(a, o) && !b(n)) {
55
- const i = d(s, o), r = d(s, `${o}-${n}`, "");
56
- B(i) ? s[o] = B(r) && !b(r.trim()) && r.trimStart() || i : s[o] = b(r) ? i : r;
57
- }
58
- }), s;
59
- }
60
- const ht = Y((e) => {
61
- const { baseClasses: n, classes: t } = K(e);
62
- return ct(n, t);
63
- });
64
- function gt(e, n) {
65
- return d(e, `${n}_attrs`, {});
66
- }
67
- function Nt(e, n = !0) {
68
- const t = {};
69
- return Object.keys(e).forEach((a) => {
70
- if (B(e[a]) && e[a].startsWith(x)) {
71
- const s = ht(e[a]), o = gt(e, a);
72
- t[a] = {
73
- ...!b(s) && { className: s },
74
- ...o,
75
- ...n ? {
76
- "data-style-prop": a,
77
- "data-block-parent": e._id,
78
- "data-style-id": `${a}-${e._id}`
79
- } : {}
80
- };
81
- }
82
- }), t;
83
- }
84
- const Lt = Y((e) => {
85
- const n = Q(e), t = d(n, "schema.properties", {});
86
- return Object.fromEntries(Object.entries(t).filter(([, a]) => d(a, "runtime", !1)));
87
- }), St = (e, n) => {
88
- if (!I(e)) return e;
89
- let t = e, a;
90
- return typeof n.limit == "number" && n.limit > 0 && (a = n.limit), a !== void 0 && (t = t.slice(0, a)), t;
91
- }, At = (e, n) => {
92
- const t = R(e);
93
- return D(H(t), (a) => {
94
- if (B(t[a]) && !T(a, "_")) {
95
- let s = t[a];
96
- a === "repeaterItems" && (t.repeaterItemsBinding = s);
97
- const o = /\{\{(.*?)\}\}/g, i = s.match(o);
98
- i && i.forEach((r) => {
99
- let m = r.slice(2, -2);
100
- const p = d(n, m, r);
101
- s = I(p) ? p : s.replace(r, p);
102
- }), t[a] = s;
103
- }
104
- }), t;
105
- }, mt = (e) => {
106
- const n = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/, t = /(?:https?:\/\/)?(?:www\.)?(player)?.vimeo\.com/;
107
- return n.test(e) || t.test(e);
108
- }, yt = (e) => {
109
- if (b(e)) return e;
110
- const n = /<video[^>]+src=['"]([^'">]+)['"]/, t = /<iframe[^>]+src=['"]([^'">]+)['"]/, a = e.match(n), s = e.match(t), o = a ? a[1] : s ? s[1] : null, i = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/, r = /(?:https?:\/\/)?(?:www\.)?player.vimeo\.com/;
111
- return o && (i.test(o) || r.test(o)) ? o : e;
112
- }, X = ["chai-name", "data-chai-name"], bt = {
113
- img: { alt: "alt", width: "width", height: "height", src: "image" },
114
- video: {
115
- src: "url",
116
- autoplay: "controls.autoPlay",
117
- muted: "controls.muted",
118
- loop: "controls.loop",
119
- controls: "controls.widgets"
120
- },
121
- a: {
122
- href: "link.href",
123
- target: "link.target",
124
- type: ""
125
- // @TODO: Detect here what to url, email, phone, elementId
126
- },
127
- input: {
128
- placeholder: "placeholder",
129
- required: "required",
130
- type: "inputType",
131
- name: "fieldName"
132
- },
133
- textarea: {
134
- placeholder: "placeholder",
135
- required: "required",
136
- type: "inputType",
137
- name: "fieldName"
138
- },
139
- select: {
140
- placeholder: "placeholder",
141
- required: "required",
142
- multiple: "multiple",
143
- name: "fieldName"
144
- },
145
- form: {
146
- action: "action"
147
- }
148
- }, xt = (e, n) => e.children.length === 1 && k(
149
- ["Heading", "Paragraph", "Span", "ListItem", "Button", "Label", "TableCell", "Link", "RichText"],
150
- n._type
151
- ), A = (e) => e.map((n) => n.type === "text" ? d(n, "content", "") : b(n.children) ? "" : A(n.children)).join(""), W = (e) => e === null ? "" : e, Z = (e) => {
152
- const n = st(nt(e.split(/\s+/), rt)), t = y(n, (r) => /^w-/.test(r)), a = y(n, (r) => /^h-/.test(r));
153
- if (!a || !t) return { height: "", width: "" };
154
- const s = (r) => {
155
- if (!r) return;
156
- const m = r.match(/^[wh]-(?:\[(.*?)\]|(.+))$/);
157
- if (!m) return;
158
- if (m[1]) return m[1];
159
- const p = m[2];
160
- return /^\d+(\.\d+)?$/.test(p) ? `${Number(p) * 4}px` : p === "auto" || k(p, "%") ? p : "16px";
161
- }, o = s(t), i = s(a);
162
- return {
163
- width: k(o, "px") ? o : "16px",
164
- height: k(i, "px") ? i : "16px"
165
- };
166
- }, U = (e) => {
167
- if (e.tagName === "svg") return {};
168
- const n = {}, t = bt[e.tagName] || {}, a = e.attributes;
169
- return D(a, ({ key: s, value: o }) => {
170
- if (!k(X, s))
171
- if (t[s]) {
172
- if (e.tagName === "img" && s === "src" && !o.startsWith("http")) {
173
- const i = y(e.attributes, { key: "width" }), r = y(e.attributes, { key: "height" });
174
- i && r ? o = `https://via.placeholder.com/${i == null ? void 0 : i.value}x${r == null ? void 0 : r.value}` : o = "https://via.placeholder.com/150x150";
175
- }
176
- w(n, t[s], W(o));
177
- } else k(["style", "class", "srcset"], s) || (L(n, "styles_attrs") || (n.styles_attrs = {}), T(s, "@") && (s = s.replace("@", "x-on:")), n.styles_attrs[`${s}`] = W(o));
178
- }), delete n.class, n;
179
- }, ft = (e, n = "styles") => {
180
- if (!e.attributes) return { [n]: `${x},` };
181
- const t = y(e.attributes, { key: "class" });
182
- if (t) {
183
- const a = t.value;
184
- return { [n]: `${x},${a}` };
185
- }
186
- return { [n]: `${x},` };
187
- }, _t = (e) => {
188
- const n = d(e, "attributes", []), t = n.find((r) => r.key === "data-chai-richtext" || r.key === "chai-richtext"), a = n.find((r) => r.key === "data-chai-lightbox" || r.key === "chai-lightbox"), s = n.find((r) => r.key === "data-chai-dropdown" || r.key === "chai-dropdown"), o = n.find(
189
- (r) => r.key === "data-chai-dropdown-button" || r.key === "chai-dropdown-button"
190
- ), i = n.find(
191
- (r) => r.key === "data-chai-dropdown-content" || r.key === "chai-dropdown-content"
192
- );
193
- if (s)
194
- return { _type: "Dropdown" };
195
- if (o)
196
- return { _type: "DropdownButton" };
197
- if (i)
198
- return { _type: "DropdownContent" };
199
- if (t)
200
- return { _type: "RichText" };
201
- if (a)
202
- return { _type: "LightBoxLink" };
203
- switch (e.tagName) {
204
- // self closing tags
205
- case "img":
206
- return { _type: "Image" };
207
- case "input":
208
- return { _type: "Input", showLabel: !1 };
209
- // showLabel: hiding default block label
210
- case "hr":
211
- return { _type: "Divider" };
212
- case "br":
213
- return { _type: "LineBreak" };
214
- case "textarea":
215
- return { _type: "TextArea", showLabel: !1 };
216
- case "audio":
217
- return { _type: "Audio" };
218
- case "canvas":
219
- return { _type: "Canvas" };
220
- case "video":
221
- case "iframe":
222
- return { _type: "CustomHTML" };
223
- case "svg":
224
- return { _type: "Icon" };
225
- // non self closing tags
226
- // fixed structure
227
- case "select":
228
- return { _type: "Select", options: [] };
229
- case "option":
230
- return { _type: "Option" };
231
- case "ul":
232
- case "ol":
233
- case "dl":
234
- return {
235
- _type: "List",
236
- tag: e.tagName,
237
- _listType: e.tagName === "ol" ? "list-decimal" : "list-none"
238
- };
239
- case "li":
240
- case "dt":
241
- return { _type: "ListItem", tag: e.tagName };
242
- // non self closing tags
243
- // free flow structure
244
- case "span":
245
- case "figcaption":
246
- case "legend":
247
- return { _type: "Span", tag: e.tagName };
248
- case "p":
249
- return { _type: "Paragraph", content: "" };
250
- case "a":
251
- return { _type: "Link" };
252
- case "form":
253
- return { _type: "Form" };
254
- case "label":
255
- return { _type: "Label" };
256
- case "button":
257
- return { _type: "Button" };
258
- case "code":
259
- return { _type: "Box", _name: "Code" };
260
- case "h1":
261
- case "h2":
262
- case "h3":
263
- case "h4":
264
- case "h5":
265
- case "h6":
266
- return { _type: "Heading", tag: e.tagName };
267
- case "table":
268
- return { _type: "Table" };
269
- case "tr":
270
- return { _type: "TableRow" };
271
- case "td":
272
- case "th":
273
- return { _type: "TableCell", tag: e.tagName };
274
- case "thead":
275
- return { _type: "TableHead" };
276
- case "tbody":
277
- return { _type: "TableBody" };
278
- case "tfoot":
279
- return { _type: "TableFooter" };
280
- default: {
281
- const r = d(e, "children", []).length > 0 ? "Box" : "EmptyBox";
282
- return {
283
- _type: r,
284
- tag: e.tagName,
285
- _name: r == "EmptyBox" || e.tagName === "div" ? r : ot(e.tagName)
286
- };
287
- }
288
- }
289
- }, G = (e, n = null) => et(e, (t) => {
290
- var E, f, j, q, M, P, J, O, z, F;
291
- if (t.type === "comment") return [];
292
- let a = { _id: pt() };
293
- if (n && (a._parent = n.block._id), t.type === "text")
294
- return b(d(t, "content", "")) ? [] : n && xt(n.node, n.block) ? (w(n, "block.content", d(t, "content", "")), []) : { ...a, _type: "Text", content: d(t, "content", "") };
295
- const s = d(t, "attributes", []), o = s.find(
296
- (l) => l.key === "data-chai-richtext" || l.key === "chai-richtext"
297
- ), i = s.find(
298
- (l) => l.key === "data-chai-lightbox" || l.key === "chai-lightbox"
299
- ), r = s.find(
300
- (l) => l.key === "data-chai-dropdown" || l.key === "chai-dropdown"
301
- ), m = s.find(
302
- (l) => l.key === "data-chai-dropdown-button" || l.key === "chai-dropdown-button"
303
- ), p = s.find(
304
- (l) => l.key === "data-chai-dropdown-content" || l.key === "chai-dropdown-content"
305
- );
306
- if (a = {
307
- ...a,
308
- ..._t(t),
309
- ...U(t),
310
- ...ft(t)
311
- }, t.attributes) {
312
- const l = t.attributes.find((c) => k(X, c.key));
313
- l && (a._name = l.value);
314
- }
315
- if (o)
316
- return a.content = C(t.children), L(a, "styles_attrs.data-chai-richtext") && delete a.styles_attrs["data-chai-richtext"], L(a, "styles_attrs.chai-richtext") && delete a.styles_attrs["chai-richtext"], [a];
317
- if (i) {
318
- const l = [
319
- "data-chai-lightbox",
320
- "chai-lightbox",
321
- "data-vbtype",
322
- "data-autoplay",
323
- "data-maxwidth",
324
- "data-overlay",
325
- "data-gall",
326
- "href"
327
- ];
328
- a = {
329
- ...a,
330
- href: ((E = s.find((c) => c.key === "href")) == null ? void 0 : E.value) || "",
331
- hrefType: ((f = s.find((c) => c.key === "data-vbtype")) == null ? void 0 : f.value) || "video",
332
- autoplay: ((j = s.find((c) => c.key === "data-autoplay")) == null ? void 0 : j.value) === "true" ? "true" : "false",
333
- maxWidth: ((M = (q = s.find((c) => c.key === "data-maxwidth")) == null ? void 0 : q.value) == null ? void 0 : M.replace("px", "")) || "",
334
- backdropColor: ((P = s.find((c) => c.key === "data-overlay")) == null ? void 0 : P.value) || "",
335
- galleryName: ((J = s.find((c) => c.key === "data-gall")) == null ? void 0 : J.value) || ""
336
- }, D(l, (c) => {
337
- L(a, `styles_attrs.${c}`) && delete a.styles_attrs[c];
338
- });
339
- }
340
- if (r && (delete a.styles_attrs, a.showDropdown = !1), p && delete a.styles_attrs, m) {
341
- delete a.styles_attrs;
342
- const l = V(t.children || [], (u) => (u == null ? void 0 : u.tagName) !== "span");
343
- a.content = A(l);
344
- const c = y(
345
- t.children || [],
346
- (u) => (u == null ? void 0 : u.tagName) === "span" && at(u.children || [], (h) => (h == null ? void 0 : h.tagName) === "svg")
347
- );
348
- if (c) {
349
- const u = y(c.children || [], (h) => (h == null ? void 0 : h.tagName) === "svg");
350
- if (u) {
351
- a.icon = C([u]);
352
- const { height: h, width: $ } = kt(u, "16px", "16px");
353
- a.iconHeight = h, a.iconWidth = $;
354
- }
355
- }
356
- return [a];
357
- }
358
- if (a._type === "Input") {
359
- const l = a.inputType || "text";
360
- l === "checkbox" ? w(a, "_type", "Checkbox") : l === "radio" && w(a, "_type", "Radio");
361
- } else if (t.tagName === "video" || t.tagName === "iframe") {
362
- const l = C([t]);
363
- return mt(l) && (w(a, "_type", "Video"), w(a, "url", yt(l)), w(a, "styles", `${x},`), w(a, "controls", { autoPlay: !1, muted: !0, loop: !1, controls: !1 })), a.content = l, [a];
364
- } else if (t.tagName === "svg") {
365
- const l = d(y(t.attributes, { key: "class" }), "value", ""), { height: c, width: u } = Z(l);
366
- if (c && u)
367
- a.styles = `${x}, ${S(`w-${u} h-${c}`, l)}`.trim(), a.height = c == null ? void 0 : c.replace("px", ""), a.width = u == null ? void 0 : u.replace("px", "");
368
- else {
369
- const h = (O = y(t.attributes, { key: "height" })) == null ? void 0 : O.value, $ = (z = y(t.attributes, { key: "width" })) == null ? void 0 : z.value;
370
- h && $ ? (a.styles = `${x}, ${S(`w-[${$}px] h-[${h}px]`, l)}`.trim(), a.height = h, a.width = $) : a.styles = `${x}, ${S("w-full h-full", l)}`.trim();
371
- }
372
- return t.attributes = V(t.attributes, (h) => !k(["style", "width", "height", "class"], h.key)), a.icon = C([t]), [a];
373
- } else if (t.tagName == "option" && n && ((F = n.block) == null ? void 0 : F._type) === "Select")
374
- return n.block.options.push({
375
- label: A(t.children),
376
- ...U(t)
377
- }), [];
378
- const v = G(t.children, { block: a, node: t });
379
- return [a, ...v];
380
- }), kt = (e, n, t) => {
381
- var m, p;
382
- const a = d(e, "attributes", []), { height: s, width: o } = Z(
383
- d(y(a, { key: "class" }), "value", "")
384
- );
385
- if (s && o)
386
- return {
387
- height: `[${s}px]`,
388
- width: `[${o}px]`
389
- };
390
- const i = (m = y(a, { key: "height" })) == null ? void 0 : m.value, r = (p = y(a, { key: "width" })) == null ? void 0 : p.value;
391
- return {
392
- height: i ? `[${i}px]` : t,
393
- width: r ? `[${r}px]` : n
394
- };
395
- }, vt = (e) => {
396
- e = e.replace(/(\w+)=\\?"(.*?)\\?"/g, (a, s, o) => {
397
- let i = o.replace(/\\"/g, '"');
398
- return i = i.replace(/{([^}]+)}/g, (r) => r.replace(/"/g, '\\"')), `${s}="${i.replace(/\\"/g, '"')}"`;
399
- }), e = e.replace(/\\n/g, "").replace(/\\\\/g, "").replace(/\\([/<>])/g, "$1").replace(/\\./g, "").replace(/[\n\r\t\f\v]/g, ""), e = e.replace(/\$name="[^"]*"/g, "");
400
- const n = e.match(/<body[^>]*>[\s\S]*?<\/body>/);
401
- return (n && n.length > 0 ? n[0].replace(/<body/, "<div").replace(/<\/body>/, "</div>") : e).replace(/\s+/g, " ").replaceAll("> <", "><").replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "").trim();
402
- }, Rt = (e) => {
403
- const n = ut(vt(e));
404
- return b(e) ? [] : tt(G(n));
405
- }, _ = (e) => {
406
- const n = lt(e);
407
- return e.map((t) => {
408
- const a = Object.keys(t);
409
- for (let s = 0; s < a.length; s++)
410
- if (B(t[a[s]]) && T(a[s], "content")) {
411
- const o = t === n ? "" : " ";
412
- t[a[s]] = `${t[a[s]].trim()}${o}`;
413
- }
414
- return t;
415
- });
416
- };
417
- if (import.meta.vitest) {
418
- const { describe: e, it: n, expect: t } = import.meta.vitest;
419
- e("adjustSpacingInContentBlocks", () => {
420
- n("should add space after content fields except for the last block", () => {
421
- const s = _([
422
- { _id: "1", _type: "Text", content: "First" },
423
- { _id: "2", _type: "Text", content: "Second" },
424
- { _id: "3", _type: "Text", content: "Third" }
425
- ]);
426
- t(s[0].content).toBe("First "), t(s[1].content).toBe("Second "), t(s[2].content).toBe("Third");
427
- }), n("should trim whitespace from content before adding space", () => {
428
- const s = _([
429
- { _id: "1", _type: "Text", content: " First " },
430
- { _id: "2", _type: "Text", content: " Second " }
431
- ]);
432
- t(s[0].content).toBe("First "), t(s[1].content).toBe("Second");
433
- }), n("should handle multiple content fields in a single block", () => {
434
- const s = _([
435
- { _id: "1", _type: "Text", content: "Main", contentAlt: "Alt" },
436
- { _id: "2", _type: "Text", content: "Last", contentSecondary: "Secondary" }
437
- ]);
438
- t(s[0].content).toBe("Main "), t(s[0].contentAlt).toBe("Alt "), t(s[1].content).toBe("Last"), t(s[1].contentSecondary).toBe("Secondary");
439
- }), n("should only process fields that start with 'content'", () => {
440
- const s = _([
441
- { _id: "1", _type: "Text", content: "Text", title: "Title", text: "Text" },
442
- { _id: "2", _type: "Text", content: "Last" }
443
- ]);
444
- t(s[0].content).toBe("Text "), t(s[0].title).toBe("Title"), t(s[0].text).toBe("Text"), t(s[1].content).toBe("Last");
445
- }), n("should handle empty array", () => {
446
- const s = _([]);
447
- t(s).toEqual([]);
448
- }), n("should handle single block", () => {
449
- const s = _([{ _id: "1", _type: "Text", content: "Only" }]);
450
- t(s[0].content).toBe("Only");
451
- }), n("should handle blocks without content fields", () => {
452
- const s = _([
453
- { _id: "1", _type: "Container", title: "Title" },
454
- { _id: "2", _type: "Container", name: "Name" }
455
- ]);
456
- t(s[0].title).toBe("Title"), t(s[1].name).toBe("Name");
457
- }), n("should handle blocks with non-string content fields", () => {
458
- const s = _([
459
- { _id: "1", _type: "Text", content: "Text", contentNum: 123 },
460
- { _id: "2", _type: "Text", content: "Last", contentBool: !0 }
461
- ]);
462
- t(s[0].content).toBe("Text "), t(s[0].contentNum).toBe(123), t(s[1].content).toBe("Last"), t(s[1].contentBool).toBe(!0);
463
- }), n("should handle empty string content", () => {
464
- const s = _([
465
- { _id: "1", _type: "Text", content: "" },
466
- { _id: "2", _type: "Text", content: "Last" }
467
- ]);
468
- t(s[0].content).toBe(" "), t(s[1].content).toBe("Last");
469
- });
470
- });
471
- }
472
- const g = (e, n, { index: t, key: a }, s) => {
473
- if (B(e)) {
474
- let o = e;
475
- const i = /\{\{(.*?)\}\}/g, r = e.match(i);
476
- if (r) {
477
- const m = s === "image" || s === "mobileImage";
478
- r.forEach((p) => {
479
- let v = p.slice(2, -2).trim(), E = a.slice(2, -2).trim();
480
- t !== -1 && T(v, "$index.") ? v = `${E}.${t}.${v.slice(7)}` : t !== -1 && T(v, "$index") && (v = `${E}.${t}`);
481
- const f = d(n, v);
482
- f === void 0 ? o = o.replace(p, "") : m && !I(f) ? o = f : o = I(f) ? f : o.replace(p, f);
483
- });
484
- }
485
- return o;
486
- }
487
- if (I(e))
488
- return e.map((o) => g(o, n, { index: t, key: a }, s));
489
- if (e && typeof e == "object") {
490
- const o = {};
491
- return D(H(e), (i) => {
492
- !T(i, "_") && i !== "$repeaterItemsKey" ? o[i] = g(e[i], n, { index: t, key: a }, i) : o[i] = e[i];
493
- }), o;
494
- }
495
- return e;
496
- }, N = (e, n, { index: t, key: a }) => {
497
- let s = R(e);
498
- return s.repeaterItems && (s.$repeaterItemsKey = s.repeaterItems, T(s.repeaterItems, `{{${dt}`) && (s.$repeaterItemsKey = s.repeaterItems = `${s.repeaterItems.replace("}}", `/${s._id}}}`)}`), !b(s.repeaterItems) && s.pagination && (s.repeaterTotalItems = `${s.repeaterItems.replace("}}", `/${s._id}/totalItems}}`)}`)), g(s, n, { index: t, key: a });
499
- };
500
- import.meta.vitest && (describe("applyBindingToValue", () => {
501
- it("should handle string values with bindings", () => {
502
- const t = g("Hello {{user.name}}", { user: { name: "John" } }, { index: -1, key: "" });
503
- expect(t).toBe("Hello John");
504
- }), it("should handle nested object properties", () => {
505
- const t = g({
506
- name: "John",
507
- address: {
508
- city: "{{user.city}}",
509
- street: "123 Main St"
510
- }
511
- }, { user: { city: "New York" } }, { index: -1, key: "" });
512
- expect(t).toEqual({
513
- name: "John",
514
- address: {
515
- city: "New York",
516
- street: "123 Main St"
517
- }
518
- });
519
- }), it("should handle arrays of values", () => {
520
- const t = g(["Hello {{user.name}}", "Welcome {{user.role}}"], { user: { name: "John", role: "Admin" } }, { index: -1, key: "" });
521
- expect(t).toEqual(["Hello John", "Welcome Admin"]);
522
- }), it("should handle $index binding in repeater context", () => {
523
- const t = g("Item {{$index}}", { items: ["a", "b", "c"] }, { index: 1, key: "{{items}}" });
524
- expect(t).toBe("Item b");
525
- }), it("should handle $index binding with dot notation", () => {
526
- const t = g("Item {{$index.value}}", { items: [{ value: "apple" }, { value: "banana" }, { value: "cherry" }] }, { index: 1, key: "{{items}}" });
527
- expect(t).toBe("Item banana");
528
- }), it("should return an empty string for non-existing bindings", () => {
529
- const t = g("Hello {{user.nonexistent}}", { user: { name: "John" } }, { index: -1, key: "" });
530
- expect(t).toBe("Hello ");
531
- }), it("should preserve private properties starting with _", () => {
532
- const n = g({
533
- name: "John",
534
- _private: "secret"
535
- }, {}, { index: -1, key: "" });
536
- expect(n).toEqual({
537
- name: "John",
538
- _private: "secret"
539
- });
540
- }), it("should completely replace image property value when binding exists", () => {
541
- const t = g({
542
- image: "https://default.jpg{{user.avatar}}",
543
- title: "Hello {{user.name}}"
544
- }, { user: { avatar: "https://avatar.jpg", name: "John" } }, { index: -1, key: "" });
545
- expect(t).toEqual({
546
- image: "https://avatar.jpg",
547
- // Completely replaced, not concatenated
548
- title: "Hello John"
549
- // Normal replacement
550
- });
551
- }), it("should completely replace mobileImage property value when binding exists", () => {
552
- const t = g({
553
- mobileImage: "https://default-mobile.jpg{{user.mobileAvatar}}",
554
- alt: "Avatar for {{user.name}}"
555
- }, { user: { mobileAvatar: "https://mobile-avatar.jpg", name: "John" } }, { index: -1, key: "" });
556
- expect(t).toEqual({
557
- mobileImage: "https://mobile-avatar.jpg",
558
- // Completely replaced
559
- alt: "Avatar for John"
560
- // Normal replacement
561
- });
562
- }), it("should handle image binding with only binding syntax", () => {
563
- const t = g({
564
- image: "{{product.thumbnail}}"
565
- }, { product: { thumbnail: "https://product.jpg" } }, { index: -1, key: "" });
566
- expect(t).toEqual({
567
- image: "https://product.jpg"
568
- });
569
- }), it("should not affect non-image properties with similar bindings", () => {
570
- const t = g({
571
- url: "https://default.com{{page.slug}}",
572
- link: "https://example.com/{{page.id}}"
573
- }, { page: { slug: "/about", id: "123" } }, { index: -1, key: "" });
574
- expect(t).toEqual({
575
- url: "https://default.com/about",
576
- // Concatenated normally
577
- link: "https://example.com/123"
578
- // Concatenated normally
579
- });
580
- });
581
- }), describe("applyBindingToBlockProps", () => {
582
- it("should handle basic block with bindings", () => {
583
- const t = N({
584
- _id: "test-block",
585
- _type: "text",
586
- type: "text",
587
- content: "Hello {{user.name}}",
588
- style: {
589
- color: "{{theme.color}}"
590
- }
591
- }, { user: { name: "John" }, theme: { color: "blue" } }, { index: -1, key: "" });
592
- expect(t).toEqual({
593
- _id: "test-block",
594
- _type: "text",
595
- type: "text",
596
- content: "Hello John",
597
- style: {
598
- color: "blue"
599
- }
600
- });
601
- }), it("should handle repeaterItems and repeaterItemsBinding", () => {
602
- const t = N({
603
- _id: "test-block",
604
- _type: "repeater",
605
- type: "repeater",
606
- repeaterItems: "{{items}}",
607
- items: ["a", "b", "c"]
608
- }, { items: ["x", "y", "z"] }, { index: -1, key: "" });
609
- expect(t).toEqual({
610
- _id: "test-block",
611
- _type: "repeater",
612
- type: "repeater",
613
- $repeaterItemsKey: "{{items}}",
614
- repeaterItems: ["x", "y", "z"],
615
- items: ["a", "b", "c"]
616
- });
617
- }), it("should handle nested blocks with bindings", () => {
618
- const t = N({
619
- _id: "test-block",
620
- _type: "container",
621
- type: "container",
622
- children: [
623
- {
624
- _id: "child-block",
625
- _type: "text",
626
- type: "text",
627
- content: "Item {{$index}}",
628
- style: {
629
- color: "{{theme.color}}"
630
- }
631
- }
632
- ]
633
- }, { theme: { color: "red" }, items: ["x", "y", "z"] }, { index: 2, key: "{{items}}" });
634
- expect(t).toEqual({
635
- _id: "test-block",
636
- _type: "container",
637
- type: "container",
638
- children: [
639
- {
640
- _id: "child-block",
641
- _type: "text",
642
- type: "text",
643
- content: "Item z",
644
- style: {
645
- color: "red"
646
- }
647
- }
648
- ]
649
- });
650
- }), it("should handle arrays of blocks", () => {
651
- const t = N({
652
- _id: "test-block",
653
- _type: "list",
654
- type: "list",
655
- items: [
656
- { _id: "item1", content: "Item {{$index}}" },
657
- { _id: "item2", content: "Item {{$index}}" }
658
- ]
659
- }, { items: ["x", "y", "z"] }, { index: 0, key: "{{items}}" });
660
- expect(t).toEqual({
661
- _id: "test-block",
662
- _type: "list",
663
- type: "list",
664
- items: [
665
- { _id: "item1", content: "Item x" },
666
- { _id: "item2", content: "Item x" }
667
- ]
668
- });
669
- });
670
- }));
671
- export {
672
- dt as C,
673
- Dt as R,
674
- x as S,
675
- N as a,
676
- Ct as b,
677
- Lt as c,
678
- St as d,
679
- _ as e,
680
- At as f,
681
- Nt as g,
682
- Rt as h,
683
- K as i,
684
- It as j
685
- };