UNPKG

@effectai/sdk

Version:

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

69 lines 2.41 kB
import templateScript from "./templateScript"; export 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); 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>`; } } //# sourceMappingURL=template.js.map