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
|
import { UndoManager } from "../util/UndoManager";
import { SelectionManager } from "../util/SelectionManager";
import { CollectionDockingView } from "./collections/CollectionDockingView";
import { MainView } from "./MainView";
import { DragManager } from "../util/DragManager";
import { action, runInAction } from "mobx";
import { Doc } from "../../new_fields/Doc";
import { DictationManager } from "../util/DictationManager";
import SharingManager from "../util/SharingManager";
import { CurrentUserUtils } from "../../server/authentication/models/current_user_utils";
import { Cast, PromiseValue } from "../../new_fields/Types";
import { ScriptField } from "../../new_fields/ScriptField";
import { InkingControl } from "./InkingControl";
import { InkTool } from "../../new_fields/InkField";
import { DocumentView } from "./nodes/DocumentView";
const modifiers = ["control", "meta", "shift", "alt"];
type KeyHandler = (keycode: string, e: KeyboardEvent) => KeyControlInfo | Promise<KeyControlInfo>;
type KeyControlInfo = {
preventDefault: boolean,
stopPropagation: boolean
};
export default class KeyManager {
public static Instance: KeyManager = new KeyManager();
private router = new Map<string, KeyHandler>();
constructor() {
const isMac = navigator.platform.toLowerCase().indexOf("mac") >= 0;
// SHIFT CONTROL ALT META
this.router.set("0000", this.unmodified);
this.router.set(isMac ? "0001" : "0100", this.ctrl);
this.router.set(isMac ? "0100" : "0010", this.alt);
this.router.set(isMac ? "1001" : "1100", this.ctrl_shift);
this.router.set("1000", this.shift);
}
public handle = async (e: KeyboardEvent) => {
const keyname = e.key && e.key.toLowerCase();
this.handleGreedy(keyname);
if (modifiers.includes(keyname)) {
return;
}
const bit = (value: boolean) => value ? "1" : "0";
const modifierIndex = bit(e.shiftKey) + bit(e.ctrlKey) + bit(e.altKey) + bit(e.metaKey);
const handleConstrained = this.router.get(modifierIndex);
if (!handleConstrained) {
return;
}
const control = await handleConstrained(keyname, e);
control.stopPropagation && e.stopPropagation();
control.preventDefault && e.preventDefault();
}
private handleGreedy = action((keyname: string) => {
switch (keyname) {
}
});
private unmodified = action((keyname: string, e: KeyboardEvent) => {
switch (keyname) {
case "escape":
const main = MainView.Instance;
InkingControl.Instance.switchTool(InkTool.None);
if (main.isPointerDown) {
DragManager.AbortDrag();
} else {
if (CollectionDockingView.Instance.HasFullScreen()) {
CollectionDockingView.Instance.CloseFullScreen();
} else {
SelectionManager.DeselectAll();
}
}
SelectionManager.DeselectAll();
DictationManager.Controls.stop();
SharingManager.Instance.close();
break;
case "delete":
case "backspace":
if (document.activeElement) {
if (document.activeElement.tagName === "INPUT" || document.activeElement.tagName === "TEXTAREA") {
return { stopPropagation: false, preventDefault: false };
}
}
UndoManager.RunInBatch(() => {
SelectionManager.SelectedDocuments().map(docView => {
const doc = docView.props.Document;
const remove = docView.props.removeDocument;
remove && remove(doc);
});
}, "delete");
break;
}
return {
stopPropagation: false,
preventDefault: false
};
});
private shift = async (keyname: string) => {
let stopPropagation = false;
let preventDefault = false;
switch (keyname) {
case "~":
DictationManager.Controls.listen({ useOverlay: true, tryExecute: true });
stopPropagation = true;
preventDefault = true;
}
return {
stopPropagation: stopPropagation,
preventDefault: preventDefault
};
}
private alt = action((keyname: string) => {
const stopPropagation = true;
const preventDefault = true;
switch (keyname) {
case "f":
const dv = SelectionManager.SelectedDocuments()?.[0];
if (dv) {
const ex = dv.props.ScreenToLocalTransform().inverse().transformPoint(0, 0)[0];
const ey = dv.props.ScreenToLocalTransform().inverse().transformPoint(0, 0)[1];
DocumentView.FloatDoc(dv, ex, ey);
}
// case "n":
// let toggle = MainView.Instance.addMenuToggle.current!;
// toggle.checked = !toggle.checked;
// break;
}
return {
stopPropagation: stopPropagation,
preventDefault: preventDefault
};
});
private ctrl = action((keyname: string, e: KeyboardEvent) => {
let stopPropagation = true;
let preventDefault = true;
switch (keyname) {
case "arrowright":
if (document.activeElement) {
if (document.activeElement.tagName === "INPUT" || document.activeElement.tagName === "TEXTAREA") {
return { stopPropagation: false, preventDefault: false };
}
}
MainView.Instance.mainFreeform && CollectionDockingView.AddRightSplit(MainView.Instance.mainFreeform, undefined);
break;
case "arrowleft":
if (document.activeElement) {
if (document.activeElement.tagName === "INPUT" || document.activeElement.tagName === "TEXTAREA") {
return { stopPropagation: false, preventDefault: false };
}
}
MainView.Instance.mainFreeform && CollectionDockingView.CloseRightSplit(MainView.Instance.mainFreeform);
break;
case "backspace":
if (document.activeElement) {
if (document.activeElement.tagName === "INPUT" || document.activeElement.tagName === "TEXTAREA") {
return { stopPropagation: false, preventDefault: false };
}
}
break;
case "t":
PromiseValue(Cast(CurrentUserUtils.UserDocument.Create, Doc)).then(pv => pv && (pv.onClick as ScriptField).script.run({ this: pv }));
if (MainView.Instance.flyoutWidth === 240) {
MainView.Instance.flyoutWidth = 0;
} else {
MainView.Instance.flyoutWidth = 240;
}
break;
case "l":
PromiseValue(Cast(CurrentUserUtils.UserDocument.Library, Doc)).then(pv => pv && (pv.onClick as ScriptField).script.run({ this: pv }));
if (MainView.Instance.flyoutWidth === 250) {
MainView.Instance.flyoutWidth = 0;
} else {
MainView.Instance.flyoutWidth = 250;
}
break;
case "f":
PromiseValue(Cast(CurrentUserUtils.UserDocument.Search, Doc)).then(pv => pv && (pv.onClick as ScriptField).script.run({ this: pv }));
if (MainView.Instance.flyoutWidth === 400) {
MainView.Instance.flyoutWidth = 0;
} else {
MainView.Instance.flyoutWidth = 400;
}
break;
case "o":
const target = SelectionManager.SelectedDocuments()[0];
target && CollectionDockingView.Instance && CollectionDockingView.Instance.OpenFullScreen(target);
break;
case "r":
preventDefault = false;
break;
case "y":
UndoManager.Redo();
stopPropagation = false;
break;
case "z":
UndoManager.Undo();
stopPropagation = false;
break;
case "a":
case "v":
case "x":
case "c":
stopPropagation = false;
preventDefault = false;
break;
}
return {
stopPropagation: stopPropagation,
preventDefault: preventDefault
};
});
async printClipboard() {
const text: string = await navigator.clipboard.readText();
}
private ctrl_shift = action((keyname: string) => {
const stopPropagation = true;
const preventDefault = true;
switch (keyname) {
case "z":
UndoManager.Redo();
break;
}
return {
stopPropagation: stopPropagation,
preventDefault: preventDefault
};
});
}
|