Merge pull request #12637 from calixteman/buttons

JS -- Add support for buttons
This commit is contained in:
Brendan Dahl 2020-12-22 22:18:37 -08:00 committed by GitHub
commit d060cd2a61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 280 additions and 24 deletions

View File

@ -1944,18 +1944,19 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
getFieldObject() {
let type = "button";
let value = null;
let exportValues;
if (this.data.checkBox) {
type = "checkbox";
value = this.data.fieldValue && this.data.fieldValue !== "Off";
exportValues = this.data.exportValue;
} else if (this.data.radioButton) {
type = "radiobutton";
value = this.data.fieldValue === this.data.buttonValue;
exportValues = this.data.buttonValue;
}
return {
id: this.data.id,
value,
value: this.data.fieldValue || null,
defaultValue: this.data.defaultFieldValue,
exportValues,
editable: !this.data.readOnly,
name: this.data.fieldName,
rect: this.data.rect,
@ -2036,6 +2037,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
editable: !this.data.readOnly,
name: this.data.fieldName,
rect: this.data.rect,
numItems: this.data.fieldValue.length,
multipleSelection: this.data.multiSelect,
hidden: this.data.hidden,
actions: this.data.actions,

View File

@ -653,6 +653,9 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
value() {
elementData.userValue = detail.value || "";
storage.setValue(id, { value: elementData.userValue.toString() });
if (!elementData.formattedValue) {
event.target.value = elementData.userValue;
}
},
valueAsString() {
elementData.formattedValue = detail.valueAsString || "";

View File

@ -92,6 +92,9 @@ class EventDispatcher {
if (source.obj._isButton()) {
source.obj._id = id;
event.value = source.obj._getExportValue(event.value);
if (name === "Action") {
source.obj._value = event.value;
}
}
if (name === "Keystroke") {

View File

@ -47,7 +47,7 @@ class Field extends PDFObject {
this.highlight = data.highlight;
this.lineWidth = data.lineWidth;
this.multiline = data.multiline;
this.multipleSelection = data.multipleSelection;
this.multipleSelection = !!data.multipleSelection;
this.name = data.name;
this.numItems = data.numItems;
this.page = data.page;
@ -66,15 +66,12 @@ class Field extends PDFObject {
this.textSize = data.textSize;
this.type = data.type;
this.userName = data.userName;
this.value = data.value || "";
// Need getter/setter
this._valueAsString = data.valueAsString;
// Private
this._document = data.doc;
this._value = data.value || "";
this._valueAsString = data.valueAsString;
this._actions = createActionsMap(data.actions);
this._fillColor = data.fillColor || ["T"];
this._strokeColor = data.strokeColor || ["G", 0];
this._textColor = data.textColor || ["G", 0];
@ -112,6 +109,16 @@ class Field extends PDFObject {
}
}
get value() {
return this._value;
}
set value(value) {
if (!this.multipleSelection) {
this._value = value;
}
}
get valueAsString() {
return this._valueAsString;
}
@ -120,6 +127,16 @@ class Field extends PDFObject {
this._valueAsString = val ? val.toString() : "";
}
checkThisBox(nWidget, bCheckIt = true) {}
isBoxChecked(nWidget) {
return false;
}
isDefaultChecked(nWidget) {
return false;
}
setAction(cTrigger, cScript) {
if (typeof cTrigger !== "string" || typeof cScript !== "string") {
return;
@ -159,4 +176,121 @@ class Field extends PDFObject {
}
}
export { Field };
class RadioButtonField extends Field {
constructor(otherButtons, data) {
super(data);
this.exportValues = [this.exportValues];
this._radioIds = [this._id];
this._radioActions = [this._actions];
for (const radioData of otherButtons) {
this.exportValues.push(radioData.exportValues);
this._radioIds.push(radioData.id);
this._radioActions.push(createActionsMap(radioData.actions));
if (this._value === radioData.exportValues) {
this._id = radioData.id;
}
}
}
get value() {
return this._value;
}
set value(value) {
const i = this.exportValues.indexOf(value);
if (0 <= i && i < this._radioIds.length) {
this._id = this._radioIds[i];
this._value = value;
} else if (value === "Off" && this._radioIds.length === 2) {
const nextI = (1 + this._radioIds.indexOf(this._id)) % 2;
this._id = this._radioIds[nextI];
this._value = this.exportValues[nextI];
}
}
checkThisBox(nWidget, bCheckIt = true) {
if (nWidget < 0 || nWidget >= this._radioIds.length || !bCheckIt) {
return;
}
this._id = this._radioIds[nWidget];
this._value = this.exportValues[nWidget];
this._send({ id: this._id, value: this._value });
}
isBoxChecked(nWidget) {
return (
nWidget >= 0 &&
nWidget < this._radioIds.length &&
this._id === this._radioIds[nWidget]
);
}
isDefaultChecked(nWidget) {
return (
nWidget >= 0 &&
nWidget < this.exportValues.length &&
this.defaultValue === this.exportValues[nWidget]
);
}
_getExportValue(state) {
const i = this._radioIds.indexOf(this._id);
return this.exportValues[i];
}
_runActions(event) {
const i = this._radioIds.indexOf(this._id);
this._actions = this._radioActions[i];
return super._runActions(event);
}
_isButton() {
return true;
}
}
class CheckboxField extends RadioButtonField {
get value() {
return this._value;
}
set value(value) {
if (value === "Off") {
this._value = "Off";
} else {
super.value = value;
}
}
_getExportValue(state) {
return state ? super._getExportValue(state) : "Off";
}
isBoxChecked(nWidget) {
if (this._value === "Off") {
return false;
}
return super.isBoxChecked(nWidget);
}
isDefaultChecked(nWidget) {
if (this.defaultValue === "Off") {
return this._value === "Off";
}
return super.isDefaultChecked(nWidget);
}
checkThisBox(nWidget, bCheckIt = true) {
if (nWidget < 0 || nWidget >= this._radioIds.length) {
return;
}
this._id = this._radioIds[nWidget];
this._value = bCheckIt ? this.exportValues[nWidget] : "Off";
this._send({ id: this._id, value: this._value });
}
}
export { CheckboxField, Field, RadioButtonField };

View File

@ -26,12 +26,12 @@ import {
Trans,
ZoomType,
} from "./constants.js";
import { CheckboxField, Field, RadioButtonField } from "./field.js";
import { AForm } from "./aform.js";
import { App } from "./app.js";
import { Color } from "./color.js";
import { Console } from "./console.js";
import { Doc } from "./doc.js";
import { Field } from "./field.js";
import { ProxyHandler } from "./proxy.js";
import { Util } from "./util.js";
@ -74,10 +74,23 @@ function initSandbox(params) {
obj.send = send;
obj.globalEval = globalEval;
obj.doc = _document.wrapped;
const field = new Field(obj);
let field;
if (obj.type === "radiobutton") {
const otherButtons = objs.slice(1);
field = new RadioButtonField(otherButtons, obj);
} else if (obj.type === "checkbox") {
const otherButtons = objs.slice(1);
field = new CheckboxField(otherButtons, obj);
} else {
field = new Field(obj);
}
const wrapped = new Proxy(field, proxyHandler);
doc._addField(name, wrapped);
app._objects[obj.id] = { obj: field, wrapped };
const _object = { obj: field, wrapped };
for (const object of objs) {
app._objects[object.id] = _object;
}
}
}

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
const { closePages, loadAndWait } = require("./test_utils.js");
const { clearInput, closePages, loadAndWait } = require("./test_utils.js");
describe("Interaction", () => {
describe("in 160F-2019.pdf", () => {
@ -65,11 +65,7 @@ describe("Interaction", () => {
.toEqual("visible");
// Clear the textfield
await page.click("#\\34 16R");
await page.keyboard.down("Control");
await page.keyboard.press("A");
await page.keyboard.up("Control");
await page.keyboard.press("Backspace");
await clearInput(page, "#\\34 16R");
// and leave it
await page.click("#\\34 19R");
@ -109,10 +105,7 @@ describe("Interaction", () => {
expect(text).withContext(`In ${browserName}`).toEqual("61803");
// Clear the textfield
await page.keyboard.down("Control");
await page.keyboard.press("A");
await page.keyboard.up("Control");
await page.keyboard.press("Backspace");
await clearInput(page, "#\\34 48R");
await page.type("#\\34 48R", "1.61803", { delay: 200 });
await page.click("#\\34 19R");
@ -194,4 +187,97 @@ describe("Interaction", () => {
);
});
});
describe("in js-buttons.pdf", () => {
let pages;
beforeAll(async () => {
pages = await loadAndWait("js-buttons.pdf", "#\\38 0R");
});
afterAll(async () => {
await closePages(pages);
});
it("must show values in a text input when clicking on radio buttons", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);
const expected = [
["#\\36 8R", "Group1=Choice1::1"],
["#\\36 9R", "Group1=Choice2::2"],
["#\\37 0R", "Group1=Choice3::3"],
["#\\37 1R", "Group1=Choice4::4"],
];
for (const [selector, expectedText] of expected) {
// Clear the textfield
await clearInput(page, "#\\36 7R");
await page.click(selector);
await page.waitForFunction(
`document.querySelector("#\\\\36 7R").value !== ""`
);
const text = await page.$eval("#\\36 7R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual(expectedText);
}
})
);
});
it("must show values in a text input when clicking on checkboxes", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
const expected = [
["#\\37 2R", "Check1=Yes::5"],
["#\\37 4R", "Check2=Yes::6"],
["#\\37 5R", "Check3=Yes::7"],
["#\\37 6R", "Check4=Yes::8"],
["#\\37 2R", "Check1=Off::5"],
["#\\37 4R", "Check2=Off::6"],
["#\\37 5R", "Check3=Off::7"],
["#\\37 6R", "Check4=Off::8"],
];
for (const [selector, expectedText] of expected) {
// Clear the textfield
await clearInput(page, "#\\36 7R");
await page.click(selector);
await page.waitForFunction(
`document.querySelector("#\\\\36 7R").value !== ""`
);
const text = await page.$eval("#\\36 7R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual(expectedText);
}
})
);
});
it("must show values in a text input when clicking on checkboxes in a group", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
const expected = [
["#\\37 7R", "Check5=Yes1::9"],
["#\\37 8R", "Check5=Yes2::10"],
["#\\37 9R", "Check5=Yes3::11"],
["#\\38 0R", "Check5=Yes4::12"],
["#\\38 0R", "Check5=Off::12"],
];
for (const [selector, expectedText] of expected) {
// Clear the textfield
await clearInput(page, "#\\36 7R");
await page.click(selector);
await page.waitForFunction(
`document.querySelector("#\\\\36 7R").value !== ""`
);
const text = await page.$eval("#\\36 7R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual(expectedText);
}
})
);
});
});
});

View File

@ -34,3 +34,11 @@ exports.closePages = pages =>
await page.close();
})
);
exports.clearInput = async (page, selector) => {
await page.click(selector);
await page.keyboard.down("Control");
await page.keyboard.press("A");
await page.keyboard.up("Control");
await page.keyboard.press("Backspace");
};

View File

@ -365,6 +365,7 @@
!issue6108.pdf
!issue6113.pdf
!openoffice.pdf
!js-buttons.pdf
!issue7014.pdf
!issue8187.pdf
!annotation-link-text-popup.pdf

BIN
test/pdfs/js-buttons.pdf Normal file

Binary file not shown.

View File

@ -4298,6 +4298,12 @@
"rounds": 1,
"type": "eq"
},
{ "id": "js-buttons",
"file": "pdfs/js-buttons.pdf",
"md5": "2c56d419c1fb533349fd1ddef3f14da6",
"rounds": 1,
"type": "eq"
},
{ "id": "issue2956",
"file": "pdfs/issue2956.pdf",
"md5": "d8f68cbbb4bf54cde9f7f878acb6d7cd",