Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/keydownhandlers/ve.ce.LinearArrowKeyDownHandler.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable linear arrow key down handler
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /* istanbul ignore next */
8 /**
9 * Arrow key down handler for linear selections.
10 *
11 * @class
12 * @extends ve.ce.KeyDownHandler
13 *
14 * @constructor
15 */
16 ve.ce.LinearArrowKeyDownHandler = function VeCeLinearArrowKeyDownHandler() {
17 // Parent constructor - never called because class is fully static
18 // ve.ui.LinearArrowKeyDownHandler.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ce.LinearArrowKeyDownHandler, ve.ce.KeyDownHandler );
24
25 /* Static properties */
26
27 ve.ce.LinearArrowKeyDownHandler.static.name = 'linearArrow';
28
29 ve.ce.LinearArrowKeyDownHandler.static.keys = [
30 OO.ui.Keys.UP, OO.ui.Keys.DOWN, OO.ui.Keys.LEFT, OO.ui.Keys.RIGHT,
31 OO.ui.Keys.HOME, OO.ui.Keys.END, OO.ui.Keys.PAGEUP, OO.ui.Keys.PAGEDOWN
32 ];
33
34 ve.ce.LinearArrowKeyDownHandler.static.supportedSelections = [ 'linear' ];
35
36 /* Static methods */
37
38 /**
39 * @inheritdoc
40 */
41 ve.ce.LinearArrowKeyDownHandler.static.execute = function ( surface, e ) {
42 const isBlockMove = e.keyCode === OO.ui.Keys.UP || e.keyCode === OO.ui.Keys.DOWN ||
43 e.keyCode === OO.ui.Keys.PAGEUP || e.keyCode === OO.ui.Keys.PAGEDOWN ||
44 e.keyCode === OO.ui.Keys.HOME || e.keyCode === OO.ui.Keys.END,
45 keyBlockDirection = e.keyCode === OO.ui.Keys.DOWN || e.keyCode === OO.ui.Keys.PAGEDOWN || e.keyCode === OO.ui.Keys.END ? 1 : -1,
46 activeNode = surface.getActiveNode();
47 let range = surface.model.getSelection().getRange();
48
49 // TODO: onDocumentKeyDown did this already
50 surface.surfaceObserver.stopTimerLoop();
51 // TODO: onDocumentKeyDown did this already
52 surface.surfaceObserver.pollOnce();
53
54 function moveOffFocusableNode( focusableRange, dir ) {
55 return surface.model.getDocument().getRelativeRange(
56 focusableRange,
57 dir,
58 'character',
59 e.shiftKey,
60 activeNode && ( e.shiftKey || activeNode.trapsCursor() ) ? activeNode.getRange() : null
61 );
62 }
63
64 let direction, directionality;
65 if ( surface.focusedBlockSlug ) {
66 // Block level selection, so directionality is just css directionality
67 if ( isBlockMove ) {
68 direction = keyBlockDirection;
69 } else {
70 directionality = $( surface.focusedBlockSlug ).css( 'direction' );
71 // eslint-disable-next-line no-bitwise
72 if ( e.keyCode === OO.ui.Keys.LEFT ^ directionality === 'rtl' ) {
73 // Left arrow in ltr, or right arrow in rtl
74 direction = -1;
75 } else {
76 // Left arrow in rtl, or right arrow in ltr
77 direction = 1;
78 }
79 }
80 range = moveOffFocusableNode( range, direction );
81 surface.model.setLinearSelection( range );
82 e.preventDefault();
83 return true;
84 }
85
86 if ( surface.focusedNode ) {
87 if ( isBlockMove ) {
88 direction = keyBlockDirection;
89 } else {
90 directionality = surface.getFocusedNodeDirectionality();
91 // eslint-disable-next-line no-bitwise
92 if ( e.keyCode === OO.ui.Keys.LEFT ^ directionality === 'rtl' ) {
93 // Left arrow in ltr, or right arrow in rtl
94 direction = -1;
95 } else {
96 // Left arrow in rtl, or right arrow in ltr
97 direction = 1;
98 }
99 }
100
101 if ( e.shiftKey ) {
102 // There is no DOM range to expand (because the selection is faked), so
103 // use "collapse to focus - observe - expand". Define "focus" to be the
104 // edge of the focusedNode in the direction of motion (so the selection
105 // always grows). This means that clicking on the focusableNode then
106 // modifying the selection will always include the node.
107 // eslint-disable-next-line no-bitwise
108 if ( direction === -1 ^ range.isBackwards() ) {
109 range = range.flip();
110 }
111 let observationRange = new ve.Range( range.to );
112
113 if ( !surface.focusedNode.isContent() ) {
114 // Block focusable node: move the observation range to an adjacent content offset
115 observationRange = moveOffFocusableNode( observationRange, direction );
116 observationRange = new ve.Range( observationRange.to );
117 }
118 surface.model.setLinearSelection( observationRange );
119 } else {
120 range = moveOffFocusableNode( range, direction );
121 // Move to start/end of node in the model in DM (and DOM)
122 range = new ve.Range( direction === 1 ? range.end : range.start );
123 surface.model.setLinearSelection( range );
124 if ( !isBlockMove ) {
125 // Un-shifted left/right: we've already moved so preventDefault
126 e.preventDefault();
127 return true;
128 }
129 // Else keep going with the cursor in the new place
130 }
131 }
132 // Else keep DM range and DOM selection as-is
133
134 let collapseNode, collapseOffset;
135 if ( e.shiftKey && !ve.supportsSelectionExtend && range.isBackwards() ) {
136 // If the browser doesn't support backwards selections, but the dm range
137 // is backwards, then use "collapse to anchor - observe - expand".
138 collapseNode = surface.nativeSelection.anchorNode;
139 collapseOffset = surface.nativeSelection.anchorOffset;
140 } else if ( e.shiftKey && !range.isCollapsed() && isBlockMove ) {
141 // If selection is expanded and cursoring is up/down, use
142 // "collapse to focus - observe - expand" to work round quirks.
143 collapseNode = surface.nativeSelection.focusNode;
144 collapseOffset = surface.nativeSelection.focusOffset;
145 }
146 // Else don't collapse the selection
147
148 if ( collapseNode ) {
149 const nativeRange = surface.getElementDocument().createRange();
150 nativeRange.setStart( collapseNode, collapseOffset );
151 nativeRange.setEnd( collapseNode, collapseOffset );
152 surface.nativeSelection.removeAllRanges();
153 surface.nativeSelection.addRange( nativeRange );
154 }
155
156 const startFocusNode = surface.nativeSelection.focusNode;
157 const startFocusOffset = surface.nativeSelection.focusOffset;
158
159 // Re-expand (or fixup) the selection after the native action, if necessary
160 surface.eventSequencer.afterOne( { keydown: function () {
161 // Support: Chrome
162 // Chrome bug lets you cursor into a multi-line contentEditable=false with up/down…
163 const viewNode = $( surface.nativeSelection.focusNode ).closest( '.ve-ce-leafNode,.ve-ce-branchNode' ).data( 'view' );
164 if ( !viewNode ) {
165 // Irrelevant selection (or none)
166 return;
167 }
168
169 let newRange;
170 if ( viewNode.isFocusable() ) {
171 let afterDirection;
172 // We've landed in a focusable node; fixup the range
173 if ( isBlockMove ) {
174 // The intended direction is clear, even if the cursor did not move
175 // or did something completely preposterous
176 afterDirection = keyBlockDirection;
177 } else {
178 // Observe which way the cursor moved
179 afterDirection = ve.compareDocumentOrder(
180 surface.nativeSelection.focusNode,
181 surface.nativeSelection.focusOffset,
182 startFocusNode,
183 startFocusOffset
184 );
185 }
186 newRange = (
187 afterDirection > 0 ?
188 viewNode.getOuterRange() :
189 viewNode.getOuterRange().flip()
190 );
191 } else {
192 // Check where the range has moved to
193 surface.surfaceObserver.pollOnceNoCallback();
194 newRange = new ve.Range( surface.surfaceObserver.getRange().to );
195 }
196
197 // Adjust range to use old anchor, if necessary
198 if ( e.shiftKey ) {
199 newRange = new ve.Range( range.from, newRange.to );
200 surface.getModel().setLinearSelection( newRange );
201 }
202 surface.updateActiveAnnotations();
203 surface.surfaceObserver.pollOnce();
204 } } );
205
206 return true;
207 };
208
209 /* Registration */
210
211 ve.ce.keyDownHandlerFactory.register( ve.ce.LinearArrowKeyDownHandler );