-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyboard.js
155 lines (118 loc) · 3.18 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
"use strict";
const makeKeyboardCtx = imports => {
const {morphCtx, processingCtx} = imports;
const setup = (editorNode, editor) => {
let hToggle = false;
let showStepsToggle = true;
editorNode.addEventListener("paste", event => {
event.preventDefault();
const rawContent = event.clipboardData.getData("text/plain");
const content = processingCtx.preprocess(rawContent);
const delay = 0;
editor.replaceSelected(content, delay);
});
editorNode.addEventListener("cut", event => {
event.preventDefault();
document.execCommand('copy');
const delay = 0;
editor.replaceSelected('', delay);
});
editorNode.addEventListener("keydown", (() => {
function isKeyModified(event) {
return event.ctrlKey || event.metaKey || event.altKey;
}
function processModKey(event) {
if (event.ctrlKey) {
if (event.key == 'h') {
hToggle = !hToggle;
return true;
}
if (event.key == 's') {
showStepsToggle = !showStepsToggle;
const stepsContainer = document.querySelector("#steps-container");
stepsContainer.style.display = showStepsToggle ?
"block" :
"none";
return true;
}
if (event.key == 'z') {
editor.undo();
return true;
}
if (event.key == 'y') {
editor.redo();
return true;
}
if (event.key == "i") { // stop CE from making italics
return true;
}
if (event.key == "b") { // stop CE from making bold
return true;
}
}
return false;
}
function processActionKey(event) {
const sel = window.getSelection();
switch(event.key) {
case 'Backspace':
if (sel.isCollapsed) {
editor.growSelectionLeft();
}
editor.replaceSelected('');
return true;
case 'Delete':
if (sel.isCollapsed) {
editor.growSelectionRight();
}
editor.replaceSelected('');
return true;
case 'Enter':
editor.replaceSelected('\n');
return true;
case 'Tab':
editor.replaceSelected('\t');
return true;
}
return false;
}
// returns true if a command was issued to editing
// this means that event.preventDefault should be called
function processCommand(event) {
const sel = window.getSelection();
if (isKeyModified(event)) {
return processModKey(event);
}
if (processActionKey(event)) {
return true;
}
// maybe an onchange event for the editornode would be a better way of detecting this
if (morphCtx.isCharPrintable(event.key)) {
let {key} = event;
if (key == "h" && !hToggle) {
key = "'";
}
editor.replaceSelected(key);
return true;
}
return false; // allow by default
}
return event => {
if (processCommand(event)) {
event.preventDefault();
}
};
})());
// NOTE: can also preventDefault onclick when isStepping
editorNode.addEventListener("keyup", event => {
editor.mutateHistory(0, [], []);
});
// the need to triple ctrl-z to remove after pasting is probably because of the clicks at the start
editorNode.addEventListener("mouseup", event => {
editor.mutateHistory(0, [], []);
});
};
return {
setup
};
};