@chaibuilder/sdk 3.2.5 → 3.2.6

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 @@
1
+ "use strict";const T=require("./common-functions-B3a4xKt_.cjs"),t=require("lodash-es"),k=require("himalaya"),z=require("@chaibuilder/runtime"),U=require("tailwind-merge"),m="#styles:",Y="__ADD_BLOCK_INTERNAL_ROOT",K="@",V="#",v="dt#",H=e=>{if(e=e.replace(m,""),!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,"")}},X=e=>{const i=/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/,s=/(?:https?:\/\/)?(?:www\.)?(player)?.vimeo\.com/;return i.test(e)||s.test(e)},G=e=>{if(t.isEmpty(e))return e;const i=/<video[^>]+src=['"]([^'">]+)['"]/,s=/<iframe[^>]+src=['"]([^'">]+)['"]/,n=e.match(i),a=e.match(s),l=n?n[1]:a?a[1]:null,r=/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/,o=/(?:https?:\/\/)?(?:www\.)?player.vimeo\.com/;return l&&(r.test(l)||o.test(l))?l:e},M=["chai-name","data-chai-name"],Z={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=>e.map(i=>i.type==="text"?t.get(i,"content",""):t.isEmpty(i.children)?"":E(i.children)).join(""),B=e=>e===null?"":e,O=e=>{const i=t.compact(t.map(e.split(/\s+/),t.trim)),s=t.find(i,o=>/^w-/.test(o)),n=t.find(i,o=>/^h-/.test(o));if(!n||!s)return{height:"",width:""};const a=o=>{if(!o)return;const d=o.match(/^[wh]-(?:\[(.*?)\]|(.+))$/);if(!d)return;if(d[1])return d[1];const c=d[2];return/^\d+(\.\d+)?$/.test(c)?`${Number(c)*4}px`:c==="auto"||t.includes(c,"%")?c:"16px"},l=a(s),r=a(n);return{width:t.includes(l,"px")?l:"16px",height:t.includes(r,"px")?r:"16px"}},q=e=>{if(e.tagName==="svg")return{};const i={},s=Z[e.tagName]||{},n=e.attributes;return t.forEach(n,({key:a,value:l})=>{var r;if(!t.includes(M,a))if(a==="bid"&&(i._bid=B(l)),s[a]){if(e.tagName==="img"&&a==="src"&&!l.startsWith("http")){const o=t.find(e.attributes,{key:"width"}),d=t.find(e.attributes,{key:"height"});o&&d?l=`https://picsum.photos/${o==null?void 0:o.value}x${d==null?void 0:d.value}`:l="https://picsum.photos/150x150"}else if(e.tagName==="a"){const o=t.find(e.attributes,{key:"href"});o&&typeof(o==null?void 0:o.value)=="string"&&((r=o==null?void 0:o.value)!=null&&r.startsWith("pageType:"))&&t.set(i,"link.type","pageType")}t.set(i,s[a],B(l))}else t.includes(["style","class","srcset","bid"],a)||(t.has(i,"styles_attrs")||(i.styles_attrs={}),t.startsWith(a,"@")&&(a=a.replace("@","x-on:")),i.styles_attrs[`${a}`]=B(l))}),delete i.class,i},tt=(e,i="styles")=>{if(!e.attributes)return{[i]:`${m},`};const s=t.find(e.attributes,{key:"class"});if(s){const n=s.value;return{[i]:`${m},${n}`}}return{[i]:`${m},`}},et=e=>{const i=t.get(e,"attributes",[]),s=i.find(c=>c.key==="data-chai-richtext"||c.key==="chai-richtext"),n=i.find(c=>c.key==="data-chai-lightbox"||c.key==="chai-lightbox"),a=i.find(c=>c.key==="data-chai-dropdown"||c.key==="chai-dropdown"),l=i.find(c=>c.key==="data-chai-dropdown-button"||c.key==="chai-dropdown-button"),r=i.find(c=>c.key==="data-chai-dropdown-content"||c.key==="chai-dropdown-content"),o=i.find(c=>c.key==="class"),d=o&&o.value.split(/\s+/).includes("rte");if(a)return{_type:"Dropdown"};if(l)return{_type:"DropdownButton"};if(r)return{_type:"DropdownContent"};if(s||d)return{_type:"Paragraph"};if(n)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 c=t.get(e,"children",[]).length>0?"Box":"EmptyBox";return{_type:c,tag:e.tagName,_name:c=="EmptyBox"||e.tagName==="div"?c:t.capitalize(e.tagName)}}}},w=(e,i=null)=>t.flatMapDeep(e,s=>{var D,I,C,$,N,L,W,A,P,R;if(s.type==="comment")return[];let n={_id:T.generateUUID()};if(i&&(n._parent=i.block._id),s.type==="text")return t.isEmpty(t.get(s,"content",""))?[]:i&&Q(i.node,i.block)?(t.set(i,"block.content",t.get(s,"content","")),[]):{...n,_type:"Text",content:t.get(s,"content","")};if(t.startsWith(s.tagName,"chai-")){const p=s.attributes,u=t.find(p,{key:"chai-type"}),y=(u==null?void 0:u.value)||t.startCase(s.tagName.replace("chai-","")).replace(/\s+/g,"");n._type=y,t.forEach(p,({key:h,value:j})=>{if(h==="about-this-component"||h==="chai-type"||h==="can-move"||h==="can-delete")return;if(h==="id"){n._id=j;return}const J=t.startsWith(h,"_")?h:t.camelCase(h);let f=B(j);typeof f=="string"&&t.startsWith(f,"#styles:")&&(f=f.replace("#styles:","#styles:,")),n[J]=f});const g=w(s.children,{block:n,node:s});return[n,...g]}const a=t.get(s,"attributes",[]),l=s.tagName==="p"||a.find(p=>p.key==="data-chai-richtext"||p.key==="chai-richtext"),r=a.find(p=>p.key==="class"),o=r&&r.value.split(/\s+/).includes("rte"),d=a.find(p=>p.key==="data-chai-lightbox"||p.key==="chai-lightbox"),c=a.find(p=>p.key==="data-chai-dropdown"||p.key==="chai-dropdown"),b=a.find(p=>p.key==="data-chai-dropdown-button"||p.key==="chai-dropdown-button"),_=a.find(p=>p.key==="data-chai-dropdown-content"||p.key==="chai-dropdown-content");if(n={...n,...et(s),...q(s),...tt(s)},s.attributes){const p=s.attributes.find(u=>t.includes(M,u.key));p&&(n._name=p.value)}if(l||o)return n.content=`<p>${k.stringify(s.children||[])}</p>`,t.has(n,"styles_attrs.data-chai-richtext")&&delete n.styles_attrs["data-chai-richtext"],t.has(n,"styles_attrs.chai-richtext")&&delete n.styles_attrs["chai-richtext"],[n];if(d){const p=["data-chai-lightbox","chai-lightbox","data-vbtype","data-autoplay","data-maxwidth","data-overlay","data-gall","href"];n={...n,href:((D=a.find(u=>u.key==="href"))==null?void 0:D.value)||"",hrefType:((I=a.find(u=>u.key==="data-vbtype"))==null?void 0:I.value)||"video",autoplay:((C=a.find(u=>u.key==="data-autoplay"))==null?void 0:C.value)==="true"?"true":"false",maxWidth:((N=($=a.find(u=>u.key==="data-maxwidth"))==null?void 0:$.value)==null?void 0:N.replace("px",""))||"",backdropColor:((L=a.find(u=>u.key==="data-overlay"))==null?void 0:L.value)||"",galleryName:((W=a.find(u=>u.key==="data-gall"))==null?void 0:W.value)||""},t.forEach(p,u=>{t.has(n,`styles_attrs.${u}`)&&delete n.styles_attrs[u]})}if(c&&(delete n.styles_attrs,n.showDropdown=!1),_&&delete n.styles_attrs,b){delete n.styles_attrs;const p=t.filter(s.children||[],y=>(y==null?void 0:y.tagName)!=="span");n.content=E(p);const u=t.find(s.children||[],y=>(y==null?void 0:y.tagName)==="span"&&t.some(y.children||[],g=>(g==null?void 0:g.tagName)==="svg"));if(u){const y=t.find(u.children||[],g=>(g==null?void 0:g.tagName)==="svg");if(y){n.icon=k.stringify([y]);const{height:g,width:h}=st(y,"16px","16px");n.iconHeight=g,n.iconWidth=h}}return[n]}if(n._type==="Input"){const p=n.inputType||"text";p==="checkbox"?t.set(n,"_type","Checkbox"):p==="radio"&&t.set(n,"_type","Radio")}else if(s.tagName==="video"||s.tagName==="iframe"){const p=k.stringify([s]);return X(p)&&(t.set(n,"_type","Video"),t.set(n,"url",G(p)),t.set(n,"styles",`${m},`),t.set(n,"controls",{autoPlay:!1,muted:!0,loop:!1,controls:!1})),n.content=p,[n]}else if(s.tagName==="svg"){const p=t.get(t.find(s.attributes,{key:"class"}),"value",""),{height:u,width:y}=O(p);if(u&&y)n.styles=`${m}, ${T.cn(`w-${y} h-${u}`,p)}`.trim(),n.height=u==null?void 0:u.replace("px",""),n.width=y==null?void 0:y.replace("px","");else{const g=(A=t.find(s.attributes,{key:"height"}))==null?void 0:A.value,h=(P=t.find(s.attributes,{key:"width"}))==null?void 0:P.value;g&&h?(n.styles=`${m}, ${T.cn(`w-[${h}px] h-[${g}px]`,p)}`.trim(),n.height=g,n.width=h):n.styles=`${m}, ${T.cn("w-full h-full",p)}`.trim()}return s.attributes=t.filter(s.attributes,g=>!t.includes(["style","width","height","class"],g.key)),n.icon=k.stringify([s]),[n]}else if(s.tagName=="option"&&i&&((R=i.block)==null?void 0:R._type)==="Select")return i.block.options.push({label:E(s.children),...q(s)}),[];const x=w(s.children,{block:n,node:s});return[n,...x]}),st=(e,i,s)=>{var d,c;const n=t.get(e,"attributes",[]),{height:a,width:l}=O(t.get(t.find(n,{key:"class"}),"value",""));if(a&&l)return{height:`[${a}px]`,width:`[${l}px]`};const r=(d=t.find(n,{key:"height"}))==null?void 0:d.value,o=(c=t.find(n,{key:"width"}))==null?void 0:c.value;return{height:r?`[${r}px]`:s,width:o?`[${o}px]`:i}},nt=e=>{e=e.replace(/(\w+)=\\?"(.*?)\\?"/g,(n,a,l)=>{let r=l.replace(/\\"/g,'"');return r=r.replace(/{([^}]+)}/g,o=>o.replace(/"/g,'\\"')),`${a}="${r.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,i)=>t.find(e,{_id:i}),lt=(e,i)=>t.isEmpty(i)?e.map(s=>(t.unset(s,"_bid"),s)):t.map(e,s=>{const n=t.isEmpty(s._bid)?void 0:at(i,s._bid);if(n){n._type==="Icon"&&t.get(s,"icon","").match(/chai-default-svg/)&&delete s.icon;const a={...n,...s};return t.unset(a,"_bid"),a}return t.unset(s,"_bid"),s}),ot=e=>{const i=k.parse(nt(e));return t.isEmpty(e)?[]:t.flatten(w(i))};function rt(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=>{if(t.includes(n,l)&&!t.isEmpty(i)){const r=t.get(a,l),o=t.get(a,`${l}-${i}`,"");t.isString(r)?a[l]=t.isString(o)&&!t.isEmpty(o.trim())&&o.trimStart()||r:a[l]=t.isEmpty(o)?r:o}}),a}const F=(e,i)=>{const{baseClasses:s,classes:n}=H(e),l=n.split(" ").filter(o=>o.startsWith(v)).map(o=>{var d;return(d=i[o])==null?void 0:d.value}),r=n.split(" ").filter(o=>!o.startsWith(v)).join(" ");return U.twMerge.apply(null,[s,...l,r])};function ct(e,i){return t.get(e,`${i}_attrs`,{})}function pt(e,i=!0,s={}){const n={};return Object.keys(e).forEach(a=>{if(t.isString(e[a])&&e[a].startsWith(m)){const l=F(e[a],s),r=ct(e,a);n[a]={...!t.isEmpty(l)&&{className:l},...r,...i?{"data-style-prop":a,"data-block-parent":e._id,"data-style-id":`${a}-${e._id}`}:{}}}}),n}const ut=t.memoize(e=>{const i=z.getRegisteredChaiBlock(e),s=t.get(i,"schema.properties",{});return Object.fromEntries(Object.entries(s).filter(([,n])=>t.get(n,"runtime",!1)))}),dt=(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},yt=(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,r=a.match(l);r&&r.forEach(o=>{let d=o.slice(2,-2);const c=t.get(i,d,o);a=t.isArray(c)?c:a.replace(o,c)}),s[n]=a}}),s},gt=(e,i)=>e.map(s=>{const n=t.keys(s).filter(a=>t.isString(s[a])&&s[a].startsWith(m));return t.each(n,a=>{s[a]=`${m},${F(s[a],i)}`}),s}),ht=e=>{const i=t.last(e);return e.map(s=>{const n=Object.keys(s);for(let a=0;a<n.length;a++)if(t.isString(s[n[a]])&&t.startsWith(n[a],"content")){const l=s===i?"":" ";s[n[a]]=`${s[n[a]].trim()}${l}`}return s})},S=(e,i,{index:s,key:n},a)=>{if(t.isString(e)){let l=e;const r=/\{\{(.*?)\}\}/g,o=e.match(r);if(o){const d=a==="image"||a==="mobileImage";o.forEach(c=>{let b=c.slice(2,-2).trim(),_=n.slice(2,-2).trim();s!==-1&&t.startsWith(b,"$index.")?b=`${_}.${s}.${b.slice(7)}`:s!==-1&&t.startsWith(b,"$index")&&(b=`${_}.${s}`);const x=t.get(i,b);x===void 0?l=l.replace(c,""):d&&!t.isArray(x)?l=x:l=t.isArray(x)?x:l.replace(c,x)})}return l}if(t.isArray(e))return e.map(l=>S(l,i,{index:s,key:n},a));if(e&&typeof e=="object"){const l={};return t.forEach(t.keys(e),r=>{!t.startsWith(r,"_")&&r!=="$repeaterItemsKey"?l[r]=S(e[r],i,{index:s,key:n},r):l[r]=e[r]}),l}return e},mt=(e,i,{index:s,key:n})=>{let a=t.cloneDeep(e);return a.repeaterItems&&(a.$repeaterItemsKey=a.repeaterItems,t.startsWith(a.repeaterItems,`{{${V}`)&&(a.$repeaterItemsKey=a.repeaterItems=`${a.repeaterItems.replace("}}",`/${a._id}}}`)}`),!t.isEmpty(a.repeaterItems)&&a.pagination&&(a.repeaterTotalItems=`${a.repeaterItems.replace("}}",`/${a._id}/totalItems}}`)}`)),S(a,i,{index:s,key:n})};exports.COLLECTION_PREFIX=V;exports.DESIGN_TOKEN_PREFIX=v;exports.REPEATER_PREFIX=K;exports.ROOT_TEMP_KEY=Y;exports.STYLES_KEY=m;exports.adjustSpacingInContentBlocks=ht;exports.applyBindingToBlockProps=mt;exports.applyChaiDataBinding=yt;exports.applyDesignTokens=gt;exports.applyLanguage=rt;exports.applyLimit=dt;exports.getBlockRuntimeProps=ut;exports.getBlockTagAttributes=pt;exports.getBlocksFromHTML=ot;exports.getSplitChaiClasses=H;exports.mergeBlocksWithExisting=lt;