From 853768378a8a7b0fdfa7c819a6390efea37e9616 Mon Sep 17 00:00:00 2001 From: wesleybl Date: Thu, 12 Dec 2024 17:18:15 -0300 Subject: [PATCH] Pass intl object to initialValue function in volto-slate Pass intl object to functions that internally call initialValue --- packages/volto-slate/news/6529.bugfix | 1 + .../src/blocks/Text/DefaultTextBlockEditor.jsx | 4 ++-- .../src/blocks/Text/extensions/breakList.js | 9 +++++---- .../src/blocks/Text/extensions/insertBreak.js | 5 +++-- .../src/blocks/Text/keyboard/joinBlocks.js | 10 +++++----- packages/volto-slate/src/utils/volto-blocks.js | 14 ++++++++++---- 6 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 packages/volto-slate/news/6529.bugfix diff --git a/packages/volto-slate/news/6529.bugfix b/packages/volto-slate/news/6529.bugfix new file mode 100644 index 0000000000..75d42324e0 --- /dev/null +++ b/packages/volto-slate/news/6529.bugfix @@ -0,0 +1 @@ +Pass `intl` object to `initialValue` function. @wesleybl diff --git a/packages/volto-slate/src/blocks/Text/DefaultTextBlockEditor.jsx b/packages/volto-slate/src/blocks/Text/DefaultTextBlockEditor.jsx index 385cc84a65..b0c7b8bbe0 100644 --- a/packages/volto-slate/src/blocks/Text/DefaultTextBlockEditor.jsx +++ b/packages/volto-slate/src/blocks/Text/DefaultTextBlockEditor.jsx @@ -142,10 +142,10 @@ export const DefaultTextBlockEditor = (props) => { const url = flattenToAppURL(imageId); setNewImageId(imageId); - createImageBlock(url, index, props); + createImageBlock(url, index, props, intl); } prevReq.current = loaded; - }, [props, loaded, loading, prevLoaded, imageId, newImageId, index]); + }, [props, loaded, loading, prevLoaded, imageId, newImageId, index, intl]); const handleUpdate = React.useCallback( (editor) => { diff --git a/packages/volto-slate/src/blocks/Text/extensions/breakList.js b/packages/volto-slate/src/blocks/Text/extensions/breakList.js index 515b8c4c67..a3249a1117 100644 --- a/packages/volto-slate/src/blocks/Text/extensions/breakList.js +++ b/packages/volto-slate/src/blocks/Text/extensions/breakList.js @@ -11,6 +11,7 @@ import { createEmptyParagraph } from '@plone/volto-slate/utils/blocks'; * Handles `Enter` key on empty and non-empty list items. * * @param {Editor} editor The editor which should be modified by this extension + * @param {Object} intl intl object. * with a new version of the `insertBreak` method of the Slate editor. * * @description If the selection does not exist or is expanded, handle with the @@ -20,7 +21,7 @@ import { createEmptyParagraph } from '@plone/volto-slate/utils/blocks'; * text cursor and then split the editor in two fragments, and convert them to * separate Slate Text blocks, based on the selection. */ -export const breakList = (editor) => { +export const breakList = (editor, intl) => { const { insertBreak } = editor; editor.insertBreak = () => { @@ -84,7 +85,7 @@ export const breakList = (editor) => { }); Transforms.select(editor, Editor.end(editor, [])); } else { - createAndSelectNewBlockAfter(editor, [createEmptyParagraph()]); + createAndSelectNewBlockAfter(editor, [createEmptyParagraph()], intl); Transforms.removeNodes(editor, { at: ref.current }); } return true; @@ -96,7 +97,7 @@ export const breakList = (editor) => { if (detached) { Editor.insertNode(editor, createEmptyParagraph()); } else { - createAndSelectNewBlockAfter(editor, [createEmptyParagraph()]); + createAndSelectNewBlockAfter(editor, [createEmptyParagraph()], intl); } return true; } @@ -104,7 +105,7 @@ export const breakList = (editor) => { if (!detached) { const [top, bottom] = splitEditorInTwoFragments(editor, ref.current); setEditorContent(editor, top); - createAndSelectNewBlockAfter(editor, bottom); + createAndSelectNewBlockAfter(editor, bottom, intl); } return true; }; diff --git a/packages/volto-slate/src/blocks/Text/extensions/insertBreak.js b/packages/volto-slate/src/blocks/Text/extensions/insertBreak.js index 91846daf3e..2836bba490 100644 --- a/packages/volto-slate/src/blocks/Text/extensions/insertBreak.js +++ b/packages/volto-slate/src/blocks/Text/extensions/insertBreak.js @@ -8,6 +8,7 @@ import { rangeIsInSplittableNode } from '@plone/volto-slate/utils/internals'; /** * @param {Editor} editor The Slate editor object to extend. + * @param {Object} intl intl object. * @description If the selection exists and touches with one of its edges a * closest-to-root `Text` node (`Path` with length `2`) * @@ -18,7 +19,7 @@ import { rangeIsInSplittableNode } from '@plone/volto-slate/utils/internals'; * and if the selection does not exist or does not touch with one of its edges a * closest-to-root `Text` node, call the default behavior. */ -export const withSplitBlocksOnBreak = (editor) => { +export const withSplitBlocksOnBreak = (editor, intl) => { const { insertBreak } = editor; editor.insertBreak = () => { @@ -40,7 +41,7 @@ export const withSplitBlocksOnBreak = (editor) => { ReactDOM.unstable_batchedUpdates(() => { const [top, bottom] = splitEditorInTwoFragments(editor); // ReactEditor.blur(editor); - createAndSelectNewBlockAfter(editor, bottom); + createAndSelectNewBlockAfter(editor, bottom, intl); setEditorContent(editor, top); }); } diff --git a/packages/volto-slate/src/blocks/Text/keyboard/joinBlocks.js b/packages/volto-slate/src/blocks/Text/keyboard/joinBlocks.js index 8c166f8c0f..d00097f8e0 100644 --- a/packages/volto-slate/src/blocks/Text/keyboard/joinBlocks.js +++ b/packages/volto-slate/src/blocks/Text/keyboard/joinBlocks.js @@ -26,7 +26,7 @@ import { * @param {Editor} editor * @param {KeyboardEvent} event */ -export function joinWithPreviousBlock({ editor, event }) { +export function joinWithPreviousBlock({ editor, event }, intl) { if (!isCursorAtBlockStart(editor)) return; const blockProps = editor.getBlockProps(); @@ -60,7 +60,7 @@ export function joinWithPreviousBlock({ editor, event }) { const text = Editor.string(editor, []); if (!text) { const cursor = getBlockEndAsRange(otherBlock); - const newFormData = deleteBlock(properties, block); + const newFormData = deleteBlock(properties, block, intl); ReactDOM.unstable_batchedUpdates(() => { saveSlateBlockSelection(otherBlockId, cursor); @@ -89,7 +89,7 @@ export function joinWithPreviousBlock({ editor, event }) { value: combined, plaintext: serializeNodesToText(combined || []), }); - const newFormData = deleteBlock(formData, block); + const newFormData = deleteBlock(formData, block, intl); ReactDOM.unstable_batchedUpdates(() => { saveSlateBlockSelection(otherBlockId, cursor); @@ -107,7 +107,7 @@ export function joinWithPreviousBlock({ editor, event }) { * @param {Editor} editor * @param {KeyboardEvent} event */ -export function joinWithNextBlock({ editor, event }) { +export function joinWithNextBlock({ editor, event }, intl) { if (!isCursorAtBlockEnd(editor)) return; const blockProps = editor.getBlockProps(); @@ -146,7 +146,7 @@ export function joinWithNextBlock({ editor, event }) { value: combined, plaintext: serializeNodesToText(combined || []), }); - const newFormData = deleteBlock(formData, block); + const newFormData = deleteBlock(formData, block, intl); ReactDOM.unstable_batchedUpdates(() => { // saveSlateBlockSelection(otherBlockId, cursor); diff --git a/packages/volto-slate/src/utils/volto-blocks.js b/packages/volto-slate/src/utils/volto-blocks.js index e01a40cc68..f4bba8b8de 100644 --- a/packages/volto-slate/src/utils/volto-blocks.js +++ b/packages/volto-slate/src/utils/volto-blocks.js @@ -118,7 +118,7 @@ export function syncCreateSlateBlock(value) { return [id, block]; } -export function createImageBlock(url, index, props) { +export function createImageBlock(url, index, props, intl) { const { properties, onChangeField, onSelectBlock } = props; const blocksFieldname = getBlocksFieldname(properties); const blocksLayoutFieldname = getBlocksLayoutFieldname(properties); @@ -128,7 +128,7 @@ export function createImageBlock(url, index, props) { let id, newFormData; if (currBlockHasValue) { - [id, newFormData] = addBlock(properties, 'image', index + 1); + [id, newFormData] = addBlock(properties, 'image', index + 1, {}, intl); newFormData = changeBlock(newFormData, id, { '@type': 'image', url }); } else { [id, newFormData] = insertBlock(properties, currBlockId, { @@ -144,12 +144,18 @@ export function createImageBlock(url, index, props) { }); } -export const createAndSelectNewBlockAfter = (editor, blockValue) => { +export const createAndSelectNewBlockAfter = (editor, blockValue, intl) => { const blockProps = editor.getBlockProps(); const { onSelectBlock, properties, index, onChangeField } = blockProps; - const [blockId, formData] = addBlock(properties, 'slate', index + 1); + const [blockId, formData] = addBlock( + properties, + 'slate', + index + 1, + {}, + intl, + ); const options = { '@type': 'slate',