-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcore.js
287 lines (254 loc) · 8.99 KB
/
core.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
async function cropExtractAndAddInFlight() {
const extractBtn = document.getElementById("extract-btn")
extractBtn.classList.add("loading")
let cropped = crop()
let allExtracted = []
for (let crop of cropped) {
let extracted = await extractText(crop);
extracted.forEach(e => allExtracted.push(e))
}
for (let text of allExtracted.reverse()) {
if (text) {
addInFlight(text)
}
}
extractBtn.classList.remove("loading")
}
function showSplash() {
const dialog = document.querySelector("dialog.splash")
dialog?.showModal()
dialog?.addEventListener("click", function onclick() {
dialog.close()
})
}
function listenForKeys(elem, keys) {
const keyMap = {}
keys.forEach(key => {
keyMap[key.keyCode] = keyMap[key.keyCode] || []
keyMap[key.keyCode].push(key)
})
elem.addEventListener("keyup", (e) => {
if (e.isComposing) {
return
}
const commandsForKey = keyMap[e.keyCode]
if (commandsForKey) {
for (let command of commandsForKey) {
if (
!command.altKey === !e.altKey
&& !command.ctrlKey === !e.ctrlKey
&& !command.metaKey === !e.metaKey
&& !command.shiftKey === !e.shiftKey
) {
command.invoke()
}
}
}
})
}
function compareStr(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
function addKeyboardCommand(key, invoke) {
MyAltTextOrg.cmd.push({
key,
invoke
})
}
function showHelp() {
const help = document.querySelector(".help-dialog")
help?.showModal()
help?.addEventListener("click", () => {
help.close()
})
}
function showExecMenu(overlay, button) {
return () => {
showEscapable(overlay, true)
button.click()
}
}
function focusClick(id) {
document.getElementById(id).click();
}
function addDots() {
let openLangMenu = null
const options = [
{
display: "Open File",
sortKey: "AAA",
closeMenu: true,
keyCmd: {
alt: true,
keyCode: 79 // O
},
makeElement: () => {
const wrapper = document.createElement("div")
wrapper.classList.add("file-uploader", "dropdown-option")
const inputName = "dots-open-file";
const input = document.createElement("input")
input.id = inputName
input.type = "file"
input.accept = "image/*"
input.name = inputName
wrapper.appendChild(input)
const lbl = document.createElement("label")
lbl.htmlFor = inputName
lbl.innerHTML = "Open File"
wrapper.appendChild(lbl)
lbl.addEventListener("click", () => input.click())
input.addEventListener("change", async () => {
const file = input.files[0]
await loadFile(file)
})
return wrapper
}
},
{
display: "Close File",
sortKey: "BBB",
closeMenu: true,
keyCmd: {
altKey: true,
keyCode: 81 // Q
},
onclick: clearImage
},
{
display: "Clear Work Area",
sortKey: "BBC",
closeMenu: true,
keyCmd: {
altKey: true,
keyCode: 87 // W
},
onclick: clearInFlight
},
{
display: "Set Site Colors",
sortKey: "BBD",
closeMenu: true,
onclick: showPaletteChooser
},
{
display: "Page Language",
closeMenu: false,
sortKey: "CCC",
makeElement: () => {
const button = document.createElement("button")
button.classList.add("submenu-button")
button.innerHTML = `
<span>Page Language</span>
<img src="images/dropdown.svg" class="inline-icon dropdown-img rotated text-svg" aria-hidden="true" alt="">`
let dropdown = buildSimpleDropdownMenu(
button,
MyAltTextOrg.i18n.pageOptions,
makePageLangFooter(),
updatePageLanguage,
"side-dropdown"
)
dropdown.firstChild.addEventListener("click", () => {
if (openLangMenu) {
hideEscapable(openLangMenu)
}
openLangMenu = dropdown.querySelector(".dropdown-content")
})
dropdown.classList.add("dropdown-option")
return dropdown
}
},
{
display: "Text Extraction Language",
closeMenu: false,
sortKey: "DDD",
makeElement: () => {
const button = document.createElement("button")
button.classList.add("submenu-button")
button.innerHTML = `
<span>Text Extraction Language</span>
<img src="images/dropdown.svg" class="inline-icon dropdown-img rotated text-svg" aria-hidden="true" alt="">`
let dropdown = buildSimpleDropdownMenu(
button,
MyAltTextOrg.tesseract.humanToTesseractLang,
null,
setExtractionLang,
"side-dropdown"
)
dropdown.firstChild.addEventListener("click", () => {
if (openLangMenu) {
hideEscapable(openLangMenu)
}
openLangMenu = dropdown.querySelector(".dropdown-content")
})
dropdown.classList.add("dropdown-option")
return dropdown
}
},
{
display: "Help",
closeMenu: true,
sortKey: "EEE",
keyCmd: {
altKey: true,
keyCode: 72 // W
},
onclick: showHelp,
},
{
display: "Announcements",
closeMenu: true,
sortKey: "FFF",
onclick: showSplash
}
]
const footer = document.createElement("div")
footer.classList.add("links")
footer.innerHTML = `
<a href="https://alt-text.org" target="_blank"><img class="link-logo" src="images/alt-text-org.svg"
alt="Alt-Text.org"></a>
<a href="https://github.com/alt-text-org/my.alt-text.org" target="_blank"><img class="link-logo"
src="images/github.svg"
alt="GitHub"></a>
<a href="https://social.alt-text.org/@hannah" target="_blank"><img class="link-logo" src="images/masto.svg"
alt="Mastodon"></a>`
let button = document.createElement("button")
button.classList.add("page-button", "dots-btn")
button.ariaLabel = "General Menu"
button.innerHTML = `<img id="dots-btn-img" src="images/dots.svg" class="text-svg" alt="" aria-hidden="true" width="32" height="32">`
const dots = buildComplexDropdownMenu(button, options, footer)
document.getElementById("dots-btn-placeholder").appendChild(dots)
MyAltTextOrg.exec.push(...options)
options.forEach(option => {
if (!option.keyCmd) {
return
}
let cmd = option.onclick
if (!cmd) {
let elem = option.makeElement()
cmd = () => elem.click()
}
addKeyboardCommand(option.keyCmd, cmd)
})
}
function addWorkAreaButtons () {
let addBtn = document.createElement("button");
addBtn.classList.add("page-button", "emoji-button", "large-emoji");
addBtn.ariaLabel = getLocalized("addInFlightBtnTxt");
addBtn.textContent = "+";
addBtn.addEventListener("click", addBlankInFlight);
let saveBtn = document.createElement("button");
saveBtn.classList.add("page-button", "emoji-button", "large-emoji");
saveBtn.ariaLabel = getLocalized("saveInFlightBtnTxt");
saveBtn.textContent = "💾";
saveBtn.addEventListener("click", saveAllInFlight);
const addInFlightContainer = document.getElementById("add-in-flight");
const inFlightControlsContainer = document.getElementById("in-flight-controls");
addInFlightContainer.appendChild(addBtn);
inFlightControlsContainer.appendChild(saveBtn);
}