-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
key-bindings-on-input-fields.js
executable file
·77 lines (64 loc) · 1.83 KB
/
key-bindings-on-input-fields.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
import { paths } from "../libs/paths";
import { newReplyTextareasObserver } from "../libs/dom-utils";
const shortcuts = {
italicise(event) {
const { target } = event;
const { value, selectionEnd, selectionStart } = target;
const selection = value.substring(selectionStart, selectionEnd);
let before;
let after;
if (
value.charAt(selectionStart - 1) === "*" &&
value.charAt(selectionEnd) === "*"
) {
before = value.substring(0, selectionStart - 1);
after = value.substring(selectionEnd + 1, value.length);
target.value = `${before}${selection}${after}`;
target.selectionStart = selectionStart - 1;
target.selectionEnd = selectionEnd - 1;
} else {
before = value.substring(0, selectionStart);
after = value.substring(selectionEnd, value.length);
target.value = `${before}*${selection}*${after}`;
target.selectionStart = selectionStart + 1;
target.selectionEnd = selectionEnd + 1;
}
},
quickSubmit(event) {
event.target.form.submit();
},
};
function handleKeydown(event) {
if (event.ctrlKey || event.metaKey) {
switch (event.keyCode) {
case 73:
shortcuts.italicise(event);
break;
case 13:
shortcuts.quickSubmit(event);
break;
default:
break;
}
}
}
function init() {
const textareas = document.querySelectorAll("textarea");
const inputs = document.querySelectorAll("input");
const fields = [...textareas, ...inputs];
for (const field of fields) {
field.addEventListener("keydown", handleKeydown);
}
newReplyTextareasObserver(handleKeydown);
return true;
}
const details = {
id: "key-bindings-on-input-fields",
pages: {
include: [...paths.comments, ...paths.forms],
exclude: [],
},
loginRequired: false,
init,
};
export default details;