-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathmode.js
327 lines (291 loc) · 9.92 KB
/
mode.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// A mode implements a number of keyboard (and possibly other) event handlers which are pushed onto
// the handler stack when the mode is activated, and popped off when it is deactivated. The Mode
// class constructor takes a single argument "options" which can define (amongst other things):
//
// name:
// A name for this mode.
//
// keydown:
// keypress:
// keyup:
// Key handlers. Optional: provide these as required. The default is to continue bubbling all key
// events.
//
// Further options are described in the constructor, below.
//
// Additional handlers associated with a mode can be added by using the push method. For example, if
// a mode responds to "focus" events, then push an additional handler:
// @push
// "focus": (event) => ....
// Such handlers are removed when the mode is deactivated.
//
// The following events can be handled:
// keydown, keypress, keyup, click, focus and blur
// Debug only.
let count = 0;
class Mode {
// This is a function rather than a constructor, becausae often subclasses need to reference
// `this` when setting up the options argument. `this` can't be referenced in subclasses prior to
// calling their superclass constructor.
init(options) {
// Constants; short, readable names for the return values expected by handlerStack.bubbleEvent,
// used here and by subclasses.
if (options == null) {
options = {};
}
this.options = options;
this.continueBubbling = handlerStack.continueBubbling;
this.suppressEvent = handlerStack.suppressEvent;
this.passEventToPage = handlerStack.passEventToPage;
this.suppressPropagation = handlerStack.suppressPropagation;
this.restartBubbling = handlerStack.restartBubbling;
this.alwaysContinueBubbling = handlerStack.alwaysContinueBubbling;
this.alwaysSuppressPropagation = handlerStack.alwaysSuppressPropagation;
this.handlers = [];
this.exitHandlers = [];
this.modeIsActive = true;
this.modeIsExiting = false;
this.name = this.options.name || "anonymous";
this.count = ++count;
this.id = `${this.name}-${this.count}`;
this.log("activate:", this.id);
// If options.suppressAllKeyboardEvents is truthy, then all keyboard events are suppressed. This
// avoids the need for modes which suppress all keyboard events 1) to provide handlers for all
// of those events, or 2) to worry about event suppression and event-handler return values.
if (this.options.suppressAllKeyboardEvents) {
// TODO(philc): Make a let statement.
const downHanlder = this.options["keydown"];
this.options["keydown"] = (event) =>
this.alwaysSuppressPropagation(() => {
if (downHanlder) {
return downHanlder(event);
}
});
const pressHandler = this.options["keypress"];
this.options["keypress"] = (event) =>
this.alwaysSuppressPropagation(() => {
if (pressHandler) {
return pressHandler(event);
}
});
}
this.push({
keydown: this.options.keydown || null,
keypress: this.options.keypress || null,
keyup: this.options.keyup || null,
indicator: () => {
// Update the mode indicator. Setting @options.indicator to a string shows a mode indicator
// in the HUD. Setting @options.indicator to 'false' forces no mode indicator.
// If @options.indicator is undefined, then the request propagates to the next mode.
// The active indicator can also be changed with @setIndicator().
if (this.options.indicator != null) {
if (this.options.indicator) {
HUD.show(this.options.indicator);
} else {
HUD.hide(true, false);
}
return this.passEventToPage;
} else {
return this.continueBubbling;
}
},
});
// If @options.exitOnEscape is truthy, then the mode will exit when the escape key is pressed.
if (this.options.exitOnEscape) {
// Note. This handler ends up above the mode's own key handlers on the handler stack, so it
// takes priority.
this.push({
_name: `mode-${this.id}/exitOnEscape`,
"keydown": (event) => {
if (!KeyboardUtils.isEscape(event)) {
return this.continueBubbling;
}
this.exit(event, event.target);
return this.suppressEvent;
},
});
}
// If @options.exitOnBlur is truthy, then it should be an element. The mode will exit when that
// element loses the focus.
if (this.options.exitOnBlur) {
this.push({
_name: `mode-${this.id}/exitOnBlur`,
"blur": (event) =>
this.alwaysContinueBubbling(() => {
if (event.target === this.options.exitOnBlur) {
return this.exit(event);
}
}),
});
}
// If @options.exitOnClick is truthy, then the mode will exit on any click event.
if (this.options.exitOnClick) {
this.push({
_name: `mode-${this.id}/exitOnClick`,
"click": (event) => this.alwaysContinueBubbling(() => this.exit(event)),
});
}
//If @options.exitOnFocus is truthy, then the mode will exit whenever a focusable element is
//activated.
if (this.options.exitOnFocus) {
this.push({
_name: `mode-${this.id}/exitOnFocus`,
"focus": (event) =>
this.alwaysContinueBubbling(() => {
if (DomUtils.isFocusable(event.target)) {
return this.exit(event);
}
}),
});
}
// If @options.exitOnScroll is truthy, then the mode will exit on any scroll event.
if (this.options.exitOnScroll) {
this.push({
_name: `mode-${this.id}/exitOnScroll`,
"scroll": (event) => this.alwaysContinueBubbling(() => this.exit(event)),
});
}
// Some modes are singletons: there may be at most one instance active at any time. A mode is a
// singleton if @options.singleton is set. The value of @options.singleton should be the key
// which is intended to be unique. New instances deactivate existing instances with the same
// key.
if (this.options.singleton) {
const singletons = Mode.singletons || (Mode.singletons = {});
const key = this.options.singleton;
this.onExit(() => delete singletons[key]);
if (singletons[key] != null) {
singletons[key].exit();
}
singletons[key] = this;
}
// if @options.suppressTrailingKeyEvents is set, then -- on exit -- we suppress all key events
// until a subsquent (non-repeat) keydown or keypress. In particular, the intention is to catch
// keyup events for keys which we have handled, but which otherwise might trigger page actions
// (if the page is listening for keyup events).
if (this.options.suppressTrailingKeyEvents) {
this.onExit(function () {
const handler = function (event) {
if (event.repeat) {
return handlerStack.suppressEvent;
} else {
this.remove();
return handlerStack.continueBubbling;
}
};
return handlerStack.push({
name: "suppress-trailing-key-events",
keydown: handler,
keypress: handler,
});
});
}
Mode.modes.push(this);
this.setIndicator();
this.logModes();
}
// End of Mode constructor.
setIndicator(indicator) {
if (indicator) {
this.options.indicator = indicator;
}
return Mode.setIndicator();
}
static setIndicator() {
return handlerStack.bubbleEvent("indicator");
}
push(handlers) {
if (!handlers._name) {
handlers._name = `mode-${this.id}`;
}
return this.handlers.push(handlerStack.push(handlers));
}
unshift(handlers) {
if (!handlers._name) {
handlers._name = `mode-${this.id}`;
}
this.handlers.push(handlerStack.unshift(handlers));
}
onExit(handler) {
this.exitHandlers.push(handler);
}
exit(...args) {
if (this.modeIsExiting || !this.modeIsActive) {
return;
}
this.log("deactivate:", this.id);
this.modeIsExiting = true;
for (const handler of this.exitHandlers) {
// TODO(philc): Is this array.from necessary?
handler(...Array.from(args || []));
}
for (const handlerId of this.handlers) {
handlerStack.remove(handlerId);
}
Mode.modes = Mode.modes.filter((mode) => mode !== this);
this.modeIsActive = false;
return this.setIndicator();
}
// Debugging routines.
logModes() {
if (Mode.debug) {
this.log("active modes (top to bottom):");
for (const mode of Mode.modes.slice().reverse()) {
this.log(" ", mode.id);
}
}
}
log(...args) {
if (Mode.debug) {
console.log(...Array.from(args || []));
}
}
// For tests only.
static top() {
return this.modes[this.modes.length - 1];
}
// For tests only.
static reset() {
for (const mode of this.modes) {
mode.exit();
}
this.modes = [];
}
}
// If Mode.debug is true, then we generate a trace of modes being activated and deactivated on the
// console.
Mode.debug = false;
Mode.modes = [];
class SuppressAllKeyboardEvents extends Mode {
constructor(options) {
if (options == null) {
options = {};
}
super();
const defaults = {
name: "suppressAllKeyboardEvents",
suppressAllKeyboardEvents: true,
};
super.init(Object.assign(defaults, options));
}
}
class CacheAllKeydownEvents extends SuppressAllKeyboardEvents {
constructor(options) {
if (options == null) {
options = {};
}
const keydownEvents = [];
const defaults = {
name: "cacheAllKeydownEvents",
keydown(event) {
return keydownEvents.push(event);
},
};
super(Object.assign(defaults, options));
this.keydownEvents = [];
}
replayKeydownEvents() {
return this.keydownEvents.map((event) => handlerStack.bubbleEvent("keydown", event));
}
}
Object.assign(globalThis, { Mode, SuppressAllKeyboardEvents, CacheAllKeydownEvents });