Skip to content

Commit 2cb85a3

Browse files
committed
Warn user they will lose changes if they pick a new PDF
1 parent 056cebf commit 2cb85a3

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Main.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ function onPageLoad() {
3131

3232
parsePageUrl();
3333

34-
view.setOnPdfFileChosenListener(async function (pdfFile: File) {
34+
view.setOnPdfFileChosenListener(
35+
() => true,
36+
async function (pdfFile: File) {
37+
await loadPdfFromFile(pdfFile);
38+
}
39+
);
40+
41+
async function loadPdfFromFile(pdfFile: File) {
3542
await readFileAsArrayBuffer(pdfFile).then(function (fileData: ArrayBuffer) {
3643
loadPdf(pdfFile.name, fileData);
3744
});
38-
});
45+
}
3946

4047
function readFileAsArrayBuffer(file: File): Promise<ArrayBuffer> {
4148
return new Promise((resolve, reject) => {
@@ -155,6 +162,21 @@ function onPageLoad() {
155162

156163
view.enableNavButtons();
157164

165+
view.setOnPdfFileChosenListener(
166+
function () {
167+
const overlays: Overlays = extractOverlays();
168+
return (
169+
overlays.pagesOverlays.size == 0 ||
170+
confirm(
171+
"If you load another PDF, all changes in the current PDF will be lost. Are you sure?"
172+
)
173+
);
174+
},
175+
async function (pdfFile: File) {
176+
await loadPdfFromFile(pdfFile);
177+
}
178+
);
179+
158180
view.setOnNextClickedListener(function () {
159181
if (currentPage + 1 <= pdfDocument.getPageCount()) {
160182
gotoPage(currentPage + 1);

src/View.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,17 @@ export class View {
300300
}
301301

302302
public setOnPdfFileChosenListener(
303+
canChoosePdfPrecheck: () => boolean,
303304
onPdfFileChosen: (pdfFile: File) => Promise<void>
304305
) {
305306
const pdfInput = document.getElementById(
306307
"pdf-file-input"
307308
) as HTMLInputElement;
308-
pdfInput.onclick = function () {
309+
pdfInput.onclick = function (e: Event) {
309310
pdfInput.value = "";
311+
if (!canChoosePdfPrecheck()) {
312+
e.preventDefault();
313+
}
310314
};
311315
pdfInput.onchange = async function (ev: Event) {
312316
const input = ev.target as HTMLInputElement;

0 commit comments

Comments
 (0)