Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface Trigger class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Key trigger.
9 *
10 * @class
11 *
12 * @constructor
13 * @param {jQuery.Event|string} [e] Event or string to create trigger from
14 * @param {boolean} [allowInvalidPrimary] Allow invalid primary keys
15 */
16 ve.ui.Trigger = function VeUiTrigger( e, allowInvalidPrimary ) {
17 const keyAliases = ve.ui.Trigger.static.keyAliases,
18 primaryKeys = ve.ui.Trigger.static.primaryKeys,
19 primaryKeyMap = ve.ui.Trigger.static.primaryKeyMap;
20
21 // Properties
22 this.modifiers = {
23 meta: false,
24 ctrl: false,
25 alt: false,
26 shift: false
27 };
28 this.primary = false;
29
30 // Initialization
31 if ( e instanceof $.Event ) {
32 this.modifiers.meta = e.metaKey || false;
33 this.modifiers.ctrl = e.ctrlKey || false;
34 this.modifiers.alt = e.altKey || false;
35 this.modifiers.shift = e.shiftKey || false;
36 this.primary = primaryKeyMap[ e.which ] || false;
37 } else if ( typeof e === 'string' ) {
38 // Normalization: remove whitespace and force lowercase
39 const parts = e.replace( /\s+/g, '' ).toLowerCase().split( '+' );
40 for ( let i = 0, len = parts.length; i < len; i++ ) {
41 let key = parts[ i ];
42 // Resolve key aliases
43 if ( Object.prototype.hasOwnProperty.call( keyAliases, key ) ) {
44 key = keyAliases[ key ];
45 }
46 // Apply key to trigger
47 if ( Object.prototype.hasOwnProperty.call( this.modifiers, key ) ) {
48 // Modifier key
49 this.modifiers[ key ] = true;
50 } else if ( primaryKeys.indexOf( key ) !== -1 || allowInvalidPrimary ) {
51 // WARNING: Only the last primary key will be used
52 this.primary = key;
53 }
54 }
55 }
56 };
57
58 /* Inheritance */
59
60 OO.initClass( ve.ui.Trigger );
61
62 /* Static Properties */
63
64 /**
65 * @property {string[]} Symbolic modifier key names. The order of this array affects the canonical order of a trigger string.
66 *
67 * @static
68 * @inheritable
69 */
70 ve.ui.Trigger.static.modifierKeys = [ 'meta', 'ctrl', 'alt', 'shift' ];
71
72 /**
73 * @property {string[]} Symbolic primary key names.
74 *
75 * @static
76 * @inheritable
77 */
78 ve.ui.Trigger.static.primaryKeys = [
79 // Special keys
80 'backspace',
81 'tab',
82 'enter',
83 'escape',
84 'space',
85 'page-up',
86 'page-down',
87 'end',
88 'home',
89 'left',
90 'up',
91 'right',
92 'down',
93 'insert',
94 'delete',
95 'clear',
96 // Numbers
97 '0',
98 '1',
99 '2',
100 '3',
101 '4',
102 '5',
103 '6',
104 '7',
105 '8',
106 '9',
107 // Letters
108 'a',
109 'b',
110 'c',
111 'd',
112 'e',
113 'f',
114 'g',
115 'h',
116 'i',
117 'j',
118 'k',
119 'l',
120 'm',
121 'n',
122 'o',
123 'p',
124 'q',
125 'r',
126 's',
127 't',
128 'u',
129 'v',
130 'w',
131 'x',
132 'y',
133 'z',
134 // Numpad special keys
135 'multiply',
136 'add',
137 'subtract',
138 'decimal',
139 'divide',
140 // Function keys
141 'f1',
142 'f2',
143 'f3',
144 'f4',
145 'f5',
146 'f6',
147 'f7',
148 'f8',
149 'f9',
150 'f10',
151 'f11',
152 'f12',
153 // Punctuation
154 ';',
155 '=',
156 ',',
157 '-',
158 '.',
159 '/',
160 '`',
161 '[',
162 '\\',
163 ']',
164 '\''
165 ];
166
167 /**
168 * @property {Object} Mappings to use when rendering string for a specific platform.
169 *
170 * @static
171 * @inheritable
172 */
173 ve.ui.Trigger.static.platformMapping = {
174 mac: {
175 alt: '⌥',
176 backspace: '⌫',
177 ctrl: '^',
178 delete: '⌦',
179 down: '↓',
180 end: '↗',
181 // Technically 'enter' is ⌤, but JS doesn't distinguish between 'enter' and
182 // 'return', and the return-arrow is better known
183 enter: '⏎',
184 escape: '⎋',
185 home: '↖',
186 left: '←',
187 meta: '⌘',
188 'page-down': '⇟',
189 'page-up': '⇞',
190 right: '→',
191 shift: '⇧',
192 space: '␣',
193 tab: '⇥',
194 up: '↑'
195 }
196 };
197
198 /**
199 * @property {Object} Symbol to use when concatenating keys in a sequence.
200 *
201 * @static
202 * @inheritable
203 */
204 ve.ui.Trigger.static.platformStringJoiners = {
205 default: '+',
206 mac: ''
207 };
208
209 /**
210 * @property {string[]} Special keys which have i18n messages
211 *
212 * @static
213 * @inheritable
214 */
215 ve.ui.Trigger.static.translatableKeys = [
216 'alt',
217 'backspace',
218 'ctrl',
219 'delete',
220 'down',
221 'end',
222 'enter',
223 'escape',
224 'home',
225 'insert',
226 'left',
227 'meta',
228 'page-down',
229 'page-up',
230 'right',
231 'shift',
232 'space',
233 'tab',
234 'up'
235 ];
236
237 /**
238 * @property {Object} Aliases for modifier or primary key names.
239 *
240 * @static
241 * @inheritable
242 */
243 ve.ui.Trigger.static.keyAliases = {
244 // Platform differences
245 command: 'meta',
246 apple: 'meta',
247 windows: 'meta',
248 option: 'alt',
249 return: 'enter',
250 // Shorthand
251 esc: 'escape',
252 cmd: 'meta',
253 del: 'delete',
254 // Longhand
255 control: 'ctrl',
256 alternate: 'alt',
257 // Symbols
258 '⌘': 'meta',
259 '⎇': 'alt',
260 '⇧': 'shift',
261 '⏎': 'enter',
262 '⌫': 'backspace',
263 '⎋': 'escape'
264 };
265
266 /**
267 * @property {Object} Mapping of key codes and symbolic key names.
268 *
269 * @static
270 * @inheritable
271 */
272 ve.ui.Trigger.static.primaryKeyMap = {
273 // Special keys
274 8: 'backspace',
275 9: 'tab',
276 12: 'clear',
277 13: 'enter',
278 27: 'escape',
279 32: 'space',
280 33: 'page-up',
281 34: 'page-down',
282 35: 'end',
283 36: 'home',
284 37: 'left',
285 38: 'up',
286 39: 'right',
287 40: 'down',
288 45: 'insert',
289 46: 'delete',
290 // Numbers
291 48: '0',
292 49: '1',
293 50: '2',
294 51: '3',
295 52: '4',
296 53: '5',
297 54: '6',
298 55: '7',
299 56: '8',
300 57: '9',
301 // Punctuation
302 59: ';',
303 61: '=',
304 // Letters
305 65: 'a',
306 66: 'b',
307 67: 'c',
308 68: 'd',
309 69: 'e',
310 70: 'f',
311 71: 'g',
312 72: 'h',
313 73: 'i',
314 74: 'j',
315 75: 'k',
316 76: 'l',
317 77: 'm',
318 78: 'n',
319 79: 'o',
320 80: 'p',
321 81: 'q',
322 82: 'r',
323 83: 's',
324 84: 't',
325 85: 'u',
326 86: 'v',
327 87: 'w',
328 88: 'x',
329 89: 'y',
330 90: 'z',
331 // Numpad numbers
332 96: '0',
333 97: '1',
334 98: '2',
335 99: '3',
336 100: '4',
337 101: '5',
338 102: '6',
339 103: '7',
340 104: '8',
341 105: '9',
342 // Numpad special keys
343 106: 'multiply',
344 107: 'add',
345 109: 'subtract',
346 110: 'decimal',
347 111: 'divide',
348 // Function keys
349 112: 'f1',
350 113: 'f2',
351 114: 'f3',
352 115: 'f4',
353 116: 'f5',
354 117: 'f6',
355 118: 'f7',
356 119: 'f8',
357 120: 'f9',
358 121: 'f10',
359 122: 'f11',
360 123: 'f12',
361 // Punctuation
362 186: ';',
363 187: '=',
364 188: ',',
365 189: '-',
366 190: '.',
367 191: '/',
368 192: '`',
369 219: '[',
370 220: '\\',
371 221: ']',
372 222: '\''
373 };
374
375 /* Methods */
376
377 /**
378 * Check if trigger is complete.
379 *
380 * For a trigger to be complete, there must be a valid primary key.
381 *
382 * @return {boolean} Trigger is complete
383 */
384 ve.ui.Trigger.prototype.isComplete = function () {
385 return this.primary !== false;
386 };
387
388 /**
389 * Get a trigger string.
390 *
391 * Trigger strings are canonical representations of triggers made up of the symbolic names of all
392 * active modifier keys and the primary key joined together with a '+' sign.
393 *
394 * To normalize a trigger string simply create a new trigger from a string and then run this method.
395 *
396 * An incomplete trigger will return an empty string.
397 *
398 * @return {string} Canonical trigger string
399 */
400 ve.ui.Trigger.prototype.toString = function () {
401 const modifierKeys = ve.ui.Trigger.static.modifierKeys,
402 keys = [];
403 // Add modifier keywords in the correct order
404 for ( let i = 0, len = modifierKeys.length; i < len; i++ ) {
405 if ( this.modifiers[ modifierKeys[ i ] ] ) {
406 keys.push( modifierKeys[ i ] );
407 }
408 }
409 // Check that there were modifiers and the primary key is whitelisted
410 if ( this.primary ) {
411 // Add a symbolic name for the primary key
412 keys.push( this.primary );
413 return keys.join( '+' );
414 }
415 // Alternatively return an empty string
416 return '';
417 };
418
419 /**
420 * Get a trigger message.
421 *
422 * This is similar to #toString but the resulting string will be formatted in a way that makes it
423 * appear more native for the platform, and special keys will be translated.
424 *
425 * @param {boolean} explode Whether to return the message split up into some
426 * reasonable sequence of inputs required
427 * @return {string[]|string} Seprate key messages, or a joined string
428 */
429 ve.ui.Trigger.prototype.getMessage = function ( explode ) {
430 const hasOwn = Object.prototype.hasOwnProperty,
431 translatableKeys = this.constructor.static.translatableKeys,
432 platformMapping = this.constructor.static.platformMapping,
433 platform = ve.getSystemPlatform();
434
435 let keys = this.toString().split( '+' );
436 // Platform mappings
437 if ( hasOwn.call( platformMapping, platform ) ) {
438 keys = keys.map( ( key ) => hasOwn.call( platformMapping[ platform ], key ) ? platformMapping[ platform ][ key ] : key );
439 }
440
441 // The following messages are used here:
442 // * visualeditor-key-alt
443 // * visualeditor-key-backspace
444 // * visualeditor-key-ctrl
445 // * visualeditor-key-delete
446 // * visualeditor-key-down
447 // * visualeditor-key-end
448 // * visualeditor-key-enter
449 // * visualeditor-key-escape
450 // * visualeditor-key-home
451 // * visualeditor-key-insert
452 // * visualeditor-key-left
453 // * visualeditor-key-meta
454 // * visualeditor-key-page-down
455 // * visualeditor-key-page-up
456 // * visualeditor-key-right
457 // * visualeditor-key-shift
458 // * visualeditor-key-space
459 // * visualeditor-key-tab
460 // * visualeditor-key-up
461 keys = keys.map( ( key ) => translatableKeys.indexOf( key ) !== -1 ? ve.msg( 'visualeditor-key-' + key ) : key.toUpperCase() );
462
463 // Concatenation
464 if ( explode ) {
465 return keys;
466 } else {
467 const joiners = this.constructor.static.platformStringJoiners;
468 const joiner = hasOwn.call( joiners, platform ) ? joiners[ platform ] : joiners.default;
469 return keys.join( joiner );
470 }
471 };