From 9660eea0d28c99c03e9feb712d2d4e82f5bc901d Mon Sep 17 00:00:00 2001 From: Marc MOREAU Date: Thu, 3 Oct 2024 09:11:38 +0200 Subject: [PATCH] Improve acro checkbox set value - base check value on all widgets, when there are multiple widgets - check possibility on widget --- .gitignore | 3 ++- package.json | 5 +++-- src/api/form/PDFCheckBox.ts | 7 ++++--- src/core/acroform/PDFAcroCheckBox.ts | 12 +++++++----- yarn.lock | 28 ++++++++++++++++++++++++---- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 7b6199b96..e8821d2ac 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ isolate*.log out.pdf *.tgz -.vscode/settings.json \ No newline at end of file +.vscode/settings.json +/.idea diff --git a/package.json b/package.json index 090b03eac..dfa35a256 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "Bj Tecu (https://github.com/btecu)", "Brent McSharry (https://github.com/mcshaz)", "Tim Knapp (https://github.com/duffyd)", - "Ching Chang (https://github.com/ChingChang9)" + "Ching Chang (https://github.com/ChingChang9)", + "Marc Moreau (https://github.com/MockingMagician)" ], "scripts": { "release:latest": "yarn publish --tag latest && yarn pack && yarn release:tag", @@ -106,7 +107,7 @@ "flamebearer": "^1.1.3", "http-server": "^0.12.3", "jest": "^26.0.1", - "node-fetch": "^2.6.0", + "node-fetch": "^2.6.7", "prettier": "^2.0.5", "rimraf": "^3.0.2", "rollup": "^2.17.1", diff --git a/src/api/form/PDFCheckBox.ts b/src/api/form/PDFCheckBox.ts index 1060b4276..db8c7dfa7 100644 --- a/src/api/form/PDFCheckBox.ts +++ b/src/api/form/PDFCheckBox.ts @@ -81,8 +81,9 @@ export default class PDFCheckBox extends PDFField { * streams will display a check mark inside the widgets of this check box * field. */ - check() { - const onValue = this.acroField.getOnValue() ?? PDFName.of('Yes'); + check(widgetIndex = 0) { + const onValue = + this.acroField.getOnValue()[widgetIndex] || PDFName.of('Yes'); this.markAsDirty(); this.acroField.setValue(onValue); } @@ -119,7 +120,7 @@ export default class PDFCheckBox extends PDFField { */ isChecked(): boolean { const onValue = this.acroField.getOnValue(); - return !!onValue && onValue === this.acroField.getValue(); + return onValue.length > 0 && onValue.includes(this.acroField.getValue()); } /** diff --git a/src/core/acroform/PDFAcroCheckBox.ts b/src/core/acroform/PDFAcroCheckBox.ts index b1dc2bdb5..21ad7306b 100644 --- a/src/core/acroform/PDFAcroCheckBox.ts +++ b/src/core/acroform/PDFAcroCheckBox.ts @@ -19,8 +19,9 @@ class PDFAcroCheckBox extends PDFAcroButton { }; setValue(value: PDFName) { - const onValue = this.getOnValue() ?? PDFName.of('Yes'); - if (value !== onValue && value !== PDFName.of('Off')) { + const onValue = + this.getOnValue().length > 0 ? this.getOnValue() : [PDFName.of('Yes')]; + if (!onValue.includes(value) && value !== PDFName.of('Off')) { throw new InvalidAcroFieldValueError(); } @@ -40,9 +41,10 @@ class PDFAcroCheckBox extends PDFAcroButton { return PDFName.of('Off'); } - getOnValue(): PDFName | undefined { - const [widget] = this.getWidgets(); - return widget?.getOnValue(); + getOnValue(): PDFName[] { + return this.getWidgets() + .map((widget) => widget.getOnValue()) + .filter((v) => v !== undefined) as PDFName[]; } } diff --git a/yarn.lock b/yarn.lock index 520d474b0..786137c47 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3004,10 +3004,12 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -node-fetch@^2.6.0: - version "2.6.1" - resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" - integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw== +node-fetch@^2.6.7: + version "2.7.0" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d" + integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A== + dependencies: + whatwg-url "^5.0.0" node-int64@^0.4.0: version "0.4.0" @@ -4039,6 +4041,11 @@ tr46@^2.0.2: dependencies: punycode "^2.1.1" +tr46@~0.0.3: + version "0.0.3" + resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" + integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw== + trim-right@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003" @@ -4283,6 +4290,11 @@ walker@^1.0.7, walker@~1.0.5: dependencies: makeerror "1.0.x" +webidl-conversions@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" + integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== + webidl-conversions@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff" @@ -4305,6 +4317,14 @@ whatwg-mimetype@^2.3.0: resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf" integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g== +whatwg-url@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d" + integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw== + dependencies: + tr46 "~0.0.3" + webidl-conversions "^3.0.0" + whatwg-url@^8.0.0: version "8.1.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-8.1.0.tgz#c628acdcf45b82274ce7281ee31dd3c839791771"