Skip to content

Commit

Permalink
Add guard in case of malformed blocks are present (at least id and ti… (
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh authored May 24, 2023
1 parent db63a16 commit 0eec24c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions news/4802.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add guard in case of malformed blocks are present (at least id and title should be present) @sneridagh
8 changes: 4 additions & 4 deletions packages/volto-slate/src/blocks/Text/SlashMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const PersistentSlashMenu = ({ editor }) => {

const [slashMenuSelected, setSlashMenuSelected] = React.useState(0);

const hasAllowedBlocks = !isEmpty(allowedBlocks);
const hasAllowedBlocks = !isEmpty(allowedBlocks);
const slashCommand = data.plaintext
?.toLowerCase()
.trim()
Expand All @@ -119,12 +119,13 @@ const PersistentSlashMenu = ({ editor }) => {
const availableBlocks = React.useMemo(
() =>
filter(blocksConfig, (item) =>
hasAllowedBlocks
hasAllowedBlocks
? allowedBlocks.includes(item.id)
: typeof item.restricted === 'function'
? !item.restricted({ properties, block: item })
: !item.restricted,
)
.filter((block) => Boolean(block.title && block.id))
.filter((block) => {
// typed text is a substring of the title or id
const title = translateBlockTitle(block, intl).toLowerCase();
Expand All @@ -150,8 +151,7 @@ const PersistentSlashMenu = ({ editor }) => {
intl,
properties,
slashCommand,
hasAllowedBlocks
,
hasAllowedBlocks,
],
);

Expand Down
11 changes: 8 additions & 3 deletions src/components/manage/BlockChooser/BlockChooser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ const BlockChooser = ({
properties = {},
}) => {
const intl = useIntl();
const useAllowedBlocks = !isEmpty(allowedBlocks);
const hasAllowedBlocks = !isEmpty(allowedBlocks);

const filteredBlocksConfig = filter(blocksConfig, (item) => {
// Check if the block is well formed (has at least id and title)
const blockIsWellFormed = Boolean(item.title && item.id);
if (!blockIsWellFormed) {
return false;
}
if (showRestricted) {
if (useAllowedBlocks) {
if (hasAllowedBlocks) {
return allowedBlocks.includes(item.id);
} else {
return true;
}
} else {
if (useAllowedBlocks) {
if (hasAllowedBlocks) {
return allowedBlocks.includes(item.id);
} else {
// Overload restricted as a function, so we can decide the availability of a block
Expand Down
5 changes: 5 additions & 0 deletions src/components/manage/BlockChooser/BlockChooser.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ config.blocks.blocksConfig = {
restricted: false,
mostUsed: false,
},
malformedBlock: {
icon: blockSVG,
group: 'common',
restricted: false,
},
};

const mockStore = configureStore();
Expand Down

0 comments on commit 0eec24c

Please sign in to comment.