Skip to content

Commit

Permalink
Block Action: Implement cut functionality in block actions and settin…
Browse files Browse the repository at this point in the history
…gs menu
  • Loading branch information
yogeshbhutkar committed Jan 8, 2025
1 parent ef7afef commit 5f74d54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/block-editor/src/components/block-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ export default function BlockActions( {
}
notifyCopy( 'copy', clientIds );
},
onCut() {
if ( clientIds.length === 1 ) {
flashBlock( clientIds[ 0 ] );
}
notifyCopy( 'cut', clientIds );
removeBlocks( clientIds, updateSelection );
},
async onPasteStyles() {
await pasteStyles( getBlocksByClientId( clientIds ) );
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ function CopyMenuItem( { clientIds, onCopy, label, shortcut } ) {
);
}

function CutMenuItem( { clientIds, onCut, label, shortcut } ) {
const { getBlocksByClientId } = useSelect( blockEditorStore );
const ref = useCopyToClipboard(
() => serialize( getBlocksByClientId( clientIds ) ),
onCut
);
const cutMenuItemLabel = label ? label : __( 'Cut' );
return (
<MenuItem ref={ ref } shortcut={ shortcut }>
{ cutMenuItemLabel }
</MenuItem>
);
}

export function BlockSettingsDropdown( {
block,
clientIds,
Expand Down Expand Up @@ -117,6 +131,7 @@ export function BlockSettingsDropdown( {
duplicate: getShortcutRepresentation(
'core/block-editor/duplicate'
),
cut: getShortcutRepresentation( 'core/block-editor/cut' ),
remove: getShortcutRepresentation( 'core/block-editor/remove' ),
insertAfter: getShortcutRepresentation(
'core/block-editor/insert-after'
Expand Down Expand Up @@ -204,6 +219,7 @@ export function BlockSettingsDropdown( {
onInsertBefore,
onRemove,
onCopy,
onCut,
onPasteStyles,
} ) => {
// It is possible that some plugins register fills for this menu
Expand Down Expand Up @@ -257,6 +273,13 @@ export function BlockSettingsDropdown( {
'c'
) }
/>
<CutMenuItem
clientIds={ clientIds }
onCut={ onCut }
shortcut={ displayShortcut.primary(
'x'
) }
/>
{ canDuplicate && (
<MenuItem
onClick={ pipe(
Expand Down

0 comments on commit 5f74d54

Please sign in to comment.