UNPKG

3.32 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var KeyBindingUtil = require("./KeyBindingUtil");
14
15var Keys = require("fbjs/lib/Keys");
16
17var UserAgent = require("fbjs/lib/UserAgent");
18
19var isOSX = UserAgent.isPlatform('Mac OS X'); // Firefox on OSX had a bug resulting in navigation instead of cursor movement.
20// This bug was fixed in Firefox 29. Feature detection is virtually impossible
21// so we just check the version number. See #342765.
22
23var shouldFixFirefoxMovement = isOSX && UserAgent.isBrowser('Firefox < 29');
24var hasCommandModifier = KeyBindingUtil.hasCommandModifier,
25 isCtrlKeyCommand = KeyBindingUtil.isCtrlKeyCommand;
26
27function shouldRemoveWord(e) {
28 return isOSX && e.altKey || isCtrlKeyCommand(e);
29}
30/**
31 * Get the appropriate undo/redo command for a Z key command.
32 */
33
34
35function getZCommand(e) {
36 if (!hasCommandModifier(e)) {
37 return null;
38 }
39
40 return e.shiftKey ? 'redo' : 'undo';
41}
42
43function getDeleteCommand(e) {
44 // Allow default "cut" behavior for PCs on Shift + Delete.
45 if (!isOSX && e.shiftKey) {
46 return null;
47 }
48
49 return shouldRemoveWord(e) ? 'delete-word' : 'delete';
50}
51
52function getBackspaceCommand(e) {
53 if (hasCommandModifier(e) && isOSX) {
54 return 'backspace-to-start-of-line';
55 }
56
57 return shouldRemoveWord(e) ? 'backspace-word' : 'backspace';
58}
59/**
60 * Retrieve a bound key command for the given event.
61 */
62
63
64function getDefaultKeyBinding(e) {
65 switch (e.keyCode) {
66 case 66:
67 // B
68 return hasCommandModifier(e) ? 'bold' : null;
69
70 case 68:
71 // D
72 return isCtrlKeyCommand(e) ? 'delete' : null;
73
74 case 72:
75 // H
76 return isCtrlKeyCommand(e) ? 'backspace' : null;
77
78 case 73:
79 // I
80 return hasCommandModifier(e) ? 'italic' : null;
81
82 case 74:
83 // J
84 return hasCommandModifier(e) ? 'code' : null;
85
86 case 75:
87 // K
88 return isOSX && isCtrlKeyCommand(e) ? 'secondary-cut' : null;
89
90 case 77:
91 // M
92 return isCtrlKeyCommand(e) ? 'split-block' : null;
93
94 case 79:
95 // O
96 return isCtrlKeyCommand(e) ? 'split-block' : null;
97
98 case 84:
99 // T
100 return isOSX && isCtrlKeyCommand(e) ? 'transpose-characters' : null;
101
102 case 85:
103 // U
104 return hasCommandModifier(e) ? 'underline' : null;
105
106 case 87:
107 // W
108 return isOSX && isCtrlKeyCommand(e) ? 'backspace-word' : null;
109
110 case 89:
111 // Y
112 if (isCtrlKeyCommand(e)) {
113 return isOSX ? 'secondary-paste' : 'redo';
114 }
115
116 return null;
117
118 case 90:
119 // Z
120 return getZCommand(e) || null;
121
122 case Keys.RETURN:
123 return 'split-block';
124
125 case Keys.DELETE:
126 return getDeleteCommand(e);
127
128 case Keys.BACKSPACE:
129 return getBackspaceCommand(e);
130 // LEFT/RIGHT handlers serve as a workaround for a Firefox bug.
131
132 case Keys.LEFT:
133 return shouldFixFirefoxMovement && hasCommandModifier(e) ? 'move-selection-to-start-of-block' : null;
134
135 case Keys.RIGHT:
136 return shouldFixFirefoxMovement && hasCommandModifier(e) ? 'move-selection-to-end-of-block' : null;
137
138 default:
139 return null;
140 }
141}
142
143module.exports = getDefaultKeyBinding;
\No newline at end of file