UNPKG

@effectai/effect-js

Version:

Effect Network Javscript/Typescript SDK (for [https://effect.network](https://effect.network))

74 lines 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Template = void 0; const tslib_1 = require("tslib"); const templateScript_1 = tslib_1.__importDefault(require("./templateScript")); class Template { constructor(html, placeholders = {}, options = {}, info = {}) { this.html = html; this.placeholders = placeholders; this.options = options; this.info = info; } render() { this.replacePlaceholders(); this.injectJSVar('FORCE_OPTIONS', this.options); this.injectJSVar('FORCE_INFO', this.info); this.injectJSVar('FORCE_PLACEHOLDERS', this.placeholders); this.injectHTML(templateScript_1.default); this.wrapForm(); return this.rendered; } static htmlEntities(str) { return String(str) .replace(/&/g, '&amp;') .replace(/</g, '&lt;') .replace(/>/g, '&gt;') .replace(/"/g, '&quot;') .replace(/'/g, '&#39;') .replace(/`/g, '&#96;'); } replacePlaceholders() { this.rendered = this.html.replace(/\$\{\s?(\w+)\s?\|?\s?(\w*)\s?\}/g, (all, name, option) => { if (name in this.placeholders) { let value = this.placeholders[name]; if (Array.isArray(value) || value === Object(value)) { value = JSON.stringify(value); } switch (option) { case 'raw': case 'html': return value; default: return Template.htmlEntities(value); } } return ''; }); } injectHTML(html, prepend = false) { if (prepend) { this.rendered = html + this.rendered; } else { this.rendered += html; } } injectJSVar(name, value) { const html = `<script>window.${name} = ${JSON.stringify(value)};</script>`; this.injectHTML(html, true); } injectJSFile(url, prepend = false) { const html = `<script src="${url}"></script>`; this.injectHTML(html, prepend); } injectCSSFile(url, prepend = false) { const html = `<link rel="stylesheet" type="text/css" href="${url}">`; this.injectHTML(html, prepend); } wrapForm() { this.rendered = `<meta charset="UTF-8" /><style>html {overflow-y: auto !important;}</style><form id="FORCE_FORM">${this.rendered}</form>`; } } exports.Template = Template; //# sourceMappingURL=template.js.map