From b996af63a03b6daa87f53ff5dacb8212b2e97705 Mon Sep 17 00:00:00 2001 From: Alin Voinea Date: Sat, 12 Jun 2021 11:59:30 +0300 Subject: [PATCH] Add helper method volto-slate/utils getAllBlocks --- src/utils/blocks.js | 30 +++++++++++++++++++ .../BlocksBrowser/BlocksBrowserNav.jsx | 1 + 2 files changed, 31 insertions(+) diff --git a/src/utils/blocks.js b/src/utils/blocks.js index 6aa43560..018ac0a2 100644 --- a/src/utils/blocks.js +++ b/src/utils/blocks.js @@ -1,6 +1,10 @@ /* eslint no-console: ["error", { allow: ["error", "warn"] }] */ import { Editor, Transforms, Text } from 'slate'; // Range, RangeRef import config from '@plone/volto/registry'; +import { + getBlocksFieldname, + getBlocksLayoutFieldname, +} from '@plone/volto/helpers'; import { deconstructToVoltoBlocks } from 'volto-slate/utils'; import _ from 'lodash'; @@ -231,3 +235,29 @@ export const toggleFormat = (editor, format) => { type, }); }; + +/** + * @param {object} properties A prop received by the View component + * which is read by the `getBlocksFieldname` and + * `getBlocksLayoutFieldname` Volto helpers to produce the return value. + * @returns {Array} All the blocks data taken from the Volto form. + */ +export const getAllBlocks = (properties, blocks) => { + const blocksFieldName = getBlocksFieldname(properties); + const blocksLayoutFieldname = getBlocksLayoutFieldname(properties); + + for (const n of properties[blocksLayoutFieldname].items) { + const block = properties[blocksFieldName][n]; + // TODO Make this configurable via block config getBlocks + if ( + block?.data?.[blocksLayoutFieldname] && + block?.data?.[blocksFieldName] + ) { + getAllBlocks(block.data, blocks); + } else if (block?.[blocksLayoutFieldname] && block?.[blocksFieldName]) { + getAllBlocks(block, blocks); + } + blocks.push(block); + } + return blocks; +}; diff --git a/src/widgets/BlocksBrowser/BlocksBrowserNav.jsx b/src/widgets/BlocksBrowser/BlocksBrowserNav.jsx index 77d78166..aae58afe 100644 --- a/src/widgets/BlocksBrowser/BlocksBrowserNav.jsx +++ b/src/widgets/BlocksBrowser/BlocksBrowserNav.jsx @@ -26,6 +26,7 @@ const messages = defineMessages({ }, }); // TODO: getBlocks in Block configuration +// TODO: Merge/Use 'volto-slate/utils' getAllBlocks const getBlocks = (data) => { if (!data?.blocks_layout?.items?.length) return []; return data.blocks_layout.items.map((block, index) => {