-
Notifications
You must be signed in to change notification settings - Fork 4k
/
inbetween.js
259 lines (232 loc) · 6.38 KB
/
inbetween.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import {
useMemo,
createContext,
useReducer,
useLayoutEffect,
} from '@wordpress/element';
import { Popover } from '@wordpress/components';
import { isRTL } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs';
import usePopoverScroll from './use-popover-scroll';
export const InsertionPointOpenRef = createContext();
function BlockPopoverInbetween( {
previousClientId,
nextClientId,
children,
__unstablePopoverSlot,
__unstableContentRef,
...props
} ) {
// This is a temporary hack to get the inbetween inserter to recompute properly.
const [ positionRecompute, forceRecompute ] = useReducer(
( s ) => s + 1,
0
);
const { orientation, rootClientId, isVisible } = useSelect(
( select ) => {
const {
getBlockListSettings,
getBlockRootClientId,
isBlockVisible,
} = select( blockEditorStore );
const _rootClientId = getBlockRootClientId(
previousClientId ?? nextClientId
);
return {
orientation:
getBlockListSettings( _rootClientId )?.orientation ||
'vertical',
rootClientId: _rootClientId,
isVisible:
isBlockVisible( previousClientId ) &&
isBlockVisible( nextClientId ),
};
},
[ previousClientId, nextClientId ]
);
const previousElement = useBlockElement( previousClientId );
const nextElement = useBlockElement( nextClientId );
const isVertical = orientation === 'vertical';
const style = useMemo( () => {
if ( ( ! previousElement && ! nextElement ) || ! isVisible ) {
return {};
}
const previousRect = previousElement
? previousElement.getBoundingClientRect()
: null;
const nextRect = nextElement
? nextElement.getBoundingClientRect()
: null;
if ( isVertical ) {
return {
width: previousRect ? previousRect.width : nextRect.width,
height:
nextRect && previousRect
? nextRect.top - previousRect.bottom
: 0,
};
}
let width = 0;
if ( previousRect && nextRect ) {
width = isRTL()
? previousRect.left - nextRect.right
: nextRect.left - previousRect.right;
}
return {
width,
height: previousRect ? previousRect.height : nextRect.height,
};
}, [
previousElement,
nextElement,
isVertical,
positionRecompute,
isVisible,
] );
const popoverAnchor = useMemo( () => {
if ( ( ! previousElement && ! nextElement ) || ! isVisible ) {
return undefined;
}
const { ownerDocument } = previousElement || nextElement;
return {
ownerDocument,
getBoundingClientRect() {
const previousRect = previousElement
? previousElement.getBoundingClientRect()
: null;
const nextRect = nextElement
? nextElement.getBoundingClientRect()
: null;
let left = 0;
let top = 0;
if ( isVertical ) {
// vertical
top = previousRect ? previousRect.bottom : nextRect.top;
if ( isRTL() ) {
// vertical, rtl
left = previousRect
? previousRect.right
: nextRect.right;
} else {
// vertical, ltr
left = previousRect ? previousRect.left : nextRect.left;
}
} else {
top = previousRect ? previousRect.top : nextRect.top;
if ( isRTL() ) {
// non vertical, rtl
left = previousRect
? previousRect.left
: nextRect.right;
} else {
// non vertical, ltr
left = previousRect
? previousRect.right
: nextRect.left;
}
}
return new window.DOMRect( left, top, 0, 0 );
},
};
}, [
previousElement,
nextElement,
positionRecompute,
isVertical,
isVisible,
] );
const popoverScrollRef = usePopoverScroll( __unstableContentRef );
// This is only needed for a smooth transition when moving blocks.
useLayoutEffect( () => {
if ( ! previousElement ) {
return;
}
const observer = new window.MutationObserver( forceRecompute );
observer.observe( previousElement, { attributes: true } );
return () => {
observer.disconnect();
};
}, [ previousElement ] );
useLayoutEffect( () => {
if ( ! nextElement ) {
return;
}
const observer = new window.MutationObserver( forceRecompute );
observer.observe( nextElement, { attributes: true } );
return () => {
observer.disconnect();
};
}, [ nextElement ] );
useLayoutEffect( () => {
if ( ! previousElement ) {
return;
}
previousElement.ownerDocument.defaultView.addEventListener(
'resize',
forceRecompute
);
return () => {
previousElement.ownerDocument.defaultView.removeEventListener(
'resize',
forceRecompute
);
};
}, [ previousElement ] );
// If there's either a previous or a next element, show the inbetween popover.
// Note that drag and drop uses the inbetween popover to show the drop indicator
// before the first block and after the last block.
if ( ( ! previousElement && ! nextElement ) || ! isVisible ) {
return null;
}
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
// 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
return (
<Popover
ref={ popoverScrollRef }
animate={ false }
anchor={ popoverAnchor }
focusOnMount={ false }
// Render in the old slot if needed for backward compatibility,
// otherwise render in place (not in the default popover slot).
__unstableSlotName={ __unstablePopoverSlot || null }
// Forces a remount of the popover when its position changes
// This makes sure the popover doesn't animate from its previous position.
key={ nextClientId + '--' + rootClientId }
{ ...props }
className={ classnames(
'block-editor-block-popover',
'block-editor-block-popover__inbetween',
props.className
) }
resize={ false }
flip={ false }
placement="bottom-start"
>
<div
className="block-editor-block-popover__inbetween-container"
style={ style }
>
{ children }
</div>
</Popover>
);
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
}
export default BlockPopoverInbetween;