|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import classnames from 'classnames'; |
| 5 | + |
| 6 | +/** |
| 7 | + * WordPress dependencies |
| 8 | + */ |
| 9 | +import { |
| 10 | + Button, |
| 11 | + __experimentalHStack as HStack, |
| 12 | + __experimentalTruncate as Truncate, |
| 13 | +} from '@wordpress/components'; |
| 14 | +import { forwardRef } from '@wordpress/element'; |
| 15 | +import { Icon, lock } from '@wordpress/icons'; |
| 16 | +import { SPACE, ENTER } from '@wordpress/keycodes'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Internal dependencies |
| 20 | + */ |
| 21 | +import BlockIcon from '../block-icon'; |
| 22 | +import useBlockDisplayInformation from '../use-block-display-information'; |
| 23 | +import useBlockDisplayTitle from '../block-title/use-block-display-title'; |
| 24 | +import ListViewExpander from './expander'; |
| 25 | +import { useBlockLock } from '../block-lock'; |
| 26 | + |
| 27 | +function ListViewBlockSelectButton( |
| 28 | + { |
| 29 | + className, |
| 30 | + block: { clientId }, |
| 31 | + onClick, |
| 32 | + onToggleExpanded, |
| 33 | + tabIndex, |
| 34 | + onFocus, |
| 35 | + onDragStart, |
| 36 | + onDragEnd, |
| 37 | + draggable, |
| 38 | + }, |
| 39 | + ref |
| 40 | +) { |
| 41 | + const blockInformation = useBlockDisplayInformation( clientId ); |
| 42 | + const blockTitle = useBlockDisplayTitle( { |
| 43 | + clientId, |
| 44 | + context: 'list-view', |
| 45 | + } ); |
| 46 | + const { isLocked } = useBlockLock( clientId ); |
| 47 | + |
| 48 | + // The `href` attribute triggers the browser's native HTML drag operations. |
| 49 | + // When the link is dragged, the element's outerHTML is set in DataTransfer object as text/html. |
| 50 | + // We need to clear any HTML drag data to prevent `pasteHandler` from firing |
| 51 | + // inside the `useOnBlockDrop` hook. |
| 52 | + const onDragStartHandler = ( event ) => { |
| 53 | + event.dataTransfer.clearData(); |
| 54 | + onDragStart?.( event ); |
| 55 | + }; |
| 56 | + |
| 57 | + function onKeyDownHandler( event ) { |
| 58 | + if ( event.keyCode === ENTER || event.keyCode === SPACE ) { |
| 59 | + onClick( event ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <> |
| 65 | + <Button |
| 66 | + className={ classnames( |
| 67 | + 'block-editor-list-view-block-select-button', |
| 68 | + className |
| 69 | + ) } |
| 70 | + onClick={ onClick } |
| 71 | + onKeyDown={ onKeyDownHandler } |
| 72 | + ref={ ref } |
| 73 | + tabIndex={ tabIndex } |
| 74 | + onFocus={ onFocus } |
| 75 | + onDragStart={ onDragStartHandler } |
| 76 | + onDragEnd={ onDragEnd } |
| 77 | + draggable={ draggable } |
| 78 | + href={ `#block-${ clientId }` } |
| 79 | + aria-hidden={ true } |
| 80 | + > |
| 81 | + <ListViewExpander onClick={ onToggleExpanded } /> |
| 82 | + <BlockIcon icon={ blockInformation?.icon } showColors /> |
| 83 | + <HStack |
| 84 | + alignment="center" |
| 85 | + className="block-editor-list-view-block-select-button__label-wrapper" |
| 86 | + justify="flex-start" |
| 87 | + spacing={ 1 } |
| 88 | + > |
| 89 | + <span className="block-editor-list-view-block-select-button__title"> |
| 90 | + <Truncate ellipsizeMode="auto">{ blockTitle }</Truncate> |
| 91 | + </span> |
| 92 | + { blockInformation?.anchor && ( |
| 93 | + <span className="block-editor-list-view-block-select-button__anchor-wrapper"> |
| 94 | + <Truncate |
| 95 | + className="block-editor-list-view-block-select-button__anchor" |
| 96 | + ellipsizeMode="auto" |
| 97 | + > |
| 98 | + { blockInformation.anchor } |
| 99 | + </Truncate> |
| 100 | + </span> |
| 101 | + ) } |
| 102 | + { isLocked && ( |
| 103 | + <span className="block-editor-list-view-block-select-button__lock"> |
| 104 | + <Icon icon={ lock } /> |
| 105 | + </span> |
| 106 | + ) } |
| 107 | + </HStack> |
| 108 | + </Button> |
| 109 | + </> |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +export default forwardRef( ListViewBlockSelectButton ); |
0 commit comments