Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable linear enter key down handler
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /* istanbul ignore next */
8 /**
9 * Enter key down handler for linear selections.
10 *
11 * @class
12 * @extends ve.ce.KeyDownHandler
13 *
14 * @constructor
15 */
16 ve.ce.LinearEnterKeyDownHandler = function VeCeLinearEnterKeyDownHandler() {
17 // Parent constructor - never called because class is fully static
18 // ve.ui.LinearEnterKeyDownHandler.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ce.LinearEnterKeyDownHandler, ve.ce.KeyDownHandler );
24
25 /* Static properties */
26
27 ve.ce.LinearEnterKeyDownHandler.static.name = 'linearEnter';
28
29 ve.ce.LinearEnterKeyDownHandler.static.keys = [ OO.ui.Keys.ENTER ];
30
31 ve.ce.LinearEnterKeyDownHandler.static.supportedSelections = [ 'linear' ];
32
33 /* Static methods */
34
35 /**
36 * @inheritdoc
37 */
38 ve.ce.LinearEnterKeyDownHandler.static.execute = function ( surface, e ) {
39 const documentModel = surface.model.getDocument(),
40 emptyParagraph = [ { type: 'paragraph' }, { type: '/paragraph' } ];
41 let range = surface.model.getSelection().getRange(),
42 cursor = range.from,
43 advanceCursor = true,
44 outermostNode = null,
45 nodeModel = null,
46 nodeModelRange = null;
47
48 e.preventDefault();
49
50 if ( e.ctrlKey || e.metaKey ) {
51 // CTRL+Enter triggers the submit command
52 return false;
53 }
54
55 const focusedNode = surface.getFocusedNode();
56 if ( focusedNode ) {
57 if ( focusedNode.getModel().isEditable() ) {
58 focusedNode.executeCommand();
59 }
60 return true;
61 }
62
63 if ( surface.isReadOnly() ) {
64 return true;
65 }
66
67 let node = surface.getDocument().getBranchNodeFromOffset( range.from );
68
69 if ( !node.isMultiline() ) {
70 return true;
71 }
72
73 // Handle removal first
74 if ( !range.isCollapsed() ) {
75 const txRemove = ve.dm.TransactionBuilder.static.newFromRemoval( documentModel, range );
76 range = txRemove.translateRange( range );
77 // We do want this to propagate to the surface
78 surface.model.change( txRemove, new ve.dm.LinearSelection( range ) );
79 // Remove may have changed node at range.from
80 node = surface.getDocument().getBranchNodeFromOffset( range.from );
81 }
82
83 if ( node !== null ) {
84 // Assertion: node is certainly a contentBranchNode
85 nodeModel = node.getModel();
86 nodeModelRange = nodeModel.getRange();
87 }
88
89 let txInsert;
90 // Handle insertion
91 if ( node === null ) {
92 throw new Error( 'node === null' );
93 } else if ( e.shiftKey && nodeModel.hasSignificantWhitespace() ) {
94 // Insert newline
95 txInsert = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, range.from, '\n' );
96 } else if (
97 nodeModel.getType() !== 'paragraph' &&
98 (
99 cursor === nodeModelRange.from ||
100 cursor === nodeModelRange.to
101 )
102 ) {
103 // If we're at the start/end of something that's not a paragraph, insert a paragraph
104 // before/after. Insert after for empty nodes (from === to).
105 if ( cursor === nodeModelRange.to ) {
106 txInsert = ve.dm.TransactionBuilder.static.newFromInsertion(
107 documentModel, nodeModel.getOuterRange().to, emptyParagraph
108 );
109 } else if ( cursor === nodeModelRange.from ) {
110 txInsert = ve.dm.TransactionBuilder.static.newFromInsertion(
111 documentModel, nodeModel.getOuterRange().from, emptyParagraph
112 );
113 advanceCursor = false;
114 }
115 } else if ( !node.splitOnEnter() ) {
116 // Cannot split, so insert some appropriate node
117
118 let insertEmptyParagraph = false;
119 let prevContentOffset;
120 if ( documentModel.hasSlugAtOffset( range.from ) ) {
121 insertEmptyParagraph = true;
122 } else {
123 prevContentOffset = documentModel.data.getNearestContentOffset(
124 cursor,
125 -1
126 );
127 if ( prevContentOffset === -1 ) {
128 insertEmptyParagraph = true;
129 }
130 }
131
132 if ( insertEmptyParagraph ) {
133 txInsert = ve.dm.TransactionBuilder.static.newFromInsertion(
134 documentModel, cursor, emptyParagraph
135 );
136 } else {
137 // Act as if cursor were at previous content offset
138 cursor = prevContentOffset;
139 node = surface.documentView.getBranchNodeFromOffset( cursor );
140 txInsert = undefined;
141 // Continue to traverseUpstream below. That will succeed because all
142 // ContentBranchNodes have splitOnEnter === true.
143 }
144 insertEmptyParagraph = undefined;
145 }
146
147 // Assertion: if txInsert === undefined then node.splitOnEnter() === true
148
149 function getSplitData( n ) {
150 const stack = [];
151 n.traverseUpstream( ( parent ) => {
152 if ( !parent.splitOnEnter() ) {
153 return false;
154 }
155 stack.splice(
156 stack.length / 2,
157 0,
158 { type: '/' + parent.type },
159 parent.getModel().getClonedElement( true, true )
160 );
161 outermostNode = parent;
162 if ( e.shiftKey ) {
163 return false;
164 } else {
165 return true;
166 }
167 } );
168 return stack;
169 }
170
171 if ( txInsert === undefined ) {
172 // This node has splitOnEnter = true. Traverse upstream until the first node
173 // that has splitOnEnter = false, splitting each node as it is reached. Set
174 // outermostNode to the last splittable node.
175 let splitData = getSplitData( node );
176
177 const outerParent = outermostNode.getParent();
178 const outerChildrenCount = outerParent.getChildren().length;
179
180 if (
181 // Parent removes empty last children
182 outerParent.removeEmptyLastChildOnEnter() &&
183 // This is the last child
184 outerParent.getChildren()[ outerChildrenCount - 1 ] === outermostNode && (
185 // Contains one empty ContentBranchNode
186 ( outermostNode.children.length === 1 && node.getModel().length === 0 ) ||
187 // ..or is an empty ContentBranchNode
188 ( outermostNode.canContainContent() && outermostNode.getModel().length === 0 )
189 )
190 ) {
191 // Enter was pressed in an empty last child
192 const container = outerParent.getParent();
193 advanceCursor = false;
194 if ( outerChildrenCount === 1 ) {
195 // The item we're about to remove is the only child
196 // Remove the ouerParent
197 txInsert = ve.dm.TransactionBuilder.static.newFromRemoval(
198 documentModel, outerParent.getOuterRange()
199 );
200 } else {
201 // Remove the item
202 txInsert = ve.dm.TransactionBuilder.static.newFromRemoval(
203 documentModel, outermostNode.getOuterRange()
204 );
205 }
206
207 surface.model.change( txInsert );
208 range = txInsert.translateRange( range );
209
210 // The removed item was in a splitOnEnter node, split it
211 if ( container.splitOnEnter() ) {
212 splitData = getSplitData( container ).concat( emptyParagraph );
213 txInsert = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, container.getOuterRange().to - 1, splitData );
214 } else if ( outerParent.getChildren().length ) {
215 // Otherwise just insert a paragraph
216 txInsert = ve.dm.TransactionBuilder.static.newFromInsertion(
217 documentModel, outerParent.getOuterRange().to, emptyParagraph
218 );
219 } else {
220 // Parent was emptied, nothing more to do
221 txInsert = null;
222 }
223 // Advance the cursor into the new paragraph
224 advanceCursor = true;
225 } else {
226 // We must process the transaction first because getRelativeContentOffset can't help us yet
227 txInsert = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, range.from, splitData );
228 }
229 }
230
231 // Commit the transaction
232 if ( txInsert ) {
233 surface.model.change( txInsert );
234 range = txInsert.translateRange( range );
235 }
236
237 // Now we can move the cursor forward
238 if ( advanceCursor ) {
239 cursor = documentModel.data.getRelativeContentOffset( range.from, 1 );
240 } else {
241 cursor = documentModel.data.getNearestContentOffset( range.from );
242 }
243 if ( cursor === -1 ) {
244 // Cursor couldn't be placed in a nearby content node, so create an empty paragraph
245 surface.model.change(
246 ve.dm.TransactionBuilder.static.newFromInsertion(
247 documentModel, range.from, emptyParagraph
248 )
249 );
250 surface.model.setLinearSelection( new ve.Range( range.from + 1 ) );
251 } else {
252 surface.model.setLinearSelection( new ve.Range( cursor ) );
253 }
254 // Reset and resume polling
255 surface.surfaceObserver.clear();
256 // TODO: This setTimeout appears to be unnecessary (we're not render-locked)
257 setTimeout( () => {
258 surface.findAndExecuteSequences();
259 } );
260
261 return true;
262 };
263
264 /* Registration */
265
266 ve.ce.keyDownHandlerFactory.register( ve.ce.LinearEnterKeyDownHandler );