-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathuseChatInputKeyEvents.js
More file actions
113 lines (105 loc) · 3.31 KB
/
useChatInputKeyEvents.js
File metadata and controls
113 lines (105 loc) · 3.31 KB
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
import formatSelection from '../lib/formatSelection';
/**
* useChatInputKeyEvents - A hook to manage custom keyboard event logic for the ChatInput.
* Directly supports the GSoC 2026 'Accessibility (WCAG) & Keyboard Navigation' deliverable.
*/
const useChatInputKeyEvents = ({
messageRef,
editMessage,
setDisableButton,
setEditMessage,
showCommandList,
showMembersList,
sendTypingStop,
sendMessage,
handleNewLine,
}) => {
const onKeyDown = (e) => {
switch (true) {
case e.ctrlKey && e.code === 'KeyI': {
e.preventDefault();
formatSelection(messageRef, '_{{text}}_');
break;
}
case e.ctrlKey && e.code === 'KeyB': {
e.preventDefault();
formatSelection(messageRef, '*{{text}}*');
break;
}
case (e.ctrlKey || e.metaKey || e.shiftKey) && e.code === 'Enter':
e.preventDefault();
handleNewLine(e);
break;
case e.code === 'Escape':
if (editMessage.msg || editMessage.attachments) {
e.preventDefault();
messageRef.current.value = '';
setDisableButton(true);
setEditMessage({});
}
break;
case e.code === 'Enter':
e.preventDefault();
if (!showCommandList && !showMembersList) {
sendTypingStop();
sendMessage();
}
break;
case (e.ctrlKey || e.altKey) && e.code === 'ArrowLeft': {
e.preventDefault();
if (messageRef && messageRef.current) {
const { value, selectionStart } = messageRef.current;
let newPosition = selectionStart;
while (newPosition > 0 && /\s/.test(value[newPosition - 1])) {
newPosition -= 1;
}
while (newPosition > 0 && !/\s/.test(value[newPosition - 1])) {
newPosition -= 1;
}
messageRef.current.setSelectionRange(newPosition, newPosition);
messageRef.current.focus();
}
break;
}
case (e.ctrlKey || e.altKey) && e.code === 'ArrowRight': {
e.preventDefault();
if (messageRef && messageRef.current) {
const { value, selectionEnd } = messageRef.current;
let newPosition = selectionEnd;
while (newPosition < value.length && /\s/.test(value[newPosition])) {
newPosition += 1;
}
while (newPosition < value.length && !/\s/.test(value[newPosition])) {
newPosition += 1;
}
messageRef.current.setSelectionRange(newPosition, newPosition);
messageRef.current.focus();
}
break;
}
case (e.ctrlKey || e.altKey) && e.code === 'ArrowUp': {
e.preventDefault();
if (messageRef && messageRef.current) {
messageRef.current.setSelectionRange(0, 0);
messageRef.current.focus();
}
break;
}
case (e.ctrlKey || e.altKey) && e.code === 'ArrowDown': {
e.preventDefault();
if (messageRef && messageRef.current) {
const { current } = messageRef;
const { value } = current;
const { length } = value;
messageRef.current.setSelectionRange(length, length);
messageRef.current.focus();
}
break;
}
default:
break;
}
};
return onKeyDown;
};
export default useChatInputKeyEvents;