mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
[JS] Fix few bugs present in the pdf for issue #14862
- since resetForm function reset a field value a calculateNow is consequently triggered. But the calculate callback can itself call resetForm, hence an infinite recursive loop. So basically, prevent calculeNow to be triggered by itself. - in Firefox, the letters entered in some fields were duplicated: "AaBb" instead of "AB". It was mainly because beforeInput was triggering a Keystroke which was itself triggering an input value update and then the input event was triggered. So in order to avoid that, beforeInput calls preventDefault and then it's up to the JS to handle the event. - fields have a property valueAsString which returns the value as a string. In the implementation it was wrongly used to store the formatted value of a field (2€ when the user entered 2). So this patch implements correctly valueAsString. - non-rendered fields can be updated in using JS but when they're, they must take some properties in the annotationStorage. It was implemented for field values, but it wasn't for display, colors, ... - it fixes #14862 and #14705.
This commit is contained in:
parent
85b7e60425
commit
094ff38da0
14 changed files with 558 additions and 205 deletions
|
@ -820,8 +820,8 @@ class Doc extends PDFObject {
|
|||
/* Not implemented */
|
||||
}
|
||||
|
||||
getField(cName) {
|
||||
if (typeof cName === "object") {
|
||||
_getField(cName) {
|
||||
if (cName && typeof cName === "object") {
|
||||
cName = cName.cName;
|
||||
}
|
||||
if (typeof cName !== "string") {
|
||||
|
@ -859,6 +859,14 @@ class Doc extends PDFObject {
|
|||
return null;
|
||||
}
|
||||
|
||||
getField(cName) {
|
||||
const field = this._getField(cName);
|
||||
if (!field) {
|
||||
return null;
|
||||
}
|
||||
return field.wrapped;
|
||||
}
|
||||
|
||||
_getChildren(fieldName) {
|
||||
// Children of foo.bar are foo.bar.oof, foo.bar.rab
|
||||
// but not foo.bar.oof.FOO.
|
||||
|
@ -889,7 +897,7 @@ class Doc extends PDFObject {
|
|||
}
|
||||
|
||||
getNthFieldName(nIndex) {
|
||||
if (typeof nIndex === "object") {
|
||||
if (nIndex && typeof nIndex === "object") {
|
||||
nIndex = nIndex.nIndex;
|
||||
}
|
||||
if (typeof nIndex !== "number") {
|
||||
|
@ -1027,7 +1035,7 @@ class Doc extends PDFObject {
|
|||
bAnnotations = true,
|
||||
printParams = null
|
||||
) {
|
||||
if (typeof bUI === "object") {
|
||||
if (bUI && typeof bUI === "object") {
|
||||
nStart = bUI.nStart;
|
||||
nEnd = bUI.nEnd;
|
||||
bSilent = bUI.bSilent;
|
||||
|
@ -1103,30 +1111,52 @@ class Doc extends PDFObject {
|
|||
}
|
||||
|
||||
resetForm(aFields = null) {
|
||||
if (aFields && !Array.isArray(aFields) && typeof aFields === "object") {
|
||||
// Handle the case resetForm({ aFields: ... })
|
||||
if (aFields && typeof aFields === "object") {
|
||||
aFields = aFields.aFields;
|
||||
}
|
||||
|
||||
if (aFields && !Array.isArray(aFields)) {
|
||||
aFields = [aFields];
|
||||
}
|
||||
|
||||
let mustCalculate = false;
|
||||
let fieldsToReset;
|
||||
if (aFields) {
|
||||
fieldsToReset = [];
|
||||
for (const fieldName of aFields) {
|
||||
if (!fieldName) {
|
||||
continue;
|
||||
}
|
||||
const field = this.getField(fieldName);
|
||||
if (typeof fieldName !== "string") {
|
||||
// In Acrobat if a fieldName is not a string all the fields are reset.
|
||||
fieldsToReset = null;
|
||||
break;
|
||||
}
|
||||
const field = this._getField(fieldName);
|
||||
if (!field) {
|
||||
continue;
|
||||
}
|
||||
field.value = field.defaultValue;
|
||||
field.valueAsString = field.value;
|
||||
fieldsToReset.push(field);
|
||||
mustCalculate = true;
|
||||
}
|
||||
} else {
|
||||
mustCalculate = this._fields.size !== 0;
|
||||
for (const field of this._fields.values()) {
|
||||
field.value = field.defaultValue;
|
||||
field.valueAsString = field.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!fieldsToReset) {
|
||||
fieldsToReset = this._fields.values();
|
||||
mustCalculate = this._fields.size !== 0;
|
||||
}
|
||||
|
||||
for (const field of fieldsToReset) {
|
||||
field.obj.value = field.obj.defaultValue;
|
||||
this._send({
|
||||
id: field.obj._id,
|
||||
value: field.obj.defaultValue,
|
||||
formattedValue: null,
|
||||
selRange: [0, 0],
|
||||
});
|
||||
}
|
||||
|
||||
if (mustCalculate) {
|
||||
this.calculateNow();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue