From 84b7ee825d82019882a9f323b6ab184551a965b6 Mon Sep 17 00:00:00 2001 From: Wesley Barroso Lopes Date: Mon, 26 Aug 2024 13:38:45 -0300 Subject: [PATCH] Fix initialValue block setting (#5978) Co-authored-by: David Glick --- packages/volto/news/5971.bugfix | 1 + .../manage/Blocks/Block/EditBlockWrapper.jsx | 18 +++++++++++++++++- packages/volto/src/helpers/Blocks/Blocks.js | 19 ++++++++++++------- .../volto/src/helpers/Blocks/Blocks.test.js | 15 +++++++++++++++ packages/volto/src/helpers/index.js | 1 + 5 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 packages/volto/news/5971.bugfix diff --git a/packages/volto/news/5971.bugfix b/packages/volto/news/5971.bugfix new file mode 100644 index 0000000000..71d9814b79 --- /dev/null +++ b/packages/volto/news/5971.bugfix @@ -0,0 +1 @@ +Fix `initialValue` block setting. @wesleybl diff --git a/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx b/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx index f09006f33a..31508127bf 100644 --- a/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx +++ b/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx @@ -1,6 +1,8 @@ import React from 'react'; import { Icon } from '@plone/volto/components'; import { + applyBlockInitialValue, + getBlocksFieldname, blockHasValue, buildStyleClassNamesFromData, buildStyleObjectFromData, @@ -111,7 +113,21 @@ const EditBlockWrapper = (props) => { if (blockHasValue(data)) { onSelectBlock(onInsertBlock(id, value)); } else { - onChangeBlock(id, value); + const blocksFieldname = getBlocksFieldname(properties); + const newFormData = applyBlockInitialValue({ + id, + value, + blocksConfig, + formData: { + ...properties, + [blocksFieldname]: { + ...properties[blocksFieldname], + [id]: value || null, + }, + }, + }); + const newValue = newFormData[blocksFieldname][id]; + onChangeBlock(id, newValue); } }} onMutateBlock={onMutateBlock} diff --git a/packages/volto/src/helpers/Blocks/Blocks.js b/packages/volto/src/helpers/Blocks/Blocks.js index 9a2a5b405a..88f1a5ed15 100644 --- a/packages/volto/src/helpers/Blocks/Blocks.js +++ b/packages/volto/src/helpers/Blocks/Blocks.js @@ -161,7 +161,7 @@ export function addBlock(formData, type, index, blocksConfig) { return [ id, - _applyBlockInitialValue({ + applyBlockInitialValue({ id, value, blocksConfig, @@ -197,8 +197,12 @@ export function addBlock(formData, type, index, blocksConfig) { * to call `onChangeBlock` at their creation time, as this is prone to racing * issue on block data storage. */ -const _applyBlockInitialValue = ({ id, value, blocksConfig, formData }) => { - const blocksFieldname = getBlocksFieldname(formData); +export const applyBlockInitialValue = ({ + id, + value, + blocksConfig, + formData, +}) => { const type = value['@type']; blocksConfig = blocksConfig || config.blocks.blocksConfig; @@ -208,6 +212,7 @@ const _applyBlockInitialValue = ({ id, value, blocksConfig, formData }) => { value, formData, }); + const blocksFieldname = getBlocksFieldname(formData); formData[blocksFieldname][id] = value; } @@ -238,7 +243,7 @@ export function mutateBlock(formData, id, value, blocksConfig) { const trailId = formData[blocksLayoutFieldname].items[index]; if (trailId) { const block = formData[blocksFieldname][trailId]; - newFormData = _applyBlockInitialValue({ + newFormData = applyBlockInitialValue({ id, value, blocksConfig, @@ -256,7 +261,7 @@ export function mutateBlock(formData, id, value, blocksConfig) { } const idTrailingBlock = uuid(); - newFormData = _applyBlockInitialValue({ + newFormData = applyBlockInitialValue({ id, value, blocksConfig, @@ -307,8 +312,8 @@ export function insertBlock( }); const newBlockId = uuid(); - const newFormData = _applyBlockInitialValue({ - id, + const newFormData = applyBlockInitialValue({ + id: newBlockId, value, blocksConfig, formData: { diff --git a/packages/volto/src/helpers/Blocks/Blocks.test.js b/packages/volto/src/helpers/Blocks/Blocks.test.js index e97fbf4edf..900171717f 100644 --- a/packages/volto/src/helpers/Blocks/Blocks.test.js +++ b/packages/volto/src/helpers/Blocks/Blocks.test.js @@ -511,6 +511,21 @@ describe('Blocks', () => { }); }); + it('initializes data for new block with initialValue in insertBlock', () => { + const [newId, form] = insertBlock( + { + blocks: { a: { value: 1 }, b: { value: 2 } }, + blocks_layout: { items: ['a', 'b'] }, + }, + 'b', + { '@type': 'dummyText' }, + ); + expect(form.blocks[newId]).toStrictEqual({ + '@type': 'dummyText', + marker: true, + }); + }); + it('initializes data for new block based on schema defaults', () => { const [newId, form] = addBlock( { diff --git a/packages/volto/src/helpers/index.js b/packages/volto/src/helpers/index.js index a7f2625f09..6ec85dcec8 100644 --- a/packages/volto/src/helpers/index.js +++ b/packages/volto/src/helpers/index.js @@ -40,6 +40,7 @@ export { getLanguageIndependentFields, } from '@plone/volto/helpers/Content/Content'; export { + applyBlockInitialValue, addBlock, insertBlock, blockHasValue,