From 7c0f235de6a03d5d0b39d17d7dead7187aa26e15 Mon Sep 17 00:00:00 2001 From: wesleybl Date: Thu, 12 Dec 2024 11:00:32 -0300 Subject: [PATCH] Pass intl object to initialValue function The function that calls initialValue is applyBlockInitialValue. So all functions that call applyBlockInitialValue must receive intl. --- packages/volto/news/6529.bugfix | 2 + .../manage/Blocks/Block/BlocksForm.jsx | 10 ++-- .../manage/Blocks/Block/EditBlockWrapper.jsx | 1 + packages/volto/src/helpers/Blocks/Blocks.js | 30 ++++++++++-- .../volto/src/helpers/Blocks/Blocks.test.js | 46 +++++++++++++++++++ 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 packages/volto/news/6529.bugfix diff --git a/packages/volto/news/6529.bugfix b/packages/volto/news/6529.bugfix new file mode 100644 index 0000000000..c9e1029a3f --- /dev/null +++ b/packages/volto/news/6529.bugfix @@ -0,0 +1,2 @@ +Pass `intl` object to `initialValue` function. @wesleybl + diff --git a/packages/volto/src/components/manage/Blocks/Block/BlocksForm.jsx b/packages/volto/src/components/manage/Blocks/Block/BlocksForm.jsx index a8fd1e7c05..cba2390706 100644 --- a/packages/volto/src/components/manage/Blocks/Block/BlocksForm.jsx +++ b/packages/volto/src/components/manage/Blocks/Block/BlocksForm.jsx @@ -138,7 +138,7 @@ const BlocksForm = (props) => { }; const onMutateBlock = (id, value) => { - const newFormData = mutateBlock(properties, id, value); + const newFormData = mutateBlock(properties, id, value, {}, intl); onChangeFormData(newFormData); }; @@ -149,6 +149,8 @@ const BlocksForm = (props) => { value, current, config.experimental.addBlockButton.enabled ? 1 : 0, + {}, + intl, ); const blocksFieldname = getBlocksFieldname(newFormData); @@ -166,7 +168,7 @@ const BlocksForm = (props) => { const onAddBlock = (type, index) => { if (editable) { - const [id, newFormData] = addBlock(properties, type, index); + const [id, newFormData] = addBlock(properties, type, index, {}, intl); const blocksFieldname = getBlocksFieldname(newFormData); const blockData = newFormData[blocksFieldname][id]; newFormData[blocksFieldname][id] = applyBlockDefaults({ @@ -188,7 +190,7 @@ const BlocksForm = (props) => { const onDeleteBlock = (id, selectPrev) => { const previous = previousBlockId(properties, id); - const newFormData = deleteBlock(properties, id); + const newFormData = deleteBlock(properties, id, intl); onChangeFormData(newFormData); onSelectBlock(selectPrev ? previous : null); @@ -260,7 +262,7 @@ const BlocksForm = (props) => { for (const [n, v] of blockList) { if (!v) { - const newFormData = deleteBlock(properties, n); + const newFormData = deleteBlock(properties, n, intl); onChangeFormData(newFormData); } } diff --git a/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx b/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx index 3ef354708b..31351f8439 100644 --- a/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx +++ b/packages/volto/src/components/manage/Blocks/Block/EditBlockWrapper.jsx @@ -135,6 +135,7 @@ const EditBlockWrapper = (props) => { [id]: value || null, }, }, + intl, }); const newValue = newFormData[blocksFieldname][id]; onChangeBlock(id, newValue); diff --git a/packages/volto/src/helpers/Blocks/Blocks.js b/packages/volto/src/helpers/Blocks/Blocks.js index d5559828e5..bff936b4ca 100644 --- a/packages/volto/src/helpers/Blocks/Blocks.js +++ b/packages/volto/src/helpers/Blocks/Blocks.js @@ -120,9 +120,10 @@ export function moveBlock(formData, source, destination) { * @function deleteBlock * @param {Object} formData Form data * @param {string} blockId Block uid + * @param {Object} intl intl object. * @return {Object} New form data */ -export function deleteBlock(formData, blockId) { +export function deleteBlock(formData, blockId, intl) { const blocksFieldname = getBlocksFieldname(formData); const blocksLayoutFieldname = getBlocksLayoutFieldname(formData); @@ -135,7 +136,13 @@ export function deleteBlock(formData, blockId) { }; if (newFormData[blocksLayoutFieldname].items.length === 0) { - newFormData = addBlock(newFormData, config.settings.defaultBlockType, 0); + newFormData = addBlock( + newFormData, + config.settings.defaultBlockType, + 0, + {}, + intl, + ); } return newFormData; @@ -147,9 +154,11 @@ export function deleteBlock(formData, blockId) { * @param {Object} formData Form data * @param {string} type Block type * @param {number} index Destination index + * @param {Object} blocksConfig Blocks configuration. + * @param {Object} intl intl object. * @return {Array} New block id, New form data */ -export function addBlock(formData, type, index, blocksConfig) { +export function addBlock(formData, type, index, blocksConfig, intl) { const { settings } = config; const id = uuid(); const idTrailingBlock = uuid(); @@ -192,6 +201,7 @@ export function addBlock(formData, type, index, blocksConfig) { }, selected: id, }, + intl, }), ]; } @@ -208,6 +218,7 @@ export const applyBlockInitialValue = ({ value, blocksConfig, formData, + intl, }) => { const type = value['@type']; blocksConfig = blocksConfig || config.blocks.blocksConfig; @@ -217,6 +228,7 @@ export const applyBlockInitialValue = ({ id, value, formData, + intl, }); const blocksFieldname = getBlocksFieldname(formData); formData[blocksFieldname][id] = value; @@ -231,9 +243,11 @@ export const applyBlockInitialValue = ({ * @param {Object} formData Form data * @param {string} id Block uid to mutate * @param {number} value Block's new value + * @param {Object} blocksConfig Blocks configuration. + * @param {Object} intl intl object. * @return {Object} New form data */ -export function mutateBlock(formData, id, value, blocksConfig) { +export function mutateBlock(formData, id, value, blocksConfig, intl) { const { settings } = config; const blocksFieldname = getBlocksFieldname(formData); const blocksLayoutFieldname = getBlocksLayoutFieldname(formData); @@ -260,6 +274,7 @@ export function mutateBlock(formData, id, value, blocksConfig) { [id]: value || null, }, }, + intl, }); if (!blockHasValue(block)) { return newFormData; @@ -288,6 +303,7 @@ export function mutateBlock(formData, id, value, blocksConfig) { ], }, }, + intl, }); return newFormData; } @@ -298,6 +314,10 @@ export function mutateBlock(formData, id, value, blocksConfig) { * @param {Object} formData Form data * @param {string} id Insert new block before the block with this id * @param {number} value New block's value + * @param {Object} current Current block + * @param {number} offset offset position + * @param {Object} blocksConfig Blocks configuration. + * @param {Object} intl intl object. * @return {Array} New block id, New form data */ export function insertBlock( @@ -307,6 +327,7 @@ export function insertBlock( current = {}, offset = 0, blocksConfig, + intl, ) { const blocksFieldname = getBlocksFieldname(formData); const blocksLayoutFieldname = getBlocksLayoutFieldname(formData); @@ -340,6 +361,7 @@ export function insertBlock( ], }, }, + intl, }); return [newBlockId, newFormData]; diff --git a/packages/volto/src/helpers/Blocks/Blocks.test.js b/packages/volto/src/helpers/Blocks/Blocks.test.js index 659b3c9b50..2887d8d0a3 100644 --- a/packages/volto/src/helpers/Blocks/Blocks.test.js +++ b/packages/volto/src/helpers/Blocks/Blocks.test.js @@ -568,6 +568,52 @@ describe('Blocks', () => { marker: true, }); }); + + it('initialValue with intl', () => { + // Mock intl with formatMessage function + const intl = { + formatMessage: jest.fn(({ id }) => id), + }; + + const messages = { + intl: { + id: 'intl', + defaultMessage: 'intl', + }, + }; + + config.blocks.blocksConfig.text.initialValue = ({ + id, + value, + formData, + intl, + }) => { + return { + ...formData.blocks[id], + intl: intl.formatMessage(messages.intl), + }; + }; + const [newId, form] = addBlock( + { + blocks: { a: { value: 1 }, b: { value: 2 } }, + blocks_layout: { items: ['a', 'b'] }, + }, + 'text', + 1, + config.blocks.blocksConfig, + intl, + ); + + delete config.blocks.blocksConfig.text.initialValue; + + expect(form.blocks[newId]).toStrictEqual({ + '@type': 'text', + booleanField: false, + description: 'Default description', + title: 'Default title', + intl: 'intl', + }); + }); }); describe('moveBlock', () => {