/
keyboard.js
200 lines (184 loc) · 6.52 KB
/
keyboard.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import clone from 'clone';
import equal from 'deep-equal';
import Delta from 'rich-text/lib/delta';
import Emitter from '../core/emitter';
import logger from '../core/logger';
import Module from '../core/module';
import { Range } from '../core/selection';
import Block from '../blots/block';
let debug = logger('quill:keyboard');
class Keyboard extends Module {
constructor(quill, options) {
super(quill, options);
this.bindings = {};
this.addBinding({ key: 'B', metaKey: true }, this.onFormat.bind(this, 'bold'));
this.addBinding({ key: 'I', metaKey: true }, this.onFormat.bind(this, 'italic'));
this.addBinding({ key: 'U', metaKey: true }, this.onFormat.bind(this, 'underline'));
this.addBinding({ key: Keyboard.keys.ENTER, shiftKey: null }, this.onEnter.bind(this));
this.addBinding({ key: Keyboard.keys.BACKSPACE }, this.onDelete.bind(this, true));
this.addBinding({ key: Keyboard.keys.DELETE }, this.onDelete.bind(this, false));
// TODO implement
// this.addBinding({ key: Keyboard.keys.BACKSPACE }, this.onDeleteWord.bind(this, true));
// this.addBinding({ key: Keyboard.keys.DELETE }, this.onDeleteWord.bind(this, false));
this.addBinding({ key: Keyboard.keys.TAB, shiftKey: null }, this.onTab.bind(this));
this.quill.root.addEventListener('keydown', (evt) => {
let which = evt.which || evt.keyCode;
let handlers = (this.bindings[which] || []).reduce(function(handlers, binding) {
let [key, handler] = binding;
if (match(evt, key)) handlers.push(handler);
return handlers;
}, []);
if (handlers.length > 0) {
let range = this.quill.getSelection();
handlers.forEach((handler) => {
handler(range, evt);
});
evt.preventDefault();
}
});
}
addBinding(binding, handler) {
binding = normalize(binding);
if (binding == null) {
return debug.warn('Attempted to add invalid keyboard binding', binding);
}
this.bindings[binding.key] = this.bindings[binding.key] || [];
this.bindings[binding.key].push([binding, handler]);
}
onDelete(backspace, range) {
if (range.length > 0) {
this.quill.deleteText(range, Emitter.sources.USER);
} else if (!backspace) {
this.quill.deleteText(range.index, 1, Emitter.sources.USER);
} else {
let [line, offset] = this.quill.scroll.descendant(Block, range.index);
let formats = this.quill.getFormat(range);
if (line != null && offset === 0 && (formats['indent'] || formats['list'])) {
if (formats['indent'] != null) {
line.format('indent', parseInt(formats['indent']) - 1, Emitter.sources.USER);
} else {
line.format('list', false);
}
} else {
this.quill.deleteText(range.index - 1, 1, Emitter.sources.USER);
range = new Range(Math.max(0, range.index - 1));
}
}
this.quill.setSelection(range.index, Emitter.sources.SILENT);
}
onEnter(range) {
let formats = this.quill.getFormat(range);
let lineFormats = Object.keys(formats, function(lineFormats, format) {
if (Parchment.query(format, Parchment.Scope.BLOCK)) {
lineFormats[name] = formats[name];
}
return lineFormats;
}, {});
let delta = new Delta()
.retain(range.index)
.insert('\n', lineFormats)
.delete(range.length);
this.quill.updateContents(delta, Emitter.sources.USER);
this.quill.setSelection(range.index + 1, Emitter.sources.SILENT);
Object.keys(formats).forEach((name) => {
if (lineFormats[name] == null) {
this.quill.formatCursor(name, formats[name]);
}
});
}
onFormat(format, range) {
let formats = this.quill.getFormat(range);
this.quill.format(format, formats[format], Emitter.sources.USER);
}
onTab(range, evt) {
if (range.length === 0) {
let delta = new Delta().retain(range.index).insert('\t').delete(range.length);
this.quill.updateContents(delta, Emitter.sources.USER);
this.quill.setSelection(range.index + 1, Emitter.sources.SILENT);
} else {
let modifier = evt.shiftKey ? -1 : 1;
this.quill.scroll.descendants(Block, range.index, range.length).forEach(function(line) {
let format = line.formats();
let indent = parseInt(format['indent'] || 0);
line.format('indent', Math.max(0, indent + modifier));
});
this.quill.update(Emitter.sources.USER);
this.quill.setSelection(range, Emitter.sources.SILENT);
}
}
removeAllBindings(binding, handler = null) {
binding = normalize(binding);
if (binding == null || this.bindings[binding.key] == null) return [];
let removed = [];
this.bindings[binding.key] = this.bindings[binding.key].filter(function(target) {
let [key, callback] = target;
if (equal(key, binding) && (handler == null || callback === handler)) {
removed.push(handler);
return false;
}
return true;
});
return removed;
}
removeBinding(binding, handler) {
this.removeAllBindings(binding, handler);
}
}
Keyboard.keys = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
ESCAPE: 27,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46
}
function bindKeys(target, keys) {
target.addEventListener('keydown', function(evt) {
if (Object.keys(keys).some(function(key) {
let which = evt.which || evt.keyCode;
if (which === key || which === Keyboard.keys[key.toUpperCase()]) {
keys[key]();
return true;
}
return false;
})) {
event.preventDefault();
}
});
}
function match(evt, binding) {
let metaKey = /Mac/i.test(navigator.platform) ? evt.metaKey : evt.metaKey || evt.ctrlKey;
if (!!binding.metaKey !== metaKey && binding.metaKey !== null) return false;
if (!!binding.shiftKey !== evt.shiftKey && binding.shiftKey !== null) return false;
if (!!binding.altKey !== evt.altKey && binding.altKey !== null) return false;
return true;
}
function normalize(binding) {
switch (typeof binding) {
case 'string':
if (Keyboard.keys[binding.toUpperCase()] != null) {
binding = clone(Keyboard.keys[binding.toUpperCase()], false);
} else if (binding.length === 1) {
binding = { key: binding };
} else {
return null;
}
break;
case 'number':
binding = { key: binding };
break;
case 'object':
binding = clone(binding, false);
break;
default:
return null;
}
if (typeof binding.key === 'string') {
binding.key = binding.key.toUpperCase().charCodeAt(0);
}
return binding;
}
export { Keyboard as default, bindKeys };