UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

48 lines 2.23 kB
import { jsxs as _jsxs } from "react/jsx-runtime"; import { Text } from '@botonic/react'; import { ACCESS_TOKEN_VARIABLE_KEY, VARIABLE_PATTERN } from '../constants'; import { getValueFromKeyPath } from '../utils'; import { ContentFieldsBase } from './content-fields-base'; import { FlowButton } from './flow-button'; import { HtButtonStyle } from './hubtype-fields'; export class FlowText extends ContentFieldsBase { constructor() { super(...arguments); this.text = ''; this.code = ''; this.buttons = []; this.buttonStyle = HtButtonStyle.BUTTON; } static fromHubtypeCMS(cmsText, locale, cmsApi) { const newText = new FlowText(cmsText.id); newText.code = cmsText.code; newText.buttonStyle = cmsText.content.buttons_style || HtButtonStyle.BUTTON; newText.text = this.getTextByLocale(locale, cmsText.content.text); newText.buttons = cmsText.content.buttons.map(button => FlowButton.fromHubtypeCMS(button, locale, cmsApi)); return newText; } static replaceVariables(text, request) { const matches = text.match(VARIABLE_PATTERN); let replacedText = text; if (matches && request) { matches.forEach(match => { const keyPath = match.slice(1, -1); const botVariable = keyPath.endsWith(ACCESS_TOKEN_VARIABLE_KEY) ? match : 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 (_jsxs(Text, { children: [replacedText, this.buttons.map((button, buttonIndex) => button.renderButton(buttonIndex, this.buttonStyle))] }, id)); } } //# sourceMappingURL=flow-text.js.map