Skip to content

Commit

Permalink
Backport plone/volto#6753 to fix empty block rendering issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffersonBledsoe committed Feb 13, 2025
1 parent 6104c3a commit 9b46897
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/customizations/volto/helpers/Blocks/Blocks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Blocks helper.
* @module helpers/Blocks
*
* Backport https://github.com/plone/volto/pull/6753
*/

import { omit, without, endsWith, find, isObject, keys, merge } from 'lodash';
Expand Down Expand Up @@ -115,17 +117,35 @@ export function moveBlock(formData, source, destination) {
export function deleteBlock(formData, blockId) {
const blocksFieldname = getBlocksFieldname(formData);
const blocksLayoutFieldname = getBlocksLayoutFieldname(formData);
const { [formData[blocksFieldname]]: _, ...newBlocks } = formData[
blocksFieldname
];

let newFormData = {
...formData,
[blocksLayoutFieldname]: {
items: without(formData[blocksLayoutFieldname].items, blockId),
},
[blocksFieldname]: omit(formData[blocksFieldname], [blockId]),
[blocksFieldname]: newBlocks ?? {},
};

if (newFormData[blocksLayoutFieldname].items.length === 0) {
newFormData = addBlock(newFormData, config.settings.defaultBlockType, 0);
if (Object.keys(newFormData.blocks).length > 0) {
const existingTitleBlock = Object.entries(formData.blocks).find(
([blockId, blockValue]) => blockValue['@type'] === 'title',
)?.[0];
// Some messy syntax to get every block other than the already found title block
const {
[existingTitleBlock]: _,
...existingOtherBlocks
} = newFormData.blocks;
newFormData[blocksLayoutFieldname].items = [
existingTitleBlock,
...Object.keys(existingOtherBlocks ?? {}),
];
} else {
newFormData = addBlock(newFormData, config.settings.defaultBlockType, 0);
}
}

return newFormData;
Expand Down

0 comments on commit 9b46897

Please sign in to comment.