Skip to content

Commit

Permalink
Pass intl object to initialValue function in volto-slate
Browse files Browse the repository at this point in the history
Pass intl object to functions that internally call initialValue
  • Loading branch information
wesleybl committed Dec 12, 2024
1 parent 7c0f235 commit 8537683
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/volto-slate/news/6529.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pass `intl` object to `initialValue` function. @wesleybl
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
9 changes: 5 additions & 4 deletions packages/volto-slate/src/blocks/Text/extensions/breakList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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;
Expand All @@ -96,15 +97,15 @@ export const breakList = (editor) => {
if (detached) {
Editor.insertNode(editor, createEmptyParagraph());
} else {
createAndSelectNewBlockAfter(editor, [createEmptyParagraph()]);
createAndSelectNewBlockAfter(editor, [createEmptyParagraph()], intl);
}
return true;
}

if (!detached) {
const [top, bottom] = splitEditorInTwoFragments(editor, ref.current);
setEditorContent(editor, top);
createAndSelectNewBlockAfter(editor, bottom);
createAndSelectNewBlockAfter(editor, bottom, intl);
}
return true;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
*
Expand All @@ -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 = () => {
Expand All @@ -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);
});
}
Expand Down
10 changes: 5 additions & 5 deletions packages/volto-slate/src/blocks/Text/keyboard/joinBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions packages/volto-slate/src/utils/volto-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, {
Expand All @@ -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',
Expand Down

0 comments on commit 8537683

Please sign in to comment.