@grapesjs/studio-sdk-plugins 1.0.22 → 1.0.23
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
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
${
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
`},resolveCondition(t,
|
|
8
|
-
<${
|
|
1
|
+
(function(d,p){typeof exports=="object"&&typeof module<"u"?module.exports=p(require("handlebars/dist/cjs/handlebars")):typeof define=="function"&&define.amd?define(["handlebars/dist/cjs/handlebars"],p):(d=typeof globalThis<"u"?globalThis:d||self,d.StudioSdkPlugins_dataSourceHandlebars=p(d.Handlebars))})(this,function(d){"use strict";const p="app.grapesjs.com",P="app-stage.grapesjs.com",b=[p,P,"localhost","127.0.0.1",".local-credentialless.webcontainer.io",".local.webcontainer.io","-sandpack.codesandbox.io"],v="license:check:start",I="license:check:end",T=()=>typeof window<"u",k=({isDev:s,isStage:t})=>`${s?"":`https://${t?P:p}`}/api`,L=()=>{const s=T()&&window.location.hostname;return!!s&&(b.includes(s)||b.some(t=>s.endsWith(t)))};async function N({path:s,baseApiUrl:t,method:e="GET",headers:n={},params:a,body:r}){const o=`${t||k({isDev:!1,isStage:!1})}${s}`,c={method:e,headers:{"Content-Type":"application/json",...n}};r&&(c.body=JSON.stringify(r));const h=a?new URLSearchParams(a).toString():"",l=h?`?${h}`:"",u=await fetch(`${o}${l}`,c);if(!u.ok)throw new Error(`HTTP error! status: ${u.status}`);return u.json()}var g=(s=>(s.free="free",s.startup="startup",s.business="business",s.enterprise="enterprise",s))(g||{});const x={[g.free]:0,[g.startup]:10,[g.business]:20,[g.enterprise]:30};function _(s){const t=s;return t.init=e=>n=>s(n,e),t}const H=s=>_(s);async function w({editor:s,plan:t,pluginName:e,licenseKey:n,cleanup:a}){let r="",i=!1;const o=L(),c=l=>{console.warn("Cleanup plugin:",e,"Reason:",l),a()},h=(l={})=>{var E;const{error:u,sdkLicense:G}=l,$=(E=l.plan)==null?void 0:E.category;if(!(G||l.license)||u)c(u||"Invalid license");else if($){const F=x[t],J=x[$];F>J&&c({pluginRequiredPlan:t,licensePlan:$})}};s.on(v,l=>{r=l==null?void 0:l.baseApiUrl,i=!0}),s.on(I,l=>{h(l)}),setTimeout(async()=>{if(!i){if(o)return;if(n){const l=await O({licenseKey:n,pluginName:e,baseApiUrl:r});l&&h(l)}else c("The `licenseKey` option not provided")}},2e3)}async function O(s){const{licenseKey:t,pluginName:e,baseApiUrl:n}=s;try{return(await N({baseApiUrl:n,path:`/sdk/${t||"na"}`,method:"POST",params:{d:window.location.hostname,pn:e}})).result||{}}catch(a){return console.error("Error during SDK license check:",a),!1}}var m=(s=>(s.variable="data-variable",s.condition="data-condition",s.conditionTrue="data-condition-true-content",s.conditionFalse="data-condition-false-content",s.collection="data-collection",s.collectionItem="data-collection-item",s))(m||{});class M{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 R(s){return typeof s=="object"&&s.type===m.variable}function f({component:s,value:t,getCustomPath:e}){if(R(t)&&!t.collectionId&&e){const n=t.path??"",a=n.split(".");return{...t,path:e({component:s,path:n,paths:a})}}return t}const A=s=>{S(s,m.variable,{toHTML(t){var i;const e=this.tagName,n=this.__attrToString(t),a=(i=this.getDataResolver)==null?void 0:i.call(this),r=f({component:this,value:a,getCustomPath:t==null?void 0:t.getCustomPath});return`<${e}${n?` ${n}`:""}>${t.exporter.generateVariable(r)}</${e}>`}}),S(s,m.condition,{toHTML(t){var l,u;const e=t.exporter,n=t==null?void 0:t.getCustomPath,a=this.components(),[r,i]=(a==null?void 0:a.models)??[],o=(l=this.getDataResolver)==null?void 0:l.call(this),c=((u=this.resolveCondition)==null?void 0:u.call(this,o==null?void 0:o.condition,n))??{};let h="";return i&&(h=`
|
|
2
|
+
${e.generateConditionElse()}
|
|
3
|
+
${i.toHTML(t)??""}`),`
|
|
4
|
+
${e.generateConditionalStart(c)}
|
|
5
|
+
${(r==null?void 0:r.toHTML(t))??""}${h}
|
|
6
|
+
${e.generateConditionalEnd()}
|
|
7
|
+
`},resolveCondition(t,e){var a,r;if(!t)return{};if(t.operator==="and"||t.operator==="or")return{operator:t.operator,statements:((r=(a=t.statements)==null?void 0:a.map)==null?void 0:r.call(a,i=>{var o;return((o=this.resolveCondition)==null?void 0:o.call(this,i,e))??{}}))??[]};const n={component:this,getCustomPath:e};return{...t,left:f({...n,value:t.left}),right:f({...n,value:t.right})}}}),S(s,m.collection,{toHTML(t){const e=t.exporter,n=this.tagName,a=this.__attrToString(t),r=this.getDataResolver(),i={...r,dataSource:f({component:this,value:r==null?void 0:r.dataSource,getCustomPath:t==null?void 0:t.getCustomPath})},o=this.components().at(0);return`
|
|
8
|
+
<${n}${a?` ${a}`:""}>${e.generateCollectionStart(i)}
|
|
9
9
|
${(o==null?void 0:o.toHTML(t))??""}
|
|
10
|
-
|
|
11
|
-
`}})},S=(s,t,
|
|
10
|
+
${e.generateCollectionEnd()}</${n}>
|
|
11
|
+
`}})},S=(s,t,e)=>{const n=s.Components.getType(t);if(!n){console.warn(`Component type ${t} not found`);return}const{model:a}=n;s.Components.addType(t,{model:{...e,toHTML(r={}){var o;const{exporter:i}=r;return i?i.export?i.export({component:this}):((o=e.toHTML)==null?void 0:o.call(this,r))??"":a.prototype.toHTML.call(this,r)??""}}})},j=(s,t)=>(s.config.optsHtml={...s.config.optsHtml,exporter:new M(t)},A(s),()=>{s.config.optsHtml.exporter=void 0}),D=(s,t)=>{const e=a=>{const{input:r}=a,{contains:i}=t;(!i||r.includes(i))&&(a.input=t.importer.import(r))},n=s.Parser.events.htmlBefore;return s.on(n,e),()=>{s.off(n,e)}},C={equals:"equals",isTruthy:"isTruthy",isFalsy:"isFalsy",isDefined:"isDefined",isNull:"isNull",isUndefined:"isUndefined",isArray:"isArray",isObject:"isObject",isString:"isString",isNumber:"isNumber",isBoolean:"isBoolean",isDefaultValue:"isDefaultValue"},B={...C,...{">":"numGt","<":"numLt",">=":"numGte","<=":"numLte","=":"numEq","!=":"numNeq"},...{contains:"strContains",startsWith:"strStartsWith",endsWith:"strEndsWith",equalsIgnoreCase:"strEqualsIgnoreCase",trimEquals:"strTrimEquals"}};class U{getHelperId(t){return B[t]||t}getFullPath({collectionId:t,path:e}){let n=e;return t&&(n=[this._sanitizeVariableName(t),e].filter(Boolean).join(".")),n||""}getVariableSyntax(t){const{defaultValue:e}=t,n=this.getFullPath(t);return e?`{{#if ${n}}}{{{${n}}}}{{else}}${e}{{/if}}`:`{{{${n}}}}`}getCollectionStartSyntax(t){const{collectionId:e,dataSource:n}=t,a=n==null?void 0:n.path;let r="";if(t.startIndex!==void 0||t.endIndex!==void 0){const h=t.startIndex??0,l=t.endIndex!==void 0?t.endIndex:"";l!==""?r=` (slice ${a} ${h} ${l})`:r=` (slice ${a} ${h})`}const o=` as |${this._sanitizeVariableName(e)}|`;return`{{#each ${r||a}${o}}}`}getCollectionEndSyntax(){return"{{/each}}"}getConditionalStartSyntax(t){if(!t)return"";const e=t,n=t;if(n.statements&&n.logicalOperator==="and"||n.logicalOperator==="or"){const{statements:a}=n,r=n.logicalOperator,i=a.map(c=>this._parseCondition(c)).join(" ");return`{{#if ${a.length>1?`(${r} ${i})`:i}}}`}else return`{{#if ${this._parseCondition(e)}}}`}_parseCondition(t){if(!t)return"";const e=t,n=t;if(n.statements&&n.logicalOperator==="and"||n.logicalOperator==="or"){const{statements:a}=n,r=n.logicalOperator,i=n.statements.map(o=>this._parseCondition(o)).join(" ");return a.length>1?`(${r} ${i})`:i}else{const a=e.operator,r=this.getHelperId(a),i=this._parseValue(e.left),o=this._parseValue(e.right);return this.getExpressionStr(r,i,o)}}getExpressionStr(t,e,n){return C[t]?t==="isTruthy"?e:t==="isFalsy"?`!${e}`:`${t} ${e}`:`(${t} ${e} ${n})`}_parseValue(t){return typeof t=="object"?this.getFullPath(t):typeof t=="string"?`'${t.replace(/'/g,"\\'")}'`:String(t)}_sanitizeVariableName(t){return t.replace(/[^a-zA-Z0-9_$]/g,"_")}getConditionElseSyntax(){return"{{else}}"}getConditionalEndSyntax(){return"{{/if}}"}}class V{constructor(){this.messages=[],this.collectionStack=[]}import(t){this.messages=[],this.collectionStack=[];const e=this.parse(t);return this.astToGrapesJS(e)}parse(t){try{return d.parse(t)}catch(e){if(e instanceof Error&&e.message.includes("Parse error")){const n=e.message.match(/line (\d+)/),a=n?parseInt(n[1]):0,r=e.message.match(/column (\d+)/),i=r?parseInt(r[1]):0;throw new Error(`Handlebars syntax error at line ${a}, column ${i}: ${e.message}`)}throw e}}astToGrapesJS(t){switch(t.type){case"Program":return this.handleProgram(t);case"ContentStatement":return this.handleContentStatement(t);case"MustacheStatement":return this.handleMustacheStatement(t);case"BlockStatement":return this.handleBlockStatement(t);case"PartialStatement":return this.handlePartialStatement(t);case"CommentStatement":return this.handleCommentStatement(t);case"ElementNode":return this.handleElementNode(t);case"TextNode":return this.handleTextNode(t);default:return this.addMessage("warning",`Unsupported node type: ${t.type}`,t.loc),""}}handleProgram(t){let e="";if(t.body)for(const n of t.body)e+=this.astToGrapesJS(n);return e}handleContentStatement(t){return t.value||""}handleMustacheStatement(t,e={}){const n=this.getExpressionString(t.path),a={...e};return this.collectionStack.length>0?this.processPathWithCollectionContext(n,a):a.path=n,`<data-variable data-gjs-data-resolver='${JSON.stringify(a)}'></data-variable>`}processPathWithCollectionContext(t,e){t.startsWith("@")?this.handleDirectCollectionReference(t,e):this.handleRelativePathReference(t,e)}handleDirectCollectionReference(t,e){if(this.isSpecialIterationVariable(t)){this.setSpecialVariableResolver(this.collectionStack[this.collectionStack.length-1],t,e);return}const[n,...a]=t.substring(1).split("/"),r=a.join(".");this.collectionStack.includes(n)?this.setCurrentItemResolver(n,r,e):this.handleParentReferenceFallback(t,e)}isSpecialIterationVariable(t){return["index","@index","key","@key"].includes(t)}setSpecialVariableResolver(t,e,n){n.collectionId=t;const a={index:"currentIndex","@index":"currentIndex",key:"currentKey","@key":"currentKey"};n.variableType=a[e]}setCurrentItemResolver(t,e,n){n.collectionId=t,n.variableType="currentItem",e&&(n.path=e)}handleRelativePathReference(t,e){const n=y(t),{parentLevel:a,remainingPathParts:r}=this.calculateParentLevel(n),i=r.join("."),o=this.collectionStack.length-1-a;o>=0&&o<this.collectionStack.length?this.resolveRelativePath(i,o,e):e.path=r.join(".")}calculateParentLevel(t){const e=this.collectionStack.findLastIndex(r=>r===t[0]);if(e!==-1)return{parentLevel:this.collectionStack.length-1-e,remainingPathParts:t.slice(1)};let n=0;const a=[...t];for(;a[0]==="..";)n++,a.shift();return{parentLevel:n,remainingPathParts:a}}resolveRelativePath(t,e,n){const a=this.collectionStack[e];n.collectionId=a,n.variableType="currentItem",t!=="this"&&(n.path=t.startsWith("this.")?t.split(".").slice(1).join("."):t)}handleParentReferenceFallback(t,e){const n=y(t),{parentLevel:a,remainingPathParts:r}=this.calculateParentLevel(n),i=r.join("."),o=this.collectionStack.length-1-a;o>=0&&o<this.collectionStack.length?this.resolveRelativePath(i,o,e):e.path=r.join(".")}handleBlockStatement(t){const e=t.path.original;switch(e){case"if":case"unless":return this.handleConditionalBlock(t,e);case"each":return this.handleEachBlock(t);default:return this.addMessage("warning",`Unsupported block helper: ${e}`,t.loc),this.handleGenericBlock(t)}}handleConditionalBlock(t,e){const r={logicalOperator:"and",statements:[{left:{type:"data-variable",path:this.getExpressionString(t.params[0])},operator:e==="unless"?"isFalsy":"isTruthy"}]},i=this.handleProgram(t.program),o=t.inverse?this.handleProgram(t.inverse):"";return`
|
|
12
12
|
<data-condition data-gjs-data-resolver='${JSON.stringify({condition:r})}'>
|
|
13
13
|
<data-condition-true-content>${i}</data-condition-true-content>
|
|
14
14
|
<data-condition-false-content>${o}</data-condition-false-content>
|
|
15
15
|
</data-condition>
|
|
16
|
-
`}handleEachBlock(t){const
|
|
17
|
-
<data-collection data-gjs-data-resolver='${JSON.stringify({dataSource:{type:"data-variable",path:
|
|
16
|
+
`}handleEachBlock(t){var r,i,o;const e=this.getExpressionString(t.params[0]);let n;t.params.length>=3&&t.params[1]==="as"&&((r=t.params[2])==null?void 0:r.type)==="PathExpression"?n=t.params[2].original:(o=(i=t.program)==null?void 0:i.blockParams)!=null&&o.length?n=t.program.blockParams[0]:n=`collection-${Math.random().toString(36).substring(2,10)}`,this.collectionStack.push(n);const a=this.handleProgram(t.program);return this.collectionStack.pop(),`
|
|
17
|
+
<data-collection data-gjs-data-resolver='${JSON.stringify({dataSource:{type:"data-variable",path:e},collectionId:n})}'>
|
|
18
18
|
<data-collection-item>${a}</data-collection-item>
|
|
19
19
|
</data-collection>
|
|
20
|
-
`}handleCommentStatement(t){return`<!--${t.value}-->`}handleElementNode(t){let
|
|
20
|
+
`}handleCommentStatement(t){return`<!--${t.value}-->`}handleElementNode(t){let e="";if(t.attributes)for(const a of t.attributes)a.value.type==="TextNode"?e+=` ${a.name}="${a.value.chars}"`:(this.addMessage("warning",`Complex attribute values not fully supported: ${a.name}`,t.loc),e+=` ${a.name}="${this.getExpressionString(a.value)}"`);let n="";if(t.children)for(const a of t.children)n+=this.astToGrapesJS(a);return`<${t.tag}${e}>${n}</${t.tag}>`}handleTextNode(t){return t.chars||""}handlePartialStatement(t){return this.addMessage("warning",`Partials are not fully supported: ${t.name.original}`,t.loc),`{{> ${t.name.original}}}`}handleGenericBlock(t){const e=this.handleProgram(t.program);return`<!-- Unsupported block: ${t.path.original} -->${e}`}getExpressionString(t){return t.type==="PathExpression"?t.original:t.type==="StringLiteral"?t.value:t.type==="NumberLiteral"?t.value.toString():t.type==="BooleanLiteral"?t.value?"true":"false":t.type==="SubExpression"?this.getExpressionString(t.params[0]):t.type==="Hash"?`{${t.pairs.map(n=>`${n.key}=${this.getExpressionString(n.value)}`).join(", ")}}`:(this.addMessage("warning",`Complex expression type not fully supported: ${t.type}`,t.loc),"")}addMessage(t,e,n){this.messages.push({type:t,message:e,location:n?{line:n.start.line,column:n.start.column}:void 0})}}const y=s=>{if(s==null||s==="")return[];if(s===".")return["this"];if(s===".."||s==="../")return[".."];if(s.startsWith("../")){const i=s.substring(3);return["..",...y(i)]}const t=[];let e=0,n=s.replace(/\[(?:(['"])(.*?)\1|([^\]]*))\]/g,(i,o,c,h)=>{const l=c!==void 0?c:h||"";return t[e]=l,`__HANDLEBARS_PLACEHOLDER_${e++}__`});n=n.replace(/\//g,".");const a=n.split("."),r=[];for(const i of a){const o=/^__HANDLEBARS_PLACEHOLDER_(\d+)__$/.exec(i);if(o){const c=parseInt(o[1],10);r.push(t[c])}else i!==""&&r.push(i)}return r},W="dataSourceHandlebars",q=g.startup;return H(function(s,t={}){const e=j(s,new U),n=D(s,{contains:"{{",importer:new V});w({editor:s,licenseKey:t.licenseKey,plan:q,pluginName:W,cleanup:()=>{e(),n()}})})});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|