gradio/js/wasm/svelte/DownloadLink.svelte
pngwn 561579d9b7
fix-tests (#7345)
* fix-tests

* [tmp] Comment-out

* Fix the URL constructor calls in `resolve_wasm_src()`, `should_proxy_wasm_src()`, and `<DownloadLink />` to handle relative URLs

* Remove a circular dependency between lite/index.ts and lite/custom-element/index.ts to solve a bug that the dev app is mounted twice sometimes

* Fix js/app/test/image_component_events.spec.ts

* Set the `testIgnore` in `.config/playwright.config.js`

* Fix the Lite dev mode only to create an app and expose the controller for Playwright, without editors etc.

* add changeset

* Set the mocked ruff version as 0.2.2

* Extend timeout

* Fix to use the built lite files instead of the dev server

* add changeset

* comment out failed tests

* Revert "comment out failed tests"

This reverts commit 3580d79887.

* Fix the Gellery component to work in Wasm

* Fix js/app/test/file_explorer_component_events.spec.ts to run on Wasm

* Ignore queue_full_e2e_test.spec.ts

* Revert "[tmp] Comment-out"

This reverts commit c775c0cc29.

* Revert "Extend timeout"

This reverts commit 742d1e1e83.

* Remove a commented out line

* Refactor file_explorer_component_events.spec.ts

* Revert "fix-tests", restoring the original test-functional.yml content

This reverts commit 9ff2a7ddc5.

* Set CI step names

* [tmp] Revert "Revert "fix-tests", restoring the original test-functional.yml content"

This reverts commit de2dbe3317.

* Revert "[tmp] Revert "Revert "fix-tests", restoring the original test-functional.yml content""

This reverts commit 32154f3bb1.

* [tmp] Revert "Revert "[tmp] Revert "Revert "fix-tests", restoring the original test-functional.yml content"""

This reverts commit 204075e190.

* Fix vite.config.js removing unnecessary code

* Revert "Set the `testIgnore` in `.config/playwright.config.js`"

This reverts commit 98dccc5be9.

* Add gallery_component_events.spec.ts

* Revert js/app/test

* tweak

* tweak

* revert workflow changes

* add changeset

---------

Co-authored-by: Yuichiro Tachibana (Tsuchiya) <t.yic.yt@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
2024-03-04 14:03:46 +00:00

91 lines
2.1 KiB
Svelte

<script lang="ts">
import type { HTMLAnchorAttributes } from "svelte/elements";
import { createEventDispatcher } from "svelte";
interface DownloadLinkAttributes
extends Omit<HTMLAnchorAttributes, "target"> {
download: NonNullable<HTMLAnchorAttributes["download"]>;
}
type $$Props = DownloadLinkAttributes;
import { getWorkerProxyContext } from "./context";
import { should_proxy_wasm_src } from "./file-url";
import { getHeaderValue } from "../src/http";
export let href: DownloadLinkAttributes["href"] = undefined;
export let download: DownloadLinkAttributes["download"];
const dispatch = createEventDispatcher();
let is_downloading = false;
const worker_proxy = getWorkerProxyContext();
async function wasm_click_handler(): Promise<void> {
if (is_downloading) {
return;
}
dispatch("click");
if (href == null) {
throw new Error("href is not defined.");
}
if (worker_proxy == null) {
throw new Error("Wasm worker proxy is not available.");
}
const url = new URL(href, window.location.href);
const path = url.pathname;
is_downloading = true;
worker_proxy
.httpRequest({
method: "GET",
path,
headers: {},
query_string: ""
})
.then((response) => {
if (response.status !== 200) {
throw new Error(`Failed to get file ${path} from the Wasm worker.`);
}
const blob = new Blob([response.body], {
type: getHeaderValue(response.headers, "content-type")
});
const blobUrl = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = blobUrl;
link.download = download;
link.click();
URL.revokeObjectURL(blobUrl);
})
.finally(() => {
is_downloading = false;
});
}
</script>
{#if worker_proxy && should_proxy_wasm_src(href)}
{#if is_downloading}
<slot />
{:else}
<a {...$$restProps} {href} on:click|preventDefault={wasm_click_handler}>
<slot />
</a>
{/if}
{:else}
<a
{href}
target={typeof window !== "undefined" && window.__is_colab__
? "_blank"
: null}
rel="noopener noreferrer"
{download}
{...$$restProps}
on:click={dispatch.bind(null, "click")}
>
<slot />
</a>
{/if}