From a672aac8d817748cbbee8786a43520d682401ece Mon Sep 17 00:00:00 2001 From: Allan Lasser Date: Fri, 10 Jan 2025 13:09:09 -0500 Subject: [PATCH] Prevent navigation away when upload form has selections or is loading --- src/langs/json/en.json | 1 + src/lib/components/forms/DocumentUpload.svelte | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/langs/json/en.json b/src/langs/json/en.json index fec1b0c91..0c9bd44a8 100644 --- a/src/langs/json/en.json +++ b/src/langs/json/en.json @@ -144,6 +144,7 @@ "tesseract": "Tesseract is an open source OCR engine available free to all DocumentCloud accounts.", "textract": "Textract is a more powerful OCR engine that can better handle messy text, handwriting and fuzzy scans.", "empty": "Get started by selecting, pasting or dragging-and-dropping files", + "confirmLeave": "Your upload is not complete. Are you sure you want to leave the page?", "steps": { "ready": "Ready", "created": "Created", diff --git a/src/lib/components/forms/DocumentUpload.svelte b/src/lib/components/forms/DocumentUpload.svelte index e11a55e4b..9cfe9730c 100644 --- a/src/lib/components/forms/DocumentUpload.svelte +++ b/src/lib/components/forms/DocumentUpload.svelte @@ -41,6 +41,7 @@ progress through the three-part upload process. import { filesize } from "filesize"; import { onMount } from "svelte"; + import { beforeNavigate } from "$app/navigation"; import { _ } from "svelte-i18n"; import { Paperclip16, Paperclip24, Upload16 } from "svelte-octicons"; @@ -139,6 +140,14 @@ progress through the three-part upload process. addFiles(getFilesToUpload()); }); + beforeNavigate((navigation) => { + if (!empty && !loading) { + if (!window.confirm($_("uploadDialog.confirmLeave"))) { + navigation.cancel(); + } + } + }); + function uniqueId(): string { // random enough for a small set return Math.random().toString(32).slice(2);