From 1c46b25d1f314c1ca65914ec6cd145d1bc5853be Mon Sep 17 00:00:00 2001 From: Zebulan Stanphill Date: Thu, 14 May 2020 17:23:14 -0500 Subject: [PATCH] Only use local state while editing. --- packages/block-library/src/block/edit.js | 75 +++++++++++++++--------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index 8d65ef2bab45ac..0a8fdc1eb469d0 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -76,9 +76,12 @@ export default function ReusableBlockEdit( { attributes, isSelected } ) { // Start in preview mode when we're working with an existing reusable block. const [ isEditing, setIsEditing ] = useState( isTemporary ?? false ); - // Local state so changes can be made to the reusable block without having to save them. - const [ localTitle, setLocalTitle ] = useState( title ); - const [ localBlocks, setLocalBlocks ] = useState( blocks ?? [] ); + // Local state used while editing so changes can be made to the reusable + // block without having to save them. + const [ localTitle, setLocalTitle ] = useState( isEditing ? title : null ); + const [ localBlocks, setLocalBlocks ] = useState( + isEditing ? blocks : null + ); useEffect( () => { if ( ! reusableBlock ) { @@ -86,24 +89,30 @@ export default function ReusableBlockEdit( { attributes, isSelected } ) { } }, [] ); - // If the reusable block was changed by saving another instance of the same - // reusable block in the editor, and if this instance is not currently being - // edited, update the local state of this instance to match. - useEffect( () => { - if ( ! isEditing && ! isSaving ) { - setLocalTitle( title ); - } - }, [ title, isEditing, isSaving ] ); + function startEditing() { + // Copy saved reusable block data into local state. + setLocalBlocks( blocks ?? [] ); + setLocalTitle( title ); - useEffect( () => { - if ( ! isEditing && ! isSaving ) { - setLocalBlocks( blocks ); - } - }, [ blocks, isEditing, isSaving ] ); + setIsEditing( true ); + } + + function cancelEditing() { + // Clear local state. + setLocalBlocks( null ); + setLocalTitle( null ); + + setIsEditing( false ); + } - function save() { + function saveAndStopEditing() { onChange( { title: localTitle, content: serialize( localBlocks ) } ); onSave(); + + // Clear local state. + setLocalBlocks( null ); + setLocalTitle( null ); + setIsEditing( false ); } @@ -122,12 +131,22 @@ export default function ReusableBlockEdit( { attributes, isSelected } ) { ); } + function handleModifyBlocks( modifedBlocks ) { + // We shouldn't change local state when the blocks are loading + // from the saved reusable block. + if ( isEditing ) { + setLocalBlocks( modifedBlocks ); + } + } + let content = ( @@ -144,17 +163,17 @@ export default function ReusableBlockEdit( { attributes, isSelected } ) { { ( isSelected || isEditing ) && ( { - setIsEditing( true ); - } } - onChangeTitle={ setLocalTitle } - onSave={ save } - onCancel={ () => { - setIsEditing( false ); + onEdit={ startEditing } + onChangeTitle={ ( updatedTitle ) => { + if ( isEditing ) { + setLocalTitle( updatedTitle ); + } } } + onSave={ saveAndStopEditing } + onCancel={ cancelEditing } /> ) } { content }