forked from karlicoss/grasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.ts
162 lines (125 loc) · 4.01 KB
/
popup.ts
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
import browser from "webextension-polyfill"
import {METHOD_CAPTURE_WITH_EXTRAS} from './common.js'
import {getOptions} from './options.js'
// TODO template it in html too?
const BUTTON_ID = 'button_id'
const COMMENT_ID = 'comment_id'
const TAGS_ID = 'tags_id'
/*
* normal popup logging is pretty annoying because chrome closes devtools when the popup is closed
*/
/* eslint-disable @typescript-eslint/no-unused-vars */
function logbg(msg: any) {
console.error('%o', msg)
browser.runtime.sendMessage({
method: 'logging',
source: 'popup.js',
data: msg,
})
}
type State = {
comment: string
tag_str: string
}
function saveState(state: State | null) {
window.localStorage.setItem('state', JSON.stringify(state))
}
function clearState() {
saveState(null)
}
function loadState(): State | null {
const sts = window.localStorage.getItem('state') || null
return sts === null ? null : JSON.parse(sts)
}
function getComment(): HTMLInputElement {
return document.getElementById(COMMENT_ID) as HTMLInputElement
}
function getTags(): HTMLInputElement {
return document.getElementById(TAGS_ID) as HTMLInputElement
}
function getButton(): HTMLElement {
return document.getElementById(BUTTON_ID) as HTMLElement
}
function getUiState(): State {
const comment_text = getComment().value
const tag_str = getTags().value
return {
'comment': comment_text,
'tag_str': tag_str,
}
}
async function restoreState(state: State | null) {
// @ts-expect-error
window.justSubmitted = false
if (state == null) {
// comment just relies on default
const opts = await getOptions()
getTags().value = opts.default_tags
} else {
getComment().value = state.comment
getTags().value = state.tag_str
}
}
async function submitCapture () {
const state = getUiState()
const message = {
'method': METHOD_CAPTURE_WITH_EXTRAS,
...state,
}
const result = await browser.runtime.sendMessage(message)
if (result.success) {
// @ts-expect-error
window.justSubmitted = true
clearState()
console.log("[popup] captured!")
} else {
// if capture wasn't successful, keep the state intact
}
window.close()
}
const ctrlEnterSubmit = (e: KeyboardEvent) => {
// note: there is also e.metaKey which triggers on mac when cmd is pressed
// but it doesn't seem to allow chords like cmd+Enter
if ((e.ctrlKey || e.shiftKey) && e.key === 'Enter') {
submitCapture()
}
}
// https://stackoverflow.com/a/6003829/706389
function moveCaretToEnd(el: HTMLInputElement) {
if (typeof el.selectionStart == "number") {
el.selectionStart = el.selectionEnd = el.value.length
// @ts-expect-error
} else if (typeof el.createTextRange != "undefined") {
el.focus()
// @ts-expect-error
const range = el.createTextRange()
range.collapse(false)
range.select()
}
}
function setupPage () {
const comment = getComment()
comment.focus()
comment.addEventListener('keydown', ctrlEnterSubmit)
const tags = getTags()
tags.addEventListener('keydown', ctrlEnterSubmit)
tags.addEventListener('focus', () => moveCaretToEnd(tags)) // to put cursor to the end of tags when tabbed
getButton().addEventListener('click', submitCapture)
const state = loadState()
restoreState(state)
}
// hmm interesting that on chrome we can use visibilitychange for both
// on firefox dev edition however 'visible' doesn't fire, only 'hidden' :shrug:
document.addEventListener('DOMContentLoaded', setupPage)
window.addEventListener('visibilitychange', (_: Event) => {
// event doesn't contain visibility status
// docs recomment to use this instead of unload
const visible = document.visibilityState === 'visible'
if (!visible) {
// if we just sent, no need to save anything
// @ts-ignore
if (!window.justSubmitted) {
saveState(getUiState())
}
}
})