@chaibuilder/sdk 3.0.8 → 3.0.10
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.
- package/dist/apply-binding-C4AQBX3p.cjs +1 -0
- package/dist/apply-binding-CSYk-NOA.js +641 -0
- package/dist/core.cjs +4 -4
- package/dist/core.js +1172 -1080
- package/dist/render.cjs +2 -2
- package/dist/render.d.ts +1 -15
- package/dist/render.js +92 -66
- package/dist/web-blocks.cjs +1 -1
- package/dist/web-blocks.js +75 -63
- package/package.json +2 -3
- package/dist/apply-binding-HQ9gBNJP.cjs +0 -1
- package/dist/apply-binding-tfi-KyyV.js +0 -573
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const j=require("@chaibuilder/runtime"),t=require("lodash-es"),q=require("tailwind-merge"),b=require("./common-functions-BZmyleS1.cjs"),x=require("himalaya"),y="#styles:",O="__ADD_BLOCK_INTERNAL_ROOT",V="@",A="#",R=e=>{if(e=e.replace(y,""),!e)return{baseClasses:"",classes:""};const i=e.split(/,(?![^\[]*\])/);if(i.length===1)return{baseClasses:"",classes:i[0].trim()};const[n,...s]=i;return{baseClasses:n.trim(),classes:s.join(",").trim().replace(/ +(?= )/g,"")}};function W(e,i,n){const s=t.get(n,"i18nProps",[]);if(t.isEmpty(i)||t.isEmpty(s))return e;const a=t.cloneDeep(e);return t.forEach(t.keys(a),o=>{if(t.includes(s,o)&&!t.isEmpty(i)){const l=t.get(a,o),r=t.get(a,`${o}-${i}`,"");t.isString(l)?a[o]=t.isString(r)&&!t.isEmpty(r.trim())&&r.trimStart()||l:a[o]=t.isEmpty(r)?l:r}}),a}const F=t.memoize(e=>{const{baseClasses:i,classes:n}=R(e);return q.twMerge(i,n)});function z(e,i){return t.get(e,`${i}_attrs`,{})}function J(e,i=!0){const n={};return Object.keys(e).forEach(s=>{if(t.isString(e[s])&&e[s].startsWith(y)){const a=F(e[s]),o=z(e,s);n[s]={...!t.isEmpty(a)&&{className:a},...o,...i?{"data-style-prop":s,"data-block-parent":e._id,"data-style-id":`${s}-${e._id}`}:{}}}}),n}const Y=t.memoize(e=>{const i=j.getRegisteredChaiBlock(e),n=t.get(i,"schema.properties",{});return Object.fromEntries(Object.entries(n).filter(([,s])=>t.get(s,"runtime",!1)))}),K=(e,i)=>{if(!t.isArray(e))return e;let n=e,s;return typeof i.limit=="number"&&i.limit>0&&(s=i.limit),s!==void 0&&(n=n.slice(0,s)),n},U=(e,i)=>{const n=t.cloneDeep(e);return t.forEach(t.keys(n),s=>{if(t.isString(n[s])&&!t.startsWith(s,"_")){let a=n[s];s==="repeaterItems"&&(n.repeaterItemsBinding=a);const o=/\{\{(.*?)\}\}/g,l=a.match(o);l&&l.forEach(r=>{let u=r.slice(2,-2);const g=t.get(i,u,r);a=t.isArray(g)?g:a.replace(r,g)}),n[s]=a}}),n},X=e=>{const i=/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/,n=/(?:https?:\/\/)?(?:www\.)?(player)?.vimeo\.com/;return i.test(e)||n.test(e)},Z=e=>{if(t.isEmpty(e))return e;const i=/<video[^>]+src=['"]([^'">]+)['"]/,n=/<iframe[^>]+src=['"]([^'">]+)['"]/,s=e.match(i),a=e.match(n),o=s?s[1]:a?a[1]:null,l=/(?: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/;return o&&(l.test(o)||r.test(o))?o:e},P=["chai-name","data-chai-name"],G={img:{alt:"alt",width:"width",height:"height",src:"image"},video:{src:"url",autoplay:"controls.autoPlay",muted:"controls.muted",loop:"controls.loop",controls:"controls.widgets"},a:{href:"link.href",target:"link.target",type:""},input:{placeholder:"placeholder",required:"required",type:"inputType",name:"fieldName"},textarea:{placeholder:"placeholder",required:"required",type:"inputType",name:"fieldName"},select:{placeholder:"placeholder",required:"required",multiple:"multiple",name:"fieldName"},form:{action:"action"}},Q=(e,i)=>e.children.length===1&&t.includes(["Heading","Paragraph","Span","ListItem","Button","Label","TableCell","Link","RichText"],i._type),_=e=>e.map(i=>i.type==="text"?t.get(i,"content",""):t.isEmpty(i.children)?"":_(i.children)).join(""),N=e=>e===null?"":e,M=e=>{const i=t.compact(t.map(e.split(/\s+/),t.trim)),n=t.find(i,r=>/^w-/.test(r)),s=t.find(i,r=>/^h-/.test(r));if(!s||!n)return{height:"",width:""};const a=r=>{if(!r)return;const u=r.match(/^[wh]-(?:\[(.*?)\]|(.+))$/);if(!u)return;if(u[1])return u[1];const g=u[2];return/^\d+(\.\d+)?$/.test(g)?`${Number(g)*4}px`:g==="auto"||t.includes(g,"%")?g:"16px"},o=a(n),l=a(s);return{width:t.includes(o,"px")?o:"16px",height:t.includes(l,"px")?l:"16px"}},D=e=>{if(e.tagName==="svg")return{};const i={},n=G[e.tagName]||{},s=e.attributes;return t.forEach(s,({key:a,value:o})=>{if(!t.includes(P,a))if(n[a]){if(e.tagName==="img"&&a==="src"&&!o.startsWith("http")){const l=t.find(e.attributes,{key:"width"}),r=t.find(e.attributes,{key:"height"});l&&r?o=`https://via.placeholder.com/${l==null?void 0:l.value}x${r==null?void 0:r.value}`:o="https://via.placeholder.com/150x150"}t.set(i,n[a],N(o))}else t.includes(["style","class","srcset"],a)||(t.has(i,"styles_attrs")||(i.styles_attrs={}),t.startsWith(a,"@")&&(a=a.replace("@","x-on:")),i.styles_attrs[`${a}`]=N(o))}),delete i.class,i},tt=(e,i="styles")=>{if(!e.attributes)return{[i]:`${y},`};const n=t.find(e.attributes,{key:"class"});if(n){const s=n.value;return{[i]:`${y},${s}`}}return{[i]:`${y},`}},et=e=>{const i=t.get(e,"attributes",[]),n=i.find(r=>r.key==="data-chai-richtext"||r.key==="chai-richtext"),s=i.find(r=>r.key==="data-chai-lightbox"||r.key==="chai-lightbox"),a=i.find(r=>r.key==="data-chai-dropdown"||r.key==="chai-dropdown"),o=i.find(r=>r.key==="data-chai-dropdown-button"||r.key==="chai-dropdown-button"),l=i.find(r=>r.key==="data-chai-dropdown-content"||r.key==="chai-dropdown-content");if(a)return{_type:"Dropdown"};if(o)return{_type:"DropdownButton"};if(l)return{_type:"DropdownContent"};if(n)return{_type:"RichText"};if(s)return{_type:"LightBoxLink"};switch(e.tagName){case"img":return{_type:"Image"};case"input":return{_type:"Input",showLabel:!1};case"hr":return{_type:"Divider"};case"br":return{_type:"LineBreak"};case"textarea":return{_type:"TextArea",showLabel:!1};case"audio":return{_type:"Audio"};case"canvas":return{_type:"Canvas"};case"video":case"iframe":return{_type:"CustomHTML"};case"svg":return{_type:"Icon"};case"select":return{_type:"Select",options:[]};case"option":return{_type:"Option"};case"ul":case"ol":case"dl":return{_type:"List",tag:e.tagName,_listType:e.tagName==="ol"?"list-decimal":"list-none"};case"li":case"dt":return{_type:"ListItem",tag:e.tagName};case"span":case"figcaption":case"legend":return{_type:"Span",tag:e.tagName};case"p":return{_type:"Paragraph",content:""};case"a":return{_type:"Link"};case"form":return{_type:"Form"};case"label":return{_type:"Label"};case"button":return{_type:"Button"};case"code":return{_type:"Box",_name:"Code"};case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":return{_type:"Heading",tag:e.tagName};case"table":return{_type:"Table"};case"tr":return{_type:"TableRow"};case"td":case"th":return{_type:"TableCell",tag:e.tagName};case"thead":return{_type:"TableHead"};case"tbody":return{_type:"TableBody"};case"tfoot":return{_type:"TableFooter"};default:{const r=t.get(e,"children",[]).length>0?"Box":"EmptyBox";return{_type:r,tag:e.tagName,_name:r=="EmptyBox"||e.tagName==="div"?r:t.capitalize(e.tagName)}}}},H=(e,i=null)=>t.flatMapDeep(e,n=>{var T,B,w,E,v,$,C,I,S,L;if(n.type==="comment")return[];let s={_id:b.generateUUID()};if(i&&(s._parent=i.block._id),n.type==="text")return t.isEmpty(t.get(n,"content",""))?[]:i&&Q(i.node,i.block)?(t.set(i,"block.content",t.get(n,"content","")),[]):{...s,_type:"Text",content:t.get(n,"content","")};const a=t.get(n,"attributes",[]),o=a.find(c=>c.key==="data-chai-richtext"||c.key==="chai-richtext"),l=a.find(c=>c.key==="data-chai-lightbox"||c.key==="chai-lightbox"),r=a.find(c=>c.key==="data-chai-dropdown"||c.key==="chai-dropdown"),u=a.find(c=>c.key==="data-chai-dropdown-button"||c.key==="chai-dropdown-button"),g=a.find(c=>c.key==="data-chai-dropdown-content"||c.key==="chai-dropdown-content");if(s={...s,...et(n),...D(n),...tt(n)},n.attributes){const c=n.attributes.find(p=>t.includes(P,p.key));c&&(s._name=c.value)}if(o)return s.content=x.stringify(n.children),t.has(s,"styles_attrs.data-chai-richtext")&&delete s.styles_attrs["data-chai-richtext"],t.has(s,"styles_attrs.chai-richtext")&&delete s.styles_attrs["chai-richtext"],[s];if(l){const c=["data-chai-lightbox","chai-lightbox","data-vbtype","data-autoplay","data-maxwidth","data-overlay","data-gall","href"];s={...s,href:((T=a.find(p=>p.key==="href"))==null?void 0:T.value)||"",hrefType:((B=a.find(p=>p.key==="data-vbtype"))==null?void 0:B.value)||"video",autoplay:((w=a.find(p=>p.key==="data-autoplay"))==null?void 0:w.value)==="true"?"true":"false",maxWidth:((v=(E=a.find(p=>p.key==="data-maxwidth"))==null?void 0:E.value)==null?void 0:v.replace("px",""))||"",backdropColor:(($=a.find(p=>p.key==="data-overlay"))==null?void 0:$.value)||"",galleryName:((C=a.find(p=>p.key==="data-gall"))==null?void 0:C.value)||""},t.forEach(c,p=>{t.has(s,`styles_attrs.${p}`)&&delete s.styles_attrs[p]})}if(r&&(delete s.styles_attrs,s.showDropdown=!1),g&&delete s.styles_attrs,u){delete s.styles_attrs;const c=t.filter(n.children||[],d=>(d==null?void 0:d.tagName)!=="span");s.content=_(c);const p=t.find(n.children||[],d=>(d==null?void 0:d.tagName)==="span"&&t.some(d.children||[],h=>(h==null?void 0:h.tagName)==="svg"));if(p){const d=t.find(p.children||[],h=>(h==null?void 0:h.tagName)==="svg");if(d){s.icon=x.stringify([d]);const{height:h,width:f}=st(d,"16px","16px");s.iconHeight=h,s.iconWidth=f}}return[s]}if(s._type==="Input"){const c=s.inputType||"text";c==="checkbox"?t.set(s,"_type","Checkbox"):c==="radio"&&t.set(s,"_type","Radio")}else if(n.tagName==="video"||n.tagName==="iframe"){const c=x.stringify([n]);return X(c)&&(t.set(s,"_type","Video"),t.set(s,"url",Z(c)),t.set(s,"styles",`${y},`),t.set(s,"controls",{autoPlay:!1,muted:!0,loop:!1,controls:!1})),s.content=c,[s]}else if(n.tagName==="svg"){const c=t.get(t.find(n.attributes,{key:"class"}),"value",""),{height:p,width:d}=M(c);if(p&&d)s.styles=`${y}, ${b.cn(`w-${d} h-${p}`,c)}`.trim(),s.height=p==null?void 0:p.replace("px",""),s.width=d==null?void 0:d.replace("px","");else{const h=(I=t.find(n.attributes,{key:"height"}))==null?void 0:I.value,f=(S=t.find(n.attributes,{key:"width"}))==null?void 0:S.value;h&&f?(s.styles=`${y}, ${b.cn(`w-[${f}px] h-[${h}px]`,c)}`.trim(),s.height=h,s.width=f):s.styles=`${y}, ${b.cn("w-full h-full",c)}`.trim()}return n.attributes=t.filter(n.attributes,h=>!t.includes(["style","width","height","class"],h.key)),s.icon=x.stringify([n]),[s]}else if(n.tagName=="option"&&i&&((L=i.block)==null?void 0:L._type)==="Select")return i.block.options.push({label:_(n.children),...D(n)}),[];const m=H(n.children,{block:s,node:n});return[s,...m]}),st=(e,i,n)=>{var u,g;const s=t.get(e,"attributes",[]),{height:a,width:o}=M(t.get(t.find(s,{key:"class"}),"value",""));if(a&&o)return{height:`[${a}px]`,width:`[${o}px]`};const l=(u=t.find(s,{key:"height"}))==null?void 0:u.value,r=(g=t.find(s,{key:"width"}))==null?void 0:g.value;return{height:l?`[${l}px]`:n,width:r?`[${r}px]`:i}},nt=e=>{e=e.replace(/(\w+)=\\?"(.*?)\\?"/g,(s,a,o)=>{let l=o.replace(/\\"/g,'"');return l=l.replace(/{([^}]+)}/g,r=>r.replace(/"/g,'\\"')),`${a}="${l.replace(/\\"/g,'"')}"`}),e=e.replace(/\\n/g,"").replace(/\\\\/g,"").replace(/\\([/<>])/g,"$1").replace(/\\./g,"").replace(/[\n\r\t\f\v]/g,""),e=e.replace(/\$name="[^"]*"/g,"");const i=e.match(/<body[^>]*>[\s\S]*?<\/body>/);return(i&&i.length>0?i[0].replace(/<body/,"<div").replace(/<\/body>/,"</div>"):e).replace(/\s+/g," ").replaceAll("> <","><").replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,"").trim()},at=e=>{const i=x.parse(nt(e));return t.isEmpty(e)?[]:t.flatten(H(i))},rt=e=>{const i=t.last(e);return e.map(n=>{const s=Object.keys(n);for(let a=0;a<s.length;a++)if(t.isString(n[s[a]])&&t.startsWith(s[a],"content")){const o=n===i?"":" ";n[s[a]]=`${n[s[a]].trim()}${o}`}return n})},k=(e,i,{index:n,key:s})=>{if(t.isString(e)){let a=e;const o=/\{\{(.*?)\}\}/g,l=e.match(o);return l&&l.forEach(r=>{let u=r.slice(2,-2).trim(),g=s.slice(2,-2).trim();n!==-1&&t.startsWith(u,"$index.")?u=`${g}.${n}.${u.slice(7)}`:n!==-1&&t.startsWith(u,"$index")&&(u=`${g}.${n}`);const m=t.get(i,u);m===void 0?a=a.replace(r,""):a=t.isArray(m)?m:a.replace(r,m)}),a}if(t.isArray(e))return e.map(a=>k(a,i,{index:n,key:s}));if(e&&typeof e=="object"){const a={};return t.forEach(t.keys(e),o=>{!t.startsWith(o,"_")&&o!=="$repeaterItemsKey"?a[o]=k(e[o],i,{index:n,key:s}):a[o]=e[o]}),a}return e},ot=(e,i,{index:n,key:s})=>{let a=t.cloneDeep(e);return a.repeaterItems&&(a.$repeaterItemsKey=a.repeaterItems,t.startsWith(a.repeaterItems,`{{${A}`)&&(a.$repeaterItemsKey=a.repeaterItems=`${a.repeaterItems.replace("}}",`/${a._id}}}`)}`)),k(a,i,{index:n,key:s})};exports.COLLECTION_PREFIX=A;exports.REPEATER_PREFIX=V;exports.ROOT_TEMP_KEY=O;exports.STYLES_KEY=y;exports.adjustSpacingInContentBlocks=rt;exports.applyBindingToBlockProps=ot;exports.applyChaiDataBinding=U;exports.applyLanguage=W;exports.applyLimit=K;exports.getBlockRuntimeProps=Y;exports.getBlockTagAttributes=J;exports.getBlocksFromHTML=at;exports.getSplitChaiClasses=R;
|
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
import { getRegisteredChaiBlock as Q } from "@chaibuilder/runtime";
|
|
2
|
+
import { memoize as Y, get as u, cloneDeep as S, forEach as $, keys as A, isString as T, startsWith as v, isArray as I, isEmpty as b, includes as _, flatten as tt, flatMapDeep as et, set as k, has as N, filter as V, find as g, some as st, compact as at, map as nt, trim as rt, capitalize as ot, last as ct } from "lodash-es";
|
|
3
|
+
import { twMerge as lt } from "tailwind-merge";
|
|
4
|
+
import { g as pt, c as D } from "./common-functions-BGzDsf1z.js";
|
|
5
|
+
import { parse as dt, stringify as E } from "himalaya";
|
|
6
|
+
const f = "#styles:", Ct = "__ADD_BLOCK_INTERNAL_ROOT", Nt = "@", ut = "#", K = (e) => {
|
|
7
|
+
if (e = e.replace(f, ""), !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(`${f}${n}`);
|
|
45
|
+
expect(t).toEqual(e[n]);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
function It(e, n, t) {
|
|
50
|
+
const a = u(t, "i18nProps", []);
|
|
51
|
+
if (b(n) || b(a)) return e;
|
|
52
|
+
const s = S(e);
|
|
53
|
+
return $(A(s), (o) => {
|
|
54
|
+
if (_(a, o) && !b(n)) {
|
|
55
|
+
const c = u(s, o), r = u(s, `${o}-${n}`, "");
|
|
56
|
+
T(c) ? s[o] = T(r) && !b(r.trim()) && r.trimStart() || c : s[o] = b(r) ? c : r;
|
|
57
|
+
}
|
|
58
|
+
}), s;
|
|
59
|
+
}
|
|
60
|
+
const ht = Y((e) => {
|
|
61
|
+
const { baseClasses: n, classes: t } = K(e);
|
|
62
|
+
return lt(n, t);
|
|
63
|
+
});
|
|
64
|
+
function yt(e, n) {
|
|
65
|
+
return u(e, `${n}_attrs`, {});
|
|
66
|
+
}
|
|
67
|
+
function Dt(e, n = !0) {
|
|
68
|
+
const t = {};
|
|
69
|
+
return Object.keys(e).forEach((a) => {
|
|
70
|
+
if (T(e[a]) && e[a].startsWith(f)) {
|
|
71
|
+
const s = ht(e[a]), o = yt(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 = u(n, "schema.properties", {});
|
|
86
|
+
return Object.fromEntries(Object.entries(t).filter(([, a]) => u(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 = S(e);
|
|
93
|
+
return $(A(t), (a) => {
|
|
94
|
+
if (T(t[a]) && !v(a, "_")) {
|
|
95
|
+
let s = t[a];
|
|
96
|
+
a === "repeaterItems" && (t.repeaterItemsBinding = s);
|
|
97
|
+
const o = /\{\{(.*?)\}\}/g, c = s.match(o);
|
|
98
|
+
c && c.forEach((r) => {
|
|
99
|
+
let p = r.slice(2, -2);
|
|
100
|
+
const h = u(n, p, r);
|
|
101
|
+
s = I(h) ? h : s.replace(r, h);
|
|
102
|
+
}), t[a] = s;
|
|
103
|
+
}
|
|
104
|
+
}), t;
|
|
105
|
+
}, gt = (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
|
+
}, mt = (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, c = /(?: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 && (c.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
|
+
}, ft = (e, n) => e.children.length === 1 && _(
|
|
149
|
+
["Heading", "Paragraph", "Span", "ListItem", "Button", "Label", "TableCell", "Link", "RichText"],
|
|
150
|
+
n._type
|
|
151
|
+
), L = (e) => e.map((n) => n.type === "text" ? u(n, "content", "") : b(n.children) ? "" : L(n.children)).join(""), W = (e) => e === null ? "" : e, Z = (e) => {
|
|
152
|
+
const n = at(nt(e.split(/\s+/), rt)), t = g(n, (r) => /^w-/.test(r)), a = g(n, (r) => /^h-/.test(r));
|
|
153
|
+
if (!a || !t) return { height: "", width: "" };
|
|
154
|
+
const s = (r) => {
|
|
155
|
+
if (!r) return;
|
|
156
|
+
const p = r.match(/^[wh]-(?:\[(.*?)\]|(.+))$/);
|
|
157
|
+
if (!p) return;
|
|
158
|
+
if (p[1]) return p[1];
|
|
159
|
+
const h = p[2];
|
|
160
|
+
return /^\d+(\.\d+)?$/.test(h) ? `${Number(h) * 4}px` : h === "auto" || _(h, "%") ? h : "16px";
|
|
161
|
+
}, o = s(t), c = s(a);
|
|
162
|
+
return {
|
|
163
|
+
width: _(o, "px") ? o : "16px",
|
|
164
|
+
height: _(c, "px") ? c : "16px"
|
|
165
|
+
};
|
|
166
|
+
}, U = (e) => {
|
|
167
|
+
if (e.tagName === "svg") return {};
|
|
168
|
+
const n = {}, t = bt[e.tagName] || {}, a = e.attributes;
|
|
169
|
+
return $(a, ({ key: s, value: o }) => {
|
|
170
|
+
if (!_(X, s))
|
|
171
|
+
if (t[s]) {
|
|
172
|
+
if (e.tagName === "img" && s === "src" && !o.startsWith("http")) {
|
|
173
|
+
const c = g(e.attributes, { key: "width" }), r = g(e.attributes, { key: "height" });
|
|
174
|
+
c && r ? o = `https://via.placeholder.com/${c == null ? void 0 : c.value}x${r == null ? void 0 : r.value}` : o = "https://via.placeholder.com/150x150";
|
|
175
|
+
}
|
|
176
|
+
k(n, t[s], W(o));
|
|
177
|
+
} else _(["style", "class", "srcset"], s) || (N(n, "styles_attrs") || (n.styles_attrs = {}), v(s, "@") && (s = s.replace("@", "x-on:")), n.styles_attrs[`${s}`] = W(o));
|
|
178
|
+
}), delete n.class, n;
|
|
179
|
+
}, xt = (e, n = "styles") => {
|
|
180
|
+
if (!e.attributes) return { [n]: `${f},` };
|
|
181
|
+
const t = g(e.attributes, { key: "class" });
|
|
182
|
+
if (t) {
|
|
183
|
+
const a = t.value;
|
|
184
|
+
return { [n]: `${f},${a}` };
|
|
185
|
+
}
|
|
186
|
+
return { [n]: `${f},` };
|
|
187
|
+
}, _t = (e) => {
|
|
188
|
+
const n = u(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
|
+
), c = 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 (c)
|
|
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 = u(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 R, H, M, q, P, O, j, z, F, J;
|
|
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(u(t, "content", "")) ? [] : n && ft(n.node, n.block) ? (k(n, "block.content", u(t, "content", "")), []) : { ...a, _type: "Text", content: u(t, "content", "") };
|
|
295
|
+
const s = u(t, "attributes", []), o = s.find(
|
|
296
|
+
(i) => i.key === "data-chai-richtext" || i.key === "chai-richtext"
|
|
297
|
+
), c = s.find(
|
|
298
|
+
(i) => i.key === "data-chai-lightbox" || i.key === "chai-lightbox"
|
|
299
|
+
), r = s.find(
|
|
300
|
+
(i) => i.key === "data-chai-dropdown" || i.key === "chai-dropdown"
|
|
301
|
+
), p = s.find(
|
|
302
|
+
(i) => i.key === "data-chai-dropdown-button" || i.key === "chai-dropdown-button"
|
|
303
|
+
), h = s.find(
|
|
304
|
+
(i) => i.key === "data-chai-dropdown-content" || i.key === "chai-dropdown-content"
|
|
305
|
+
);
|
|
306
|
+
if (a = {
|
|
307
|
+
...a,
|
|
308
|
+
..._t(t),
|
|
309
|
+
...U(t),
|
|
310
|
+
...xt(t)
|
|
311
|
+
}, t.attributes) {
|
|
312
|
+
const i = t.attributes.find((l) => _(X, l.key));
|
|
313
|
+
i && (a._name = i.value);
|
|
314
|
+
}
|
|
315
|
+
if (o)
|
|
316
|
+
return a.content = E(t.children), N(a, "styles_attrs.data-chai-richtext") && delete a.styles_attrs["data-chai-richtext"], N(a, "styles_attrs.chai-richtext") && delete a.styles_attrs["chai-richtext"], [a];
|
|
317
|
+
if (c) {
|
|
318
|
+
const i = [
|
|
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: ((R = s.find((l) => l.key === "href")) == null ? void 0 : R.value) || "",
|
|
331
|
+
hrefType: ((H = s.find((l) => l.key === "data-vbtype")) == null ? void 0 : H.value) || "video",
|
|
332
|
+
autoplay: ((M = s.find((l) => l.key === "data-autoplay")) == null ? void 0 : M.value) === "true" ? "true" : "false",
|
|
333
|
+
maxWidth: ((P = (q = s.find((l) => l.key === "data-maxwidth")) == null ? void 0 : q.value) == null ? void 0 : P.replace("px", "")) || "",
|
|
334
|
+
backdropColor: ((O = s.find((l) => l.key === "data-overlay")) == null ? void 0 : O.value) || "",
|
|
335
|
+
galleryName: ((j = s.find((l) => l.key === "data-gall")) == null ? void 0 : j.value) || ""
|
|
336
|
+
}, $(i, (l) => {
|
|
337
|
+
N(a, `styles_attrs.${l}`) && delete a.styles_attrs[l];
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
if (r && (delete a.styles_attrs, a.showDropdown = !1), h && delete a.styles_attrs, p) {
|
|
341
|
+
delete a.styles_attrs;
|
|
342
|
+
const i = V(t.children || [], (d) => (d == null ? void 0 : d.tagName) !== "span");
|
|
343
|
+
a.content = L(i);
|
|
344
|
+
const l = g(
|
|
345
|
+
t.children || [],
|
|
346
|
+
(d) => (d == null ? void 0 : d.tagName) === "span" && st(d.children || [], (y) => (y == null ? void 0 : y.tagName) === "svg")
|
|
347
|
+
);
|
|
348
|
+
if (l) {
|
|
349
|
+
const d = g(l.children || [], (y) => (y == null ? void 0 : y.tagName) === "svg");
|
|
350
|
+
if (d) {
|
|
351
|
+
a.icon = E([d]);
|
|
352
|
+
const { height: y, width: B } = kt(d, "16px", "16px");
|
|
353
|
+
a.iconHeight = y, a.iconWidth = B;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return [a];
|
|
357
|
+
}
|
|
358
|
+
if (a._type === "Input") {
|
|
359
|
+
const i = a.inputType || "text";
|
|
360
|
+
i === "checkbox" ? k(a, "_type", "Checkbox") : i === "radio" && k(a, "_type", "Radio");
|
|
361
|
+
} else if (t.tagName === "video" || t.tagName === "iframe") {
|
|
362
|
+
const i = E([t]);
|
|
363
|
+
return gt(i) && (k(a, "_type", "Video"), k(a, "url", mt(i)), k(a, "styles", `${f},`), k(a, "controls", { autoPlay: !1, muted: !0, loop: !1, controls: !1 })), a.content = i, [a];
|
|
364
|
+
} else if (t.tagName === "svg") {
|
|
365
|
+
const i = u(g(t.attributes, { key: "class" }), "value", ""), { height: l, width: d } = Z(i);
|
|
366
|
+
if (l && d)
|
|
367
|
+
a.styles = `${f}, ${D(`w-${d} h-${l}`, i)}`.trim(), a.height = l == null ? void 0 : l.replace("px", ""), a.width = d == null ? void 0 : d.replace("px", "");
|
|
368
|
+
else {
|
|
369
|
+
const y = (z = g(t.attributes, { key: "height" })) == null ? void 0 : z.value, B = (F = g(t.attributes, { key: "width" })) == null ? void 0 : F.value;
|
|
370
|
+
y && B ? (a.styles = `${f}, ${D(`w-[${B}px] h-[${y}px]`, i)}`.trim(), a.height = y, a.width = B) : a.styles = `${f}, ${D("w-full h-full", i)}`.trim();
|
|
371
|
+
}
|
|
372
|
+
return t.attributes = V(t.attributes, (y) => !_(["style", "width", "height", "class"], y.key)), a.icon = E([t]), [a];
|
|
373
|
+
} else if (t.tagName == "option" && n && ((J = n.block) == null ? void 0 : J._type) === "Select")
|
|
374
|
+
return n.block.options.push({
|
|
375
|
+
label: L(t.children),
|
|
376
|
+
...U(t)
|
|
377
|
+
}), [];
|
|
378
|
+
const w = G(t.children, { block: a, node: t });
|
|
379
|
+
return [a, ...w];
|
|
380
|
+
}), kt = (e, n, t) => {
|
|
381
|
+
var p, h;
|
|
382
|
+
const a = u(e, "attributes", []), { height: s, width: o } = Z(
|
|
383
|
+
u(g(a, { key: "class" }), "value", "")
|
|
384
|
+
);
|
|
385
|
+
if (s && o)
|
|
386
|
+
return {
|
|
387
|
+
height: `[${s}px]`,
|
|
388
|
+
width: `[${o}px]`
|
|
389
|
+
};
|
|
390
|
+
const c = (p = g(a, { key: "height" })) == null ? void 0 : p.value, r = (h = g(a, { key: "width" })) == null ? void 0 : h.value;
|
|
391
|
+
return {
|
|
392
|
+
height: c ? `[${c}px]` : t,
|
|
393
|
+
width: r ? `[${r}px]` : n
|
|
394
|
+
};
|
|
395
|
+
}, vt = (e) => {
|
|
396
|
+
e = e.replace(/(\w+)=\\?"(.*?)\\?"/g, (a, s, o) => {
|
|
397
|
+
let c = o.replace(/\\"/g, '"');
|
|
398
|
+
return c = c.replace(/{([^}]+)}/g, (r) => r.replace(/"/g, '\\"')), `${s}="${c.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 = dt(vt(e));
|
|
404
|
+
return b(e) ? [] : tt(G(n));
|
|
405
|
+
}, x = (e) => {
|
|
406
|
+
const n = ct(e);
|
|
407
|
+
return e.map((t) => {
|
|
408
|
+
const a = Object.keys(t);
|
|
409
|
+
for (let s = 0; s < a.length; s++)
|
|
410
|
+
if (T(t[a[s]]) && v(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 = x([
|
|
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 = x([
|
|
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 = x([
|
|
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 = x([
|
|
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 = x([]);
|
|
447
|
+
t(s).toEqual([]);
|
|
448
|
+
}), n("should handle single block", () => {
|
|
449
|
+
const s = x([{ _id: "1", _type: "Text", content: "Only" }]);
|
|
450
|
+
t(s[0].content).toBe("Only");
|
|
451
|
+
}), n("should handle blocks without content fields", () => {
|
|
452
|
+
const s = x([
|
|
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 = x([
|
|
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 = x([
|
|
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 m = (e, n, { index: t, key: a }) => {
|
|
473
|
+
if (T(e)) {
|
|
474
|
+
let s = e;
|
|
475
|
+
const o = /\{\{(.*?)\}\}/g, c = e.match(o);
|
|
476
|
+
return c && c.forEach((r) => {
|
|
477
|
+
let p = r.slice(2, -2).trim(), h = a.slice(2, -2).trim();
|
|
478
|
+
t !== -1 && v(p, "$index.") ? p = `${h}.${t}.${p.slice(7)}` : t !== -1 && v(p, "$index") && (p = `${h}.${t}`);
|
|
479
|
+
const w = u(n, p);
|
|
480
|
+
w === void 0 ? s = s.replace(r, "") : s = I(w) ? w : s.replace(r, w);
|
|
481
|
+
}), s;
|
|
482
|
+
}
|
|
483
|
+
if (I(e))
|
|
484
|
+
return e.map((s) => m(s, n, { index: t, key: a }));
|
|
485
|
+
if (e && typeof e == "object") {
|
|
486
|
+
const s = {};
|
|
487
|
+
return $(A(e), (o) => {
|
|
488
|
+
!v(o, "_") && o !== "$repeaterItemsKey" ? s[o] = m(e[o], n, { index: t, key: a }) : s[o] = e[o];
|
|
489
|
+
}), s;
|
|
490
|
+
}
|
|
491
|
+
return e;
|
|
492
|
+
}, C = (e, n, { index: t, key: a }) => {
|
|
493
|
+
let s = S(e);
|
|
494
|
+
return s.repeaterItems && (s.$repeaterItemsKey = s.repeaterItems, v(s.repeaterItems, `{{${ut}`) && (s.$repeaterItemsKey = s.repeaterItems = `${s.repeaterItems.replace("}}", `/${s._id}}}`)}`)), m(s, n, { index: t, key: a });
|
|
495
|
+
};
|
|
496
|
+
import.meta.vitest && (describe("applyBindingToValue", () => {
|
|
497
|
+
it("should handle string values with bindings", () => {
|
|
498
|
+
const t = m("Hello {{user.name}}", { user: { name: "John" } }, { index: -1, key: "" });
|
|
499
|
+
expect(t).toBe("Hello John");
|
|
500
|
+
}), it("should handle nested object properties", () => {
|
|
501
|
+
const t = m({
|
|
502
|
+
name: "John",
|
|
503
|
+
address: {
|
|
504
|
+
city: "{{user.city}}",
|
|
505
|
+
street: "123 Main St"
|
|
506
|
+
}
|
|
507
|
+
}, { user: { city: "New York" } }, { index: -1, key: "" });
|
|
508
|
+
expect(t).toEqual({
|
|
509
|
+
name: "John",
|
|
510
|
+
address: {
|
|
511
|
+
city: "New York",
|
|
512
|
+
street: "123 Main St"
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
}), it("should handle arrays of values", () => {
|
|
516
|
+
const t = m(["Hello {{user.name}}", "Welcome {{user.role}}"], { user: { name: "John", role: "Admin" } }, { index: -1, key: "" });
|
|
517
|
+
expect(t).toEqual(["Hello John", "Welcome Admin"]);
|
|
518
|
+
}), it("should handle $index binding in repeater context", () => {
|
|
519
|
+
const t = m("Item {{$index}}", { items: ["a", "b", "c"] }, { index: 1, key: "{{items}}" });
|
|
520
|
+
expect(t).toBe("Item b");
|
|
521
|
+
}), it("should handle $index binding with dot notation", () => {
|
|
522
|
+
const t = m("Item {{$index.value}}", { items: [{ value: "apple" }, { value: "banana" }, { value: "cherry" }] }, { index: 1, key: "{{items}}" });
|
|
523
|
+
expect(t).toBe("Item banana");
|
|
524
|
+
}), it("should return an empty string for non-existing bindings", () => {
|
|
525
|
+
const t = m("Hello {{user.nonexistent}}", { user: { name: "John" } }, { index: -1, key: "" });
|
|
526
|
+
expect(t).toBe("Hello ");
|
|
527
|
+
}), it("should preserve private properties starting with _", () => {
|
|
528
|
+
const n = m({
|
|
529
|
+
name: "John",
|
|
530
|
+
_private: "secret"
|
|
531
|
+
}, {}, { index: -1, key: "" });
|
|
532
|
+
expect(n).toEqual({
|
|
533
|
+
name: "John",
|
|
534
|
+
_private: "secret"
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
}), describe("applyBindingToBlockProps", () => {
|
|
538
|
+
it("should handle basic block with bindings", () => {
|
|
539
|
+
const t = C({
|
|
540
|
+
_id: "test-block",
|
|
541
|
+
_type: "text",
|
|
542
|
+
type: "text",
|
|
543
|
+
content: "Hello {{user.name}}",
|
|
544
|
+
style: {
|
|
545
|
+
color: "{{theme.color}}"
|
|
546
|
+
}
|
|
547
|
+
}, { user: { name: "John" }, theme: { color: "blue" } }, { index: -1, key: "" });
|
|
548
|
+
expect(t).toEqual({
|
|
549
|
+
_id: "test-block",
|
|
550
|
+
_type: "text",
|
|
551
|
+
type: "text",
|
|
552
|
+
content: "Hello John",
|
|
553
|
+
style: {
|
|
554
|
+
color: "blue"
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
}), it("should handle repeaterItems and repeaterItemsBinding", () => {
|
|
558
|
+
const t = C({
|
|
559
|
+
_id: "test-block",
|
|
560
|
+
_type: "repeater",
|
|
561
|
+
type: "repeater",
|
|
562
|
+
repeaterItems: "{{items}}",
|
|
563
|
+
items: ["a", "b", "c"]
|
|
564
|
+
}, { items: ["x", "y", "z"] }, { index: -1, key: "" });
|
|
565
|
+
expect(t).toEqual({
|
|
566
|
+
_id: "test-block",
|
|
567
|
+
_type: "repeater",
|
|
568
|
+
type: "repeater",
|
|
569
|
+
$repeaterItemsKey: "{{items}}",
|
|
570
|
+
repeaterItems: ["x", "y", "z"],
|
|
571
|
+
items: ["a", "b", "c"]
|
|
572
|
+
});
|
|
573
|
+
}), it("should handle nested blocks with bindings", () => {
|
|
574
|
+
const t = C({
|
|
575
|
+
_id: "test-block",
|
|
576
|
+
_type: "container",
|
|
577
|
+
type: "container",
|
|
578
|
+
children: [
|
|
579
|
+
{
|
|
580
|
+
_id: "child-block",
|
|
581
|
+
_type: "text",
|
|
582
|
+
type: "text",
|
|
583
|
+
content: "Item {{$index}}",
|
|
584
|
+
style: {
|
|
585
|
+
color: "{{theme.color}}"
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
]
|
|
589
|
+
}, { theme: { color: "red" }, items: ["x", "y", "z"] }, { index: 2, key: "{{items}}" });
|
|
590
|
+
expect(t).toEqual({
|
|
591
|
+
_id: "test-block",
|
|
592
|
+
_type: "container",
|
|
593
|
+
type: "container",
|
|
594
|
+
children: [
|
|
595
|
+
{
|
|
596
|
+
_id: "child-block",
|
|
597
|
+
_type: "text",
|
|
598
|
+
type: "text",
|
|
599
|
+
content: "Item z",
|
|
600
|
+
style: {
|
|
601
|
+
color: "red"
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
]
|
|
605
|
+
});
|
|
606
|
+
}), it("should handle arrays of blocks", () => {
|
|
607
|
+
const t = C({
|
|
608
|
+
_id: "test-block",
|
|
609
|
+
_type: "list",
|
|
610
|
+
type: "list",
|
|
611
|
+
items: [
|
|
612
|
+
{ _id: "item1", content: "Item {{$index}}" },
|
|
613
|
+
{ _id: "item2", content: "Item {{$index}}" }
|
|
614
|
+
]
|
|
615
|
+
}, { items: ["x", "y", "z"] }, { index: 0, key: "{{items}}" });
|
|
616
|
+
expect(t).toEqual({
|
|
617
|
+
_id: "test-block",
|
|
618
|
+
_type: "list",
|
|
619
|
+
type: "list",
|
|
620
|
+
items: [
|
|
621
|
+
{ _id: "item1", content: "Item x" },
|
|
622
|
+
{ _id: "item2", content: "Item x" }
|
|
623
|
+
]
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
}));
|
|
627
|
+
export {
|
|
628
|
+
ut as C,
|
|
629
|
+
Nt as R,
|
|
630
|
+
f as S,
|
|
631
|
+
C as a,
|
|
632
|
+
It as b,
|
|
633
|
+
Lt as c,
|
|
634
|
+
St as d,
|
|
635
|
+
x as e,
|
|
636
|
+
At as f,
|
|
637
|
+
Dt as g,
|
|
638
|
+
Rt as h,
|
|
639
|
+
K as i,
|
|
640
|
+
Ct as j
|
|
641
|
+
};
|