From 877520461d9e19e3b47b384fdd0341bda6a694c4 Mon Sep 17 00:00:00 2001 From: Loren Norman Date: Tue, 12 Dec 2023 14:45:24 -0500 Subject: [PATCH] expose blocks toolbox creation --- src/blocks/index.js | 9 +++++---- src/toolbox.js | 11 +++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/blocks/index.js b/src/blocks/index.js index 555bb71..a10e70f 100644 --- a/src/blocks/index.js +++ b/src/blocks/index.js @@ -6,13 +6,14 @@ import generators from './generators.js' export const allBlocksJson = [], - allBlockCategories = {}, + allBlocksByCategory = {}, allBlockLabels = {}, allGenerators = generators Object.keys(ALL_BLOCKS).map(key => { const - { json={}, commonType, toolbox, generators } = ALL_BLOCKS[key], + block = ALL_BLOCKS[key], + { json={}, commonType, toolbox, generators } = block, type = commonType || json.type if(!type) { throw new Error(`No "type" declared for block: ${key}`) } @@ -31,8 +32,8 @@ Object.keys(ALL_BLOCKS).map(key => { // sort blocks into their declared categories const { category, categories=[] } = toolbox categories.concat([category]).forEach(category => { - allBlockCategories[category] = (allBlockCategories[category] || []) - allBlockCategories[category].push(type) + allBlocksByCategory[category] = (allBlocksByCategory[category] || {}) + allBlocksByCategory[category][type] = block }) // toolbox labels diff --git a/src/toolbox.js b/src/toolbox.js index 9afde16..570667a 100644 --- a/src/toolbox.js +++ b/src/toolbox.js @@ -1,4 +1,6 @@ -import { allBlockCategories, allBlockLabels } from './blocks/index.js' +import { reduce } from 'lodash' + +import { allBlocksByCategory, allBlockLabels } from './blocks/index.js' const @@ -30,11 +32,12 @@ export default { name: category.name, colour: category.colour, ...category.extras, - contents: (allBlockCategories[category.name] || []).reduce((acc, type) => { + contents: reduce(allBlocksByCategory[category.name] || {}, (acc, block, type) => { + const { inputs={}, fields={} } = block // add tooltip as a toolbox label, if present allBlockLabels[type] && acc.push({ kind: 'label', text: allBlockLabels[type] }) - // add the block - acc.push({ kind: 'block', type }) + // add the block. inputs and fields provide defaults, shadows, etc + acc.push({ kind: 'block', type, ...{ inputs, fields } }) return acc } , []) })