UNPKG

botui

Version:

Build customizable conversational UIs and bots

49 lines (48 loc) 1.48 kB
export function createBlock(type, meta, data, key) { return { key: key !== null && key !== void 0 ? key : -1, type: type, meta: meta, data: data, }; } // it only manages the listed blocks in the UI, not the action. export function blockManager(callback = (history = []) => { }) { let history = []; const getBlockIndexByKey = (key = -1) => history.findIndex((block) => block.key === key); const insertBlock = (block) => { const length = history.length; block.key = length; history.push(block); return length; }; return { getAll: () => history, setAll: (blocks) => { blocks.forEach((block) => insertBlock(Object.assign({}, block))); // copied, to not to write to orignal callback(history); }, get: (key) => { return history[getBlockIndexByKey(key)]; }, add: (block) => { const key = insertBlock(block); callback(history); return key; }, update: (key, block) => { const index = getBlockIndexByKey(key); history[index] = block; callback(history); }, remove: (key) => { const index = getBlockIndexByKey(key); history.splice(index, 1); callback(history); }, clear: () => { history = []; callback(history); }, }; }