Skip to content

Commit

Permalink
Add confirmation dialogue if leaving playground (gradio-app#8871)
Browse files Browse the repository at this point in the history
* add dialogue

* remove

* add changeset

* add changeset

* share button logic

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
aliabd and gradio-pr-bot authored Jul 23, 2024
1 parent 70b45f4 commit 7f1a78c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-buckets-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"website": patch
---

feat:Add confirmation dialogue if leaving playground
31 changes: 31 additions & 0 deletions js/_website/src/lib/components/DemosLite.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@
});
let copied_link = false;
let shared = false;
async function copy_link(name: string) {
let code_b64 = btoa(code);
name = name.replaceAll(" ", "_");
await navigator.clipboard.writeText(
`${$page.url.href.split("?")[0]}?demo=${name}&code=${code_b64}`
);
copied_link = true;
shared = true;
setTimeout(() => (copied_link = false), 2000);
}
Expand Down Expand Up @@ -113,6 +115,34 @@
}
}
}
function show_dialog(
current_demos: typeof demos,
original_demos: typeof demos,
has_shared: boolean
) {
let changes =
!(JSON.stringify(current_demos) === JSON.stringify(original_demos)) &&
!has_shared;
if (browser) {
if (changes) {
window.onbeforeunload = function () {
return true;
};
} else {
window.onbeforeunload = function () {
return null;
};
}
}
}
let demos_copy: typeof demos = JSON.parse(JSON.stringify(demos));
$: show_dialog(demos, demos_copy, shared);
$: if (code) {
shared = false;
}
</script>

<svelte:head>
Expand Down Expand Up @@ -160,6 +190,7 @@

<Code
bind:value={demos[i].code}
on:input={() => console.log("input")}
label=""
language="python"
target={dummy_elem}
Expand Down

0 comments on commit 7f1a78c

Please sign in to comment.