dropkiq
Version:
Liquid Expressions Simplified
1 lines • 4.01 kB
JavaScript
;var __read=this&&this.__read||function(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var i,a,n=r.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(i=n.next()).done;)s.push(i.value)}catch(e){a={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(a)throw a.error}}return s},__spread=this&&this.__spread||function(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(__read(arguments[t]));return e};Object.defineProperty(exports,"__esModule",{value:!0});var ScopeCapture_1=require("./ScopeCapture"),value_1=require("./liquidjs/src/template/value"),output_1=require("./liquidjs/src/template/output"),tag_1=require("./liquidjs/src/template/tag/tag"),html_1=require("./liquidjs/src/template/html"),LivePreviewer=function(){function e(e,t,r,i,a,n){this.path=e,this.suggestions=t,this.templates=r,this.caretToken=i,this.dataSchema=a,this.caretLiquidPosition=n,this.originalRawExpression=""+this.caretToken.raw,this.templatesWithoutHTML=__spread(r),this.removeHTMLFromTemplates(this.templatesWithoutHTML)}return e.prototype.suggestionsWithPreviews=function(){var e=this;return Object.keys(this.suggestions).forEach((function(t){var r,i=e.suggestions[t];r="ColumnTypes::Filter"===i.type?e.constructLiquidStatementForFilterSuggestion(i,t):e.constructLiquidStatementForSuggestion(i,t);var a=__spread(e.templatesWithoutHTML);e.modifyCaretTokenInTemplates(r,a);var n=new ScopeCapture_1.ScopeCapture(a,e.caretToken,e.dataSchema);n.capture(e.dataSchema.scope);var s=n.getCapturedRendered();i.template=e.writePreviewTemplate(a),i.preview=s})),this.suggestions},e.prototype.constructLiquidStatementForSuggestion=function(e,t){var r=(""+this.originalRawExpression).replace("{{","").replace("}}","").split("|"),i=(r.shift(),__spread(this.path));i.push(t);var a=i.join(".");return r.unshift(a),r.join(" | ")},e.prototype.constructLiquidStatementForFilterSuggestion=function(e,t){var r=""+this.originalRawExpression,i=r.slice(0,this.caretLiquidPosition).replace("{{","").replace(/\|(?!.*\|).*/,""),a=r.slice(this.caretLiquidPosition,r.length).replace("}}","").replace(/.*\||\}{2}/,"");return[i,e.insertionTemplate,a].map((function(e){return e.trim()})).filter((function(e){return e.length>0})).join(" | ")},e.prototype.modifyCaretTokenInTemplates=function(e,t){for(var r=this,i=0;i<t.length;i++){var a=t[i];if(a instanceof tag_1.Tag){["increment","include","cycle","decrement","layout","tablerow","raw","comment"].includes(a.name)&&(t.splice(i,1),i--)}a instanceof output_1.Output&&(a.token===this.caretToken?(a.token.raw="{{ "+e+" }}",a.value=new value_1.Value(e,!1)):(t.splice(i,1),i--)),this.traverseChildren(a,(function(t){r.modifyCaretTokenInTemplates(e,t)}))}},e.prototype.removeHTMLFromTemplates=function(e){for(var t=this,r=0;r<e.length;r++){var i=e[r];i instanceof html_1.HTML&&(e.splice(r,1),r--),this.traverseChildren(i,(function(e){t.removeHTMLFromTemplates(e)}))}},e.prototype.traverseChildren=function(e,t){e.impl&&e.impl.templates&&t(e.impl.templates),e.impl&&e.impl.branches&&e.impl.branches.forEach((function(e,r){var i=r>0?"{% elsif "+e.cond+" %}":null;t(e.templates,i)})),e.impl&&e.impl.cases&&e.impl.cases.forEach((function(e){t(e.templates,"{% when "+e.val+" %}")})),e.impl&&e.impl.elseTemplates&&e.impl.elseTemplates.length>0&&t(e.impl.elseTemplates,"{% else %}")},e.prototype.writePreviewTemplate=function(e,t){var r=this;return void 0===t&&(t=""),e.forEach((function(e,i){if("for"===e.name){var a=e.token.raw.replace("%}"," limit:1 %}");t=t.concat(a)}else t=t.concat(e.token.raw);if(r.traverseChildren(e,(function(e,i){i&&(t=t.concat(i)),t=r.writePreviewTemplate(e,t)})),e instanceof tag_1.Tag)switch(e.name){case"for":t=t.concat("{% endfor %}");break;case"if":t=t.concat("{% endif %}");break;case"unless":t=t.concat("{% endunless %}");break;case"capture":t=t.concat("{% endcapture %}");break;case"case":t=t.concat("{% endcase %}");break;case"assign":break;default:throw new Error('Unhandled Tag: "'+e.name+'"')}})),t},e}();exports.LivePreviewer=LivePreviewer;