1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

confirm if leaving a modified form without saving

This commit is contained in:
Aki Sasaki 2020-08-18 13:50:23 -07:00
parent 965d20db2a
commit 83365a3756
4 changed files with 94 additions and 6 deletions

View file

@ -19,6 +19,14 @@
class AnnotationStorage {
constructor() {
this._storage = new Map();
this._modified = false;
// Callbacks to signal when the modification state is set or reset.
// This is used by the viewer to only bind on `beforeunload` if forms
// are actually edited to prevent doing so unconditionally since that
// can have undesirable efffects.
this.onSetModified = null;
this.onResetModified = null;
}
/**
@ -49,6 +57,9 @@ class AnnotationStorage {
* @param {Object} value
*/
setValue(key, value) {
if (this._storage.get(key) !== value) {
this.setModified();
}
this._storage.set(key, value);
}
@ -62,6 +73,24 @@ class AnnotationStorage {
get size() {
return this._storage.size;
}
setModified() {
if (!this._modified) {
this._modified = true;
if (typeof this.onSetModified === "function") {
this.onSetModified();
}
}
}
resetModified() {
if (this._modified) {
this._modified = false;
if (typeof this.onResetModified === "function") {
this.onResetModified();
}
}
}
}
export { AnnotationStorage };

View file

@ -2536,12 +2536,16 @@ class WorkerTransport {
}
saveDocument(annotationStorage) {
return this.messageHandler.sendWithPromise("SaveDocument", {
numPages: this._numPages,
annotationStorage:
(annotationStorage && annotationStorage.getAll()) || null,
filename: this._fullReader.filename,
});
return this.messageHandler
.sendWithPromise("SaveDocument", {
numPages: this._numPages,
annotationStorage:
(annotationStorage && annotationStorage.getAll()) || null,
filename: this._fullReader.filename,
})
.finally(() => {
annotationStorage.resetModified();
});
}
getDestinations() {