Resolve filename in typescript

This commit is contained in:
Edward Shen 2022-01-16 01:01:38 -08:00
parent 732e2bd2f3
commit 18ee415f46
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -16,7 +16,16 @@
// Exported to main.rs // Exported to main.rs
function loadFromDb(mimeType: string, name?: string, language?: string) { function loadFromDb(mimeType: string, name?: string, language?: string) {
console.log("[js] Got name:", name); let resolvedName;
if (name) {
resolvedName = name;
} else {
const pathName = window.location.pathname;
const leafIndex = pathName.indexOf("/");
resolvedName = pathName.slice(leafIndex + 1);
}
console.log("[js] Resolved name:", resolvedName);
console.log("[js] Got language:", language); console.log("[js] Got language:", language);
console.log("[js] Got mime type:", mimeType); console.log("[js] Got mime type:", mimeType);
@ -31,22 +40,22 @@ function loadFromDb(mimeType: string, name?: string, language?: string) {
const data = (evt.target as IDBRequest).result; const data = (evt.target as IDBRequest).result;
switch (data.type) { switch (data.type) {
case "string": case "string":
createStringPasteUi(data, mimeType, name, language); createStringPasteUi(data, mimeType, resolvedName, language);
break; break;
case "blob": case "blob":
createBlobPasteUi(data, name); createBlobPasteUi(data, resolvedName);
break; break;
case "image": case "image":
createImagePasteUi(data, name); createImagePasteUi(data, resolvedName);
break; break;
case "audio": case "audio":
createAudioPasteUi(data, name); createAudioPasteUi(data, resolvedName);
break; break;
case "video": case "video":
createVideoPasteUi(data, name); createVideoPasteUi(data, resolvedName);
break; break;
case "archive": case "archive":
createArchivePasteUi(data, name); createArchivePasteUi(data, resolvedName);
break; break;
default: default:
renderMessage("Something went wrong. Try clearing local data."); renderMessage("Something went wrong. Try clearing local data.");
@ -74,7 +83,7 @@ function loadFromDb(mimeType: string, name?: string, language?: string) {
}; };
} }
function createStringPasteUi(data, mimeType: string, name?: string, lang?: string) { function createStringPasteUi(data, mimeType: string, name: string, lang?: string) {
const bodyEle = document.getElementsByTagName("body")[0]; const bodyEle = document.getElementsByTagName("body")[0];
bodyEle.textContent = ''; bodyEle.textContent = '';
@ -154,7 +163,7 @@ function createStringPasteUi(data, mimeType: string, name?: string, lang?: strin
hljs.initLineNumbersOnLoad(); hljs.initLineNumbersOnLoad();
} }
function createBlobPasteUi(data, name?: string) { function createBlobPasteUi(data, name: string) {
const bodyEle = document.getElementsByTagName("body")[0]; const bodyEle = document.getElementsByTagName("body")[0];
bodyEle.textContent = ''; bodyEle.textContent = '';
@ -186,14 +195,14 @@ function createBlobPasteUi(data, name?: string) {
displayAnywayEle.onclick = () => { displayAnywayEle.onclick = () => {
data.data.text().then(text => { data.data.text().then(text => {
data.data = text; data.data = text;
createStringPasteUi(data, "application/octet-stream"); createStringPasteUi(data, "application/octet-stream", name);
}) })
}; };
mainEle.appendChild(displayAnywayEle); mainEle.appendChild(displayAnywayEle);
bodyEle.appendChild(mainEle); bodyEle.appendChild(mainEle);
} }
function createImagePasteUi({ expiration, data, file_size }, name?: string) { function createImagePasteUi({ expiration, data, file_size }, name: string) {
createMultiMediaPasteUi("img", expiration, data, name, (downloadEle, imgEle) => { createMultiMediaPasteUi("img", expiration, data, name, (downloadEle, imgEle) => {
imgEle.onload = () => { imgEle.onload = () => {
const width = imgEle.naturalWidth || imgEle.width; const width = imgEle.naturalWidth || imgEle.width;
@ -203,15 +212,15 @@ function createImagePasteUi({ expiration, data, file_size }, name?: string) {
}); });
} }
function createAudioPasteUi({ expiration, data }, name?: string) { function createAudioPasteUi({ expiration, data }, name: string) {
createMultiMediaPasteUi("audio", expiration, data, name, "Download"); createMultiMediaPasteUi("audio", expiration, data, name, "Download");
} }
function createVideoPasteUi({ expiration, data }, name?: string) { function createVideoPasteUi({ expiration, data }, name: string) {
createMultiMediaPasteUi("video", expiration, data, name, "Download"); createMultiMediaPasteUi("video", expiration, data, name, "Download");
} }
function createArchivePasteUi({ expiration, data, entries }, name?: string) { function createArchivePasteUi({ expiration, data, entries }, name: string) {
const bodyEle = document.getElementsByTagName("body")[0]; const bodyEle = document.getElementsByTagName("body")[0];
bodyEle.textContent = ''; bodyEle.textContent = '';
@ -272,7 +281,7 @@ function createArchivePasteUi({ expiration, data, entries }, name?: string) {
bodyEle.appendChild(mainEle); bodyEle.appendChild(mainEle);
} }
function createMultiMediaPasteUi(tag, expiration, data, name?: string, on_create?: Function | string) { function createMultiMediaPasteUi(tag, expiration, data, name: string, on_create?: Function | string) {
const bodyEle = document.getElementsByTagName("body")[0]; const bodyEle = document.getElementsByTagName("body")[0];
bodyEle.textContent = ''; bodyEle.textContent = '';