From 493eb0a58daf8de62c317d4f1c60ba31ad4389bd Mon Sep 17 00:00:00 2001 From: Simone Date: Tue, 3 Oct 2023 14:25:33 +0200 Subject: [PATCH] [Blocks editor] Manage mixed blocks selection (#18181) * first draft multiblocks selection fix * refactor multi blocks selection logic * remove useless console log * refactor getAchorBlockKey based on comments * change test order * refactor the search using matchNode * add some comments to explain the useEffect * fix comments syntax and typos * fix comments syntax --- .../components/BlocksEditor/Toolbar/index.js | 52 ++++++-------- .../BlocksEditor/Toolbar/tests/index.test.js | 68 ++++++++++++++++--- 2 files changed, 79 insertions(+), 41 deletions(-) diff --git a/packages/core/admin/admin/src/content-manager/components/BlocksEditor/Toolbar/index.js b/packages/core/admin/admin/src/content-manager/components/BlocksEditor/Toolbar/index.js index 5cb763a24f..2146a8e24d 100644 --- a/packages/core/admin/admin/src/content-manager/components/BlocksEditor/Toolbar/index.js +++ b/packages/core/admin/admin/src/content-manager/components/BlocksEditor/Toolbar/index.js @@ -131,21 +131,6 @@ ModifierButton.propTypes = { disabled: PropTypes.bool.isRequired, }; -const isBlockActive = (editor, matchNode) => { - const { selection } = editor; - - if (!selection) return false; - - const match = Array.from( - Editor.nodes(editor, { - at: Editor.unhangRange(editor, selection), - match: (n) => !Editor.isEditor(n) && SlateElement.isElement(n) && matchNode(n), - }) - ); - - return match.length > 0; -}; - const toggleBlock = (editor, value) => { const { type, level } = value; @@ -202,7 +187,7 @@ const ImageDialog = ({ handleClose }) => { const handleSelectAssets = (images) => { const formattedImages = images.map((image) => { - // create an object with imageSchema defined and exclude unnecessary props coming from media library config + // Create an object with imageSchema defined and exclude unnecessary props coming from media library config const expectedImage = pick(image, IMAGE_SCHEMA_FIELDS); return { @@ -215,7 +200,7 @@ const ImageDialog = ({ handleClose }) => { insertImages(formattedImages); if (isLastBlockType(editor, 'image')) { - // insert blank line to add new blocks below image block + // Insert blank line to add new blocks below image block insertEmptyBlockAtLast(editor); } @@ -295,7 +280,7 @@ export const BlocksDropdown = ({ disabled }) => { setBlockSelected(optionKey); if (optionKey === 'code' && isLastBlockType(editor, 'code')) { - // insert blank line to add new blocks below code block + // Insert blank line to add new blocks below code block insertEmptyBlockAtLast(editor); } @@ -304,6 +289,23 @@ export const BlocksDropdown = ({ disabled }) => { } }; + // Listen to the selection change and update the selected block in the dropdown + React.useEffect(() => { + if (editor.selection) { + // Get the parent node of the anchor + const [anchorNode] = Editor.parent(editor, editor.selection.anchor); + // Find the block key that matches the anchor node + const anchorBlockKey = Object.keys(blocks).find((blockKey) => + blocks[blockKey].matchNode(anchorNode) + ); + + // Change the value selected in the dropdown if it doesn't match the anchor block key + if (anchorBlockKey && anchorBlockKey !== blockSelected) { + setBlockSelected(anchorBlockKey); + } + } + }, [editor.selection, editor, blocks, blockSelected]); + return ( <>