-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.js
228 lines (196 loc) · 6.16 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
/**
* External dependencies
*/
import { connect } from 'react-redux';
import 'element-closest';
import { find, reverse, get } from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import {
keycodes,
focus,
computeCaretRect,
isHorizontalEdge,
isVerticalEdge,
placeCaretAtHorizontalEdge,
placeCaretAtVerticalEdge,
} from '@wordpress/utils';
/**
* Internal dependencies
*/
import {
getPreviousBlockUid,
getNextBlockUid,
getMultiSelectedBlocksStartUid,
getMultiSelectedBlocks,
getSelectedBlock,
} from '../../store/selectors';
import {
multiSelect,
selectBlock,
} from '../../store/actions';
/**
* Module Constants
*/
const { UP, DOWN, LEFT, RIGHT } = keycodes;
class WritingFlow extends Component {
constructor() {
super( ...arguments );
this.onKeyDown = this.onKeyDown.bind( this );
this.bindContainer = this.bindContainer.bind( this );
this.clearVerticalRect = this.clearVerticalRect.bind( this );
this.verticalRect = null;
}
bindContainer( ref ) {
this.container = ref;
}
clearVerticalRect() {
this.verticalRect = null;
}
getEditables( target ) {
const outer = target.closest( '.editor-block-list__block-edit' );
if ( ! outer || target === outer ) {
return [ target ];
}
const elements = outer.querySelectorAll( '[contenteditable="true"]' );
return [ ...elements ];
}
getVisibleTabbables() {
return focus.tabbable
.find( this.container )
.filter( ( node ) => (
node.nodeName === 'INPUT' ||
node.nodeName === 'TEXTAREA' ||
node.contentEditable === 'true' ||
node.classList.contains( 'editor-block-list__block-edit' )
) );
}
getClosestTabbable( target, isReverse ) {
let focusableNodes = this.getVisibleTabbables();
if ( isReverse ) {
focusableNodes = reverse( focusableNodes );
}
focusableNodes = focusableNodes.slice( focusableNodes.indexOf( target ) );
return find( focusableNodes, ( node, i, array ) => {
if ( node.contains( target ) ) {
return false;
}
const nextNode = array[ i + 1 ];
// Skip node if it contains a focusable node.
if ( nextNode && node.contains( nextNode ) ) {
return false;
}
return true;
} );
}
expandSelection( currentStartUid, isReverse ) {
const { previousBlockUid, nextBlockUid } = this.props;
const expandedBlockUid = isReverse ? previousBlockUid : nextBlockUid;
if ( expandedBlockUid ) {
this.props.onMultiSelect( currentStartUid, expandedBlockUid );
}
}
moveSelection( isReverse ) {
const { previousBlockUid, nextBlockUid } = this.props;
const focusedBlockUid = isReverse ? previousBlockUid : nextBlockUid;
if ( focusedBlockUid ) {
this.props.onSelectBlock( focusedBlockUid );
}
}
isEditableEdge( moveUp, target ) {
const editables = this.getEditables( target );
const index = editables.indexOf( target );
const edgeIndex = moveUp ? 0 : editables.length - 1;
return editables.length > 0 && index === edgeIndex;
}
/**
* Function called to ensure the block parent of the target node is selected.
*
* @param {DOMElement} target
*/
selectParentBlock( target ) {
if ( ! target ) {
return;
}
const parentBlock = target.hasAttribute( 'data-block' ) ? target : target.closest( '[data-block]' );
if (
parentBlock &&
( ! this.props.selectedBlockUID || parentBlock.getAttribute( 'data-block' ) !== this.props.selectedBlockUID )
) {
this.props.onSelectBlock( parentBlock.getAttribute( 'data-block' ) );
}
}
onKeyDown( event ) {
const { selectedBlockUID, selectionStart, hasMultiSelection } = this.props;
const { keyCode, target } = event;
const isUp = keyCode === UP;
const isDown = keyCode === DOWN;
const isLeft = keyCode === LEFT;
const isRight = keyCode === RIGHT;
const isReverse = isUp || isLeft;
const isHorizontal = isLeft || isRight;
const isVertical = isUp || isDown;
const isNav = isHorizontal || isVertical;
const isShift = event.shiftKey;
const isNavEdge = isVertical ? isVerticalEdge : isHorizontalEdge;
if ( ! isVertical ) {
this.verticalRect = null;
} else if ( ! this.verticalRect ) {
this.verticalRect = computeCaretRect( target );
}
if ( isNav && isShift && hasMultiSelection ) {
// Shift key is down and existing block multi-selection
event.preventDefault();
this.expandSelection( selectionStart, isReverse );
} else if ( isNav && isShift && this.isEditableEdge( isReverse, target ) && isNavEdge( target, isReverse, true ) ) {
// Shift key is down, but no existing block multi-selection
event.preventDefault();
this.expandSelection( selectedBlockUID, isReverse );
} else if ( isNav && hasMultiSelection ) {
// Moving from block multi-selection to single block selection
event.preventDefault();
this.moveSelection( isReverse );
} else if ( isVertical && isVerticalEdge( target, isReverse, isShift ) ) {
const closestTabbable = this.getClosestTabbable( target, isReverse );
placeCaretAtVerticalEdge( closestTabbable, isReverse, this.verticalRect );
this.selectParentBlock( closestTabbable );
event.preventDefault();
} else if ( isHorizontal && isHorizontalEdge( target, isReverse, isShift ) ) {
const closestTabbable = this.getClosestTabbable( target, isReverse );
placeCaretAtHorizontalEdge( closestTabbable, isReverse );
this.selectParentBlock( closestTabbable );
event.preventDefault();
}
}
render() {
const { children } = this.props;
// Disable reason: Wrapper itself is non-interactive, but must capture
// bubbling events from children to determine focus transition intents.
/* eslint-disable jsx-a11y/no-static-element-interactions */
return (
<div
ref={ this.bindContainer }
onKeyDown={ this.onKeyDown }
onMouseDown={ this.clearVerticalRect }
>
{ children }
</div>
);
/* eslint-disable jsx-a11y/no-static-element-interactions */
}
}
export default connect(
( state ) => ( {
previousBlockUid: getPreviousBlockUid( state ),
nextBlockUid: getNextBlockUid( state ),
selectionStart: getMultiSelectedBlocksStartUid( state ),
hasMultiSelection: getMultiSelectedBlocks( state ).length > 1,
selectedBlockUID: get( getSelectedBlock( state ), [ 'uid' ] ),
} ),
{
onMultiSelect: multiSelect,
onSelectBlock: selectBlock,
}
)( WritingFlow );