From 36e149800e17ae72463307f3e56fd4723c2373e5 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sat, 22 Aug 2020 14:29:00 +0200 Subject: [PATCH] Unconditionally set the field value for choice widgets in the annotation storage This commit makes the following improvements: - The code is similar to the other interactive form widgets now, with a clear note for the only difference. - Calling `getOrCreateValue` unconditionally ensures that choice widgets always have a value in the annotation storage. Previously we only inserted a value in the annotation storage when an option matched or when a selection was changed. However, this causes breakage when saving/printing because comboboxes, which we don't fully support yet but are rendered, might not have a value in storage at all. Their field value might not match any option since it allows the user to enter a custom value. - Calling `getOrCreateValue` unconditionally ensures that forms with choice widgets no longer always trigger a warning when the user navigates away from the page. This fixes https://github.com/mozilla/pdf.js/pull/12241#discussion_r474279654 --- src/display/annotation_layer.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index c1c6b4207..795617615 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -665,6 +665,18 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement { const storage = this.annotationStorage; const id = this.data.id; + // For printing/saving we currently only support choice widgets with one + // option selection. Therefore, listboxes (#12189) and comboboxes (#12224) + // are not properly printed/saved yet, so we only store the first item in + // the field value array instead of the entire array. Once support for those + // two field types is implemented, we should use the same pattern as the + // other interactive widgets where the return value of `getOrCreateValue` is + // used and the full array of field values is stored. + storage.getOrCreateValue( + id, + this.data.fieldValue.length > 0 ? this.data.fieldValue[0] : null + ); + const selectElement = document.createElement("select"); selectElement.disabled = this.data.readOnly; selectElement.name = this.data.fieldName; @@ -684,7 +696,6 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement { optionElement.value = option.exportValue; if (this.data.fieldValue.includes(option.exportValue)) { optionElement.setAttribute("selected", true); - storage.setValue(id, option.exportValue); } selectElement.appendChild(optionElement); }