/
block-select-button.js
100 lines (94 loc) · 2.53 KB
/
block-select-button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { VisuallyHidden } from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { forwardRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import BlockIcon from '../block-icon';
import useBlockDisplayInformation from '../use-block-display-information';
import { getBlockPositionDescription } from './utils';
import BlockTitle from '../block-title';
import ListViewExpander from './expander';
function ListViewBlockSelectButton(
{
className,
block: { clientId },
isSelected,
onClick,
onToggleExpanded,
position,
siblingBlockCount,
level,
tabIndex,
onFocus,
onDragStart,
onDragEnd,
draggable,
},
ref
) {
const blockInformation = useBlockDisplayInformation( clientId );
const instanceId = useInstanceId( ListViewBlockSelectButton );
const descriptionId = `list-view-block-select-button__${ instanceId }`;
const blockPositionDescription = getBlockPositionDescription(
position,
siblingBlockCount,
level
);
// The `href` attribute triggers the browser's native HTML drag operations.
// When the link is dragged, the element's outerHTML is set in DataTransfer object as text/html.
// We need to clear any HTML drag data to prevent `pasteHandler` from firing
// inside the `useOnBlockDrop` hook.
const onDragStartHandler = ( event ) => {
event.dataTransfer.clearData();
onDragStart( event );
};
return (
<>
<a
className={ classnames(
'block-editor-list-view-block-select-button',
className
) }
onClick={ onClick }
aria-describedby={ descriptionId }
ref={ ref }
tabIndex={ tabIndex }
onFocus={ onFocus }
onDragStart={ onDragStartHandler }
onDragEnd={ onDragEnd }
draggable={ draggable }
href={ `#block-${ clientId }` }
>
<ListViewExpander onClick={ onToggleExpanded } />
<BlockIcon icon={ blockInformation?.icon } showColors />
<BlockTitle clientId={ clientId } />
{ blockInformation?.anchor && (
<span className="block-editor-list-view-block-select-button__anchor">
{ blockInformation.anchor }
</span>
) }
{ isSelected && (
<VisuallyHidden>
{ __( '(selected block)' ) }
</VisuallyHidden>
) }
</a>
<div
className="block-editor-list-view-block-select-button__description"
id={ descriptionId }
>
{ blockPositionDescription }
</div>
</>
);
}
export default forwardRef( ListViewBlockSelectButton );