Skip to content

Commit

Permalink
Only use local state while editing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZebulanStanphill committed Jul 17, 2020
1 parent e258686 commit 1c46b25
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,43 @@ 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 ) {
fetchReusableBlock();
}
}, [] );

// 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 );
}

Expand All @@ -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 = (
<BlockEditorProvider
settings={ settings }
value={ localBlocks }
onChange={ setLocalBlocks }
onInput={ setLocalBlocks }
// If editing, use local state; otherwise, load the blocks from the
// saved reusable block.
value={ isEditing ? localBlocks : blocks }
onChange={ handleModifyBlocks }
onInput={ handleModifyBlocks }
>
<WritingFlow>
<BlockList />
Expand All @@ -144,17 +163,17 @@ export default function ReusableBlockEdit( { attributes, isSelected } ) {
{ ( isSelected || isEditing ) && (
<ReusableBlockEditPanel
isEditing={ isEditing }
title={ localTitle ?? title }
title={ isEditing ? localTitle : title }
isSaving={ isSaving && ! ( isTemporary ?? false ) }
isEditDisabled={ ! canUpdateBlock }
onEdit={ () => {
setIsEditing( true );
} }
onChangeTitle={ setLocalTitle }
onSave={ save }
onCancel={ () => {
setIsEditing( false );
onEdit={ startEditing }
onChangeTitle={ ( updatedTitle ) => {
if ( isEditing ) {
setLocalTitle( updatedTitle );
}
} }
onSave={ saveAndStopEditing }
onCancel={ cancelEditing }
/>
) }
{ content }
Expand Down

0 comments on commit 1c46b25

Please sign in to comment.