Make download button js-less most of the time
This commit is contained in:
parent
e854d88d89
commit
8142d69bb7
|
@ -15,13 +15,17 @@ const { t } = await getI18n(Astro.locals.currentLocale);
|
||||||
|
|
||||||
{/* ------------------------------------------- HTML ------------------------------------------- */}
|
{/* ------------------------------------------- HTML ------------------------------------------- */}
|
||||||
|
|
||||||
<download-button
|
{
|
||||||
href={href}
|
useBlob ? (
|
||||||
filename={filename}
|
<download-button href={href} filename={filename} class="when-js when-no-print">
|
||||||
class="when-js when-no-print"
|
|
||||||
data-use-blob={useBlob}>
|
|
||||||
<Button title={t("global.downloadButton")} icon="material-symbols:download" />
|
<Button title={t("global.downloadButton")} icon="material-symbols:download" />
|
||||||
</download-button>
|
</download-button>
|
||||||
|
) : (
|
||||||
|
<a href={href} class="when-no-print">
|
||||||
|
<Button title={t("global.downloadButton")} icon="material-symbols:download" />
|
||||||
|
</a>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
{/* ------------------------------------------- JS --------------------------------------------- */}
|
{/* ------------------------------------------- JS --------------------------------------------- */}
|
||||||
|
|
||||||
|
@ -31,19 +35,13 @@ const { t } = await getI18n(Astro.locals.currentLocale);
|
||||||
customElement("download-button", (elem) => {
|
customElement("download-button", (elem) => {
|
||||||
const href = elem.getAttribute("href");
|
const href = elem.getAttribute("href");
|
||||||
const filename = elem.getAttribute("filename");
|
const filename = elem.getAttribute("filename");
|
||||||
const useBlob = elem.hasAttribute("data-use-blob");
|
|
||||||
|
|
||||||
if (!href || !filename) return;
|
if (!href || !filename) return;
|
||||||
|
|
||||||
elem.addEventListener("click", async () => {
|
elem.addEventListener("click", async () => {
|
||||||
let url;
|
|
||||||
if (useBlob) {
|
|
||||||
const res = await fetch(href);
|
const res = await fetch(href);
|
||||||
const blob = await res.blob();
|
const blob = await res.blob();
|
||||||
url = window.URL.createObjectURL(blob);
|
const url = window.URL.createObjectURL(blob);
|
||||||
} else {
|
|
||||||
url = href;
|
|
||||||
}
|
|
||||||
|
|
||||||
var link = document.createElement("a");
|
var link = document.createElement("a");
|
||||||
link.download = filename;
|
link.download = filename;
|
||||||
|
|
Loading…
Reference in New Issue