Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Add helper method volto-slate/utils getAllBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
avoinea committed Jun 12, 2021
1 parent 4c9d3cd commit b996af6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/utils/blocks.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions src/widgets/BlocksBrowser/BlocksBrowserNav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit b996af6

Please sign in to comment.