@vivocha/public-entities
Advanced tools
Comparing version 7.1.0 to 7.1.1
@@ -6,2 +6,10 @@ # Change Log | ||
## [7.1.1](https://github.com/vivocha/vivocha/compare/@vivocha/public-entities@7.1.0...@vivocha/public-entities@7.1.1) (2022-05-24) | ||
**Note:** Version bump only for package @vivocha/public-entities | ||
# [7.1.0](https://github.com/vivocha/vivocha/compare/@vivocha/public-entities@7.0.12...@vivocha/public-entities@7.1.0) (2020-05-18) | ||
@@ -8,0 +16,0 @@ |
@@ -11,3 +11,3 @@ "use strict"; | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -14,0 +14,0 @@ __exportStar(require("./bot"), exports); |
@@ -27,156 +27,153 @@ "use strict"; | ||
exports.mergeVariables = mergeVariables; | ||
let WidgetInstanceRenderer = /** @class */ (() => { | ||
class WidgetInstanceRenderer { | ||
constructor(options) { | ||
this.options = options; | ||
this.assetsById = (options.manifest.assets || []).reduce((o, i) => { | ||
o[i.id] = i; | ||
return o; | ||
}, {}); | ||
this.assetsByPath = (options.manifest.assets || []).reduce((o, i) => { | ||
o[i.path] = i; | ||
return o; | ||
}, {}); | ||
class WidgetInstanceRenderer { | ||
constructor(options) { | ||
this.options = options; | ||
this.assetsById = (options.manifest.assets || []).reduce((o, i) => { | ||
o[i.id] = i; | ||
return o; | ||
}, {}); | ||
this.assetsByPath = (options.manifest.assets || []).reduce((o, i) => { | ||
o[i.path] = i; | ||
return o; | ||
}, {}); | ||
} | ||
resolveUrl(url, base) { | ||
if (base && base !== 'stdin') { | ||
return URL.resolve(`x://y/${base}`, url).substr(6); | ||
} | ||
resolveUrl(url, base) { | ||
if (base && base !== 'stdin') { | ||
return URL.resolve(`x://y/${base}`, url).substr(6); | ||
} | ||
else { | ||
return url; | ||
} | ||
else { | ||
return url; | ||
} | ||
getAsset(url) { | ||
const match = url.match(WidgetInstanceRenderer.assetsRegExp); | ||
if (match && match.length > 1) { | ||
let asset = this.assetsByPath[match[1]] || this.assetsByPath[match[1] + '.scss'] || this.assetsByPath[match[1] + '.css']; | ||
if (asset) { | ||
return asset; | ||
} | ||
else { | ||
console.error('unknown asset', match[1]); | ||
throw new Error('unknown_asset'); | ||
} | ||
} | ||
getAsset(url) { | ||
const match = url.match(WidgetInstanceRenderer.assetsRegExp); | ||
if (match && match.length > 1) { | ||
let asset = this.assetsByPath[match[1]] || this.assetsByPath[match[1] + '.scss'] || this.assetsByPath[match[1] + '.css']; | ||
if (asset) { | ||
return asset; | ||
} | ||
else { | ||
return undefined; | ||
console.error('unknown asset', match[1]); | ||
throw new Error('unknown_asset'); | ||
} | ||
} | ||
getAssetUrl(asset) { | ||
return this.options.assetsBaseUrl + asset.id; | ||
else { | ||
return undefined; | ||
} | ||
fetchAsset(asset) { | ||
return this.fetchURL(this.getAssetUrl(asset)); | ||
} | ||
getAssetUrl(asset) { | ||
return this.options.assetsBaseUrl + asset.id; | ||
} | ||
fetchAsset(asset) { | ||
return this.fetchURL(this.getAssetUrl(asset)); | ||
} | ||
getStrings() { | ||
if (this.options.settings.strings) { | ||
return eredita_1.Eredita.deepExtend({}, this.options.strings, language_1.getStringsObject(this.options.settings.strings, this.options.requestedLanguage, this.options.defaultLanguage)); | ||
} | ||
getStrings() { | ||
if (this.options.settings.strings) { | ||
return eredita_1.Eredita.deepExtend({}, this.options.strings, language_1.getStringsObject(this.options.settings.strings, this.options.requestedLanguage, this.options.defaultLanguage)); | ||
else { | ||
return this.options.strings || {}; | ||
} | ||
} | ||
getVariables() { | ||
return mergeVariables(this.options.manifest, this.options.settings); | ||
} | ||
async renderHTML() { | ||
const fullStrings = this.getStrings(); | ||
let rawHtml = await this.fetchAsset(this.assetsById[this.options.manifest.htmlId]); | ||
rawHtml = rawHtml.replace(/__([\w\.]+)__/g, (match, p1) => { | ||
if (p1 === 'ENGAGEMENT_ID') { | ||
return this.options.id; | ||
} | ||
else if (p1 === 'WIDGET') { | ||
return `vivocha.getWidget('${this.options.id}')`; | ||
} | ||
else if (p1 === 'BASE_URL') { | ||
return this.options.baseUrl; | ||
} | ||
else if (p1 === 'ASSETS_BASE_URL') { | ||
return this.options.assetsBaseUrl; | ||
} | ||
else if (p1 === 'VARIABLES') { | ||
return JSON.stringify(this.getVariables()); | ||
} | ||
else if (p1.indexOf('VARIABLES.') === 0 && p1.length > 10) { | ||
const key = p1.substr(10); | ||
const val = this.getVariables()[key]; | ||
return typeof val === 'undefined' ? match : val; | ||
} | ||
else { | ||
return this.options.strings || {}; | ||
} | ||
} | ||
getVariables() { | ||
return mergeVariables(this.options.manifest, this.options.settings); | ||
} | ||
async renderHTML() { | ||
const fullStrings = this.getStrings(); | ||
let rawHtml = await this.fetchAsset(this.assetsById[this.options.manifest.htmlId]); | ||
rawHtml = rawHtml.replace(/__([\w\.]+)__/g, (match, p1) => { | ||
if (p1 === 'ENGAGEMENT_ID') { | ||
return this.options.id; | ||
let val = eredita_1.Eredita.dot(fullStrings, p1); | ||
if (typeof val === 'string') { | ||
return val; | ||
} | ||
else if (p1 === 'WIDGET') { | ||
return `vivocha.getWidget('${this.options.id}')`; | ||
} | ||
else if (p1 === 'BASE_URL') { | ||
return this.options.baseUrl; | ||
} | ||
else if (p1 === 'ASSETS_BASE_URL') { | ||
return this.options.assetsBaseUrl; | ||
} | ||
else if (p1 === 'VARIABLES') { | ||
return JSON.stringify(this.getVariables()); | ||
} | ||
else if (p1.indexOf('VARIABLES.') === 0 && p1.length > 10) { | ||
const key = p1.substr(10); | ||
const val = this.getVariables()[key]; | ||
return typeof val === 'undefined' ? match : val; | ||
} | ||
else { | ||
let val = eredita_1.Eredita.dot(fullStrings, p1); | ||
if (typeof val === 'string') { | ||
return val; | ||
} | ||
else { | ||
return match; | ||
} | ||
} | ||
}); | ||
rawHtml = rawHtml.replace(/(['"])((?:\.\/)?(assets\/.*?))(\?.*?)?\1/g, (match, dummy, p1, p2) => { | ||
const asset = this.getAsset(p2); | ||
if (asset) { | ||
return match.replace(p1, this.getAssetUrl(asset)); | ||
} | ||
else { | ||
return match; | ||
} | ||
}); | ||
return rawHtml; | ||
} | ||
async renderCSS(scope) { | ||
const rawScss = await this.fetchAsset(this.assetsById[this.options.manifest.scssId]); | ||
let mainScss = ''; | ||
if (this.options.manifest.variables && this.options.manifest.variables.length) { | ||
this.options.settings.variables = this.options.settings.variables || {}; | ||
for (let def of this.options.manifest.variables) { | ||
let val = this.options.settings.variables[def.id]; | ||
if (typeof val === 'undefined') { | ||
val = def.defaultValue; | ||
} | ||
if (typeof val !== 'undefined') { | ||
switch (def.type) { | ||
case 'string': | ||
case 'enum': | ||
mainScss += `$${def.id}: '${val.toString()}';\n`; | ||
break; | ||
case 'boolean': | ||
mainScss += `$${def.id}: ${!!val};\n`; | ||
break; | ||
case 'file': | ||
const fileData = val.toString(); | ||
const prefix = fileData.substr(0, 5).toUpperCase() === 'DATA:' || WidgetInstanceRenderer.assetsRegExp.test(fileData) ? '' : this.options.assetsBaseUrl; | ||
mainScss += `$${def.id}: '${prefix}${fileData}';\n`; | ||
break; | ||
default: | ||
mainScss += `$${def.id}: ${val.toString()};\n`; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
mainScss += rawScss; | ||
if (scope) { | ||
mainScss = `${scope} { ${mainScss} }`; | ||
}); | ||
rawHtml = rawHtml.replace(/(['"])((?:\.\/)?(assets\/.*?))(\?.*?)?\1/g, (match, dummy, p1, p2) => { | ||
const asset = this.getAsset(p2); | ||
if (asset) { | ||
return match.replace(p1, this.getAssetUrl(asset)); | ||
} | ||
let out = await this.renderSass(mainScss); | ||
if (this.options.settings.customCss) { | ||
out += '\n' + this.options.settings.customCss; | ||
else { | ||
return match; | ||
} | ||
out = out.replace(/url\((['"]?)(.*?)(?:\?.*?)?\1?\)/g, (match, dummy, p1) => { | ||
const asset = this.getAsset(p1); | ||
if (asset) { | ||
return match.replace(p1, this.getAssetUrl(asset)); | ||
}); | ||
return rawHtml; | ||
} | ||
async renderCSS(scope) { | ||
const rawScss = await this.fetchAsset(this.assetsById[this.options.manifest.scssId]); | ||
let mainScss = ''; | ||
if (this.options.manifest.variables && this.options.manifest.variables.length) { | ||
this.options.settings.variables = this.options.settings.variables || {}; | ||
for (let def of this.options.manifest.variables) { | ||
let val = this.options.settings.variables[def.id]; | ||
if (typeof val === 'undefined') { | ||
val = def.defaultValue; | ||
} | ||
else { | ||
return match; | ||
if (typeof val !== 'undefined') { | ||
switch (def.type) { | ||
case 'string': | ||
case 'enum': | ||
mainScss += `$${def.id}: '${val.toString()}';\n`; | ||
break; | ||
case 'boolean': | ||
mainScss += `$${def.id}: ${!!val};\n`; | ||
break; | ||
case 'file': | ||
const fileData = val.toString(); | ||
const prefix = fileData.substr(0, 5).toUpperCase() === 'DATA:' || WidgetInstanceRenderer.assetsRegExp.test(fileData) ? '' : this.options.assetsBaseUrl; | ||
mainScss += `$${def.id}: '${prefix}${fileData}';\n`; | ||
break; | ||
default: | ||
mainScss += `$${def.id}: ${val.toString()};\n`; | ||
break; | ||
} | ||
} | ||
}); | ||
return out; | ||
} | ||
} | ||
mainScss += rawScss; | ||
if (scope) { | ||
mainScss = `${scope} { ${mainScss} }`; | ||
} | ||
let out = await this.renderSass(mainScss); | ||
if (this.options.settings.customCss) { | ||
out += '\n' + this.options.settings.customCss; | ||
} | ||
out = out.replace(/url\((['"]?)(.*?)(?:\?.*?)?\1?\)/g, (match, dummy, p1) => { | ||
const asset = this.getAsset(p1); | ||
if (asset) { | ||
return match.replace(p1, this.getAssetUrl(asset)); | ||
} | ||
else { | ||
return match; | ||
} | ||
}); | ||
return out; | ||
} | ||
WidgetInstanceRenderer.assetsRegExp = /^(?:\.\/)?(assets\/.*)$/; | ||
return WidgetInstanceRenderer; | ||
})(); | ||
} | ||
exports.WidgetInstanceRenderer = WidgetInstanceRenderer; | ||
WidgetInstanceRenderer.assetsRegExp = /^(?:\.\/)?(assets\/.*)$/; | ||
async function createEngagementInstance(renderer) { | ||
@@ -183,0 +180,0 @@ const opts = renderer.options; |
{ | ||
"name": "@vivocha/public-entities", | ||
"version": "7.1.0", | ||
"version": "7.1.1", | ||
"description": "Vivocha public entities and types", | ||
@@ -45,3 +45,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "1fc5b44d8c69a93dd0e9f0066f5543e090d9999d" | ||
"gitHead": "52f3d782c1547f2f5f0fdd1c4f48c28a0be9d161" | ||
} |
62374
1913