-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui-utils.js
64 lines (50 loc) · 1.41 KB
/
ui-utils.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
/**
* Utils used for UI.
*/
const updateInputSize = () => {
const wasAtBottom = isAtBottom()
input.style.height = 0
input.style.height = input.scrollHeight + 'px'
document.body.style.marginBottom = footer.offsetHeight + 'px'
if (wasAtBottom) {
scrollToBottom()
}
}
/**
* @param {string} text
*/
const insertAtCursor = (text) => {
const start = input.selectionStart || 0
const before = input.value.slice(0, start)
const after = input.value.slice(start)
const newbefore = before + text
input.value = newbefore + after
input.selectionStart = input.selectionEnd = newbefore.length
updateInputSize()
}
const backspaceAtCursor = (length = 0) => {
const start = input.selectionStart || 0
const before = input.value.slice(0, start)
const after = input.value.slice(start)
const newbefore = before.slice(0, -length)
input.value = newbefore + after
input.selectionStart = input.selectionEnd = newbefore.length
updateInputSize()
}
const updateTitle = () => {
const isFrontPage = myChannel === ''
if (isFrontPage || (document.hasFocus() && isAtBottom())) {
unread = 0
}
const title = (() => {
if (isFrontPage) {
return "hack.chat++"
}
if (unread > 0) {
return `(${unread}) ${myChannel} - hack.chat++`
}
return `${myChannel} - hack.chat++`
})()
document.title = title
}
export { updateInputSize, insertAtCursor, backspaceAtCursor, updateTitle }