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
This commit is contained in:
Tim van der Meij 2020-08-22 14:29:00 +02:00
parent 5fed7112a2
commit 36e149800e
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -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);
}