Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions packages/block-library/src/block/edit-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Button } from '@wordpress/components';
import { useInstanceId, usePrevious } from '@wordpress/compose';
import { useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { ESCAPE } from '@wordpress/keycodes';

/** @typedef {import('@wordpress/element').WPComponent} WPComponent */

Expand Down Expand Up @@ -44,7 +43,6 @@ export default function ReusableBlockEditPanel( {
isEditDisabled,
isEditing,
isSaving,
onCancel,
onChangeTitle,
onEdit,
onSave,
Expand Down Expand Up @@ -79,13 +77,6 @@ export default function ReusableBlockEditPanel( {
onChangeTitle( event.target.value );
}

function handleTitleKeyDown( event ) {
if ( event.keyCode === ESCAPE ) {
event.stopPropagation();
onCancel();
}
}

return (
<>
{ ! isEditing && ! isSaving && (
Expand Down Expand Up @@ -120,7 +111,6 @@ export default function ReusableBlockEditPanel( {
className="reusable-block-edit-panel__title"
value={ title }
onChange={ handleTitleChange }
onKeyDown={ handleTitleKeyDown }
id={ `reusable-block-edit-panel__title-${ instanceId }` }
/>
<Button
Expand Down
135 changes: 123 additions & 12 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,145 @@
/**
* External dependencies
*/
import { partial } from 'lodash';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { useState } from '@wordpress/element';
import {
Placeholder,
Spinner,
Disabled,
ToolbarButton,
ToolbarGroup,
ToolbarButton,
} from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import {
BlockControls,
BlockEditorProvider,
BlockList,
WritingFlow,
BlockList,
BlockControls,
} from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';
import { parse, serialize } from '@wordpress/blocks';
import { parse } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import ReusableBlockEditPanel from './edit-panel';

export default function ReusableBlockEdit( {
attributes: { ref },
clientId,
isSelected,
} ) {
const recordArgs = [ 'postType', 'wp_block', ref ];

const {
reusableBlock,
hasResolved,
isSaving,
canUserUpdate,
settings,
} = useSelect(
( select ) => ( {
reusableBlock: select( 'core' ).getEditedEntityRecord(
...recordArgs
),
hasResolved: select( 'core' ).hasFinishedResolution(
'getEditedEntityRecord',
recordArgs
),
isSaving: select( 'core' ).isSavingEntityRecord( ...recordArgs ),
canUserUpdate: select( 'core' ).canUser( 'update', 'blocks', ref ),
settings: select( 'core/block-editor' ).getSettings(),
} ),
[ ref ]
);

const { editEntityRecord, saveEditedEntityRecord } = useDispatch( 'core' );

const { replaceBlock } = useDispatch( 'core/block-editor' );

const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
'wp_block',
{ id: ref }
);

const [ isEditing, setIsEditing ] = useState( false ); // TODO: Start in edit mode when newly created

if ( ! hasResolved ) {
return (
<Placeholder>
<Spinner />
</Placeholder>
);
}

if ( ! reusableBlock ) {
return (
<Placeholder>
{ __( 'Block has been deleted or is unavailable.' ) }
</Placeholder>
);
}

let element = (
<BlockEditorProvider
value={ blocks }
onInput={ onInput }
onChange={ onChange }
settings={ settings }
>
<WritingFlow>
<BlockList />
</WritingFlow>
</BlockEditorProvider>
);

if ( ! isEditing ) {
element = <Disabled>{ element }</Disabled>;
}

return (
<>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
onClick={ () =>
replaceBlock(
clientId,
parse( reusableBlock.content )
)
}
>
{ __( 'Convert to regular blocks' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>

<div className="block-library-block__reusable-block-container">
{ ( isSelected || isEditing ) && (
<ReusableBlockEditPanel
isEditing={ isEditing }
title={ reusableBlock.title }
isSaving={ isSaving }
isEditDisabled={ ! canUserUpdate }
onEdit={ () => setIsEditing( true ) }
onChangeTitle={ ( title ) =>
editEntityRecord( ...recordArgs, { title } )
}
onSave={ () => {
saveEditedEntityRecord( ...recordArgs );
setIsEditing( false );
} }
/>
) }
{ element }
</div>
</>
);
}

/*
class ReusableBlockEdit extends Component {
constructor( { reusableBlock } ) {
super( ...arguments );
Expand Down Expand Up @@ -233,3 +343,4 @@ export default compose( [
};
} ),
] )( ReusableBlockEdit );
*/