-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathevent-manager.js
232 lines (190 loc) · 5.69 KB
/
event-manager.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import assert from 'mobiledoc-kit/utils/assert';
import {
parsePostFromPaste,
setClipboardData,
parsePostFromDrop
} from 'mobiledoc-kit/utils/parse-utils';
import Range from 'mobiledoc-kit/utils/cursor/range';
import { filter, forEach, contains } from 'mobiledoc-kit/utils/array-utils';
import Key from 'mobiledoc-kit/utils/key';
import { TAB } from 'mobiledoc-kit/utils/characters';
import TextInputHandler from 'mobiledoc-kit/editor/text-input-handler';
const ELEMENT_EVENT_TYPES = [
'keydown', 'keyup', 'cut', 'copy', 'paste', 'keypress', 'drop'
];
const DOCUMENT_EVENT_TYPES = ['mouseup'];
export default class EventManager {
constructor(editor) {
this.editor = editor;
this.logger = editor.loggerFor('event-manager');
this._textInputHandler = new TextInputHandler(editor);
this._listeners = [];
this.isShift = false;
}
init() {
let { editor: { element } } = this;
assert(`Cannot init EventManager without element`, !!element);
ELEMENT_EVENT_TYPES.forEach(type => {
this._addListener(element, type);
});
DOCUMENT_EVENT_TYPES.forEach(type => {
this._addListener(document, type);
});
}
registerInputHandler(inputHandler) {
this._textInputHandler.register(inputHandler);
}
_addListener(context, type) {
assert(`Missing listener for ${type}`, !!this[type]);
let listener = (event) => this._handleEvent(type, event);
context.addEventListener(type, listener);
this._listeners.push([context, type, listener]);
}
_removeListeners() {
this._listeners.forEach(([context, type, listener]) => {
context.removeEventListener(type, listener);
});
}
// This is primarily useful for programmatically simulating events on the
// editor from the tests.
_trigger(context, type, event) {
forEach(
filter(this._listeners, ([_context, _type]) => {
return _context === context && _type === type;
}),
([context, type, listener]) => {
listener.call(context, event);
}
);
}
destroy() {
this._textInputHandler.destroy();
this._removeListeners();
this._listeners = [];
}
_handleEvent(type, event) {
let { editor } = this;
if (contains(ELEMENT_EVENT_TYPES, type)) {
let {target: element} = event;
if (!editor.cursor.isAddressable(element)) {
// abort handling this event
return true;
}
}
this[type](event);
}
keypress(event) {
let { editor, _textInputHandler } = this;
if (!editor.hasCursor()) { return; }
let key = Key.fromEvent(event);
if (!key.isPrintable()) {
return;
} else {
event.preventDefault();
}
_textInputHandler.handle(key.toString());
}
keydown(event) {
let { editor } = this;
if (!editor.hasCursor()) { return; }
if (!editor.isEditable) { return; }
let key = Key.fromEvent(event);
if (key.isShiftKey()) {
this.isShift = true;
}
if (editor.handleKeyCommand(event)) { return; }
if (editor.post.isBlank) {
editor._insertEmptyMarkupSectionAtCursor();
}
let range = editor.range;
switch(true) {
case key.isHorizontalArrow():
let newRange;
if (key.isShift()) {
newRange = range.extend(key.direction * 1);
} else {
newRange = range.move(key.direction);
}
editor.selectRange(newRange);
event.preventDefault();
break;
case key.isDelete():
editor.handleDeletion(event);
event.preventDefault();
break;
case key.isEnter():
editor.handleNewline(event);
break;
case key.isTab():
event.preventDefault();
editor.insertText(TAB);
break;
}
}
keyup(event) {
let { editor } = this;
if (!editor.hasCursor()) { return; }
let key = Key.fromEvent(event);
if (key.isShiftKey()) {
this.isShift = false;
}
// Only movement-related keys require re-checking the active range
if (key.isMovement()) {
setTimeout(() => this.editor._notifyRangeChange());
}
}
cut(event) {
event.preventDefault();
this.copy(event);
this.editor.handleDeletion();
}
copy(event) {
event.preventDefault();
let { editor, editor: { range, post } } = this;
post = post.trimTo(range);
let data = {
html: editor.serializePost(post, 'html'),
text: editor.serializePost(post, 'text'),
mobiledoc: editor.serializePost(post, 'mobiledoc')
};
setClipboardData(event, data, window);
}
paste(event) {
event.preventDefault();
let { editor } = this;
let range = editor.range;
if (!range.isCollapsed) {
editor.handleDeletion();
}
let position = editor.range.head;
let targetFormat = this.isShift ? 'text' : 'html';
let pastedPost = parsePostFromPaste(event, editor, {targetFormat});
editor.run(postEditor => {
let nextPosition = postEditor.insertPost(position, pastedPost);
postEditor.setRange(new Range(nextPosition));
});
}
mouseup(/* event */) {
// mouseup does not correctly report a selection until the next tick
setTimeout(() => this.editor._notifyRangeChange());
}
drop(event) {
event.preventDefault();
let { clientX: x, clientY: y } = event;
let { editor } = this;
let position = editor.positionAtPoint(x, y);
if (!position) {
this.logger.log('Could not find drop position');
return;
}
let post = parsePostFromDrop(event, editor, {logger: this.logger});
if (!post) {
this.logger.log('Could not determine post from drop event');
return;
}
editor.run(postEditor => {
let nextPosition = postEditor.insertPost(position, post);
postEditor.setRange(new Range(nextPosition));
});
}
}