Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.CommandRegistry.js

raw symbols SCIP
1 /*!
2 * VisualEditor CommandRegistry class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Command registry.
9 *
10 * @class
11 * @extends OO.Registry
12 * @constructor
13 */
14 ve.ui.CommandRegistry = function VeUiCommandRegistry() {
15 // Parent constructor
16 ve.ui.CommandRegistry.super.apply( this, arguments );
17 };
18
19 /* Inheritance */
20
21 OO.inheritClass( ve.ui.CommandRegistry, OO.Registry );
22
23 /* Methods */
24
25 /**
26 * Register a command with the factory.
27 *
28 * @param {ve.ui.Command} command
29 * @throws {Error} If command is not an instance of ve.ui.Command
30 */
31 ve.ui.CommandRegistry.prototype.register = function ( command ) {
32 // Validate arguments
33 if ( !( command instanceof ve.ui.Command ) ) {
34 throw new Error(
35 'command must be an instance of ve.ui.Command, cannot be a ' + typeof command
36 );
37 }
38
39 // Parent method
40 ve.ui.CommandRegistry.super.prototype.register.call( this, command.getName(), command );
41 };
42
43 /**
44 * Returns the primary command for for node.
45 *
46 * @param {ve.ce.FocusableNode} node Node to get command for
47 * @return {ve.ui.Command}
48 */
49 ve.ui.CommandRegistry.prototype.getCommandForNode = function ( node ) {
50 return this.lookup( node.constructor.static.primaryCommandName );
51 };
52
53 /**
54 * Returns the delete command for for node.
55 *
56 * @param {ve.ce.FocusableNode} node Node to get command for
57 * @return {ve.ui.Command}
58 */
59 ve.ui.CommandRegistry.prototype.getDeleteCommandForNode = function ( node ) {
60 return this.lookup( node.constructor.static.deleteCommandName );
61 };
62
63 /**
64 * Get a list of registered command names.
65 *
66 * @return {string[]}
67 */
68 ve.ui.CommandRegistry.prototype.getNames = function () {
69 return Object.keys( this.registry );
70 };
71
72 /* Initialization */
73
74 ve.ui.commandRegistry = new ve.ui.CommandRegistry();
75
76 /* Registrations */
77
78 ve.ui.commandRegistry.register(
79 new ve.ui.Command(
80 'bold', 'annotation', 'toggle',
81 { args: [ 'textStyle/bold' ], supportedSelections: [ 'linear', 'table' ] }
82 )
83 );
84 ve.ui.commandRegistry.register(
85 new ve.ui.Command(
86 'italic', 'annotation', 'toggle',
87 { args: [ 'textStyle/italic' ], supportedSelections: [ 'linear', 'table' ] }
88 )
89 );
90 ve.ui.commandRegistry.register(
91 new ve.ui.Command(
92 'code', 'annotation', 'toggle',
93 { args: [ 'textStyle/code' ], supportedSelections: [ 'linear', 'table' ] }
94 )
95 );
96 ve.ui.commandRegistry.register(
97 new ve.ui.Command(
98 'strikethrough', 'annotation', 'toggle',
99 { args: [ 'textStyle/strikethrough' ], supportedSelections: [ 'linear', 'table' ] }
100 )
101 );
102 ve.ui.commandRegistry.register(
103 new ve.ui.Command(
104 'underline', 'annotation', 'toggle',
105 { args: [ 'textStyle/underline' ], supportedSelections: [ 'linear', 'table' ] }
106 )
107 );
108 ve.ui.commandRegistry.register(
109 new ve.ui.Command(
110 'subscript', 'annotation', 'toggle',
111 { args: [ 'textStyle/subscript' ], supportedSelections: [ 'linear', 'table' ] }
112 )
113 );
114 ve.ui.commandRegistry.register(
115 new ve.ui.Command(
116 'superscript', 'annotation', 'toggle',
117 { args: [ 'textStyle/superscript' ], supportedSelections: [ 'linear', 'table' ] }
118 )
119 );
120 ve.ui.commandRegistry.register(
121 new ve.ui.Command(
122 'big', 'annotation', 'toggle',
123 { args: [ 'textStyle/big' ], supportedSelections: [ 'linear', 'table' ] }
124 )
125 );
126 ve.ui.commandRegistry.register(
127 new ve.ui.Command(
128 'small', 'annotation', 'toggle',
129 { args: [ 'textStyle/small' ], supportedSelections: [ 'linear', 'table' ] }
130 )
131 );
132 ve.ui.commandRegistry.register(
133 new ve.ui.Command(
134 'link', 'window', 'open',
135 { args: [ 'link' ], supportedSelections: [ 'linear' ] }
136 )
137 );
138 ve.ui.commandRegistry.register(
139 new ve.ui.Command(
140 'linkNoExpand', 'window', 'open',
141 { args: [ 'link', { noExpand: true } ], supportedSelections: [ 'linear' ] }
142 )
143 );
144 ve.ui.commandRegistry.register(
145 new ve.ui.Command(
146 'specialCharacter', 'window', 'toggle',
147 { args: [ 'specialCharacter' ], supportedSelections: [ 'linear' ] }
148 )
149 );
150 ve.ui.commandRegistry.register(
151 new ve.ui.Command(
152 'table', 'window', 'open',
153 { args: [ 'table' ], supportedSelections: [ 'table' ] }
154 )
155 );
156 ve.ui.commandRegistry.register(
157 new ve.ui.Command(
158 'number', 'list', 'toggle',
159 { args: [ 'number' ], supportedSelections: [ 'linear' ] }
160 )
161 );
162 ve.ui.commandRegistry.register(
163 new ve.ui.Command(
164 'bullet', 'list', 'toggle',
165 { args: [ 'bullet' ], supportedSelections: [ 'linear' ] }
166 )
167 );
168 ve.ui.commandRegistry.register(
169 new ve.ui.Command(
170 'numberWrapOnce', 'list', 'wrapOnce',
171 { args: [ 'number', true ], supportedSelections: [ 'linear' ] }
172 )
173 );
174 ve.ui.commandRegistry.register(
175 new ve.ui.Command(
176 'bulletWrapOnce', 'list', 'wrapOnce',
177 { args: [ 'bullet', true ], supportedSelections: [ 'linear' ] }
178 )
179 );
180 ve.ui.commandRegistry.register(
181 new ve.ui.Command(
182 'commandHelp', 'window', 'open', { args: [ 'commandHelp' ] }
183 )
184 );
185 ve.ui.commandRegistry.register(
186 new ve.ui.Command(
187 'findAndReplace', 'window', 'open', { args: [ 'findAndReplace', null, 'findSelected' ] }
188 )
189 );
190 ve.ui.commandRegistry.register(
191 new ve.ui.Command(
192 'findNext', 'window', 'open', { args: [ 'findAndReplace', null, 'findNext' ] }
193 )
194 );
195 ve.ui.commandRegistry.register(
196 new ve.ui.Command(
197 'findPrevious', 'window', 'open', { args: [ 'findAndReplace', null, 'findPrevious' ] }
198 )
199 );
200 ve.ui.commandRegistry.register(
201 new ve.ui.Command(
202 'changeDirectionality', 'content', 'changeDirectionality'
203 )
204 );
205 ve.ui.commandRegistry.register(
206 new ve.ui.Command(
207 'language', 'window', 'open',
208 { args: [ 'language' ], supportedSelections: [ 'linear' ] }
209 )
210 );
211 ve.ui.commandRegistry.register(
212 new ve.ui.Command(
213 'paragraph', 'format', 'convert',
214 { args: [ 'paragraph' ], supportedSelections: [ 'linear' ] }
215 )
216 );
217 ( function () {
218 for ( let level = 1; level <= 6; level++ ) {
219 ve.ui.commandRegistry.register(
220 new ve.ui.Command(
221 'heading' + level, 'format', 'convert',
222 { args: [ 'heading', { level: level } ], supportedSelections: [ 'linear' ] }
223 )
224 );
225 }
226 }() );
227 ve.ui.commandRegistry.register(
228 new ve.ui.Command(
229 'preformatted', 'format', 'convert',
230 { args: [ 'preformatted' ], supportedSelections: [ 'linear' ] }
231 )
232 );
233 ve.ui.commandRegistry.register(
234 new ve.ui.Command(
235 'blockquote', 'blockquote', 'toggle',
236 { supportedSelections: [ 'linear' ] }
237 )
238 );
239 ve.ui.commandRegistry.register(
240 new ve.ui.Command(
241 'blockquoteWrap', 'blockquote', 'wrap',
242 { supportedSelections: [ 'linear' ] }
243 )
244 );
245 ve.ui.commandRegistry.register(
246 new ve.ui.Command(
247 'autolinkUrl', 'link', 'autolinkUrl',
248 { supportedSelections: [ 'linear' ] }
249 )
250 );
251 ve.ui.commandRegistry.register(
252 new ve.ui.Command(
253 'pasteSpecial', 'content', 'pasteSpecial',
254 { supportedSelections: [ 'linear', 'table' ] }
255 )
256 );
257 ve.ui.commandRegistry.register(
258 new ve.ui.Command(
259 'selectAll', 'content', 'selectAll',
260 { supportedSelections: [ 'linear', 'table' ] }
261 )
262 );
263 ve.ui.commandRegistry.register(
264 new ve.ui.Command(
265 'delete', 'content', 'remove',
266 { args: [ 'delete' ], supportedSelections: [ 'linear', 'table' ] }
267 )
268 );
269 ve.ui.commandRegistry.register(
270 new ve.ui.Command(
271 'backspace', 'content', 'remove',
272 { args: [ 'backspace' ], supportedSelections: [ 'linear', 'table' ] }
273 )
274 );
275 ve.ui.commandRegistry.register(
276 new ve.ui.Command(
277 'submit', 'content', 'submit'
278 )
279 );
280 ve.ui.commandRegistry.register(
281 new ve.ui.Command(
282 'cancel', 'content', 'cancel'
283 )
284 );
285 ve.ui.commandRegistry.register(
286 new ve.ui.Command(
287 'focusContext', 'content', 'focusContext',
288 { supportedSelections: [ 'linear', 'table' ] }
289 )
290 );
291 ve.ui.commandRegistry.register(
292 new ve.ui.Command(
293 'comment', 'window', 'open',
294 { args: [ 'comment' ], supportedSelections: [ 'linear' ] }
295 )
296 );
297 ve.ui.commandRegistry.register(
298 new ve.ui.Command(
299 'insertHorizontalRule', 'content', 'insert', {
300 args: [
301 [
302 { type: 'horizontalRule' },
303 { type: '/horizontalRule' },
304 { type: 'paragraph' }
305 // omit /p to leave the cursor in the correct place,
306 // fixupInsertion will balance the insertion
307 ],
308 // annotate
309 false,
310 // collapseToEnd
311 true
312 ],
313 supportedSelections: [ 'linear' ]
314 }
315 )
316 );
317 ve.ui.commandRegistry.register(
318 new ve.ui.Command(
319 'insertTable', 'table', 'create',
320 {
321 args: [ {
322 header: true,
323 rows: 3,
324 cols: 4
325 } ],
326 supportedSelections: [ 'linear' ]
327 }
328 )
329 );
330 ve.ui.commandRegistry.register(
331 new ve.ui.Command(
332 'deleteTable', 'table', 'delete',
333 { args: [ 'table' ], supportedSelections: [ 'linear', 'table' ] }
334 )
335 );
336
337 ( function () {
338 const modes = [ 'row', 'col' ],
339 sides = [ 'before', 'after' ],
340 modeNames = { row: 'Row', col: 'Column' },
341 sideNames = { before: 'Before', after: 'After' };
342
343 modes.forEach( ( mode ) => {
344 const modeName = modeNames[ mode ];
345
346 sides.forEach( ( side ) => {
347 const sideName = sideNames[ side ];
348
349 ve.ui.commandRegistry.register(
350 // Commands registered here:
351 // * insertColumnBefore
352 // * insertColumnAfter
353 // * insertRowBefore
354 // * insertRowAfter
355 new ve.ui.Command(
356 'insert' + modeName + sideName, 'table', 'insert',
357 { args: [ mode, side ], supportedSelections: [ 'table' ] }
358 )
359 );
360
361 ve.ui.commandRegistry.register(
362 // Commands registered here:
363 // * moveColumnBefore
364 // * moveColumnAfter
365 // * moveRowBefore
366 // * moveRowAfter
367 new ve.ui.Command(
368 'move' + modeName + sideName, 'table', 'moveRelative',
369 { args: [ mode, side ], supportedSelections: [ 'table' ] }
370 )
371 );
372 } );
373
374 // Commands registered here:
375 // * deleteRow
376 // * deleteColumn
377 ve.ui.commandRegistry.register(
378 new ve.ui.Command(
379 'delete' + modeName, 'table', 'delete',
380 { args: [ mode ], supportedSelections: [ 'table' ] }
381 )
382 );
383 } );
384 }() );
385
386 ve.ui.commandRegistry.register(
387 new ve.ui.Command(
388 'tableCellHeader', 'table', 'changeCellStyle',
389 { args: [ 'header' ], supportedSelections: [ 'table' ] }
390 )
391 );
392 ve.ui.commandRegistry.register(
393 new ve.ui.Command(
394 'tableCellData', 'table', 'changeCellStyle',
395 { args: [ 'data' ], supportedSelections: [ 'table' ] }
396 )
397 );
398 ve.ui.commandRegistry.register(
399 new ve.ui.Command(
400 'enterTableCell', 'table', 'enterTableCell',
401 { supportedSelections: [ 'table' ] }
402 )
403 );
404 ve.ui.commandRegistry.register(
405 new ve.ui.Command(
406 'exitTableCell', 'table', 'exitTableCell',
407 { supportedSelections: [ 'linear' ] }
408 )
409 );