-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
377 lines (375 loc) · 13.6 KB
/
index.html
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="color-scheme" content="dark" />
<title>KeyHub Translation Tool</title>
<style>
/* Variables */
:root {
--primary: #ff8800;
--background: #0e0d0f;
--important: #232323;
}
/* Variables */
/* Boiler Plate */
*,
::after,
::before {
box-sizing: border-box;
margin: 0;
padding: 0;
outline: 0;
}
html {
font-family: "system-ui", "sans-serif";
background-color: var(--background);
color: white;
}
/* Boiler Plate */
/* Helper Class */
.highlight {
color: var(--primary);
}
.d-flex {
display: flex;
}
.column {
flex-direction: column;
}
.f-size-big {
font-size: 3em;
}
.f-thin {
font-weight: 300;
}
.center-all {
align-items: center;
justify-content: center;
}
.push-bottom {
margin-bottom: 2em;
}
/* Helper Class */
/* UI COMPONENTS */
.trans-column {
background: var(--background);
border: 1px solid rgb(255 255 255 / 22%);
padding: 8px;
word-wrap: break-word;
width: auto;
}
.translate {
background: transparent;
border: none;
border-bottom: 1px solid rgb(255 255 255 / 22%);
}
.indicator {
color: gray;
display: none;
}
a {
text-decoration: none;
color: #fd8500;
}
.container-float {
background: var(--important);
/* box-shadow: 0px 20px 35px -11px rgb(255 255 255 / 30%); */
display: grid;
border-radius: 0px;
padding: 2em;
width: 100%;
grid-template-columns: minmax(4em, 30em);
gap: 3em;
}
.controllers {
background: var(--important);
width: 100%;
display: flex;
justify-content: center;
gap: 16px;
padding: 1.25em;
}
.kh-button {
width: 100px;
height: 100px;
border: 1px solid var(--primary);
background: var(--background);
font-family: inherit;
color: white;
padding: 1em;
cursor: pointer;
transition: 0.15s ease-in-out;
}
.kh-button:hover {
background-color: #ff8800;
border: 8px solid #ff8800;
}
select.kh-button {
appearance: none;
background-image: url("");
text-align: center;
}
#modal {
position: fixed;
top: 25%;
opacity: 0;
transition: 200ms ease-in-out;
z-index: 10;
width: 100%;
height: 25em;
pointer-events: none;
gap: 2em;
}
#modal.active {
pointer-events: all;
opacity: 1;
}
#overlay {
position: fixed;
transition: 200ms ease-in-out;
opacity: 0;
background: rgb(0, 0, 0);
top: 0;
left: 0;
right: 0;
bottom: 0;
pointer-events: none;
z-index: 9;
}
#overlay.active {
opacity: 0.85;
pointer-events: all;
}
/* UI COMPONENTS */
/* Media Responsiveness */
@media (min-width: 700px) {
.indicator {
display: block;
}
.container-float {
width: auto;
padding: 4em;
border-radius: 8px;
grid-template-columns: 1fr 1fr;
transition: 0.15s ease-in-out;
}
.trans-column {
background: transparent;
padding: 16px 0px;
width: 16em;
border: none;
border-bottom: 1px solid rgb(255 255 255 / 22%);
}
}
</style>
</head>
<body class="d-flex column center-all">
<input style="display: none;" type="file" name="t-file" id="t-file" />
<div id="header" class="d-flex column center-all push-bottom">
<h1 class="f-size-big f-thin">Key<text class="highlight">Hub</text></h1>
<span>Translation Tool</span>
</div>
<div class="controllers push-bottom">
<button class="kh-button" onclick="openModal('importModal');">Import</button>
<button class="kh-button" onclick="if(confirm('Are you sure you want to erase all your work?') == true) {clearAll()}">Clear</button>
<button class="kh-button" onclick="openModal('exportModal');">Export</button>
</div>
<div class="controllers push-bottom">
<h2><a href="https://git.key-hub.eu/tree/main/pages/localization" target="_blank">Translation File Repository</a></h2>
</div>
<div class="container-float" id="tt-table">
<div class="indicator">
Original string (English)
</div>
<div class="indicator">
Translation
</div>
</div>
<div class="d-flex center-all" id="modal">
<div id="importModal" style="display: none;">
<button class="kh-button" onclick="importTranslation('link',prompt('Paste your link here.'));">Link</button>
<button class="kh-button" onclick="input.click()">File</button>
<select class="kh-button" id="languageSelect" onchange="importTranslation('link',this.value);">
<option value="" disabled selected>GitHub</option>
</select>
</div>
<div id="exportModal" style="display: none;">
<button class="kh-button" onclick="exportTranslation('file');">Export as File</button>
<button class="kh-button" onclick="exportTranslation('clip');">Export to Clipboard</button>
</div>
</div>
<div id="overlay"></div>
<script>
const table = document.querySelector("#tt-table");
const modal = document.querySelector("#modal");
const overlay = document.querySelector("#overlay");
const input = document.querySelector("#t-file");
const default_url = "https://raw.githubusercontent.com/jondycz/KeyHub/main/pages/localization/en_GB_English.json";
var exportName;
// Load the default strings
fetch(default_url)
.then((res) => res.json())
.then((res) => {
const translate_column = (key, val) => `<div data-key="${key}" class="trans-column">${val.replaceAll("<", "<")}</div> <div contenteditable="true" data-key="${key}" class="trans-column translate"></div>`;
for (const [key, val] of Object.entries(res)) {
table.innerHTML += translate_column(key, val);
}
});
const githubImport = document.getElementById("languageSelect");
fetch("https://api.github.com/repos/jondycz/KeyHub/contents/pages/localization")
.then((response) => response.json())
.then((data) => {
// Loop through the array of objects and create options
data.forEach((item) => {
const option = document.createElement("option");
option.value = item.download_url; // Use the download_url as the option value
option.textContent = item.name; // Use the name as the option text
githubImport.appendChild(option); // Add the option to the select element
});
})
.catch((error) => {
console.error("Error fetching data:", error);
});
// Functions.
function getFlagEmoji(countryCode) {
const codePoints = countryCode
.toUpperCase()
.split("")
.map((char) => 127397 + char.charCodeAt());
return String.fromCodePoint(...codePoints);
}
function serializeTranslation() {
// Create List
let list = {};
document.querySelectorAll(".translate").forEach((text) => {
let id = text.dataset.key;
let content = text.textContent;
if (content !== "") {
list[id] = content;
}
});
return list;
}
function exportTranslation(exportType) {
let list = serializeTranslation();
window.lastExport = list;
closeModal();
switch (exportType) {
case "clip":
navigator.clipboard.writeText(JSON.stringify(list, null, "\t"));
break;
case "file":
// Download With Blob
const element = document.createElement("a");
let blob = new Blob([JSON.stringify(list, null, "\t")], { type: "text/json;charset=utf-8" });
element.download = window.exportName ?? "keyhub-translation.json";
element.href = URL.createObjectURL(blob);
element.click();
break;
}
}
function importTranslation(type, item) {
if (!item) return;
clearAll();
console.log(item);
switch (type) {
case "json":
for (const [key, val] of Object.entries(item)) {
document.querySelector(`.translate[data-key="${key}"]`).textContent = val;
}
window.lastExport = item;
break;
case "link":
fetch(item)
.then((res) => res.json())
.then((res) => {
for (const [key, val] of Object.entries(res)) {
let element = document.querySelector(`.translate[data-key="${key}"]`);
if (element) element.textContent = val;
}
window.lastExport = res;
});
window.exportName = new URL(item).pathname.split("/").pop();
closeModal();
break;
}
}
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
function handleDrop(e) {
handleFile(e.dataTransfer.files[0]);
table.style.boxShadow = "0 0 0 0 #ff8800";
}
function handleFile(file) {
let reader = new FileReader();
reader.readAsText(file);
reader.onloadend = function (e) {
let strings = JSON.parse(e.target.result);
importTranslation("json", strings);
window.lastExport = strings;
window.exportName = String(file.name);
closeModal();
};
}
function highlight() {
table.style.boxShadow = "0px 20px 35px 10px #ff8800";
}
function unhighlight() {
table.style.boxShadow = "0 0 0 0 #ff8800";
}
function openModal(modalItems) {
document.getElementById(modalItems).style.display = "";
modal.classList.add("active");
overlay.classList.add("active");
}
function closeModal() {
modal.classList.remove("active");
overlay.classList.remove("active");
for (childElement of modal.children) {
childElement.style.display = "none";
}
}
function clearAll() {
window.exportName = undefined;
document.querySelectorAll(".translate").forEach((x) => {
x.textContent = "";
});
window.lastExport = {};
}
// Event Listeners
table.addEventListener("drop", handleDrop, false);
table.addEventListener("dragover", highlight, false);
table.addEventListener("dragleave", unhighlight, false);
overlay.addEventListener("click", () => {
closeModal();
});
modal.addEventListener("click", (a) => {
if (a.target.id === "modal") {
closeModal();
}
});
input.addEventListener("change", () => {
handleFile(input.files[0]);
});
// https://stackoverflow.com/questions/7317273/warn-user-before-leaving-web-page-with-unsaved-changes
var lastExport = {};
window.addEventListener("beforeunload", function (e) {
let confirmationMessage = "It looks like you have been editing something. " + "If you leave before saving, your changes will be lost.";
let translationState = serializeTranslation();
if (JSON.stringify(translationState) !== JSON.stringify(window.lastExport)) {
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
}
});
// Disable default drag events.
["dragenter", "dragover", "dragleave", "drop"].forEach((eventName) => {
table.addEventListener(eventName, preventDefaults, false);
document.body.addEventListener(eventName, preventDefaults, false);
});
</script>
</body>
</html>