-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
270 lines (234 loc) · 7.13 KB
/
index.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
260
261
262
263
264
265
266
267
268
269
270
/**
* External dependencies
*/
import {
findLast,
map,
invert,
mapValues,
sortBy,
throttle,
} from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockListBlock from './block';
import BlockListAppender from '../block-list-appender';
class BlockList extends Component {
constructor( props ) {
super( props );
this.onSelectionStart = this.onSelectionStart.bind( this );
this.onSelectionEnd = this.onSelectionEnd.bind( this );
this.onShiftSelection = this.onShiftSelection.bind( this );
this.setBlockRef = this.setBlockRef.bind( this );
this.setLastClientY = this.setLastClientY.bind( this );
this.onPointerMove = throttle( this.onPointerMove.bind( this ), 100 );
// Browser does not fire `*move` event when the pointer position changes
// relative to the document, so fire it with the last known position.
this.onScroll = () => this.onPointerMove( { clientY: this.lastClientY } );
this.lastClientY = 0;
this.nodes = {};
}
componentDidMount() {
window.addEventListener( 'mousemove', this.setLastClientY );
}
componentWillUnmount() {
window.removeEventListener( 'mousemove', this.setLastClientY );
}
setLastClientY( { clientY } ) {
this.lastClientY = clientY;
}
setBlockRef( node, clientId ) {
if ( node === null ) {
delete this.nodes[ clientId ];
} else {
this.nodes = {
...this.nodes,
[ clientId ]: node,
};
}
}
/**
* Handles a pointer move event to update the extent of the current cursor
* multi-selection.
*
* @param {MouseEvent} event A mousemove event object.
*
* @return {void}
*/
onPointerMove( { clientY } ) {
// We don't start multi-selection until the mouse starts moving, so as
// to avoid dispatching multi-selection actions on an in-place click.
if ( ! this.props.isMultiSelecting ) {
this.props.onStartMultiSelect();
}
const boundaries = this.nodes[ this.selectionAtStart ].getBoundingClientRect();
const y = clientY - boundaries.top;
const key = findLast( this.coordMapKeys, ( coordY ) => coordY < y );
this.onSelectionChange( this.coordMap[ key ] );
}
/**
* Binds event handlers to the document for tracking a pending multi-select
* in response to a mousedown event occurring in a rendered block.
*
* @param {string} clientId Client ID of block where mousedown occurred.
*
* @return {void}
*/
onSelectionStart( clientId ) {
if ( ! this.props.isSelectionEnabled ) {
return;
}
const boundaries = this.nodes[ clientId ].getBoundingClientRect();
// Create a clientId to Y coördinate map.
const clientIdToCoordMap = mapValues( this.nodes, ( node ) =>
node.getBoundingClientRect().top - boundaries.top );
// Cache a Y coördinate to clientId map for use in `onPointerMove`.
this.coordMap = invert( clientIdToCoordMap );
// Cache an array of the Y coördinates for use in `onPointerMove`.
// Sort the coördinates, as `this.nodes` will not necessarily reflect
// the current block sequence.
this.coordMapKeys = sortBy( Object.values( clientIdToCoordMap ) );
this.selectionAtStart = clientId;
window.addEventListener( 'mousemove', this.onPointerMove );
// Capture scroll on all elements.
window.addEventListener( 'scroll', this.onScroll, true );
window.addEventListener( 'mouseup', this.onSelectionEnd );
}
/**
* Handles multi-selection changes in response to pointer move.
*
* @param {string} clientId Client ID of block under cursor in multi-select
* drag.
*/
onSelectionChange( clientId ) {
const { onMultiSelect, selectionStart, selectionEnd } = this.props;
const { selectionAtStart } = this;
const isAtStart = selectionAtStart === clientId;
if ( ! selectionAtStart || ! this.props.isSelectionEnabled ) {
return;
}
// If multi-selecting and cursor extent returns to the start of
// selection, cancel multi-select.
if ( isAtStart && selectionStart ) {
onMultiSelect( null, null );
}
// Expand multi-selection to block under cursor.
if ( ! isAtStart && selectionEnd !== clientId ) {
onMultiSelect( selectionAtStart, clientId );
}
}
/**
* Handles a mouseup event to end the current cursor multi-selection.
*
* @return {void}
*/
onSelectionEnd() {
// Cancel throttled calls.
this.onPointerMove.cancel();
delete this.coordMap;
delete this.coordMapKeys;
delete this.selectionAtStart;
window.removeEventListener( 'mousemove', this.onPointerMove );
window.removeEventListener( 'scroll', this.onScroll, true );
window.removeEventListener( 'mouseup', this.onSelectionEnd );
// We may or may not be in a multi-selection when mouseup occurs (e.g.
// an in-place mouse click), so only trigger stop if multi-selecting.
if ( this.props.isMultiSelecting ) {
this.props.onStopMultiSelect();
}
}
onShiftSelection( clientId ) {
if ( ! this.props.isSelectionEnabled ) {
return;
}
const { selectionStartClientId, onMultiSelect, onSelect } = this.props;
if ( selectionStartClientId ) {
onMultiSelect( selectionStartClientId, clientId );
} else {
onSelect( clientId );
}
}
render() {
const {
blockClientIds,
layout,
isGroupedByLayout,
rootClientId,
isDraggable,
} = this.props;
let defaultLayout;
if ( isGroupedByLayout ) {
defaultLayout = layout;
}
const classes = classnames( 'editor-block-list__layout', {
[ `layout-${ layout }` ]: layout,
} );
return (
<div className={ classes }>
{ map( blockClientIds, ( clientId, blockIndex ) => (
<BlockListBlock
key={ 'block-' + clientId }
index={ blockIndex }
clientId={ clientId }
blockRef={ this.setBlockRef }
onSelectionStart={ this.onSelectionStart }
onShiftSelection={ this.onShiftSelection }
rootClientId={ rootClientId }
layout={ defaultLayout }
isFirst={ blockIndex === 0 }
isLast={ blockIndex === blockClientIds.length - 1 }
isDraggable={ isDraggable }
/>
) ) }
<BlockListAppender
rootClientId={ rootClientId }
layout={ layout }
isGroupedByLayout={ isGroupedByLayout }
/>
</div>
);
}
}
export default compose( [
withSelect( ( select, ownProps ) => {
const {
getBlockOrder,
isSelectionEnabled,
isMultiSelecting,
getMultiSelectedBlocksStartClientId,
getMultiSelectedBlocksEndClientId,
getBlockSelectionStart,
} = select( 'core/editor' );
const { rootClientId } = ownProps;
return {
blockClientIds: getBlockOrder( rootClientId ),
selectionStart: getMultiSelectedBlocksStartClientId(),
selectionEnd: getMultiSelectedBlocksEndClientId(),
selectionStartClientId: getBlockSelectionStart(),
isSelectionEnabled: isSelectionEnabled(),
isMultiSelecting: isMultiSelecting(),
};
} ),
withDispatch( ( dispatch ) => {
const {
startMultiSelect,
stopMultiSelect,
multiSelect,
selectBlock,
} = dispatch( 'core/editor' );
return {
onStartMultiSelect: startMultiSelect,
onStopMultiSelect: stopMultiSelect,
onMultiSelect: multiSelect,
onSelect: selectBlock,
};
} ),
] )( BlockList );