@grapesjs/studio-sdk-plugins 1.0.22 → 1.0.23-rc.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.
- package/dist/dataSourceEjs/EjsImporter.d.ts +2 -1
- package/dist/dataSourceEjs/index.cjs.js +23 -17
- package/dist/dataSourceEjs/index.es.js +339 -290
- package/dist/dataSourceEjs/index.umd.js +23 -17
- package/dist/dataSourceEjs/tests/EjsExporter.test.d.ts +1 -0
- package/dist/dataSourceEjs/tests/EjsImporter.test.d.ts +1 -0
- package/dist/dataSourceHandlebars/HandlebarsExporter.d.ts +2 -2
- package/dist/dataSourceHandlebars/HandlebarsImporter.d.ts +9 -0
- package/dist/dataSourceHandlebars/constants.d.ts +16 -0
- package/dist/dataSourceHandlebars/index.cjs.js +13 -13
- package/dist/dataSourceHandlebars/index.es.js +272 -217
- package/dist/dataSourceHandlebars/index.umd.js +13 -13
- package/dist/dataSourceHandlebars/tests/HandlebarsExporter.test.d.ts +1 -0
- package/dist/dataSourceHandlebars/tests/HandlebarsImporter.test.d.ts +1 -0
- package/dist/index.cjs.js +70 -63
- package/dist/index.es.js +3297 -3187
- package/dist/index.umd.js +70 -63
- package/dist/presetPrintable/index.cjs.js +7 -6
- package/dist/presetPrintable/index.es.js +204 -197
- package/dist/presetPrintable/index.umd.js +7 -6
- package/dist/presetPrintable/shared.d.ts +3 -0
- package/dist/presetPrintable/types.d.ts +4 -1
- package/dist/presetPrintable/typesSchema.d.ts +18 -0
- package/dist/presetPrintable/utils.d.ts +11 -0
- package/jest.config.js +9 -0
- package/package.json +4 -2
|
@@ -5,9 +5,9 @@ export declare class EjsImporter {
|
|
|
5
5
|
private parseEjsTemplate;
|
|
6
6
|
private createAppendLiteral;
|
|
7
7
|
private createAppendExpression;
|
|
8
|
-
private findNextElse;
|
|
9
8
|
private parseIfStatement;
|
|
10
9
|
private parseForStatement;
|
|
10
|
+
private parseForEachStatement;
|
|
11
11
|
private parseTemplateBlock;
|
|
12
12
|
private parseJsExpression;
|
|
13
13
|
private astToGrapesJS;
|
|
@@ -16,6 +16,7 @@ export declare class EjsImporter {
|
|
|
16
16
|
private handleExpressionStatement;
|
|
17
17
|
private handleIfStatement;
|
|
18
18
|
private handleForStatement;
|
|
19
|
+
private handleForEachStatement;
|
|
19
20
|
private handleBlockStatement;
|
|
20
21
|
private handleLiteral;
|
|
21
22
|
private handleIdentifier;
|
|
@@ -1,25 +1,31 @@
|
|
|
1
|
-
"use strict";const
|
|
2
|
-
${e.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
`},resolveCondition(t,e){var s,
|
|
8
|
-
<${
|
|
9
|
-
${(
|
|
10
|
-
|
|
11
|
-
`}})}
|
|
1
|
+
"use strict";const C="app.grapesjs.com",w="app-stage.grapesjs.com",b=[C,w,"localhost","127.0.0.1",".local-credentialless.webcontainer.io",".local.webcontainer.io","-sandpack.codesandbox.io"],P="license:check:start",L="license:check:end",v=()=>typeof window<"u",O=({isDev:o,isStage:t})=>`${o?"":`https://${t?w:C}`}/api`,k=()=>{const o=v()&&window.location.hostname;return!!o&&(b.includes(o)||b.some(t=>o.endsWith(t)))};async function T({path:o,baseApiUrl:t,method:e="GET",headers:n={},params:s,body:r}){const a=`${t||O({isDev:!1,isStage:!1})}${o}`,c={method:e,headers:{"Content-Type":"application/json",...n}};r&&(c.body=JSON.stringify(r));const p=s?new URLSearchParams(s).toString():"",l=p?`?${p}`:"",h=await fetch(`${a}${l}`,c);if(!h.ok)throw new Error(`HTTP error! status: ${h.status}`);return h.json()}var g=(o=>(o.free="free",o.startup="startup",o.business="business",o.enterprise="enterprise",o))(g||{});const I={[g.free]:0,[g.startup]:10,[g.business]:20,[g.enterprise]:30};function N(o){const t=o;return t.init=e=>n=>o(n,e),t}const M=o=>N(o);async function B({editor:o,plan:t,pluginName:e,licenseKey:n,cleanup:s}){let r="",i=!1;const a=k(),c=l=>{console.warn("Cleanup plugin:",e,"Reason:",l),s()},p=(l={})=>{var u;const{error:h,sdkLicense:d}=l,f=(u=l.plan)==null?void 0:u.category;if(!(d||l.license)||h)c(h||"Invalid license");else if(f){const m=I[t],S=I[f];m>S&&c({pluginRequiredPlan:t,licensePlan:f})}};o.on(P,l=>{r=l==null?void 0:l.baseApiUrl,i=!0}),o.on(L,l=>{p(l)}),setTimeout(async()=>{if(!i){if(a)return;if(n){const l=await _({licenseKey:n,pluginName:e,baseApiUrl:r});l&&p(l)}else c("The `licenseKey` option not provided")}},2e3)}async function _(o){const{licenseKey:t,pluginName:e,baseApiUrl:n}=o;try{return(await T({baseApiUrl:n,path:`/sdk/${t||"na"}`,method:"POST",params:{d:window.location.hostname,pn:e}})).result||{}}catch(s){return console.error("Error during SDK license check:",s),!1}}var y=(o=>(o.variable="data-variable",o.condition="data-condition",o.conditionTrue="data-condition-true-content",o.conditionFalse="data-condition-false-content",o.collection="data-collection",o.collectionItem="data-collection-item",o))(y||{});class W{constructor(t){this.strategy=t,this.requiredHelpers=new Set,this.helperLibrary={}}registerHelper(t){this.helperLibrary[t]&&this.requiredHelpers.add(t)}generateVariable(t){return this.strategy.getVariableSyntax(t)}generateCollectionStart(t){return this.registerHelper("slice"),this.strategy.getCollectionStartSyntax(t)}generateCollectionEnd(){return this.strategy.getCollectionEndSyntax()}generateConditionalStart(t){return this.strategy.getConditionalStartSyntax(t)}generateConditionElse(){return this.strategy.getConditionElseSyntax()}generateConditionalEnd(){return this.strategy.getConditionalEndSyntax()}reset(){this.requiredHelpers.clear()}}function j(o){return typeof o=="object"&&o.type===y.variable}function x({component:o,value:t,getCustomPath:e}){if(j(t)&&!t.collectionId&&e){const n=t.path??"",s=n.split(".");return{...t,path:e({component:o,path:n,paths:s})}}return t}const A=o=>{$(o,y.variable,{toHTML(t){var i;const e=this.tagName,n=this.__attrToString(t),s=(i=this.getDataResolver)==null?void 0:i.call(this),r=x({component:this,value:s,getCustomPath:t==null?void 0:t.getCustomPath});return`<${e}${n?` ${n}`:""}>${t.exporter.generateVariable(r)}</${e}>`}}),$(o,y.condition,{toHTML(t){var l,h;const e=t.exporter,n=t==null?void 0:t.getCustomPath,s=this.components(),[r,i]=(s==null?void 0:s.models)??[],a=(l=this.getDataResolver)==null?void 0:l.call(this),c=((h=this.resolveCondition)==null?void 0:h.call(this,a==null?void 0:a.condition,n))??{};let p="";return i&&(p=`
|
|
2
|
+
${e.generateConditionElse()}
|
|
3
|
+
${i.toHTML(t)??""}`),`
|
|
4
|
+
${e.generateConditionalStart(c)}
|
|
5
|
+
${(r==null?void 0:r.toHTML(t))??""}${p}
|
|
6
|
+
${e.generateConditionalEnd()}
|
|
7
|
+
`},resolveCondition(t,e){var s,r;if(!t)return{};if(t.operator==="and"||t.operator==="or")return{operator:t.operator,statements:((r=(s=t.statements)==null?void 0:s.map)==null?void 0:r.call(s,i=>{var a;return((a=this.resolveCondition)==null?void 0:a.call(this,i,e))??{}}))??[]};const n={component:this,getCustomPath:e};return{...t,left:x({...n,value:t.left}),right:x({...n,value:t.right})}}}),$(o,y.collection,{toHTML(t){const e=t.exporter,n=this.tagName,s=this.__attrToString(t),r=this.getDataResolver(),i={...r,dataSource:x({component:this,value:r==null?void 0:r.dataSource,getCustomPath:t==null?void 0:t.getCustomPath})},a=this.components().at(0);return`
|
|
8
|
+
<${n}${s?` ${s}`:""}>${e.generateCollectionStart(i)}
|
|
9
|
+
${(a==null?void 0:a.toHTML(t))??""}
|
|
10
|
+
${e.generateCollectionEnd()}</${n}>
|
|
11
|
+
`}})},$=(o,t,e)=>{const n=o.Components.getType(t);if(!n){console.warn(`Component type ${t} not found`);return}const{model:s}=n;o.Components.addType(t,{model:{...e,toHTML(r={}){var a;const{exporter:i}=r;return i?i.export?i.export({component:this}):((a=e.toHTML)==null?void 0:a.call(this,r))??"":s.prototype.toHTML.call(this,r)??""}}})},J=o=>{const t=o;return t.logicalOperator==="and"||t.logicalOperator==="or"},F=(o,t)=>(o.config.optsHtml={...o.config.optsHtml,exporter:new W(t)},A(o),()=>{o.config.optsHtml.exporter=void 0}),H=(o,t)=>{const e=s=>{const{input:r}=s,{contains:i}=t;(!i||r.includes(i))&&(s.input=t.importer.import(r))},n=o.Parser.events.htmlBefore;return o.on(n,e),()=>{o.off(n,e)}};class U{getVariableSyntax(t){const{defaultValue:e}=t,n=this.getFullPath(t),s=e?` || \`${e}\``:"";return`<%= ${n}${s} %>`}getCollectionStartSyntax(t){const{collectionId:e,dataSource:n,startIndex:s,endIndex:r}=t,i=(n==null?void 0:n.path)||e||"",a=this._sanitizeVariableName(e);let c="";if(s!==void 0||r!==void 0){const p=s??0,l=r!==void 0?r:"";c=`.slice(${p}${l!==""?`, ${l}`:""})`}return`<% ${i}${c}.forEach(function(currentItem, index, arr) {
|
|
12
12
|
const startIndex = parseInt(${s??0}, 10);
|
|
13
13
|
const endIndex = startIndex + arr.length;
|
|
14
|
-
const ${
|
|
14
|
+
const ${a}={ currentItem, currentIndex: startIndex + index, remainingItems: endIndex - (startIndex + index)} %>`}_sanitizeVariableName(t){return t.replace(/[^a-zA-Z0-9_$]/g,"_")}getCollectionEndSyntax(){return"<% }) %>"}getConditionalStartSyntax(t){return`<% if (${this._parseCondition(t)}) { %>`}getConditionElseSyntax(){return"<% } else { %>"}getConditionalEndSyntax(){return"<% } %>"}getFullPath({collectionId:t,path:e,variableType:n}){return(t?[this._sanitizeVariableName(t),n,e].filter(Boolean).join("."):e)||""}_parseCondition(t){if(!t)return"";if(J(t)){const e=t.statements.map(r=>this._parseCondition(r)),n=D[t.logicalOperator];let s=e.length>1?`(${e.join(`)${n}(`)})`:e[0];return t.logicalOperator==="xor"&&(s=`!!(${s})`),s}else{const e=this._parseValue(t.left),n=this._parseValue(t.right);switch(t.operator){case"equals":return`${e} === ${n}`;case">":case"<":case">=":case"<=":case"=":case"!=":return`${e} ${t.operator} ${n}`;case"contains":return`${e}?.includes(${n})`;case"startsWith":return`${e}?.startsWith(${n})`;case"endsWith":return`${e}?.endsWith(${n})`;case"equalsIgnoreCase":return`${e}?.toLowerCase() === ${n}?.toLowerCase()`;case"trimEquals":return`${e}?.trim() === ${n}?.trim()`;case"isTruthy":return`!!${e}`;case"isFalsy":return`!${e}`;case"isDefined":return`${e} !== undefined`;case"isNull":return`${e} === null`;case"isUndefined":return`${e} === undefined`;case"isArray":return`Array.isArray(${e})`;case"isObject":return`typeof ${e} === 'object' && ${e} !== null && !Array.isArray(${e})`;case"isString":return`typeof ${e} === 'string'`;case"isNumber":return`typeof ${e} === 'number' && !isNaN(${e})`;case"isBoolean":return`typeof ${e} === 'boolean'`;default:return`${e} ${t.operator} ${n}`}}}_parseValue(t){return typeof t=="object"?this.getFullPath(t):typeof t=="string"?`'${t.replace(/'/g,"\\'")}'`:String(t)}}const D={and:"&&",or:"||",xor:"^"},V=["===","==","!==","!=","<=",">=","<",">","&&","||"],q={"===":"=","==":"=","!==":"!=","!=":"!=","<=":"<=",">=":">=","<":"<",">":">","&&":"&&","||":"||"};class G{constructor(){this.messages=[],this.collectionStack=[]}import(t){this.messages=[],this.collectionStack=[];try{const e=this.parseEjsTemplate(t);return this.astToGrapesJS(e)}catch(e){return console.error("Error during import:",e),this.addMessage("error",`Error during import: ${e instanceof Error?e.message:String(e)}`),""}}parseEjsTemplate(t){const e=[];let n=0;for(;n<t.length;){const s=t.indexOf("<%",n);if(s===-1){n<t.length&&e.push(this.createAppendLiteral(t.slice(n)));break}s>n&&e.push(this.createAppendLiteral(t.slice(n,s)));const r=t.indexOf("%>",s);if(r===-1)throw new Error("Unclosed EJS tag");const i=t.slice(s+2,r).trim();if(n=r+2,i.startsWith("=")||i.startsWith("-")){const a=i.slice(1).trim();e.push(this.createAppendExpression(a))}else{if(i.startsWith("#"))continue;if(i.startsWith("if")){const a=this.parseIfStatement(i,t,n);e.push(a),n=a.endPos||n}else if(i.startsWith("for")){const a=this.parseForStatement(i,t,n);e.push(a),n=a.endPos||n}else{const a=i.match(/([\w][\w.-]*[\w])\s*\.\s*forEach\s*\(\s*(\w+)\s*=>\s*\{?/);if(a){const c=a[1],p=a[2],l=this.parseForEachStatement(c,p,t,n);e.push(l),n=l.endPos||n}else this.addMessage("info",`Unsupported scriptlet content: ${i}`)}}}return{type:"Program",body:e}}createAppendLiteral(t){return{type:"ExpressionStatement",expression:{type:"CallExpression",callee:{type:"Identifier",name:"__append"},arguments:[{type:"Literal",value:t}]}}}createAppendExpression(t){return{type:"ExpressionStatement",expression:{type:"CallExpression",callee:{type:"Identifier",name:"__append"},arguments:[this.parseJsExpression(t)]}}}parseIfStatement(t,e,n){const s=t.match(/if\s*\((.*)\)\s*\{?/);if(!s)throw new Error("Invalid if statement syntax");const r=this.parseJsExpression(s[1]),{body:i,endPos:a}=this.parseTemplateBlock(e,n);let c=null,p=a;const l=e.indexOf("<%",p);if(l===p){const h=e.indexOf("%>",l);if(h!==-1){const d=e.slice(l+2,h).trim(),f=h+2-l;if(d.startsWith("} else if")){p=l+f;const E=d.substring(d.indexOf("if")).trim(),u=this.parseIfStatement(E,e,p);c={type:"IfStatement",test:u.test,consequent:u.consequent,alternate:u.alternate},p=u.endPos}else if(d.startsWith("} else")){p=l+f;const{body:E,endPos:u}=this.parseTemplateBlock(e,p);c={type:"BlockStatement",body:E},p=u;const m=e.indexOf("<%",p),S=e.indexOf("%>",m);m===p&&e.slice(m+2,S).trim()==="}"&&(p=S+2)}else d==="}"&&(p=l+f)}}return{type:"IfStatement",test:r,consequent:{type:"BlockStatement",body:i},alternate:c,endPos:p}}parseForStatement(t,e,n){const s=t.match(/for\s*\((.*)\)\s*\{?/);if(!s)throw new Error("Invalid for statement syntax");const r=s[1].split("of");if(r.length!==2)throw new Error("Only for...of loops are supported");const i=r[0].trim().replace(/^let\s+|^const\s+|^var\s+/,""),a=r[1].trim(),{body:c,endPos:p}=this.parseTemplateBlock(e,n);return{type:"ForStatement",init:{type:"VariableDeclaration",kind:"let",declarations:[{type:"VariableDeclarator",id:{type:"Identifier",name:i},init:this.parseJsExpression(a)}]},test:null,update:null,body:{type:"BlockStatement",body:c},endPos:(()=>{const l=e.indexOf("<%",p),h=e.indexOf("%>",l);return l===p&&e.slice(l+2,h).trim()==="}"?h+2:p})()}}parseForEachStatement(t,e,n,s){const{body:r,endPos:i}=this.parseTemplateBlock(n,s);return{type:"ForEachStatement",collection:this.parseJsExpression(t),itemIdentifier:{type:"Identifier",name:e},body:{type:"BlockStatement",body:r},endPos:(()=>{const a=n.indexOf("<%",i),c=n.indexOf("%>",a);return a===i&&n.slice(a+2,c).trim()==="}"?c+2:i})()}}parseTemplateBlock(t,e){const n=[];let s=e;for(;s<t.length;){const r=t.indexOf("<%",s);if(r===-1){s<t.length&&n.push(this.createAppendLiteral(t.slice(s))),s=t.length;break}r>s&&n.push(this.createAppendLiteral(t.slice(s,r)));const i=t.indexOf("%>",r);if(i===-1)throw new Error("Unclosed EJS tag within block");const a=t.slice(r+2,i).trim();if(a==="}"||a.startsWith("} else")){s=r;break}if(s=i+2,a.startsWith("=")||a.startsWith("-")){const c=a.slice(1).trim();n.push(this.createAppendExpression(c))}else{if(a.startsWith("#"))continue;if(a.startsWith("if")){const c=this.parseIfStatement(a,t,s);n.push(c),s=c.endPos||s}else if(a.startsWith("for")){const c=this.parseForStatement(a,t,s);n.push(c),s=c.endPos||s}else{const c=a.match(/([\w][\w.-]*[\w])\s*\.\s*forEach\s*\(\s*(\w+)\s*=>\s*\{?/);if(c){const p=c[1],l=c[2],h=this.parseForEachStatement(p,l,t,s);n.push(h),s=h.endPos||s}else this.addMessage("info",`Unsupported scriptlet content within block: ${a}`)}}}return{body:n,endPos:s}}parseJsExpression(t){if(/^(['"]).*\1$/.test(t))return{type:"Literal",value:t.slice(1,-1)};if(/^-?\d+$/.test(t))return{type:"Literal",value:parseInt(t,10)};if(/^-?\d+\.\d+$/.test(t))return{type:"Literal",value:parseFloat(t)};if(t==="true"||t==="false")return{type:"Literal",value:t==="true"};for(const s of V){const r=t.lastIndexOf(s);if(r>0&&t[r-1]!=="!"&&t[r-1]!=="="&&t[r-1]!=="<"&&t[r-1]!==">")return{type:s==="&&"||s==="||"?"LogicalExpression":"BinaryExpression",operator:q[s],left:this.parseJsExpression(t.slice(0,r).trim()),right:this.parseJsExpression(t.slice(r+s.length).trim())}}const e=t.indexOf("."),n=t.indexOf("[");if(e>0&&(n===-1||e<n)||n>0&&(e===-1||n<e)){const s=e>0&&(n===-1||e<n)?e:n;let r,i,a=!1;if(s===e)r=t.slice(0,e),i={type:"Identifier",name:t.slice(e+1)},a=!1;else{r=t.slice(0,n);const c=t.indexOf("]",n);if(c===-1)throw new Error("Unclosed bracket in expression");const p=t.slice(n+1,c);i=/^(['"]).*\1$/.test(p)||/^-?\d+(\.\d+)?$/.test(p)?this.parseJsExpression(p):{type:"Identifier",name:p},a=!0}return{type:"MemberExpression",object:this.parseJsExpression(r),property:i,computed:a}}return{type:"Identifier",name:t}}astToGrapesJS(t){switch(t.type){case"Program":return this.handleProgram(t);case"VariableDeclaration":return this.handleVariableDeclaration(t);case"ExpressionStatement":return this.handleExpressionStatement(t);case"IfStatement":return this.handleIfStatement(t);case"ForStatement":return this.handleForStatement(t);case"ForEachStatement":return this.handleForEachStatement(t);case"BlockStatement":return this.handleBlockStatement(t);case"Literal":return this.handleLiteral(t);case"Identifier":return this.handleIdentifier(t);case"MemberExpression":return this.handleMemberExpression(t);case"BinaryExpression":return this.handleBinaryExpression(t);case"LogicalExpression":return this.handleLogicalExpression(t);case"CallExpression":return this.handleCallExpression(t);default:return this.addMessage("warning",`Unsupported node type: ${t.type}`),""}}handleProgram(t){let e="";for(const n of t.body)e+=this.astToGrapesJS(n);return e}handleVariableDeclaration(t){return""}handleExpressionStatement(t){return t.expression.type==="CallExpression"&&t.expression.callee.type==="Identifier"&&t.expression.callee.name==="__append"&&t.expression.arguments.length>0?this.astToGrapesJS(t.expression.arguments[0]):""}handleIfStatement(t){const e=this.getConditionExpression(t.test),n=this.astToGrapesJS(t.consequent);let s="";return t.alternate&&(s=this.astToGrapesJS(t.alternate)),`
|
|
15
15
|
<data-condition data-gjs-data-resolver='${JSON.stringify({condition:e})}'>
|
|
16
|
-
<data-condition-true-content>${
|
|
16
|
+
<data-condition-true-content>${n}</data-condition-true-content>
|
|
17
17
|
<data-condition-false-content>${s}</data-condition-false-content>
|
|
18
18
|
</data-condition>
|
|
19
|
-
`}handleForStatement(t){var a;if(((a=t.init)==null?void 0:a.type)!=="VariableDeclaration"||t.init.declarations.length===0)return this.addMessage("warning","Unsupported for loop init format"),"";const e=t.init.declarations[0];if(e.id.type!=="Identifier")return this.addMessage("warning","Unsupported for loop variable declaration id"),"";let
|
|
20
|
-
<data-collection data-gjs-data-resolver='${JSON.stringify({dataSource:{type:"data-variable",path:
|
|
19
|
+
`}handleForStatement(t){var a;if(((a=t.init)==null?void 0:a.type)!=="VariableDeclaration"||t.init.declarations.length===0)return this.addMessage("warning","Unsupported for loop init format"),"";const e=t.init.declarations[0];if(e.id.type!=="Identifier")return this.addMessage("warning","Unsupported for loop variable declaration id"),"";let n="";if(e.init)n=this.getExpressionString(e.init);else return this.addMessage("warning","For loop collection is undefined"),"";const s=e.id.name,r=s;this.collectionStack.push(s);const i=this.astToGrapesJS(t.body);return this.collectionStack.pop(),`
|
|
20
|
+
<data-collection data-gjs-data-resolver='${JSON.stringify({dataSource:{type:"data-variable",path:n},collectionId:r})}'>
|
|
21
21
|
<data-collection-item>
|
|
22
|
-
${
|
|
22
|
+
${i}
|
|
23
23
|
</data-collection-item>
|
|
24
24
|
</data-collection>
|
|
25
|
-
`}
|
|
25
|
+
`}handleForEachStatement(t){const e=this.getExpressionString(t.collection),n=t.itemIdentifier.name,s=n;this.collectionStack.push(n);const r=this.astToGrapesJS(t.body);return this.collectionStack.pop(),`
|
|
26
|
+
<data-collection data-gjs-data-resolver='${JSON.stringify({dataSource:{type:"data-variable",path:e},collectionId:s})}'>
|
|
27
|
+
<data-collection-item>
|
|
28
|
+
${r}
|
|
29
|
+
</data-collection-item>
|
|
30
|
+
</data-collection>
|
|
31
|
+
`}handleBlockStatement(t){let e="";for(const n of t.body)e+=this.astToGrapesJS(n);return e}handleLiteral(t){return String(t.value??"")}handleIdentifier(t){let e={path:t.name};if(this.collectionStack.length>0){const n=this.collectionStack[this.collectionStack.length-1];t.name===n&&(e={collectionId:n,variableType:"currentItem"})}return`<data-variable data-gjs-data-resolver='${JSON.stringify(e)}'></data-variable>`}handleMemberExpression(t,e={}){let s={path:this.getExpressionString(t),...e};if(this.collectionStack.length>0){const r=this.collectionStack[this.collectionStack.length-1];let i=t;const a=[];for(;i.type==="MemberExpression";)i.property.type==="Identifier"&&!i.computed?a.unshift(i.property.name):i.property.type==="Literal"&&i.computed?a.unshift(String(i.property.value)):a.unshift(this.getExpressionString(i.property)),i=i.object;if(i.type==="Identifier"&&i.name===r){const c=a.join(".");s={...e,collectionId:r,variableType:"currentItem",path:c}}}return`<data-variable data-gjs-data-resolver='${JSON.stringify(s)}'></data-variable>`}handleBinaryExpression(t){return`${this.getExpressionString(t.left)} ${t.operator} ${this.getExpressionString(t.right)}`}handleLogicalExpression(t){return t.operator==="||"&&t.right.type==="Literal"?this.handleMemberExpression(t.left,{defaultValue:t.right.value}):`${this.getExpressionString(t.left)} ${t.operator} ${this.getExpressionString(t.right)}`}handleCallExpression(t){var e;if(t.callee.type==="Identifier"&&t.callee.name==="include"){const n=((e=t.arguments[0])==null?void 0:e.type)==="Literal"?String(t.arguments[0].value):"";return this.addMessage("info",`Partial includes are not fully supported: ${n}`),""}return""}getConditionExpression(t){if(t.type==="Identifier")return{logicalOperator:"and",statements:[{left:{type:"data-variable",path:t.name},operator:"isTruthy"}]};if(t.type==="MemberExpression"){let e=this.getExpressionString(t),n="isTruthy";return e.startsWith("!!")?(n="isTruthy",e=e.slice(2)):e.startsWith("!")&&(n="isFalsy",e=e.slice(1)),{logicalOperator:"and",statements:[{left:{type:"data-variable",path:e},operator:n}]}}if(t.type==="BinaryExpression"||t.type==="LogicalExpression"){const e=this.getConditionExpression(t.left),n=this.getConditionExpression(t.right);return`${e} ${t.operator} ${n}`}return t.type==="Literal"?typeof t.value=="string"?`'${t.value}'`:String(t.value??""):this.getExpressionString(t)}getExpressionString(t){switch(t.type){case"Identifier":return t.name;case"Literal":return String(t.value??"");case"MemberExpression":{const e=this.getExpressionString(t.object),n=this.getExpressionString(t.property);return t.computed?`${e}[${n}]`:`${e}.${n}`}case"BinaryExpression":{const e=this.getExpressionString(t.left),n=this.getExpressionString(t.right);return`${e} ${t.operator} ${n}`}case"LogicalExpression":{const e=this.getExpressionString(t.left),n=this.getExpressionString(t.right);return`${e} ${t.operator} ${n}`}default:return this.addMessage("warning",`Unsupported expression type for getExpressionString: ${t.type}`),""}}addMessage(t,e){this.messages.push({type:t,message:e})}}const R="dataSourceEjs",K=g.startup,z=function(o,t={}){const e=F(o,new U),n=H(o,{contains:"<%",importer:new G});B({editor:o,licenseKey:t.licenseKey,plan:K,pluginName:R,cleanup:()=>{e(),n()}})},Z=M(z);module.exports=Z;
|