Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable linear delete key down handler
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /* istanbul ignore next */
8 /**
9 * Delete key down handler for linear selections.
10 *
11 * @class
12 * @extends ve.ce.KeyDownHandler
13 *
14 * @constructor
15 */
16 ve.ce.LinearDeleteKeyDownHandler = function VeCeLinearDeleteKeyDownHandler() {
17 // Parent constructor - never called because class is fully static
18 // ve.ui.LinearDeleteKeyDownHandler.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ce.LinearDeleteKeyDownHandler, ve.ce.KeyDownHandler );
24
25 /* Static properties */
26
27 ve.ce.LinearDeleteKeyDownHandler.static.name = 'linearDelete';
28
29 ve.ce.LinearDeleteKeyDownHandler.static.keys = [ OO.ui.Keys.BACKSPACE, OO.ui.Keys.DELETE ];
30
31 ve.ce.LinearDeleteKeyDownHandler.static.supportedSelections = [ 'linear' ];
32
33 /* Static methods */
34
35 /**
36 * @inheritdoc
37 *
38 * The handler just schedules a poll to observe the native content removal, unless
39 * one of the following is true:
40 * - The ctrlKey is down; or
41 * - The selection is expanded; or
42 * - We are directly adjacent to an element node in the deletion direction.
43 * In these cases, it will perform the content removal itself.
44 */
45 ve.ce.LinearDeleteKeyDownHandler.static.execute = function ( surface, e ) {
46 const direction = e.keyCode === OO.ui.Keys.DELETE ? 1 : -1,
47 unit = ( e.altKey === true || e.ctrlKey === true ) ? 'word' : 'character',
48 documentModel = surface.getModel().getDocument(),
49 focusedNode = surface.getFocusedNode(),
50 uiSurface = surface.getSurface(),
51 data = documentModel.data;
52 let offset = 0,
53 rangeToRemove = surface.getModel().getSelection().getRange();
54
55 if ( surface.isReadOnly() ) {
56 e.preventDefault();
57 return true;
58 }
59
60 if ( direction === 1 && e.shiftKey && ve.getSystemPlatform() !== 'mac' ) {
61 // Shift+Del on non-Mac platforms performs 'cut', so
62 // don't handle it here.
63 return false;
64 }
65
66 if ( focusedNode ) {
67 const command = uiSurface.commandRegistry.getDeleteCommandForNode( focusedNode );
68 if ( command ) {
69 command.execute( uiSurface );
70 e.preventDefault();
71 return true;
72 }
73 }
74
75 // Use native behaviour then poll if collapsed, unless we are adjacent to some hard tag
76 // (or CTRL is down, in which case we can't reliably predict whether the native behaviour
77 // would delete far enough to remove some element)
78 if ( rangeToRemove.isCollapsed() && !e.ctrlKey ) {
79 if ( surface.nativeSelection.focusNode === null ) {
80 // Unexplained failures causing log spam: T262303
81 // How can it be null when this method should only be called for linear selections?
82 e.preventDefault();
83 return true;
84 }
85
86 let position = ve.adjacentDomPosition(
87 {
88 node: surface.nativeSelection.focusNode,
89 offset: surface.nativeSelection.focusOffset
90 },
91 direction,
92 { stop: ve.isHardCursorStep }
93 );
94 const skipNode = position.steps[ position.steps.length - 1 ].node;
95 if ( skipNode.nodeType === Node.TEXT_NODE ) {
96 surface.eventSequencer.afterOne( {
97 keydown: surface.surfaceObserver.pollOnce.bind( surface.surfaceObserver )
98 } );
99 return true;
100 }
101
102 let range;
103 // If the native action would delete an outside nail, move *two* cursor positions
104 // in the deletion direction, to get inside the link just past the inside nail,
105 // then preventDefault
106 if (
107 direction > 0 ?
108 skipNode.classList.contains( 've-ce-nail-pre-open' ) :
109 skipNode.classList.contains( 've-ce-nail-post-close' )
110 ) {
111 position = ve.adjacentDomPosition(
112 position,
113 direction,
114 { stop: ve.isHardCursorStep }
115 );
116 range = document.createRange();
117 range.setStart( position.node, position.offset );
118 surface.nativeSelection.removeAllRanges();
119 surface.nativeSelection.addRange( range );
120 surface.updateActiveAnnotations();
121 e.preventDefault();
122 return true;
123 }
124
125 let pairNode;
126 // If inside an empty link, delete it and preventDefault
127 if (
128 skipNode.classList &&
129 skipNode.classList.contains(
130 direction > 0 ?
131 've-ce-nail-pre-close' :
132 've-ce-nail-post-open'
133 ) &&
134 ( pairNode = (
135 direction > 0 ?
136 skipNode.previousSibling :
137 skipNode.nextSibling
138 ) ) &&
139 pairNode.classList &&
140 pairNode.classList.contains(
141 direction > 0 ?
142 've-ce-nail-post-open' :
143 've-ce-nail-pre-close'
144 )
145 ) {
146 const linkNode = skipNode.parentNode;
147 range = document.createRange();
148 // Set start to link's offset, minus 1 to allow for outer nail deletion
149 // (browsers actually tend to adjust range offsets automatically
150 // for previous sibling deletion, but just in case …)
151 range.setStart( linkNode.parentNode, ve.parentIndex( linkNode ) - 1 );
152 // Remove the outer nails, then the link itself
153 linkNode.parentNode.removeChild( linkNode.previousSibling );
154 linkNode.parentNode.removeChild( linkNode.nextSibling );
155 linkNode.parentNode.removeChild( linkNode );
156
157 surface.nativeSelection.removeAllRanges();
158 surface.nativeSelection.addRange( range );
159 surface.updateActiveAnnotations();
160 e.preventDefault();
161 return true;
162 }
163
164 // If the native action would delete an inside nail, move *two* cursor positions
165 // in the deletion direction, to get outside the link just past the outside nail,
166 // then preventDefault
167 if (
168 direction > 0 ?
169 skipNode.classList.contains( 've-ce-nail-pre-close' ) :
170 skipNode.classList.contains( 've-ce-nail-post-open' )
171 ) {
172 position = ve.adjacentDomPosition(
173 position,
174 direction,
175 { stop: ve.isHardCursorStep }
176 );
177 range = document.createRange();
178 range.setStart( position.node, position.offset );
179 surface.nativeSelection.removeAllRanges();
180 surface.nativeSelection.addRange( range );
181 surface.updateActiveAnnotations();
182 e.preventDefault();
183 return true;
184 }
185
186 offset = rangeToRemove.start;
187 if ( !e.ctrlKey && (
188 ( direction < 0 && !data.isElementData( offset - 1 ) ) ||
189 ( direction > 0 && !data.isElementData( offset ) )
190 ) ) {
191 surface.eventSequencer.afterOne( {
192 keydown: surface.surfaceObserver.pollOnce.bind( surface.surfaceObserver )
193 } );
194 return true;
195 }
196 }
197
198 // Else range is uncollapsed or is adjacent to a non-nail element.
199 if ( rangeToRemove.isCollapsed() ) {
200 const originalRange = new ve.Range( rangeToRemove.from, rangeToRemove.to );
201 // Expand rangeToRemove
202 rangeToRemove = documentModel.getRelativeRange( rangeToRemove, direction, unit, true );
203 if ( surface.getActiveNode() && !surface.getActiveNode().getRange().containsRange( rangeToRemove ) ) {
204 e.preventDefault();
205 return true;
206 }
207
208 const documentModelSelectedNodes = documentModel.selectNodes( rangeToRemove, 'siblings' );
209 for ( let i = 0; i < documentModelSelectedNodes.length; i++ ) {
210 const node = documentModelSelectedNodes[ i ].node;
211 const nodeOuterRange = documentModelSelectedNodes[ i ].nodeOuterRange;
212 let adjacentBlockSelection = null;
213 if ( node instanceof ve.dm.TableNode ) {
214 // Prevent backspacing/deleting over table cells
215 if ( rangeToRemove.containsOffset( nodeOuterRange.start ) ) {
216 adjacentBlockSelection = new ve.dm.TableSelection(
217 nodeOuterRange, 0, 0
218 );
219 } else {
220 const matrix = node.getMatrix();
221 const row = matrix.getRowCount() - 1;
222 const col = matrix.getColCount( row ) - 1;
223 adjacentBlockSelection = new ve.dm.TableSelection(
224 nodeOuterRange, col, row
225 );
226 }
227 } else if ( node.isFocusable() ) {
228 // Prevent backspacing/deleting over focusable nodes
229 adjacentBlockSelection = new ve.dm.LinearSelection( node.getOuterRange() );
230 }
231 if ( adjacentBlockSelection ) {
232 // Create a fragment from the selection as we might delete first
233 const adjacentFragment = surface.getModel().getFragment( adjacentBlockSelection, true );
234 const currentNode = documentModel.getDocumentNode().getNodeFromOffset( originalRange.start );
235 if ( currentNode.canContainContent() && !currentNode.getLength() ) {
236 // If starting in an empty CBN, delete the CBN instead (T338622)
237 surface.getModel().getLinearFragment( currentNode.getOuterRange(), true ).delete( direction );
238 }
239 adjacentFragment.select();
240 e.preventDefault();
241 return true;
242 }
243 }
244
245 if ( rangeToRemove.isCollapsed() ) {
246 // For some reason (most likely: we're at the beginning or end of the document) we can't
247 // expand the range. So, should we delete something or not?
248 // The rules are:
249 // * if we're literally at the start or end, and are in a content node, don't do anything
250 // * if we're in a plain paragraph, don't do anything
251 // * if we're in a list item and it's empty get rid of the item
252 offset = rangeToRemove.start;
253 const docLength = documentModel.getDocumentRange().getLength();
254 if ( offset < docLength - 1 ) {
255 while ( offset < docLength - 1 && data.isCloseElementData( offset ) ) {
256 offset++;
257 }
258 }
259 const startNode = documentModel.getDocumentNode().getNodeFromOffset( offset - 1 );
260 const nodeRange = startNode.getOuterRange();
261 if (
262 // The node is not unwrappable (e.g. table cells, text nodes)
263 !startNode.isUnwrappable() ||
264 // Content item at the start / end?
265 (
266 ( startNode.canContainContent() || surface.attachedRoot === startNode ) &&
267 ( nodeRange.start === 0 || nodeRange.end === docLength )
268 )
269 ) {
270 e.preventDefault();
271 return true;
272 } else {
273 // Expand our removal to reflect what we actually need to remove
274 switch ( startNode.getType() ) {
275 case 'list':
276 case 'listItem':
277 uiSurface.execute( 'indentation', 'decrease' );
278 e.preventDefault();
279 return;
280 default:
281 if ( direction > 0 ) {
282 rangeToRemove = new ve.Range( rangeToRemove.start, nodeRange.end );
283 } else {
284 rangeToRemove = new ve.Range( nodeRange.start, rangeToRemove.start - 1 );
285 }
286 }
287 }
288 }
289 }
290
291 surface.getModel().getLinearFragment( rangeToRemove, true ).delete( direction ).select();
292 // Rerender selection even if it didn't change
293 // TODO: is any of this necessary?
294 surface.focus();
295 surface.surfaceObserver.clear();
296 // Check delete sequences
297 surface.findAndExecuteSequences( false, true );
298 e.preventDefault();
299 return true;
300 };
301
302 /* Registration */
303
304 ve.ce.keyDownHandlerFactory.register( ve.ce.LinearDeleteKeyDownHandler );