Skip to content

Commit d25266b

Browse files
committed
minor ui tweaks
1 parent 1880be5 commit d25266b

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

src/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ declare global {
44
interface Window {
55
supportedFormatCache: Map<string, FileFormat[]>;
66
printSupportedFormatCache: () => string;
7+
showPopup: (html: string) => void;
8+
hidePopup: () => void;
79
}
810
}
911

src/main.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ window.addEventListener("dragover", e => e.preventDefault());
128128
* Display an on-screen popup.
129129
* @param html HTML content of the popup box.
130130
*/
131-
function showPopup (html: string) {
131+
window.showPopup = function (html: string) {
132132
ui.popupBox.innerHTML = html;
133133
ui.popupBox.style.display = "block";
134134
ui.popupBackground.style.display = "block";
135135
}
136136
/**
137137
* Hide the on-screen popup.
138138
*/
139-
function hidePopup () {
139+
window.hidePopup = function () {
140140
ui.popupBox.style.display = "none";
141141
ui.popupBackground.style.display = "none";
142142
}
@@ -234,7 +234,7 @@ async function buildOptionList () {
234234
filterButtonList(ui.inputList, ui.inputSearch.value);
235235
filterButtonList(ui.outputList, ui.outputSearch.value);
236236

237-
hidePopup();
237+
window.hidePopup();
238238

239239
}
240240

@@ -273,7 +273,7 @@ const convertPathCache: Array<{
273273
async function attemptConvertPath (files: FileData[], path: ConvertPathNode[]) {
274274

275275
ui.popupBox.innerHTML = `<h2>Finding conversion route...</h2>
276-
<p>Trying ${path.map(c => c.format.format).join(" -> ")}</p>`;
276+
<p>Trying <b>${path.map(c => c.format.format).join(" ")}</b>...</p>`;
277277

278278
const cacheLast = convertPathCache.at(-1);
279279
if (cacheLast) files = cacheLast.files;
@@ -297,7 +297,7 @@ async function attemptConvertPath (files: FileData[], path: ConvertPathNode[]) {
297297
convertPathCache.push({ files, node: path[i + 1] });
298298
} catch (e) {
299299
console.log(path.map(c => c.format.format));
300-
console.error(handler.name, `${path[i].format.format} -> ${path[i + 1].format.format}`, e);
300+
console.error(handler.name, `${path[i].format.format} ${path[i + 1].format.format}`, e);
301301
return null;
302302
}
303303
}
@@ -371,6 +371,14 @@ async function buildConvertPath (
371371

372372
}
373373

374+
function downloadFile (bytes: Uint8Array, name: string, mime: string) {
375+
const blob = new Blob([bytes as BlobPart], { type: mime });
376+
const link = document.createElement("a");
377+
link.href = URL.createObjectURL(blob);
378+
link.download = name;
379+
link.click();
380+
}
381+
374382
ui.convertButton.onclick = async function () {
375383

376384
const inputFiles = selectedFiles;
@@ -388,46 +396,47 @@ ui.convertButton.onclick = async function () {
388396
const inputOption = allOptions[Number(inputButton.getAttribute("format-index"))];
389397
const outputOption = allOptions[Number(outputButton.getAttribute("format-index"))];
390398

399+
const inputFormat = inputOption.format;
400+
const outputFormat = outputOption.format;
401+
391402
try {
392403

393404
const inputFileData = [];
394405
for (const inputFile of inputFiles) {
395406
const inputBuffer = await inputFile.arrayBuffer();
396407
const inputBytes = new Uint8Array(inputBuffer);
408+
if (inputFormat.mime === outputFormat.mime) {
409+
downloadFile(inputBytes, inputFile.name, inputFormat.mime);
410+
continue;
411+
}
397412
inputFileData.push({ name: inputFile.name, bytes: inputBytes });
398413
}
399414

400-
showPopup("<h2>Finding conversion route...</h2>");
415+
window.showPopup("<h2>Finding conversion route...</h2>");
401416

402417
const output = await buildConvertPath(inputFileData, outputOption, [[inputOption]]);
403418
if (!output) {
404-
hidePopup();
419+
window.hidePopup();
405420
alert("Failed to find conversion route.");
406421
return;
407422
}
408423

409-
const outputFormat = outputOption.format;
410-
411424
for (const file of output.files) {
412-
const blob = new Blob([file.bytes as BlobPart], { type: outputFormat.mime });
413-
const link = document.createElement("a");
414-
link.href = URL.createObjectURL(blob);
415-
link.download = file.name;
416-
link.click();
425+
downloadFile(file.bytes, file.name, outputFormat.mime);
417426
}
418427

419-
alert(
420-
`Converted ${inputOption.format.format} to ${outputOption.format.format}!\n` +
421-
`Path used: ${output.path.map(c => c.format.format).join(" -> ")}`
428+
window.showPopup(
429+
`<h2>Converted ${inputOption.format.format} to ${outputOption.format.format}!</h2>` +
430+
`<p>Path used: <b>${output.path.map(c => c.format.format).join(" → ")}</b>.</p>\n` +
431+
`<button onclick="window.hidePopup()">OK</button>`
422432
);
423433

424434
} catch (e) {
425435

436+
window.hidePopup();
426437
alert("Unexpected error while routing:\n" + e);
427438
console.error(e);
428439

429440
}
430441

431-
hidePopup();
432-
433442
};

style.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ button.selected {
157157
text-align: center;
158158
}
159159

160+
#popup button {
161+
font-size: 1rem;
162+
padding: 7px 20px;
163+
border: 0;
164+
border-radius: 10px;
165+
background-color: lightgray;
166+
color: black;
167+
cursor: pointer;
168+
}
169+
160170
@media only screen and (max-width: 800px) {
161171
#drop-hint-text {
162172
display: none;

0 commit comments

Comments
 (0)