From 1553ca84980eb99dd2c0aedfc45759a5814acadd Mon Sep 17 00:00:00 2001 From: Jim Pravetz Date: Fri, 19 Jul 2019 09:04:40 -0700 Subject: [PATCH] Fixes to get field heirarchy working --- .gitignore | 4 ++++ demo/out-acroform.pdf | 0 demo/test-acroform.js | 10 +++++----- lib/mixins/acroform.js | 21 +++++++++------------ tests/unit/acroform.spec.js | 10 +++++----- 5 files changed, 23 insertions(+), 22 deletions(-) delete mode 100644 demo/out-acroform.pdf diff --git a/.gitignore b/.gitignore index 589f9e8..5cfc154 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ demo/bundle.js coverage tests/integration/__pdfs__ tests/integration/pdfmake/__pdfs__ +tests/integration/**/__snapshots__/* +tests/integration/__snapshots__/* +*.snap +/*.pdf diff --git a/demo/out-acroform.pdf b/demo/out-acroform.pdf deleted file mode 100644 index e69de29..0000000 diff --git a/demo/test-acroform.js b/demo/test-acroform.js index eb96ab4..4f47f85 100755 --- a/demo/test-acroform.js +++ b/demo/test-acroform.js @@ -17,10 +17,10 @@ doc.font('Helvetica') // establishes the default font doc.initAcroForm(); let rootField = doc.field('rootField'); -let child1Field = doc.field('child1Field', { parent: rootField }); -let child2Field = doc.field('child2Field', { parent: rootField }); -doc.formText('leaf1', 10, 10, 200, 40, { parent: child1Field }) -doc.formText('leaf2', 10, 60, 200, 40, { parent: child1Field }) -doc.formText('leaf3', 10, 110, 200, 40, { parent: child2Field }) +let child1Field = doc.field('child1Field', { Parent: rootField }); +let child2Field = doc.field('child2Field', { Parent: rootField }); +doc.formText('leaf1', 10, 10, 200, 40, { Parent: child1Field }) +doc.formText('leaf2', 10, 60, 200, 40, { Parent: child1Field }) +doc.formText('leaf3', 10, 110, 200, 40, { Parent: child2Field }) doc.end(); diff --git a/lib/mixins/acroform.js b/lib/mixins/acroform.js index 52512e2..1411ebe 100644 --- a/lib/mixins/acroform.js +++ b/lib/mixins/acroform.js @@ -74,32 +74,29 @@ export default { field (name, options = {}) { let fieldDict = this._fieldDict(name, options); let fieldRef = this.ref(fieldDict); - this._addToParent(fieldRef, options.parent); - delete options.parent; + this._addToParent(fieldRef); return fieldRef; }, widgetAnnot (name, x, y, w, h, options = {}) { - let fieldDict = this._fieldDict(name, null, options); + let fieldDict = this._fieldDict(name, options); fieldDict.Subtype = 'Widget'; fieldDict.F = 4; - let parent = options.parent; - delete options.parent; // Add Field annot to page, and get it's ref this.annotate(x, y, w, h, fieldDict); let annotRef = this.page.annotations[this.page.annotations.length - 1]; - return this._addToParent(annotRef, parent); + return this._addToParent(annotRef); }, - _addToParent (fieldRef, parentRef) { - if (parentRef) { - if (!parentRef.data.Kids) { - parentRef.data.Kids = []; + _addToParent (fieldRef) { + let parent = fieldRef.data.Parent; + if (parent) { + if (!parent.data.Kids) { + parent.data.Kids = []; } - parentRef.data.Kids.push(fieldRef); - fieldRef.data.Parent = parentRef; + parent.data.Kids.push(fieldRef); } else { this._root.data.AcroForm.data.Fields.push(fieldRef); } diff --git a/tests/unit/acroform.spec.js b/tests/unit/acroform.spec.js index 502b057..5e710af 100644 --- a/tests/unit/acroform.spec.js +++ b/tests/unit/acroform.spec.js @@ -66,11 +66,11 @@ describe('AcroForm', () => { doc.initAcroForm() let rootField = doc.field('rootField'); - let child1Field = doc.field('child1Field', { parent: rootField }); - let child2Field = doc.field('child2Field', { parent: rootField }); - doc.formText('leaf1', 10, 10, 200, 40, { parent: child1Field }) - doc.formText('leaf2', 10, 60, 200, 40, { parent: child1Field }) - doc.formText('leaf3', 10, 110, 200, 40, { parent: child2Field }) + let child1Field = doc.field('child1Field', { Parent: rootField }); + let child2Field = doc.field('child2Field', { Parent: rootField }); + doc.formText('leaf1', 10, 10, 200, 40, { Parent: child1Field }) + doc.formText('leaf2', 10, 60, 200, 40, { Parent: child1Field }) + doc.formText('leaf3', 10, 110, 200, 40, { Parent: child2Field }) doc.end();