@botonic/plugin-flow-builder
Version:
Use Flow Builder to show your contents
52 lines • 2.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlowText = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("@botonic/react");
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const content_fields_base_1 = require("./content-fields-base");
const flow_button_1 = require("./flow-button");
const hubtype_fields_1 = require("./hubtype-fields");
class FlowText extends content_fields_base_1.ContentFieldsBase {
constructor() {
super(...arguments);
this.text = '';
this.code = '';
this.buttons = [];
this.buttonStyle = hubtype_fields_1.HtButtonStyle.BUTTON;
}
static fromHubtypeCMS(cmsText, locale, cmsApi) {
const newText = new FlowText(cmsText.id);
newText.code = cmsText.code;
newText.buttonStyle = cmsText.content.buttons_style || hubtype_fields_1.HtButtonStyle.BUTTON;
newText.text = this.getTextByLocale(locale, cmsText.content.text);
newText.buttons = cmsText.content.buttons.map(button => flow_button_1.FlowButton.fromHubtypeCMS(button, locale, cmsApi));
return newText;
}
static replaceVariables(text, request) {
const matches = text.match(constants_1.VARIABLE_PATTERN);
let replacedText = text;
if (matches && request) {
matches.forEach(match => {
const keyPath = match.slice(1, -1);
const botVariable = keyPath.endsWith(constants_1.ACCESS_TOKEN_VARIABLE_KEY)
? match
: (0, utils_1.getValueFromKeyPath)(request, keyPath);
// TODO In local if change variable and render multiple times the value is always the last update
replacedText = replacedText.replace(match, this.isValidType(botVariable) ? botVariable : match);
});
}
return replacedText;
}
static isValidType(botVariable) {
const validTypes = ['boolean', 'string', 'number'];
return validTypes.includes(typeof botVariable);
}
toBotonic(id, request) {
const replacedText = FlowText.replaceVariables(this.text, request);
return ((0, jsx_runtime_1.jsxs)(react_1.Text, { children: [replacedText, this.buttons.map((button, buttonIndex) => button.renderButton(buttonIndex, this.buttonStyle))] }, id));
}
}
exports.FlowText = FlowText;
//# sourceMappingURL=flow-text.js.map