Merge pull request #13001 from Snuffleupagus/AnnotationStorage-serializable

Send the `AnnotationStorage`-data to the worker-thread as a `Map`
This commit is contained in:
Tim van der Meij 2021-02-19 00:01:44 +01:00 committed by GitHub
commit 3d80c21a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 133 additions and 89 deletions

View File

@ -348,9 +348,10 @@ class Annotation {
} }
isHidden(annotationStorage) { isHidden(annotationStorage) {
const data = annotationStorage && annotationStorage[this.data.id]; const storageEntry =
if (data && "hidden" in data) { annotationStorage && annotationStorage.get(this.data.id);
return data.hidden; if (storageEntry && storageEntry.hidden !== undefined) {
return storageEntry.hidden;
} }
return this._hasFlag(this.flags, AnnotationFlag.HIDDEN); return this._hasFlag(this.flags, AnnotationFlag.HIDDEN);
} }
@ -1206,8 +1207,11 @@ class WidgetAnnotation extends Annotation {
} }
async save(evaluator, task, annotationStorage) { async save(evaluator, task, annotationStorage) {
const value = if (!annotationStorage) {
annotationStorage[this.data.id] && annotationStorage[this.data.id].value; return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const value = storageEntry && storageEntry.value;
if (value === this.data.fieldValue || value === undefined) { if (value === this.data.fieldValue || value === undefined) {
return null; return null;
} }
@ -1289,8 +1293,8 @@ class WidgetAnnotation extends Annotation {
if (!annotationStorage || isPassword) { if (!annotationStorage || isPassword) {
return null; return null;
} }
let value = const storageEntry = annotationStorage.get(this.data.id);
annotationStorage[this.data.id] && annotationStorage[this.data.id].value; let value = storageEntry && storageEntry.value;
if (value === undefined) { if (value === undefined) {
// The annotation hasn't been rendered so use the appearance // The annotation hasn't been rendered so use the appearance
return null; return null;
@ -1769,9 +1773,8 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
} }
if (annotationStorage) { if (annotationStorage) {
const value = const storageEntry = annotationStorage.get(this.data.id);
annotationStorage[this.data.id] && const value = storageEntry && storageEntry.value;
annotationStorage[this.data.id].value;
if (value === undefined) { if (value === undefined) {
return super.getOperatorList( return super.getOperatorList(
evaluator, evaluator,
@ -1826,8 +1829,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
} }
async _saveCheckbox(evaluator, task, annotationStorage) { async _saveCheckbox(evaluator, task, annotationStorage) {
const value = if (!annotationStorage) {
annotationStorage[this.data.id] && annotationStorage[this.data.id].value; return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const value = storageEntry && storageEntry.value;
if (value === undefined) { if (value === undefined) {
return null; return null;
} }
@ -1869,8 +1875,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
} }
async _saveRadioButton(evaluator, task, annotationStorage) { async _saveRadioButton(evaluator, task, annotationStorage) {
const value = if (!annotationStorage) {
annotationStorage[this.data.id] && annotationStorage[this.data.id].value; return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const value = storageEntry && storageEntry.value;
if (value === undefined) { if (value === undefined) {
return null; return null;
} }

View File

@ -91,10 +91,7 @@ class AnnotationStorage {
} }
getAll() { getAll() {
if (this._storage.size === 0) { return this._storage.size > 0 ? objectFromEntries(this._storage) : null;
return null;
}
return objectFromEntries(this._storage);
} }
get size() { get size() {
@ -121,6 +118,14 @@ class AnnotationStorage {
} }
} }
} }
/**
* PLEASE NOTE: Only intended for usage within the API itself.
* @ignore
*/
get serializable() {
return this._storage.size > 0 ? this._storage : null;
}
} }
export { AnnotationStorage }; export { AnnotationStorage };

View File

@ -1214,7 +1214,7 @@ class PDFPageProxy {
pageIndex: this._pageIndex, pageIndex: this._pageIndex,
intent: renderingIntent, intent: renderingIntent,
renderInteractiveForms: renderInteractiveForms === true, renderInteractiveForms: renderInteractiveForms === true,
annotationStorage: annotationStorage?.getAll() || null, annotationStorage: annotationStorage?.serializable || null,
}); });
} }
@ -2613,7 +2613,7 @@ class WorkerTransport {
return this.messageHandler return this.messageHandler
.sendWithPromise("SaveDocument", { .sendWithPromise("SaveDocument", {
numPages: this._numPages, numPages: this._numPages,
annotationStorage: annotationStorage?.getAll() || null, annotationStorage: annotationStorage?.serializable || null,
filename: this._fullReader?.filename ?? null, filename: this._fullReader?.filename ?? null,
}) })
.finally(() => { .finally(() => {

View File

@ -1672,9 +1672,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, { value: "test\\print" });
annotationStorage[id] = { value: "test\\print" };
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -1709,9 +1709,11 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, {
annotationStorage[id] = { value: "こんにちは世界の" }; value: "こんにちは世界の",
});
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -1756,7 +1758,8 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
return annotation.getOperatorList( return annotation.getOperatorList(
partialEvaluator, partialEvaluator,
task, task,
@ -1797,9 +1800,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, { value: "test (print)" });
annotationStorage[id] = { value: "test (print)" };
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -1834,9 +1837,11 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, {
annotationStorage[id] = { value: "こんにちは世界の" }; value: "こんにちは世界の",
});
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -1873,9 +1878,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, { value: "mypassword" });
annotationStorage[id] = { value: "mypassword" };
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -1906,13 +1911,13 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, {
annotationStorage[id] = {
value: value:
"a aa aaa aaaa aaaaa aaaaaa " + "a aa aaa aaaa aaaaa aaaaaa " +
"pneumonoultramicroscopicsilicovolcanoconiosis", "pneumonoultramicroscopicsilicovolcanoconiosis",
}; });
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -1954,9 +1959,11 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, {
annotationStorage[id] = { value: "こんにちは世界の" }; value: "こんにちは世界の",
});
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -2014,9 +2021,8 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, {
annotationStorage[id] = {
value: value:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r" + "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r" +
"Aliquam vitae felis ac lectus bibendum ultricies quis non diam.\n" + "Aliquam vitae felis ac lectus bibendum ultricies quis non diam.\n" +
@ -2026,7 +2032,8 @@ describe("annotation", function () {
"Nulla consectetur, ligula in tincidunt placerat, " + "Nulla consectetur, ligula in tincidunt placerat, " +
"velit augue consectetur orci, sed mattis libero nunc ut massa.\r" + "velit augue consectetur orci, sed mattis libero nunc ut massa.\r" +
"Etiam facilisis tempus interdum.", "Etiam facilisis tempus interdum.",
}; });
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -2058,9 +2065,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, { value: "aa(aa)a\\" });
annotationStorage[id] = { value: "aa(aa)a\\" };
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -2101,9 +2108,11 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, {
annotationStorage[id] = { value: "こんにちは世界の" }; value: "こんにちは世界の",
});
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -2138,8 +2147,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: "hello world" }; annotationStorage.set(annotation.data.id, { value: "hello world" });
return annotation.save(partialEvaluator, task, annotationStorage); return annotation.save(partialEvaluator, task, annotationStorage);
}, done.fail) }, done.fail)
.then(data => { .then(data => {
@ -2270,8 +2280,11 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: "こんにちは世界の" }; annotationStorage.set(annotation.data.id, {
value: "こんにちは世界の",
});
return annotation.save(partialEvaluator, task, annotationStorage); return annotation.save(partialEvaluator, task, annotationStorage);
}, done.fail) }, done.fail)
.then(data => { .then(data => {
@ -2440,8 +2453,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: true }; annotationStorage.set(annotation.data.id, { value: true });
return annotation.getOperatorList( return annotation.getOperatorList(
partialEvaluator, partialEvaluator,
task, task,
@ -2498,8 +2512,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: true }; annotationStorage.set(annotation.data.id, { value: true });
return Promise.all([ return Promise.all([
annotation, annotation,
annotation.getOperatorList( annotation.getOperatorList(
@ -2523,8 +2538,9 @@ describe("annotation", function () {
return annotation; return annotation;
}, done.fail) }, done.fail)
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: false }; annotationStorage.set(annotation.data.id, { value: false });
return annotation.getOperatorList( return annotation.getOperatorList(
partialEvaluator, partialEvaluator,
task, task,
@ -2581,8 +2597,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: true }; annotationStorage.set(annotation.data.id, { value: true });
return Promise.all([ return Promise.all([
annotation, annotation,
annotation.getOperatorList( annotation.getOperatorList(
@ -2606,8 +2623,9 @@ describe("annotation", function () {
return annotation; return annotation;
}) })
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: true }; annotationStorage.set(annotation.data.id, { value: true });
return annotation.getOperatorList( return annotation.getOperatorList(
partialEvaluator, partialEvaluator,
task, task,
@ -2665,7 +2683,8 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
return annotation.getOperatorList( return annotation.getOperatorList(
partialEvaluator, partialEvaluator,
task, task,
@ -2713,8 +2732,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: true }; annotationStorage.set(annotation.data.id, { value: true });
return Promise.all([ return Promise.all([
annotation, annotation,
annotation.save(partialEvaluator, task, annotationStorage), annotation.save(partialEvaluator, task, annotationStorage),
@ -2732,8 +2752,9 @@ describe("annotation", function () {
return annotation; return annotation;
}, done.fail) }, done.fail)
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: false }; annotationStorage.set(annotation.data.id, { value: false });
return annotation.save(partialEvaluator, task, annotationStorage); return annotation.save(partialEvaluator, task, annotationStorage);
}, done.fail) }, done.fail)
.then(data => { .then(data => {
@ -2875,8 +2896,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: true }; annotationStorage.set(annotation.data.id, { value: true });
return Promise.all([ return Promise.all([
annotation, annotation,
annotation.getOperatorList( annotation.getOperatorList(
@ -2900,8 +2922,9 @@ describe("annotation", function () {
return annotation; return annotation;
}, done.fail) }, done.fail)
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: false }; annotationStorage.set(annotation.data.id, { value: false });
return annotation.getOperatorList( return annotation.getOperatorList(
partialEvaluator, partialEvaluator,
task, task,
@ -2959,7 +2982,8 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
return annotation.getOperatorList( return annotation.getOperatorList(
partialEvaluator, partialEvaluator,
task, task,
@ -3018,8 +3042,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: true }; annotationStorage.set(annotation.data.id, { value: true });
return Promise.all([ return Promise.all([
annotation, annotation,
annotation.save(partialEvaluator, task, annotationStorage), annotation.save(partialEvaluator, task, annotationStorage),
@ -3044,8 +3069,9 @@ describe("annotation", function () {
return annotation; return annotation;
}, done.fail) }, done.fail)
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: false }; annotationStorage.set(annotation.data.id, { value: false });
return annotation.save(partialEvaluator, task, annotationStorage); return annotation.save(partialEvaluator, task, annotationStorage);
}, done.fail) }, done.fail)
.then(data => { .then(data => {
@ -3089,8 +3115,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: true }; annotationStorage.set(annotation.data.id, { value: true });
return Promise.all([ return Promise.all([
annotation, annotation,
annotation.save(partialEvaluator, task, annotationStorage), annotation.save(partialEvaluator, task, annotationStorage),
@ -3130,7 +3157,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
return annotation.save(partialEvaluator, task, {}); const annotationStorage = new Map();
return annotation.save(partialEvaluator, task, annotationStorage);
}) })
.then(data => { .then(data => {
expect(data).toEqual(null); expect(data).toEqual(null);
@ -3535,9 +3564,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const id = annotation.data.id; const annotationStorage = new Map();
const annotationStorage = {}; annotationStorage.set(annotation.data.id, { value: "a value" });
annotationStorage[id] = { value: "a value" };
return annotation._getAppearance( return annotation._getAppearance(
partialEvaluator, partialEvaluator,
task, task,
@ -3571,8 +3600,9 @@ describe("annotation", function () {
idFactoryMock idFactoryMock
) )
.then(annotation => { .then(annotation => {
const annotationStorage = {}; const annotationStorage = new Map();
annotationStorage[annotation.data.id] = { value: "C" }; annotationStorage.set(annotation.data.id, { value: "C" });
return annotation.save(partialEvaluator, task, annotationStorage); return annotation.save(partialEvaluator, task, annotationStorage);
}, done.fail) }, done.fail)
.then(data => { .then(data => {