@raintech-oss/jovo-dew
Version:
Dew View Engine for Jovo
88 lines • 3.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.missingInterpolationHandler = void 0;
const lodash_1 = require("lodash");
const path_1 = __importDefault(require("path"));
const url_join_ts_1 = require("url-join-ts");
function missingInterpolationHandler(text, keys) {
let result;
let lastKey;
// Sample keys: ['{{name}}', 'name']
for (const key of keys) {
lastKey = key;
if (!key.startsWith('{{')) {
// check variables file for function named 'key'
result = processAsVariable(this, key);
if (result !== undefined) {
break;
}
// check audios file for variables named 'key'
result = processAsAudio(this, key);
if (result !== undefined) {
break;
}
}
}
if (result === undefined) {
result = `[missing variable: ${lastKey}]`;
}
return result.toString();
}
exports.missingInterpolationHandler = missingInterpolationHandler;
function processAsVariable(plugin, key) {
if (plugin.viewVariables) {
const func = plugin.viewVariables[key];
if (func) {
let result = func.call(plugin.viewVariables);
let myValue;
if (result.then) {
result.then((value) => {
// console.log("processAsVariable test1");
myValue = value;
}, () => {
// console.log("processAsVariable test2");
});
result = myValue;
}
return result;
}
}
}
function processAsAudio(plugin, key) {
var _a, _b, _c;
if (plugin.audioItems) {
const items = plugin.audioItems.filter((a) => a.variableName === key);
if (items.length > 0) {
const item = (0, lodash_1.sample)(items);
if (item) {
if (item.filename && item.filename !== '') {
const baseUrl = (_a = plugin.config.audio) === null || _a === void 0 ? void 0 : _a.baseUrl;
const defaultExt = (_c = (_b = plugin.config.audio) === null || _b === void 0 ? void 0 : _b.defaultExt) !== null && _c !== void 0 ? _c : '.mp3';
if (baseUrl) {
const fullPath = getAudioFullPath(item, baseUrl, defaultExt);
return `<audio src="${fullPath}" />`;
}
}
if (item.text) {
return item.text;
}
}
}
}
return;
}
function getAudioFullPath(item, baseUrl, defaultExt) {
if (!item.filename) {
return '';
}
const ext = path_1.default.extname(item.filename);
const fileName = ext ? item.filename : item.filename + defaultExt;
if (fileName.toLowerCase().startsWith('http')) {
return fileName;
}
return (0, url_join_ts_1.urlJoin)(baseUrl, fileName);
}
//# sourceMappingURL=missingInterpolationHandler.js.map