-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
insertion-point.js
185 lines (166 loc) · 4.86 KB
/
insertion-point.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useState, useRef } from '@wordpress/element';
import { Popover } from '@wordpress/components';
import { placeCaretAtVerticalEdge } from '@wordpress/dom';
/**
* Internal dependencies
*/
import Inserter from '../inserter';
import { getClosestTabbable } from '../writing-flow';
import { getBlockDOMNode } from '../../utils/dom';
function Indicator( { clientId } ) {
const showInsertionPoint = useSelect(
( select ) => {
const {
getBlockIndex,
getBlockInsertionPoint,
isBlockInsertionPointVisible,
getBlockRootClientId,
} = select( 'core/block-editor' );
const rootClientId = getBlockRootClientId( clientId );
const blockIndex = getBlockIndex( clientId, rootClientId );
const insertionPoint = getBlockInsertionPoint();
return (
isBlockInsertionPointVisible() &&
insertionPoint.index === blockIndex &&
insertionPoint.rootClientId === rootClientId
);
},
[ clientId ]
);
if ( ! showInsertionPoint ) {
return null;
}
return (
<div className="block-editor-block-list__insertion-point-indicator" />
);
}
export default function InsertionPoint( {
className,
isMultiSelecting,
selectedBlockClientId,
children,
containerRef,
} ) {
const [ isInserterShown, setIsInserterShown ] = useState( false );
const [ isInserterForced, setIsInserterForced ] = useState( false );
const [ inserterElement, setInserterElement ] = useState( null );
const [ inserterClientId, setInserterClientId ] = useState( null );
const ref = useRef();
function onMouseMove( event ) {
if ( event.target.className !== className ) {
if ( isInserterShown ) {
setIsInserterShown( false );
}
return;
}
const rect = event.target.getBoundingClientRect();
const offset = event.clientY - rect.top;
const element = Array.from( event.target.children ).find(
( blockEl ) => {
return blockEl.offsetTop > offset;
}
);
if ( ! element ) {
return;
}
const clientId = element.id.slice( 'block-'.length );
if ( ! clientId ) {
return;
}
const elementRect = element.getBoundingClientRect();
if (
event.clientX > elementRect.right ||
event.clientX < elementRect.left
) {
if ( isInserterShown ) {
setIsInserterShown( false );
}
return;
}
setIsInserterShown( true );
setInserterElement( element );
setInserterClientId( clientId );
}
function focusClosestTabbable( event ) {
const { clientX, clientY, target } = event;
// Only handle click on the wrapper specifically, and not an event
// bubbled from the inserter itself.
if ( target !== ref.current ) {
return;
}
const targetRect = target.getBoundingClientRect();
const isReverse = clientY < targetRect.top + targetRect.height / 2;
const blockNode = getBlockDOMNode( inserterClientId );
const container = isReverse ? containerRef.current : blockNode;
const closest = getClosestTabbable( blockNode, true, container );
const rect = new window.DOMRect( clientX, clientY, 0, 16 );
if ( closest ) {
placeCaretAtVerticalEdge( closest, isReverse, rect, false );
}
}
return (
<>
{ ! isMultiSelecting && ( isInserterShown || isInserterForced ) && (
<Popover
noArrow
animate={ false }
anchorRef={ inserterElement }
position="top right left"
focusOnMount={ false }
className="block-editor-block-list__insertion-point-popover"
__unstableSlotName="block-toolbar"
__unstableFixedPosition={ false }
>
<div
className="block-editor-block-list__insertion-point"
style={ { width: inserterElement.offsetWidth } }
>
<Indicator clientId={ inserterClientId } />
{ /* eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */ }
<div
ref={ ref }
onFocus={ () => setIsInserterForced( true ) }
onBlur={ () => setIsInserterForced( false ) }
onClick={ focusClosestTabbable }
// While ideally it would be enough to capture the
// bubbling focus event from the Inserter, due to the
// characteristics of click focusing of `button`s in
// Firefox and Safari, it is not reliable.
//
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
tabIndex={ -1 }
className={ classnames(
'block-editor-block-list__insertion-point-inserter',
{
// Hide the inserter above the selected block.
'is-inserter-hidden':
inserterClientId ===
selectedBlockClientId,
}
) }
>
<Inserter clientId={ inserterClientId } />
</div>
</div>
</Popover>
) }
<div
onMouseMove={
! isInserterForced && ! isMultiSelecting
? onMouseMove
: undefined
}
>
{ children }
</div>
</>
);
}