-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentcommandable.js
130 lines (120 loc) · 2.31 KB
/
contentcommandable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var contentCommandable = (function() {
var contentCommandable = function (el, dispatch) {
el.onkeypress = nope;
el.onkeyup = nope;
el.oncut = function (evt) {
nope(evt);
dispatch(COMMANDS.CUT);
};
el.onpaste = function (evt) {
nope(evt);
dispatch(COMMANDS.PASTE);
};
el.ondrag = nope;
el.ondrop = function (evt) {
nope(evt);
dispatch(COMMANDS.DROP);
};
// TODO implement addressing and selection commands so users can track it
// TODO selectionchange
el.onkeydown = function(evt) {
// Allow non-mutative events to pass through, thanks.
if (NON_MUTATIVE[evt.key]) {
return;
}
// Special case, allow paste event to capture
// TODO Platform dependent, may be mutative on macOS etc
if (evt.ctrlKey) {
return;
}
nope(evt); // Everything that follows is mutative, prevent default behaviour
if (evt.key === 'Enter') {
dispatch(COMMANDS.ENTER);
} else if (evt.key === 'Del') {
dispatch(COMMANDS.DELETE);
} else if (evt.key === 'Backspace') {
dispatch(COMMANDS.BACKSPACE);
} else if (evt.key === 'Tab') {
dispatch(COMMANDS.TAB);
} else if (evt.key === 'Spacebar') {
dispatch(COMMANDS.TEXT, ' ');
} else {
dispatch(COMMANDS.TEXT, evt.key);
}
};
};
var nope = function (el) {
el.preventDefault();
};
var COMMANDS = {
ENTER: 'ENTER',
DELETE: 'DELETE',
BACKSPACE: 'BACKSPACE',
TEXT: 'TEXT',
PASTE: 'PASTE',
CUT: 'CUT',
DROP: 'DROP',
TAB: 'TAB'
};
/**
* Map of non-mutative key events where it's safe to allow native behaviour.
*
* Handy reference: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
*/
var NON_MUTATIVE = {
ArrowLeft: 1,
ArrowRight: 1,
ArrowDown: 1,
ArrowUp: 1,
Up: 1,
Down: 1,
Left: 1,
Right: 1,
Help: 1,
ContextMenu: 1,
Home: 1,
End: 1,
Control: 1,
PageUp: 1,
PageDown: 1,
F1: 1,
F2: 1,
F3: 1,
F4: 1,
F5: 1,
F6: 1,
F7: 1,
F8: 1,
F9: 1,
F10: 1,
F11: 1,
F12: 1,
F13: 1,
F14: 1,
F15: 1,
F16: 1,
F17: 1,
F18: 1,
F19: 1,
F20: 1,
F21: 1,
F22: 1,
F23: 1,
F24: 1,
Escape: 1,
Shift: 1,
Meta: 1,
NumLock: 1,
Insert: 1,
Clear: 1,
Alt: 1,
CapsLock: 1,
Win: 1,
Esc: 1,
ScrollLock: 1,
PrintScreen: 1,
Pause: 1
};
return contentCommandable;
}());
module.exports = contentCommandable;