@chaibuilder/sdk 2.2.16 → 2.2.18

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.
@@ -0,0 +1,267 @@
1
+ import { getRegisteredChaiBlock as B } from "@chaibuilder/runtime";
2
+ import { memoize as k, get as c, cloneDeep as h, forEach as y, keys as _, isString as f, startsWith as u, isArray as b, isEmpty as g, includes as $ } from "lodash-es";
3
+ import { twMerge as C } from "tailwind-merge";
4
+ const E = "#styles:", w = "__ADD_BLOCK_INTERNAL_ROOT", I = (e) => {
5
+ if (e = e.replace(E, ""), !e) return { baseClasses: "", classes: "" };
6
+ const s = e.split(/,(?![^\[]*\])/);
7
+ if (s.length === 1)
8
+ return { baseClasses: "", classes: s[0].trim() };
9
+ const [t, ...a] = s;
10
+ return {
11
+ baseClasses: t.trim(),
12
+ classes: a.join(",").trim().replace(/ +(?= )/g, "")
13
+ };
14
+ };
15
+ import.meta.vitest && describe("getSplitChaiClasses", () => {
16
+ it("should return the base classes and the classes", () => {
17
+ const e = {
18
+ ",styles-2": { baseClasses: "", classes: "styles-2" },
19
+ "styles-1,styles-2": { baseClasses: "styles-1", classes: "styles-2" },
20
+ ",bg-[linear-gradient(-10deg,black,transparent_100%)]": {
21
+ baseClasses: "",
22
+ classes: "bg-[linear-gradient(-10deg,black,transparent_100%)]"
23
+ },
24
+ "styles-1,bg-[linear-gradient(-10deg,black,transparent_100%)]": {
25
+ baseClasses: "styles-1",
26
+ classes: "bg-[linear-gradient(-10deg,black,transparent_100%)]"
27
+ },
28
+ someclass: {
29
+ baseClasses: "",
30
+ classes: "someclass"
31
+ },
32
+ ",bg-red-500 text-center font-bold": {
33
+ baseClasses: "",
34
+ classes: "bg-red-500 text-center font-bold"
35
+ },
36
+ "bg-[linear-gradient(-10deg,black,transparent_100%)], bg-[linear-gradient(-10deg,black,transparent_100%)]": {
37
+ baseClasses: "bg-[linear-gradient(-10deg,black,transparent_100%)]",
38
+ classes: "bg-[linear-gradient(-10deg,black,transparent_100%)]"
39
+ }
40
+ };
41
+ for (let s in e) {
42
+ const t = I(`${E}${s}`);
43
+ expect(t).toEqual(e[s]);
44
+ }
45
+ });
46
+ });
47
+ function O(e, s, t) {
48
+ const a = c(t, "i18nProps", []);
49
+ if (g(s) || g(a)) return e;
50
+ const n = h(e);
51
+ return y(_(n), (r) => {
52
+ $(a, r) && !g(s) && (n[r] = c(n, `${r}-${s}`, n[r]));
53
+ }), n;
54
+ }
55
+ const D = k((e) => {
56
+ const { baseClasses: s, classes: t } = I(e);
57
+ return C(s, t);
58
+ });
59
+ function v(e, s) {
60
+ return c(e, `${s}_attrs`, {});
61
+ }
62
+ function R(e, s = !0) {
63
+ const t = {};
64
+ return Object.keys(e).forEach((a) => {
65
+ if (f(e[a]) && e[a].startsWith(E)) {
66
+ const n = D(e[a]), r = v(e, a);
67
+ t[a] = {
68
+ ...!g(n) && { className: n },
69
+ ...r,
70
+ ...s ? {
71
+ "data-style-prop": a,
72
+ "data-block-parent": e._id,
73
+ "data-style-id": `${a}-${e._id}`
74
+ } : {}
75
+ };
76
+ }
77
+ }), t;
78
+ }
79
+ const A = k((e) => {
80
+ const s = B(e), t = c(s, "schema.properties", {});
81
+ return Object.fromEntries(Object.entries(t).filter(([, a]) => c(a, "runtime", !1)));
82
+ }), j = (e, s) => {
83
+ if (!b(e)) return e;
84
+ let t = e, a;
85
+ return typeof s.limit == "number" && s.limit > 0 && (a = s.limit), a !== void 0 && (t = t.slice(0, a)), t;
86
+ }, z = (e, s) => {
87
+ const t = h(e);
88
+ return y(_(t), (a) => {
89
+ if (f(t[a]) && !u(a, "_")) {
90
+ let n = t[a];
91
+ a === "repeaterItems" && (t.repeaterItemsBinding = n);
92
+ const r = /\{\{(.*?)\}\}/g, d = n.match(r);
93
+ d && d.forEach((o) => {
94
+ let l = o.slice(2, -2);
95
+ const p = c(s, l, o);
96
+ n = b(p) ? p : n.replace(o, p);
97
+ }), t[a] = n;
98
+ }
99
+ }), t;
100
+ }, i = (e, s, { index: t, key: a }) => {
101
+ if (f(e)) {
102
+ let n = e;
103
+ if (e === "repeaterItems")
104
+ return { value: e, repeaterItemsBinding: e };
105
+ const r = /\{\{(.*?)\}\}/g, d = e.match(r);
106
+ return d && d.forEach((o) => {
107
+ let l = o.slice(2, -2).trim(), p = a.slice(2, -2).trim();
108
+ t !== -1 && u(l, "$index.") ? l = `${p}.${t}.${l.slice(7)}` : t !== -1 && u(l, "$index") && (l = `${p}.${t}`);
109
+ const x = c(s, l, o);
110
+ n = b(x) ? x : n.replace(o, x);
111
+ }), n;
112
+ }
113
+ if (b(e))
114
+ return e.map((n) => i(n, s, { index: t, key: a }));
115
+ if (e && typeof e == "object") {
116
+ const n = {};
117
+ return y(_(e), (r) => {
118
+ u(r, "_") ? n[r] = e[r] : n[r] = i(e[r], s, { index: t, key: a });
119
+ }), n;
120
+ }
121
+ return e;
122
+ }, m = (e, s, { index: t, key: a }) => {
123
+ const n = h(e), r = i(n, s, { index: t, key: a });
124
+ return n.repeaterItems && (r.repeaterItemsBinding = n.repeaterItems), r;
125
+ };
126
+ import.meta.vitest && (describe("applyBindingToValue", () => {
127
+ it("should handle string values with bindings", () => {
128
+ const t = i("Hello {{user.name}}", { user: { name: "John" } }, { index: -1, key: "" });
129
+ expect(t).toBe("Hello John");
130
+ }), it("should handle nested object properties", () => {
131
+ const t = i({
132
+ name: "John",
133
+ address: {
134
+ city: "{{user.city}}",
135
+ street: "123 Main St"
136
+ }
137
+ }, { user: { city: "New York" } }, { index: -1, key: "" });
138
+ expect(t).toEqual({
139
+ name: "John",
140
+ address: {
141
+ city: "New York",
142
+ street: "123 Main St"
143
+ }
144
+ });
145
+ }), it("should handle arrays of values", () => {
146
+ const t = i(["Hello {{user.name}}", "Welcome {{user.role}}"], { user: { name: "John", role: "Admin" } }, { index: -1, key: "" });
147
+ expect(t).toEqual(["Hello John", "Welcome Admin"]);
148
+ }), it("should handle repeaterItems special case", () => {
149
+ const s = i("repeaterItems", {}, { index: -1, key: "" });
150
+ expect(s).toEqual({ value: "repeaterItems", repeaterItemsBinding: "repeaterItems" });
151
+ }), it("should handle $index binding in repeater context", () => {
152
+ const t = i("Item {{$index}}", { items: ["a", "b", "c"] }, { index: 1, key: "{{items}}" });
153
+ expect(t).toBe("Item b");
154
+ }), it("should handle $index binding with dot notation", () => {
155
+ const t = i("Item {{$index.value}}", { items: [{ value: "apple" }, { value: "banana" }, { value: "cherry" }] }, { index: 1, key: "{{items}}" });
156
+ expect(t).toBe("Item banana");
157
+ }), it("should preserve private properties starting with _", () => {
158
+ const s = i({
159
+ name: "John",
160
+ _private: "secret"
161
+ }, {}, { index: -1, key: "" });
162
+ expect(s).toEqual({
163
+ name: "John",
164
+ _private: "secret"
165
+ });
166
+ });
167
+ }), describe("applyBindingToBlockProps", () => {
168
+ it("should handle basic block with bindings", () => {
169
+ const t = m({
170
+ _id: "test-block",
171
+ _type: "text",
172
+ type: "text",
173
+ content: "Hello {{user.name}}",
174
+ style: {
175
+ color: "{{theme.color}}"
176
+ }
177
+ }, { user: { name: "John" }, theme: { color: "blue" } }, { index: -1, key: "" });
178
+ expect(t).toEqual({
179
+ _id: "test-block",
180
+ _type: "text",
181
+ type: "text",
182
+ content: "Hello John",
183
+ style: {
184
+ color: "blue"
185
+ }
186
+ });
187
+ }), it("should handle repeaterItems and repeaterItemsBinding", () => {
188
+ const t = m({
189
+ _id: "test-block",
190
+ _type: "repeater",
191
+ type: "repeater",
192
+ repeaterItems: "{{items}}",
193
+ items: ["a", "b", "c"]
194
+ }, { items: ["x", "y", "z"] }, { index: -1, key: "" });
195
+ expect(t).toEqual({
196
+ _id: "test-block",
197
+ _type: "repeater",
198
+ type: "repeater",
199
+ repeaterItems: ["x", "y", "z"],
200
+ repeaterItemsBinding: "{{items}}",
201
+ items: ["a", "b", "c"]
202
+ });
203
+ }), it("should handle nested blocks with bindings", () => {
204
+ const t = m({
205
+ _id: "test-block",
206
+ _type: "container",
207
+ type: "container",
208
+ children: [
209
+ {
210
+ _id: "child-block",
211
+ _type: "text",
212
+ type: "text",
213
+ content: "Item {{$index}}",
214
+ style: {
215
+ color: "{{theme.color}}"
216
+ }
217
+ }
218
+ ]
219
+ }, { theme: { color: "red" }, items: ["x", "y", "z"] }, { index: 2, key: "{{items}}" });
220
+ expect(t).toEqual({
221
+ _id: "test-block",
222
+ _type: "container",
223
+ type: "container",
224
+ children: [
225
+ {
226
+ _id: "child-block",
227
+ _type: "text",
228
+ type: "text",
229
+ content: "Item z",
230
+ style: {
231
+ color: "red"
232
+ }
233
+ }
234
+ ]
235
+ });
236
+ }), it("should handle arrays of blocks", () => {
237
+ const t = m({
238
+ _id: "test-block",
239
+ _type: "list",
240
+ type: "list",
241
+ items: [
242
+ { _id: "item1", content: "Item {{$index}}" },
243
+ { _id: "item2", content: "Item {{$index}}" }
244
+ ]
245
+ }, { items: ["x", "y", "z"] }, { index: 0, key: "{{items}}" });
246
+ expect(t).toEqual({
247
+ _id: "test-block",
248
+ _type: "list",
249
+ type: "list",
250
+ items: [
251
+ { _id: "item1", content: "Item x" },
252
+ { _id: "item2", content: "Item x" }
253
+ ]
254
+ });
255
+ });
256
+ }));
257
+ export {
258
+ w as R,
259
+ E as S,
260
+ m as a,
261
+ O as b,
262
+ A as c,
263
+ j as d,
264
+ z as e,
265
+ I as f,
266
+ R as g
267
+ };
@@ -0,0 +1 @@
1
+ "use strict";const y=require("@chaibuilder/runtime"),t=require("lodash-es"),b=require("tailwind-merge"),g="#styles:",h="__ADD_BLOCK_INTERNAL_ROOT",m=e=>{if(e=e.replace(g,""),!e)return{baseClasses:"",classes:""};const i=e.split(/,(?![^\[]*\])/);if(i.length===1)return{baseClasses:"",classes:i[0].trim()};const[s,...n]=i;return{baseClasses:s.trim(),classes:n.join(",").trim().replace(/ +(?= )/g,"")}};function x(e,i,s){const n=t.get(s,"i18nProps",[]);if(t.isEmpty(i)||t.isEmpty(n))return e;const a=t.cloneDeep(e);return t.forEach(t.keys(a),l=>{t.includes(n,l)&&!t.isEmpty(i)&&(a[l]=t.get(a,`${l}-${i}`,a[l]))}),a}const _=t.memoize(e=>{const{baseClasses:i,classes:s}=m(e);return b.twMerge(i,s)});function E(e,i){return t.get(e,`${i}_attrs`,{})}function k(e,i=!0){const s={};return Object.keys(e).forEach(n=>{if(t.isString(e[n])&&e[n].startsWith(g)){const a=_(e[n]),l=E(e,n);s[n]={...!t.isEmpty(a)&&{className:a},...l,...i?{"data-style-prop":n,"data-block-parent":e._id,"data-style-id":`${n}-${e._id}`}:{}}}}),s}const f=t.memoize(e=>{const i=y.getRegisteredChaiBlock(e),s=t.get(i,"schema.properties",{});return Object.fromEntries(Object.entries(s).filter(([,n])=>t.get(n,"runtime",!1)))}),B=(e,i)=>{if(!t.isArray(e))return e;let s=e,n;return typeof i.limit=="number"&&i.limit>0&&(n=i.limit),n!==void 0&&(s=s.slice(0,n)),s},I=(e,i)=>{const s=t.cloneDeep(e);return t.forEach(t.keys(s),n=>{if(t.isString(s[n])&&!t.startsWith(n,"_")){let a=s[n];n==="repeaterItems"&&(s.repeaterItemsBinding=a);const l=/\{\{(.*?)\}\}/g,p=a.match(l);p&&p.forEach(o=>{let r=o.slice(2,-2);const c=t.get(i,r,o);a=t.isArray(c)?c:a.replace(o,c)}),s[n]=a}}),s},u=(e,i,{index:s,key:n})=>{if(t.isString(e)){let a=e;if(e==="repeaterItems")return{value:e,repeaterItemsBinding:e};const l=/\{\{(.*?)\}\}/g,p=e.match(l);return p&&p.forEach(o=>{let r=o.slice(2,-2).trim(),c=n.slice(2,-2).trim();s!==-1&&t.startsWith(r,"$index.")?r=`${c}.${s}.${r.slice(7)}`:s!==-1&&t.startsWith(r,"$index")&&(r=`${c}.${s}`);const d=t.get(i,r,o);a=t.isArray(d)?d:a.replace(o,d)}),a}if(t.isArray(e))return e.map(a=>u(a,i,{index:s,key:n}));if(e&&typeof e=="object"){const a={};return t.forEach(t.keys(e),l=>{t.startsWith(l,"_")?a[l]=e[l]:a[l]=u(e[l],i,{index:s,key:n})}),a}return e},T=(e,i,{index:s,key:n})=>{const a=t.cloneDeep(e),l=u(a,i,{index:s,key:n});return a.repeaterItems&&(l.repeaterItemsBinding=a.repeaterItems),l};exports.ROOT_TEMP_KEY=h;exports.STYLES_KEY=g;exports.applyBindingToBlockProps=T;exports.applyChaiDataBinding=I;exports.applyLanguage=x;exports.applyLimit=B;exports.getBlockRuntimeProps=f;exports.getBlockTagAttributes=k;exports.getSplitChaiClasses=m;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),n=require("./core.cjs");require("lodash-es");require("clsx");require("tailwind-merge");require("tree-model");const c=require("react");require("jotai");require("@chaibuilder/runtime");require("sonner");const C=require("react-i18next"),v=require("./sooner-AJ7QkeLS.cjs"),j=require("@radix-ui/react-icons"),q=require("@react-hookz/web"),d=r=>{const s=document.createElement("div");return s.innerHTML=r,s.innerHTML};function g(){const{t:r}=C.useTranslation(),[s,x]=c.useState(!1),[i,m]=c.useState(""),[t,a]=n.useCodeEditor(),[u]=n.useSelectedBlockIds(),b=n.useUpdateBlocksProps(),f=n.useUpdateBlocksPropsRealtime(),h=q.useThrottledCallback(l=>{const o=d(l);f([t.blockId],{[t.blockProp]:o})},[],300),p=c.useCallback(()=>{if(s){const l=d(i);b([t.blockId],{[t.blockProp]:l})}},[s,i]);c.useEffect(()=>{u.includes(t==null?void 0:t.blockId)||(p(),a(null))},[u]);const k=()=>{a(null)};return e.jsxs("div",{className:"h-full rounded-t-lg border-t-4 border-black bg-black text-white",children:[e.jsx("button",{onClick:k,className:"fixed inset-0 z-[100000] cursor-default bg-gray-400/20"}),e.jsxs("div",{className:"relative z-[100001] h-full w-full flex-col gap-y-1",children:[e.jsxs("div",{className:"-mt-1 flex items-center justify-between px-2 py-2",children:[e.jsxs("h3",{className:"space-x-3 text-sm font-semibold",children:[e.jsx("span",{children:r("HTML Code Editor |")}),e.jsx("span",{className:"text-xs text-gray-400",children:r("Scripts will be only executed in preview and live mode.")})]}),e.jsx("div",{className:"flex gap-x-2",children:e.jsx(v.Button,{onClick:()=>a(null),size:"sm",variant:"destructive",className:"h-6 w-fit",children:e.jsx(j.Cross2Icon,{})})})]}),e.jsx("textarea",{className:"h-full w-full bg-black p-2 font-mono text-xs text-white/90",value:i||t.initialCode,onChange:l=>{const o=l.target.value;x(!0),m(o),h(o)}})]})]})}exports.default=g;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),n=require("./core.cjs");require("lodash-es");require("clsx");require("tailwind-merge");require("tree-model");const c=require("react");require("jotai");require("@chaibuilder/runtime");require("sonner");const C=require("react-i18next"),v=require("./sooner-CPeN3CGR.cjs"),j=require("@radix-ui/react-icons"),q=require("@react-hookz/web"),d=r=>{const s=document.createElement("div");return s.innerHTML=r,s.innerHTML};function g(){const{t:r}=C.useTranslation(),[s,x]=c.useState(!1),[i,m]=c.useState(""),[t,a]=n.useCodeEditor(),[u]=n.useSelectedBlockIds(),b=n.useUpdateBlocksProps(),f=n.useUpdateBlocksPropsRealtime(),h=q.useThrottledCallback(l=>{const o=d(l);f([t.blockId],{[t.blockProp]:o})},[],300),p=c.useCallback(()=>{if(s){const l=d(i);b([t.blockId],{[t.blockProp]:l})}},[s,i]);c.useEffect(()=>{u.includes(t==null?void 0:t.blockId)||(p(),a(null))},[u]);const k=()=>{a(null)};return e.jsxs("div",{className:"h-full rounded-t-lg border-t-4 border-black bg-black text-white",children:[e.jsx("button",{onClick:k,className:"fixed inset-0 z-[100000] cursor-default bg-gray-400/20"}),e.jsxs("div",{className:"relative z-[100001] h-full w-full flex-col gap-y-1",children:[e.jsxs("div",{className:"-mt-1 flex items-center justify-between px-2 py-2",children:[e.jsxs("h3",{className:"space-x-3 text-sm font-semibold",children:[e.jsx("span",{children:r("HTML Code Editor |")}),e.jsx("span",{className:"text-xs text-gray-400",children:r("Scripts will be only executed in preview and live mode.")})]}),e.jsx("div",{className:"flex gap-x-2",children:e.jsx(v.Button,{onClick:()=>a(null),size:"sm",variant:"destructive",className:"h-6 w-fit",children:e.jsx(j.Cross2Icon,{})})})]}),e.jsx("textarea",{className:"h-full w-full bg-black p-2 font-mono text-xs text-white/90",value:i||t.initialCode,onChange:l=>{const o=l.target.value;x(!0),m(o),h(o)}})]})]})}exports.default=g;
@@ -9,7 +9,7 @@ import "jotai";
9
9
  import "@chaibuilder/runtime";
10
10
  import "sonner";
11
11
  import { useTranslation as y } from "react-i18next";
12
- import { B as T } from "./sooner-DGhT8-JI.js";
12
+ import { B as T } from "./sooner-H65duPYc.js";
13
13
  import { Cross2Icon as B } from "@radix-ui/react-icons";
14
14
  import { useThrottledCallback as E } from "@react-hookz/web";
15
15
  const m = (s) => {