@docengine/pdf-renderer 0.0.0-stage → 0.0.106
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -3
- package/dist/.tsbuildinfo +1 -0
- package/dist/browser.d.ts +16 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +20 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.sf-stub.d.ts +9 -0
- package/dist/browser.sf-stub.d.ts.map +1 -0
- package/dist/browser.sf-stub.js +19 -0
- package/dist/browser.sf-stub.js.map +1 -0
- package/dist/document-pdf-definition-browser.d.ts +10 -0
- package/dist/document-pdf-definition-browser.d.ts.map +1 -0
- package/dist/document-pdf-definition-browser.js +9 -0
- package/dist/document-pdf-definition-browser.js.map +1 -0
- package/dist/document-pdf-definition-factory.d.ts +29 -0
- package/dist/document-pdf-definition-factory.d.ts.map +1 -0
- package/dist/document-pdf-definition-factory.js +170 -0
- package/dist/document-pdf-definition-factory.js.map +1 -0
- package/dist/document-pdf-definition-node.d.ts +10 -0
- package/dist/document-pdf-definition-node.d.ts.map +1 -0
- package/dist/document-pdf-definition-node.js +9 -0
- package/dist/document-pdf-definition-node.js.map +1 -0
- package/dist/document-pdf-definition-preview-browser.d.ts +9 -0
- package/dist/document-pdf-definition-preview-browser.d.ts.map +1 -0
- package/dist/document-pdf-definition-preview-browser.js +4 -0
- package/dist/document-pdf-definition-preview-browser.js.map +1 -0
- package/dist/document-pdf-definition-preview-node.d.ts +9 -0
- package/dist/document-pdf-definition-preview-node.d.ts.map +1 -0
- package/dist/document-pdf-definition-preview-node.js +4 -0
- package/dist/document-pdf-definition-preview-node.js.map +1 -0
- package/dist/document-pdf-definition-preview.d.ts +13 -0
- package/dist/document-pdf-definition-preview.d.ts.map +1 -0
- package/dist/document-pdf-definition-preview.js +42 -0
- package/dist/document-pdf-definition-preview.js.map +1 -0
- package/dist/fonts-browser-registry.d.ts +13 -0
- package/dist/fonts-browser-registry.d.ts.map +1 -0
- package/dist/fonts-browser-registry.js +7 -0
- package/dist/fonts-browser-registry.js.map +1 -0
- package/dist/fonts-browser-vfs.d.ts +12 -0
- package/dist/fonts-browser-vfs.d.ts.map +1 -0
- package/dist/fonts-browser-vfs.js +4 -0
- package/dist/fonts-browser-vfs.js.map +1 -0
- package/dist/fonts-browser-vfs.sf-stub.d.ts +5 -0
- package/dist/fonts-browser-vfs.sf-stub.d.ts.map +1 -0
- package/dist/fonts-browser-vfs.sf-stub.js +5 -0
- package/dist/fonts-browser-vfs.sf-stub.js.map +1 -0
- package/dist/fonts-browser.d.ts +22 -0
- package/dist/fonts-browser.d.ts.map +1 -0
- package/dist/fonts-browser.js +22 -0
- package/dist/fonts-browser.js.map +1 -0
- package/dist/fonts-registry.d.ts +17 -0
- package/dist/fonts-registry.d.ts.map +1 -0
- package/dist/fonts-registry.js +83 -0
- package/dist/fonts-registry.js.map +1 -0
- package/dist/fonts-registry.test.d.ts +2 -0
- package/dist/fonts-registry.test.d.ts.map +1 -0
- package/dist/fonts-registry.test.js +22 -0
- package/dist/fonts-registry.test.js.map +1 -0
- package/dist/fonts.d.ts +16 -0
- package/dist/fonts.d.ts.map +1 -0
- package/dist/fonts.js +17 -0
- package/dist/fonts.js.map +1 -0
- package/dist/generate-html.d.ts +33 -0
- package/dist/generate-html.d.ts.map +1 -0
- package/dist/generate-html.js +90 -0
- package/dist/generate-html.js.map +1 -0
- package/dist/generate-html.test.d.ts +2 -0
- package/dist/generate-html.test.d.ts.map +1 -0
- package/dist/generate-html.test.js +270 -0
- package/dist/generate-html.test.js.map +1 -0
- package/dist/generate-pdf-browser.d.ts +14 -0
- package/dist/generate-pdf-browser.d.ts.map +1 -0
- package/dist/generate-pdf-browser.js +55 -0
- package/dist/generate-pdf-browser.js.map +1 -0
- package/dist/generate-pdf.d.ts +10 -0
- package/dist/generate-pdf.d.ts.map +1 -0
- package/dist/generate-pdf.js +36 -0
- package/dist/generate-pdf.js.map +1 -0
- package/dist/html-environment.d.ts +10 -0
- package/dist/html-environment.d.ts.map +1 -0
- package/dist/html-environment.js +30 -0
- package/dist/html-environment.js.map +1 -0
- package/dist/html-text.d.ts +46 -0
- package/dist/html-text.d.ts.map +1 -0
- package/dist/html-text.js +451 -0
- package/dist/html-text.js.map +1 -0
- package/dist/html-text.test.d.ts +2 -0
- package/dist/html-text.test.d.ts.map +1 -0
- package/dist/html-text.test.js +79 -0
- package/dist/html-text.test.js.map +1 -0
- package/dist/image-prefetch.d.ts +7 -0
- package/dist/image-prefetch.d.ts.map +1 -0
- package/dist/image-prefetch.js +50 -0
- package/dist/image-prefetch.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/map-editor-pdf-options.d.ts +13 -0
- package/dist/map-editor-pdf-options.d.ts.map +1 -0
- package/dist/map-editor-pdf-options.js +28 -0
- package/dist/map-editor-pdf-options.js.map +1 -0
- package/dist/map-editor-pdf-options.test.d.ts +2 -0
- package/dist/map-editor-pdf-options.test.d.ts.map +1 -0
- package/dist/map-editor-pdf-options.test.js +16 -0
- package/dist/map-editor-pdf-options.test.js.map +1 -0
- package/dist/merge-document.d.ts +3 -0
- package/dist/merge-document.d.ts.map +1 -0
- package/dist/merge-document.js +29 -0
- package/dist/merge-document.js.map +1 -0
- package/dist/multipage-renderer.d.ts +31 -0
- package/dist/multipage-renderer.d.ts.map +1 -0
- package/dist/multipage-renderer.js +131 -0
- package/dist/multipage-renderer.js.map +1 -0
- package/dist/pdf-horizontal-rule.d.ts +18 -0
- package/dist/pdf-horizontal-rule.d.ts.map +1 -0
- package/dist/pdf-horizontal-rule.js +46 -0
- package/dist/pdf-horizontal-rule.js.map +1 -0
- package/dist/pdf-inline-field-background.d.ts +19 -0
- package/dist/pdf-inline-field-background.d.ts.map +1 -0
- package/dist/pdf-inline-field-background.js +106 -0
- package/dist/pdf-inline-field-background.js.map +1 -0
- package/dist/pdf-inline-field-background.test.d.ts +2 -0
- package/dist/pdf-inline-field-background.test.d.ts.map +1 -0
- package/dist/pdf-inline-field-background.test.js +22 -0
- package/dist/pdf-inline-field-background.test.js.map +1 -0
- package/dist/pdf-renderer.test.d.ts +2 -0
- package/dist/pdf-renderer.test.d.ts.map +1 -0
- package/dist/pdf-renderer.test.js +2614 -0
- package/dist/pdf-renderer.test.js.map +1 -0
- package/dist/pdfmake.sf-stub.d.ts +11 -0
- package/dist/pdfmake.sf-stub.d.ts.map +1 -0
- package/dist/pdfmake.sf-stub.js +16 -0
- package/dist/pdfmake.sf-stub.js.map +1 -0
- package/dist/preview-dom-to-pdf.d.ts +25 -0
- package/dist/preview-dom-to-pdf.d.ts.map +1 -0
- package/dist/preview-dom-to-pdf.js +694 -0
- package/dist/preview-dom-to-pdf.js.map +1 -0
- package/dist/preview-dom-to-pdf.test.d.ts +2 -0
- package/dist/preview-dom-to-pdf.test.d.ts.map +1 -0
- package/dist/preview-dom-to-pdf.test.js +734 -0
- package/dist/preview-dom-to-pdf.test.js.map +1 -0
- package/dist/preview-field-plugins.d.ts +6 -0
- package/dist/preview-field-plugins.d.ts.map +1 -0
- package/dist/preview-field-plugins.js +37 -0
- package/dist/preview-field-plugins.js.map +1 -0
- package/dist/repeatable-section-header.d.ts +39 -0
- package/dist/repeatable-section-header.d.ts.map +1 -0
- package/dist/repeatable-section-header.js +145 -0
- package/dist/repeatable-section-header.js.map +1 -0
- package/dist/repeatable-table-pagination.d.ts +17 -0
- package/dist/repeatable-table-pagination.d.ts.map +1 -0
- package/dist/repeatable-table-pagination.js +255 -0
- package/dist/repeatable-table-pagination.js.map +1 -0
- package/dist/repeatable-table-pagination.test.d.ts +2 -0
- package/dist/repeatable-table-pagination.test.d.ts.map +1 -0
- package/dist/repeatable-table-pagination.test.js +110 -0
- package/dist/repeatable-table-pagination.test.js.map +1 -0
- package/dist/rich-text-pdf-styles.d.ts +15 -0
- package/dist/rich-text-pdf-styles.d.ts.map +1 -0
- package/dist/rich-text-pdf-styles.js +30 -0
- package/dist/rich-text-pdf-styles.js.map +1 -0
- package/dist/segment-renderer.d.ts +26 -0
- package/dist/segment-renderer.d.ts.map +1 -0
- package/dist/segment-renderer.js +1252 -0
- package/dist/segment-renderer.js.map +1 -0
- package/dist/style-mapper.d.ts +32 -0
- package/dist/style-mapper.d.ts.map +1 -0
- package/dist/style-mapper.js +124 -0
- package/dist/style-mapper.js.map +1 -0
- package/dist/style-mapper.test.d.ts +2 -0
- package/dist/style-mapper.test.d.ts.map +1 -0
- package/dist/style-mapper.test.js +77 -0
- package/dist/style-mapper.test.js.map +1 -0
- package/dist/table-column-width.d.ts +20 -0
- package/dist/table-column-width.d.ts.map +1 -0
- package/dist/table-column-width.js +56 -0
- package/dist/table-column-width.js.map +1 -0
- package/dist/table-column-width.test.d.ts +2 -0
- package/dist/table-column-width.test.d.ts.map +1 -0
- package/dist/table-column-width.test.js +40 -0
- package/dist/table-column-width.test.js.map +1 -0
- package/dist/table-layout.d.ts +44 -0
- package/dist/table-layout.d.ts.map +1 -0
- package/dist/table-layout.js +70 -0
- package/dist/table-layout.js.map +1 -0
- package/dist/types.d.ts +73 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/units.d.ts +29 -0
- package/dist/units.d.ts.map +1 -0
- package/dist/units.js +63 -0
- package/dist/units.js.map +1 -0
- package/package.json +36 -6
|
@@ -0,0 +1,2614 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { cellFieldId } from '@docengine/editor/node';
|
|
4
|
+
import { generateDocumentPdf, generatePdfFromTemplate, mergeTemplateAndDocument, renderDocumentToPdfDefinition, } from './index.js';
|
|
5
|
+
import { buildPdfTable, createPdfRenderContext, renderSegmentsToPdfContent, renderSegmentsToPdfProseBlocks, buildRepeatableSectionTitleNode, buildPdfSectionTitleNode } from './segment-renderer.js';
|
|
6
|
+
import { htmlToPdfBlocks, isBlockLevelHtml, plainTextNewlinesToBrHtml, plainTextToPdfText, stampExplicitPdfBold, finalizePdfInlineParts } from './html-text.js';
|
|
7
|
+
import { renderDocumentToPdfContent, shouldUseLegacyPdfExport } from './multipage-renderer.js';
|
|
8
|
+
import { VISION_TABLE_PDF_LAYOUT, VISION_TABLE_BORDERLESS_PDF_LAYOUT } from './table-layout.js';
|
|
9
|
+
import { marginMmToPt, mmToPt, resolvePageSize } from './units.js';
|
|
10
|
+
import { buildRepeatableSectionPageHeader, computeRepeatableHeaderBandHeightPt, HEADER_HEIGHT_BUFFER_PT, HEADER_LINE_SLACK_PT, } from './repeatable-section-header.js';
|
|
11
|
+
import { estimatePdfContentHeightPt } from './repeatable-table-pagination.js';
|
|
12
|
+
import { buildFontRegistry } from './fonts-browser-registry.js';
|
|
13
|
+
const defaultFontRegistry = buildFontRegistry({ preset: 'Roboto' });
|
|
14
|
+
const tableId = 'table_pdf_test';
|
|
15
|
+
const tableSchema = {
|
|
16
|
+
type: 'table',
|
|
17
|
+
name: 'Acuity',
|
|
18
|
+
label: 'Acuity',
|
|
19
|
+
columns: [
|
|
20
|
+
{ key: 'od', label: 'OD' },
|
|
21
|
+
{ key: 'os', label: 'OS' },
|
|
22
|
+
],
|
|
23
|
+
rows: [
|
|
24
|
+
{ key: 'row1', label: 'Row1' },
|
|
25
|
+
{ key: 'row2', label: 'Row2' },
|
|
26
|
+
],
|
|
27
|
+
cellType: 'text',
|
|
28
|
+
};
|
|
29
|
+
function makeTemplate() {
|
|
30
|
+
const cellOd = cellFieldId(tableId, 'row1', 'od');
|
|
31
|
+
const cellOs = cellFieldId(tableId, 'row1', 'os');
|
|
32
|
+
return {
|
|
33
|
+
kind: 'template',
|
|
34
|
+
version: 1,
|
|
35
|
+
time: Date.now(),
|
|
36
|
+
fieldSchemas: {
|
|
37
|
+
[tableId]: tableSchema,
|
|
38
|
+
[cellOd]: { type: 'text', label: 'OD', name: 'OD' },
|
|
39
|
+
[cellOs]: { type: 'text', label: 'OS', name: 'OS' },
|
|
40
|
+
exam_notes: { type: 'text', label: 'Notes', name: 'Notes' },
|
|
41
|
+
},
|
|
42
|
+
blocks: [
|
|
43
|
+
{
|
|
44
|
+
type: 'documentSection',
|
|
45
|
+
data: {
|
|
46
|
+
label: 'Examination',
|
|
47
|
+
name: 'Examination',
|
|
48
|
+
segments: [
|
|
49
|
+
{ type: 'text', content: 'Patient notes: ' },
|
|
50
|
+
{ type: 'field', id: 'exam_notes', placeholder: 'Notes' },
|
|
51
|
+
{ type: 'table', id: tableId, rows: tableSchema.rows },
|
|
52
|
+
],
|
|
53
|
+
fieldValues: {
|
|
54
|
+
exam_notes: '',
|
|
55
|
+
[cellOd]: '',
|
|
56
|
+
[cellOs]: '',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function makeDocument() {
|
|
64
|
+
return {
|
|
65
|
+
kind: 'field',
|
|
66
|
+
version: 2,
|
|
67
|
+
time: Date.now(),
|
|
68
|
+
sections: {
|
|
69
|
+
Examination: {
|
|
70
|
+
Notes: 'Stable findings',
|
|
71
|
+
Acuity: [{ od: '0.8', os: '0.9' }],
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
describe('mergeTemplateAndDocument', () => {
|
|
77
|
+
it('merges sectioned document values into template blocks', () => {
|
|
78
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), makeDocument());
|
|
79
|
+
const section = doc.blocks[0].data;
|
|
80
|
+
assert.equal(section.fieldValues.exam_notes, 'Stable findings');
|
|
81
|
+
assert.equal(section.fieldValues[cellFieldId(tableId, 'row1', 'od')], '0.8');
|
|
82
|
+
});
|
|
83
|
+
it('copies template pageSetup and repeatable section instances', () => {
|
|
84
|
+
const template = makeTemplate();
|
|
85
|
+
template.pageSetup = { format: 'letter', margin: 10 };
|
|
86
|
+
template.blocks.push({
|
|
87
|
+
type: 'documentSection',
|
|
88
|
+
data: {
|
|
89
|
+
label: 'Items',
|
|
90
|
+
name: 'Items',
|
|
91
|
+
repeatable: true,
|
|
92
|
+
segments: [{ type: 'field', id: 'items_name', placeholder: 'Name' }],
|
|
93
|
+
fieldValues: { items_name: '' },
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
template.fieldSchemas.items_name = { type: 'text', name: 'Name', label: 'Name' };
|
|
97
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
98
|
+
kind: 'field',
|
|
99
|
+
version: 2,
|
|
100
|
+
time: Date.now(),
|
|
101
|
+
sections: {
|
|
102
|
+
Examination: makeDocument().sections.Examination,
|
|
103
|
+
Items: [{ Name: 'A' }, { Name: 'B' }, { Name: 'C' }],
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
assert.equal(doc.pageSetup.format, 'letter');
|
|
107
|
+
assert.equal(doc.pageSetup.margin, 10);
|
|
108
|
+
assert.deepEqual(doc.repeatableSectionInstances?.Items?.length, 3);
|
|
109
|
+
assert.equal(doc.blocks.find((b) => b.data?.repeatable)?.data.fieldValues.items_name, 'A');
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
function findPdfTable(nodes) {
|
|
113
|
+
for (const node of nodes ?? []) {
|
|
114
|
+
if (node?.table)
|
|
115
|
+
return node;
|
|
116
|
+
if (node?.stack) {
|
|
117
|
+
const found = findPdfTable(node.stack);
|
|
118
|
+
if (found)
|
|
119
|
+
return found;
|
|
120
|
+
}
|
|
121
|
+
if (node?.columns) {
|
|
122
|
+
for (const col of node.columns) {
|
|
123
|
+
const found = findPdfTable(col.stack);
|
|
124
|
+
if (found)
|
|
125
|
+
return found;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
function findAllPdfTables(nodes) {
|
|
132
|
+
const tables = [];
|
|
133
|
+
function walk(list) {
|
|
134
|
+
for (const node of list ?? []) {
|
|
135
|
+
if (node?.table)
|
|
136
|
+
tables.push(node);
|
|
137
|
+
if (node?.stack)
|
|
138
|
+
walk(node.stack);
|
|
139
|
+
if (node?.columns) {
|
|
140
|
+
for (const col of node.columns) {
|
|
141
|
+
walk(col.stack);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
walk(nodes);
|
|
147
|
+
return tables;
|
|
148
|
+
}
|
|
149
|
+
function nodeContainsText(node, text) {
|
|
150
|
+
if (!node || typeof node !== 'object')
|
|
151
|
+
return false;
|
|
152
|
+
if (typeof node.text === 'string' && node.text.includes(text))
|
|
153
|
+
return true;
|
|
154
|
+
if (Array.isArray(node.text)) {
|
|
155
|
+
const joined = node.text
|
|
156
|
+
.map((part) => (typeof part === 'string' ? part : part?.text ?? ''))
|
|
157
|
+
.join('');
|
|
158
|
+
if (joined.includes(text))
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
if (Array.isArray(node.stack)) {
|
|
162
|
+
for (const child of node.stack) {
|
|
163
|
+
if (nodeContainsText(child, text))
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
function chunkContainsText(chunk, text) {
|
|
170
|
+
return nodeContainsText(chunk, text);
|
|
171
|
+
}
|
|
172
|
+
const defaultRenderOptions = {
|
|
173
|
+
resolveFontName: defaultFontRegistry.resolveFontName,
|
|
174
|
+
defaultFont: defaultFontRegistry.defaultFont,
|
|
175
|
+
fieldValueStyle: {},
|
|
176
|
+
};
|
|
177
|
+
function assertAllPartsUseFont(parts, expectedFont = 'Roboto') {
|
|
178
|
+
for (const part of parts) {
|
|
179
|
+
if (typeof part === 'string') {
|
|
180
|
+
assert.fail(`plain string part should have been stamped with font: ${part}`);
|
|
181
|
+
}
|
|
182
|
+
assert.equal(part.font, expectedFont, `expected font on part: ${part.text}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function headerContainsText(docDefinition, text) {
|
|
186
|
+
if (typeof docDefinition.header !== 'function')
|
|
187
|
+
return false;
|
|
188
|
+
return nodeContainsText(docDefinition.header(), text);
|
|
189
|
+
}
|
|
190
|
+
function nodeTextJoined(node) {
|
|
191
|
+
if (!node)
|
|
192
|
+
return '';
|
|
193
|
+
if (typeof node.text === 'string')
|
|
194
|
+
return node.text;
|
|
195
|
+
if (Array.isArray(node.text))
|
|
196
|
+
return joinPdfParts(node.text);
|
|
197
|
+
if (Array.isArray(node.parts))
|
|
198
|
+
return joinPdfParts(node.parts);
|
|
199
|
+
if (Array.isArray(node.stack))
|
|
200
|
+
return node.stack.map((child) => nodeTextJoined(child)).join('');
|
|
201
|
+
if (Array.isArray(node.columns)) {
|
|
202
|
+
return node.columns.map((col) => nodeTextJoined(col)).join('');
|
|
203
|
+
}
|
|
204
|
+
return '';
|
|
205
|
+
}
|
|
206
|
+
function joinPdfParts(parts) {
|
|
207
|
+
return parts
|
|
208
|
+
.map((part) => {
|
|
209
|
+
if (typeof part === 'string')
|
|
210
|
+
return part;
|
|
211
|
+
if (part?.lineBreak)
|
|
212
|
+
return '\n';
|
|
213
|
+
return part.text ?? '';
|
|
214
|
+
})
|
|
215
|
+
.join('');
|
|
216
|
+
}
|
|
217
|
+
function findHeaderBodyBlocks(stack) {
|
|
218
|
+
return stack.filter((node) => node.style !== 'sectionHeader' && node.text != null);
|
|
219
|
+
}
|
|
220
|
+
function pdfCellText(cell) {
|
|
221
|
+
if (cell == null || cell === '')
|
|
222
|
+
return '';
|
|
223
|
+
if (typeof cell === 'string')
|
|
224
|
+
return cell;
|
|
225
|
+
if (Array.isArray(cell.text)) {
|
|
226
|
+
return cell.text.map((part) => (typeof part === 'string' ? part : part.text ?? '')).join('');
|
|
227
|
+
}
|
|
228
|
+
return String(cell.text ?? '');
|
|
229
|
+
}
|
|
230
|
+
function assertEmptyPdfTableCell(cell, alignment) {
|
|
231
|
+
assert.equal(pdfCellText(cell), '');
|
|
232
|
+
if (alignment)
|
|
233
|
+
assert.equal(cell.alignment, alignment);
|
|
234
|
+
assert.equal(cell.lineHeight, 1);
|
|
235
|
+
}
|
|
236
|
+
describe('renderDocumentToPdfDefinition', () => {
|
|
237
|
+
it('builds a pdfmake definition with table header rows', () => {
|
|
238
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), makeDocument());
|
|
239
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, {
|
|
240
|
+
pageSetup: {
|
|
241
|
+
title: 'Document summary',
|
|
242
|
+
footer: { showPageNumbers: true },
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
const def = docDefinition;
|
|
246
|
+
assert.equal(def.defaultStyle.fontSize, 12);
|
|
247
|
+
assert.equal(def.defaultStyle.font, 'Roboto');
|
|
248
|
+
assert.equal(def.defaultStyle.lineHeight, 1.65);
|
|
249
|
+
assert.ok(Array.isArray(def.content));
|
|
250
|
+
const tableBlock = findPdfTable(def.content);
|
|
251
|
+
assert.ok(tableBlock);
|
|
252
|
+
assert.equal(tableBlock.table.headerRows, 1);
|
|
253
|
+
assert.equal(tableBlock.table.body[0][0].text, 'OD');
|
|
254
|
+
assert.equal(tableBlock.table.body[0][0].fontSize, 12);
|
|
255
|
+
assert.equal(def.styles.tableHeader.lineHeight, 1);
|
|
256
|
+
assert.equal(def.styles.tableBody.lineHeight, 1);
|
|
257
|
+
assert.equal(typeof def.header, 'undefined');
|
|
258
|
+
assert.ok(typeof def.footer === 'function');
|
|
259
|
+
});
|
|
260
|
+
it('renders pivotTable values as a pdfmake matrix (not an empty table)', () => {
|
|
261
|
+
const doc = {
|
|
262
|
+
fieldSchemas: {
|
|
263
|
+
sales_pivot: {
|
|
264
|
+
type: 'pivotTable',
|
|
265
|
+
name: 'Sales Pivot',
|
|
266
|
+
label: 'Sales',
|
|
267
|
+
rowProperty: 'Region',
|
|
268
|
+
columnProperty: 'Product',
|
|
269
|
+
valueProperty: 'Amount',
|
|
270
|
+
aggregation: 'sum',
|
|
271
|
+
showRowTotals: true,
|
|
272
|
+
showColumnTotals: true,
|
|
273
|
+
showGrandTotal: true,
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
blocks: [
|
|
277
|
+
{
|
|
278
|
+
type: 'documentSection',
|
|
279
|
+
data: {
|
|
280
|
+
name: 'Main',
|
|
281
|
+
label: 'Main',
|
|
282
|
+
segments: [{ type: 'table', id: 'sales_pivot' }],
|
|
283
|
+
fieldValues: {
|
|
284
|
+
sales_pivot: {
|
|
285
|
+
columns: [
|
|
286
|
+
{ key: 'a', label: 'A' },
|
|
287
|
+
{ key: 'b', label: 'B' },
|
|
288
|
+
],
|
|
289
|
+
rows: [
|
|
290
|
+
{ key: 'west', label: 'West', cells: { a: 13, b: 5 }, rowTotal: 18 },
|
|
291
|
+
{ key: 'east', label: 'East', cells: { a: 7, b: null }, rowTotal: 7 },
|
|
292
|
+
],
|
|
293
|
+
columnTotals: { a: 20, b: 5 },
|
|
294
|
+
grandTotal: 25,
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
],
|
|
300
|
+
};
|
|
301
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
302
|
+
resolveFontName: (n) => n || 'Roboto',
|
|
303
|
+
defaultFont: 'Roboto',
|
|
304
|
+
});
|
|
305
|
+
const tableBlock = findPdfTable(content);
|
|
306
|
+
assert.ok(tableBlock, 'expected a pivot pdf table');
|
|
307
|
+
assert.ok(tableBlock.table.body.length >= 3, 'header + data rows');
|
|
308
|
+
assert.equal(tableBlock.table.body[0][1].text, 'A');
|
|
309
|
+
assert.equal(tableBlock.table.body[1][0].text, 'West');
|
|
310
|
+
assert.equal(tableBlock.table.body[1][1].text, '13');
|
|
311
|
+
assert.equal(String(tableBlock.table.body[tableBlock.table.body.length - 1][3].text), '25');
|
|
312
|
+
});
|
|
313
|
+
it('omits empty pivotTable when hideEmptyValues is true', () => {
|
|
314
|
+
const doc = {
|
|
315
|
+
fieldSchemas: {
|
|
316
|
+
sales_pivot: {
|
|
317
|
+
type: 'pivotTable',
|
|
318
|
+
name: 'Sales Pivot',
|
|
319
|
+
label: 'Sales',
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
blocks: [
|
|
323
|
+
{
|
|
324
|
+
type: 'documentSection',
|
|
325
|
+
data: {
|
|
326
|
+
name: 'Main',
|
|
327
|
+
label: 'Main',
|
|
328
|
+
segments: [{ type: 'table', id: 'sales_pivot' }],
|
|
329
|
+
fieldValues: {
|
|
330
|
+
sales_pivot: { columns: [], rows: [] },
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
],
|
|
335
|
+
};
|
|
336
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
337
|
+
resolveFontName: (n) => n || 'Roboto',
|
|
338
|
+
defaultFont: 'Roboto',
|
|
339
|
+
hideEmptyValues: true,
|
|
340
|
+
});
|
|
341
|
+
assert.equal(findPdfTable(content), null);
|
|
342
|
+
});
|
|
343
|
+
it('uses pageSetup saved on the document', () => {
|
|
344
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), makeDocument());
|
|
345
|
+
doc.pageSetup = { format: 'letter', margin: 10 };
|
|
346
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, {});
|
|
347
|
+
assert.equal(docDefinition.pageSize, resolvePageSize('letter'));
|
|
348
|
+
assert.equal(docDefinition.pageOrientation, 'portrait');
|
|
349
|
+
assert.deepEqual(docDefinition.pageMargins, marginMmToPt([10, 10, 10, 10]));
|
|
350
|
+
});
|
|
351
|
+
it('applies landscape orientation from pageSetup', () => {
|
|
352
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), makeDocument());
|
|
353
|
+
doc.pageSetup = { format: 'a4', orientation: 'landscape', margin: 15 };
|
|
354
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, {});
|
|
355
|
+
assert.equal(docDefinition.pageSize, resolvePageSize('a4'));
|
|
356
|
+
assert.equal(docDefinition.pageOrientation, 'landscape');
|
|
357
|
+
});
|
|
358
|
+
it('uses custom page size from pageSetup', () => {
|
|
359
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), makeDocument());
|
|
360
|
+
doc.pageSetup = { format: 'custom', widthMm: 100, heightMm: 200, margin: 10 };
|
|
361
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, {});
|
|
362
|
+
assert.deepEqual(docDefinition.pageSize, resolvePageSize('custom', { widthMm: 100, heightMm: 200 }));
|
|
363
|
+
// Custom sizes bake orientation into pageSize; omit pageOrientation so pdfmake
|
|
364
|
+
// does not swap wide pages when orientation would be "portrait".
|
|
365
|
+
assert.equal(docDefinition.pageOrientation, undefined);
|
|
366
|
+
});
|
|
367
|
+
it('keeps wide custom pages (width > height) without swapping', () => {
|
|
368
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), makeDocument());
|
|
369
|
+
doc.pageSetup = {
|
|
370
|
+
format: 'custom',
|
|
371
|
+
widthMm: 100,
|
|
372
|
+
heightMm: 50,
|
|
373
|
+
orientation: 'portrait',
|
|
374
|
+
margin: 2,
|
|
375
|
+
};
|
|
376
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, {});
|
|
377
|
+
assert.deepEqual(docDefinition.pageSize, resolvePageSize('custom', { widthMm: 100, heightMm: 50 }, 'portrait'));
|
|
378
|
+
assert.ok(docDefinition.pageSize.width > docDefinition.pageSize.height);
|
|
379
|
+
assert.equal(docDefinition.pageOrientation, undefined);
|
|
380
|
+
});
|
|
381
|
+
it('swaps custom width/height when orientation is landscape', () => {
|
|
382
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), makeDocument());
|
|
383
|
+
doc.pageSetup = {
|
|
384
|
+
format: 'custom',
|
|
385
|
+
widthMm: 100,
|
|
386
|
+
heightMm: 50,
|
|
387
|
+
orientation: 'landscape',
|
|
388
|
+
margin: 2,
|
|
389
|
+
};
|
|
390
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, {});
|
|
391
|
+
assert.deepEqual(docDefinition.pageSize, resolvePageSize('custom', { widthMm: 100, heightMm: 50 }, 'landscape'));
|
|
392
|
+
assert.ok(docDefinition.pageSize.width < docDefinition.pageSize.height);
|
|
393
|
+
});
|
|
394
|
+
it('emits canvas rules for section borderTop/borderBottom', () => {
|
|
395
|
+
const doc = {
|
|
396
|
+
fieldSchemas: {
|
|
397
|
+
note: { type: 'text', name: 'note', label: 'Note' },
|
|
398
|
+
},
|
|
399
|
+
blocks: [
|
|
400
|
+
{
|
|
401
|
+
type: 'documentSection',
|
|
402
|
+
data: {
|
|
403
|
+
label: 'Anamnesis',
|
|
404
|
+
borderTop: true,
|
|
405
|
+
borderBottom: true,
|
|
406
|
+
segments: [{ type: 'field', id: 'note' }],
|
|
407
|
+
fieldValues: { note: 'Headache' },
|
|
408
|
+
},
|
|
409
|
+
},
|
|
410
|
+
],
|
|
411
|
+
};
|
|
412
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, {});
|
|
413
|
+
const section = docDefinition.content.find((node) => Array.isArray(node?.stack));
|
|
414
|
+
assert.ok(section);
|
|
415
|
+
const canvasNodes = section.stack.filter((node) => Array.isArray(node.canvas));
|
|
416
|
+
assert.equal(canvasNodes.length, 2);
|
|
417
|
+
assert.equal(canvasNodes[0].canvas[0].lineColor, '#000000');
|
|
418
|
+
assert.equal(canvasNodes[0].canvas[0].lineWidth, 1);
|
|
419
|
+
assert.equal(canvasNodes[1].canvas[0].lineColor, '#000000');
|
|
420
|
+
});
|
|
421
|
+
it('keeps table headers independent of column cell alignment styles', () => {
|
|
422
|
+
const cell1 = cellFieldId(tableId, 'row1', 'od');
|
|
423
|
+
const cell2 = cellFieldId(tableId, 'row1', 'os');
|
|
424
|
+
const fieldSchemas = {
|
|
425
|
+
...makeTemplate().fieldSchemas,
|
|
426
|
+
[cell1]: {
|
|
427
|
+
type: 'text',
|
|
428
|
+
label: 'OD',
|
|
429
|
+
displayStyle: { textAlign: 'right', fontStyle: 'italic' },
|
|
430
|
+
},
|
|
431
|
+
[cell2]: {
|
|
432
|
+
type: 'text',
|
|
433
|
+
label: 'OS',
|
|
434
|
+
displayStyle: { textAlign: 'right', fontWeight: 'bold' },
|
|
435
|
+
},
|
|
436
|
+
};
|
|
437
|
+
const ctx = {
|
|
438
|
+
fieldSchemas,
|
|
439
|
+
blocks: [],
|
|
440
|
+
fieldValues: { [cell1]: '0.8', [cell2]: '0.9' },
|
|
441
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
442
|
+
fieldValueStyle: {},
|
|
443
|
+
};
|
|
444
|
+
const table = buildPdfTable(tableId, ctx, tableSchema.rows);
|
|
445
|
+
assert.ok(table);
|
|
446
|
+
const headerRow = table.table.body[0];
|
|
447
|
+
for (const headerCell of headerRow) {
|
|
448
|
+
assert.equal(headerCell.alignment, 'center');
|
|
449
|
+
assert.equal(headerCell.bold, false);
|
|
450
|
+
assert.notEqual(headerCell.italics, true);
|
|
451
|
+
}
|
|
452
|
+
assert.equal(table.table.body[1][0].alignment, 'right');
|
|
453
|
+
assert.equal(table.table.body[1][1].alignment, 'right');
|
|
454
|
+
});
|
|
455
|
+
it('applies page setup text style to table headers', () => {
|
|
456
|
+
const cell1 = cellFieldId(tableId, 'row1', 'od');
|
|
457
|
+
const cell2 = cellFieldId(tableId, 'row1', 'os');
|
|
458
|
+
const fieldSchemas = {
|
|
459
|
+
...makeTemplate().fieldSchemas,
|
|
460
|
+
[cell1]: {
|
|
461
|
+
type: 'text',
|
|
462
|
+
label: 'OD',
|
|
463
|
+
displayStyle: { textAlign: 'right', fontStyle: 'italic' },
|
|
464
|
+
},
|
|
465
|
+
[cell2]: {
|
|
466
|
+
type: 'text',
|
|
467
|
+
label: 'OS',
|
|
468
|
+
displayStyle: { textAlign: 'right', fontWeight: 'bold' },
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
const ctx = {
|
|
472
|
+
fieldSchemas,
|
|
473
|
+
blocks: [],
|
|
474
|
+
fieldValues: { [cell1]: '0.8', [cell2]: '0.9' },
|
|
475
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
476
|
+
fieldValueStyle: {},
|
|
477
|
+
pageSetup: {
|
|
478
|
+
textStyle: { fontSize: '15px', fontWeight: 'normal', color: '#111111' },
|
|
479
|
+
},
|
|
480
|
+
};
|
|
481
|
+
const table = buildPdfTable(tableId, ctx, tableSchema.rows);
|
|
482
|
+
assert.ok(table);
|
|
483
|
+
const headerRow = table.table.body[0];
|
|
484
|
+
for (const headerCell of headerRow) {
|
|
485
|
+
assert.equal(headerCell.bold, false);
|
|
486
|
+
assert.notEqual(headerCell.italics, true);
|
|
487
|
+
assert.equal(headerCell.fontSize, 15 * 72 / 96);
|
|
488
|
+
assert.equal(headerCell.color, '#111111');
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
it('applies custom column widths from table schema', () => {
|
|
492
|
+
const cell1 = cellFieldId(tableId, 'row1', 'od');
|
|
493
|
+
const cell2 = cellFieldId(tableId, 'row1', 'os');
|
|
494
|
+
const fieldSchemas = {
|
|
495
|
+
...makeTemplate().fieldSchemas,
|
|
496
|
+
[tableId]: {
|
|
497
|
+
...tableSchema,
|
|
498
|
+
columns: [
|
|
499
|
+
{ key: 'od', label: 'OD', width: '75%' },
|
|
500
|
+
{ key: 'os', label: 'OS', width: '25%' },
|
|
501
|
+
],
|
|
502
|
+
},
|
|
503
|
+
};
|
|
504
|
+
const ctx = {
|
|
505
|
+
fieldSchemas,
|
|
506
|
+
blocks: [],
|
|
507
|
+
fieldValues: { [cell1]: '0.8', [cell2]: '0.9' },
|
|
508
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
509
|
+
fieldValueStyle: {},
|
|
510
|
+
};
|
|
511
|
+
const table = buildPdfTable(tableId, ctx, tableSchema.rows);
|
|
512
|
+
assert.ok(table);
|
|
513
|
+
assert.deepEqual(table.table.widths, ['75%', '25%']);
|
|
514
|
+
});
|
|
515
|
+
it('omits empty table cells and does not render column label placeholders', () => {
|
|
516
|
+
const tableId = 'anamnesis_table';
|
|
517
|
+
const cell1 = cellFieldId(tableId, 'row1', 'col1');
|
|
518
|
+
const cell2 = cellFieldId(tableId, 'row1', 'col2');
|
|
519
|
+
const cell3 = cellFieldId(tableId, 'row2', 'col1');
|
|
520
|
+
const cell4 = cellFieldId(tableId, 'row2', 'col2');
|
|
521
|
+
const cell5 = cellFieldId(tableId, 'row3', 'col1');
|
|
522
|
+
const cell6 = cellFieldId(tableId, 'row3', 'col2');
|
|
523
|
+
const fieldSchemas = {
|
|
524
|
+
[tableId]: {
|
|
525
|
+
type: 'table',
|
|
526
|
+
columns: [
|
|
527
|
+
{ key: 'col1', label: 'Column 1' },
|
|
528
|
+
{ key: 'col2', label: 'Column 2' },
|
|
529
|
+
],
|
|
530
|
+
rows: [
|
|
531
|
+
{ key: 'row1', label: 'Row 1' },
|
|
532
|
+
{ key: 'row2', label: 'Row 2' },
|
|
533
|
+
{ key: 'row3', label: 'Row 3' },
|
|
534
|
+
],
|
|
535
|
+
},
|
|
536
|
+
[cell1]: { type: 'text', label: 'Column 1' },
|
|
537
|
+
[cell2]: { type: 'text', label: 'Column 2' },
|
|
538
|
+
[cell3]: { type: 'text', label: 'Column 1' },
|
|
539
|
+
[cell4]: { type: 'text', label: 'Column 2' },
|
|
540
|
+
[cell5]: { type: 'text', label: 'Column 1' },
|
|
541
|
+
[cell6]: { type: 'text', label: 'Column 2' },
|
|
542
|
+
};
|
|
543
|
+
const ctx = {
|
|
544
|
+
fieldSchemas,
|
|
545
|
+
blocks: [],
|
|
546
|
+
fieldValues: {
|
|
547
|
+
[cell1]: '1111111111',
|
|
548
|
+
[cell2]: '22222222',
|
|
549
|
+
[cell3]: '123',
|
|
550
|
+
[cell4]: '333',
|
|
551
|
+
[cell5]: '',
|
|
552
|
+
[cell6]: '89',
|
|
553
|
+
},
|
|
554
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
555
|
+
fieldValueStyle: {},
|
|
556
|
+
hideEmptyValues: true,
|
|
557
|
+
};
|
|
558
|
+
const table = buildPdfTable(tableId, ctx, fieldSchemas[tableId].rows);
|
|
559
|
+
assert.ok(table);
|
|
560
|
+
assert.equal(table.table.body.length, 4);
|
|
561
|
+
assertEmptyPdfTableCell(table.table.body[3][0]);
|
|
562
|
+
assert.notEqual(table.table.body[3][1].text?.[0]?.text ?? table.table.body[3][1].text, 'Column 2');
|
|
563
|
+
const emptyRowTable = buildPdfTable(tableId, {
|
|
564
|
+
...ctx,
|
|
565
|
+
fieldValues: {
|
|
566
|
+
[cell5]: '',
|
|
567
|
+
[cell6]: '',
|
|
568
|
+
},
|
|
569
|
+
}, [{ key: 'row3' }]);
|
|
570
|
+
assert.equal(emptyRowTable, null);
|
|
571
|
+
});
|
|
572
|
+
it('skips rows where only column label placeholders are stored as values', () => {
|
|
573
|
+
const tableId = 'anamnesis_table';
|
|
574
|
+
const cells = {
|
|
575
|
+
row1: ['col1', 'col2'],
|
|
576
|
+
row2: ['col1', 'col2'],
|
|
577
|
+
row3: ['col1', 'col2'],
|
|
578
|
+
row4: ['col1', 'col2'],
|
|
579
|
+
row5: ['col1', 'col2'],
|
|
580
|
+
};
|
|
581
|
+
const fieldSchemas = {
|
|
582
|
+
[tableId]: {
|
|
583
|
+
type: 'table',
|
|
584
|
+
columns: [
|
|
585
|
+
{ key: 'col1', label: 'Column 1' },
|
|
586
|
+
{ key: 'col2', label: 'Column 2' },
|
|
587
|
+
],
|
|
588
|
+
rows: Object.keys(cells).map((key) => ({ key, label: key })),
|
|
589
|
+
},
|
|
590
|
+
};
|
|
591
|
+
const fieldValues = {};
|
|
592
|
+
for (const [rowKey, cols] of Object.entries(cells)) {
|
|
593
|
+
for (const colKey of cols) {
|
|
594
|
+
const id = cellFieldId(tableId, rowKey, colKey);
|
|
595
|
+
fieldSchemas[id] = {
|
|
596
|
+
type: 'text',
|
|
597
|
+
label: colKey === 'col1' ? 'Column 1' : 'Column 2',
|
|
598
|
+
};
|
|
599
|
+
fieldValues[id] = '';
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
fieldValues[cellFieldId(tableId, 'row1', 'col1')] = '1111111111';
|
|
603
|
+
fieldValues[cellFieldId(tableId, 'row1', 'col2')] = '22222222';
|
|
604
|
+
fieldValues[cellFieldId(tableId, 'row2', 'col1')] = '123';
|
|
605
|
+
fieldValues[cellFieldId(tableId, 'row2', 'col2')] = '333';
|
|
606
|
+
fieldValues[cellFieldId(tableId, 'row3', 'col2')] = '132';
|
|
607
|
+
fieldValues[cellFieldId(tableId, 'row4', 'col2')] = '111';
|
|
608
|
+
fieldValues[cellFieldId(tableId, 'row5', 'col1')] = 'Column 1';
|
|
609
|
+
fieldValues[cellFieldId(tableId, 'row5', 'col2')] = 'Column 2';
|
|
610
|
+
const ctx = {
|
|
611
|
+
fieldSchemas,
|
|
612
|
+
blocks: [],
|
|
613
|
+
fieldValues,
|
|
614
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
615
|
+
fieldValueStyle: {},
|
|
616
|
+
hideEmptyValues: true,
|
|
617
|
+
};
|
|
618
|
+
const table = buildPdfTable(tableId, ctx, fieldSchemas[tableId].rows);
|
|
619
|
+
assert.ok(table);
|
|
620
|
+
assert.equal(table.table.body.length, 5);
|
|
621
|
+
assertEmptyPdfTableCell(table.table.body[4][0]);
|
|
622
|
+
assert.equal(table.table.body[4][1].text?.[0]?.text ?? table.table.body[4][1].text, '111');
|
|
623
|
+
});
|
|
624
|
+
it('treats caret-anchor zero-width space as an empty table cell', () => {
|
|
625
|
+
const tableId = 'anamnesis_table';
|
|
626
|
+
const cell1 = cellFieldId(tableId, 'row1', 'col1');
|
|
627
|
+
const cell2 = cellFieldId(tableId, 'row1', 'col2');
|
|
628
|
+
const fieldSchemas = {
|
|
629
|
+
[tableId]: {
|
|
630
|
+
type: 'table',
|
|
631
|
+
columns: [
|
|
632
|
+
{ key: 'col1', label: 'Column 1', displayStyle: { textAlign: 'right' } },
|
|
633
|
+
{ key: 'col2', label: 'Column 2', displayStyle: { textAlign: 'left' } },
|
|
634
|
+
],
|
|
635
|
+
rows: [{ key: 'row1', label: 'Row 1' }],
|
|
636
|
+
},
|
|
637
|
+
[cell1]: { type: 'text', label: 'Column 1', displayStyle: { textAlign: 'right' } },
|
|
638
|
+
[cell2]: { type: 'text', label: 'Column 2', displayStyle: { textAlign: 'left' } },
|
|
639
|
+
};
|
|
640
|
+
const ctx = {
|
|
641
|
+
fieldSchemas,
|
|
642
|
+
blocks: [],
|
|
643
|
+
fieldValues: {
|
|
644
|
+
[cell1]: '\u200B',
|
|
645
|
+
[cell2]: '111',
|
|
646
|
+
},
|
|
647
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
648
|
+
fieldValueStyle: {},
|
|
649
|
+
};
|
|
650
|
+
const table = buildPdfTable(tableId, ctx, fieldSchemas[tableId].rows);
|
|
651
|
+
assert.ok(table);
|
|
652
|
+
assertEmptyPdfTableCell(table.table.body[1][0], 'right');
|
|
653
|
+
assert.equal(table.table.body[1][1].text?.[0]?.text ?? table.table.body[1][1].text, '111');
|
|
654
|
+
assert.equal(table.table.body[1][1].alignment, 'left');
|
|
655
|
+
});
|
|
656
|
+
it('emits fillColor from cell displayStyle backgroundColor', () => {
|
|
657
|
+
const localTableId = 'fill_color_table';
|
|
658
|
+
const labelCell = cellFieldId(localTableId, 'row1', 'label');
|
|
659
|
+
const valueCell = cellFieldId(localTableId, 'row1', 'value');
|
|
660
|
+
const fieldSchemas = {
|
|
661
|
+
[localTableId]: {
|
|
662
|
+
type: 'table',
|
|
663
|
+
hideHeader: true,
|
|
664
|
+
columns: [
|
|
665
|
+
{ key: 'label', label: 'Label' },
|
|
666
|
+
{ key: 'value', label: 'Value' },
|
|
667
|
+
],
|
|
668
|
+
rows: [{ key: 'row1', label: 'Row 1' }],
|
|
669
|
+
},
|
|
670
|
+
[labelCell]: {
|
|
671
|
+
type: 'text',
|
|
672
|
+
label: 'Label',
|
|
673
|
+
displayStyle: { backgroundColor: '#e8eef5' },
|
|
674
|
+
},
|
|
675
|
+
[valueCell]: { type: 'text', label: 'Value' },
|
|
676
|
+
};
|
|
677
|
+
const table = buildPdfTable(localTableId, {
|
|
678
|
+
fieldSchemas,
|
|
679
|
+
blocks: [],
|
|
680
|
+
fieldValues: {
|
|
681
|
+
[labelCell]: 'Patient',
|
|
682
|
+
[valueCell]: 'Alice',
|
|
683
|
+
},
|
|
684
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
685
|
+
fieldValueStyle: {},
|
|
686
|
+
}, fieldSchemas[localTableId].rows);
|
|
687
|
+
assert.ok(table);
|
|
688
|
+
assert.equal(table.table.body[0][0].fillColor, '#e8eef5');
|
|
689
|
+
assert.equal(table.table.body[0][1].fillColor, undefined);
|
|
690
|
+
});
|
|
691
|
+
it('applies text background for inline fields with backgroundColor', () => {
|
|
692
|
+
const content = renderSegmentsToPdfContent([
|
|
693
|
+
{ type: 'text', content: 'Complaints: ' },
|
|
694
|
+
{ type: 'field', id: 'complaints' },
|
|
695
|
+
], {
|
|
696
|
+
fieldSchemas: {
|
|
697
|
+
complaints: {
|
|
698
|
+
type: 'text',
|
|
699
|
+
label: 'Complaints',
|
|
700
|
+
displayStyle: { backgroundColor: '#e8eef5' },
|
|
701
|
+
},
|
|
702
|
+
},
|
|
703
|
+
fieldValues: { complaints: 'Vision disturbance' },
|
|
704
|
+
blocks: [],
|
|
705
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
706
|
+
fieldValueStyle: {},
|
|
707
|
+
bodyPdfStyle: {},
|
|
708
|
+
defaultFont: 'Roboto',
|
|
709
|
+
});
|
|
710
|
+
assert.ok(content.length >= 1);
|
|
711
|
+
const row = content[0];
|
|
712
|
+
assert.ok(row.text, 'label + field stay in one prose paragraph');
|
|
713
|
+
assert.equal(row.columns, undefined, 'no fill-chip columns layout');
|
|
714
|
+
const parts = Array.isArray(row.text) ? row.text : [row.text];
|
|
715
|
+
const fieldPart = parts.find((p) => typeof p === 'object' && String(p?.text ?? '').includes('Vision disturbance'));
|
|
716
|
+
assert.ok(fieldPart);
|
|
717
|
+
assert.equal(fieldPart.background, '#e8eef5');
|
|
718
|
+
assert.equal(fieldPart.fillColor, undefined);
|
|
719
|
+
});
|
|
720
|
+
it('uses vision-table grid layout for pdf tables', () => {
|
|
721
|
+
const table = buildPdfTable(tableId, {
|
|
722
|
+
fieldSchemas: makeTemplate().fieldSchemas,
|
|
723
|
+
blocks: [],
|
|
724
|
+
fieldValues: {
|
|
725
|
+
[cellFieldId(tableId, 'row1', 'od')]: '0.8',
|
|
726
|
+
[cellFieldId(tableId, 'row1', 'os')]: '0.9',
|
|
727
|
+
},
|
|
728
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
729
|
+
fieldValueStyle: {},
|
|
730
|
+
}, tableSchema.rows);
|
|
731
|
+
assert.ok(table);
|
|
732
|
+
const layout = table.layout;
|
|
733
|
+
assert.equal(table.layout, VISION_TABLE_PDF_LAYOUT);
|
|
734
|
+
assert.equal(layout.paddingLeft(), 6);
|
|
735
|
+
assert.equal(layout.paddingTop(), 3);
|
|
736
|
+
assert.equal(layout.paddingBottom(), 3);
|
|
737
|
+
assert.equal(layout.hLineColor(), '#999999');
|
|
738
|
+
assert.equal(table.table.body[1][0].lineHeight, 1);
|
|
739
|
+
assert.equal(table.table.body[0][0].lineHeight, 1);
|
|
740
|
+
});
|
|
741
|
+
it('honors hideHeader and hideBorders on pdf tables', () => {
|
|
742
|
+
const schemas = makeTemplate().fieldSchemas;
|
|
743
|
+
schemas[tableId] = {
|
|
744
|
+
...schemas[tableId],
|
|
745
|
+
hideHeader: true,
|
|
746
|
+
hideBorders: true,
|
|
747
|
+
};
|
|
748
|
+
const table = buildPdfTable(tableId, {
|
|
749
|
+
fieldSchemas: schemas,
|
|
750
|
+
blocks: [],
|
|
751
|
+
fieldValues: {
|
|
752
|
+
[cellFieldId(tableId, 'row1', 'od')]: '0.8',
|
|
753
|
+
[cellFieldId(tableId, 'row1', 'os')]: '0.9',
|
|
754
|
+
},
|
|
755
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
756
|
+
fieldValueStyle: {},
|
|
757
|
+
}, tableSchema.rows);
|
|
758
|
+
assert.ok(table);
|
|
759
|
+
assert.equal(table.table.headerRows, 0);
|
|
760
|
+
assert.equal(table.table.keepWithHeaderRows, 0);
|
|
761
|
+
assert.equal(table.table.body.length, 2);
|
|
762
|
+
assert.equal(table.table.body[0][0].text?.[0]?.text ?? table.table.body[0][0].text, '0.8');
|
|
763
|
+
assert.equal(table.layout, VISION_TABLE_BORDERLESS_PDF_LAYOUT);
|
|
764
|
+
assert.equal(table.layout.hLineWidth(), 0);
|
|
765
|
+
assert.equal(table.layout.vLineWidth(), 0);
|
|
766
|
+
});
|
|
767
|
+
it('converts rich text HTML in table cells with formatting', () => {
|
|
768
|
+
const htmlCellId = cellFieldId(tableId, 'row1', 'od');
|
|
769
|
+
const template = makeTemplate();
|
|
770
|
+
template.fieldSchemas[htmlCellId] = {
|
|
771
|
+
type: 'text',
|
|
772
|
+
label: 'OD',
|
|
773
|
+
name: 'OD',
|
|
774
|
+
htmlEditor: true,
|
|
775
|
+
};
|
|
776
|
+
const doc = mergeTemplateAndDocument(template, makeDocument());
|
|
777
|
+
doc.blocks[0].data.fieldValues[htmlCellId] = '<b>test</b><br><ul><li>column 1</li></ul><br>';
|
|
778
|
+
const ctx = {
|
|
779
|
+
fieldSchemas: doc.fieldSchemas,
|
|
780
|
+
blocks: doc.blocks,
|
|
781
|
+
fieldValues: doc.blocks[0].data.fieldValues,
|
|
782
|
+
previewContext: {
|
|
783
|
+
previewMode: true,
|
|
784
|
+
fieldSchemas: doc.fieldSchemas,
|
|
785
|
+
blocks: doc.blocks,
|
|
786
|
+
},
|
|
787
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
788
|
+
defaultFont: 'Roboto',
|
|
789
|
+
fieldValueStyle: {},
|
|
790
|
+
bodyPdfStyle: {},
|
|
791
|
+
};
|
|
792
|
+
const table = buildPdfTable(tableId, ctx, tableSchema.rows);
|
|
793
|
+
assert.ok(table);
|
|
794
|
+
const cell = table.table.body[1][0];
|
|
795
|
+
const rendered = extractPdfCellText(cell);
|
|
796
|
+
assert.match(rendered, /test/);
|
|
797
|
+
assert.match(rendered, /column 1/);
|
|
798
|
+
assert.doesNotMatch(rendered, /<b>/);
|
|
799
|
+
const boldPart = findPdfBoldPart(cell);
|
|
800
|
+
assert.equal(boldPart?.text, 'test\n');
|
|
801
|
+
});
|
|
802
|
+
it('applies segment align metadata to centered prose', () => {
|
|
803
|
+
const doc = {
|
|
804
|
+
kind: 'template',
|
|
805
|
+
version: 1,
|
|
806
|
+
time: Date.now(),
|
|
807
|
+
fieldSchemas: {},
|
|
808
|
+
blocks: [
|
|
809
|
+
{
|
|
810
|
+
type: 'documentSection',
|
|
811
|
+
data: {
|
|
812
|
+
label: 'Anamnesis',
|
|
813
|
+
segments: [
|
|
814
|
+
{ type: 'text', content: '234234', align: 'center' },
|
|
815
|
+
{ type: 'text', html: '<ul><li>1111</li><li>22222</li></ul>', align: 'center' },
|
|
816
|
+
],
|
|
817
|
+
fieldValues: {},
|
|
818
|
+
},
|
|
819
|
+
},
|
|
820
|
+
],
|
|
821
|
+
};
|
|
822
|
+
const ctx = {
|
|
823
|
+
fieldSchemas: {},
|
|
824
|
+
blocks: doc.blocks,
|
|
825
|
+
fieldValues: {},
|
|
826
|
+
previewContext: { previewMode: true, fieldSchemas: {}, blocks: doc.blocks },
|
|
827
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
828
|
+
defaultFont: 'Roboto',
|
|
829
|
+
fieldValueStyle: {},
|
|
830
|
+
bodyPdfStyle: { fontSize: 11.25 },
|
|
831
|
+
};
|
|
832
|
+
const content = renderSegmentsToPdfContent(doc.blocks[0].data.segments, ctx);
|
|
833
|
+
const combined = content
|
|
834
|
+
.map((node) => (Array.isArray(node.text)
|
|
835
|
+
? node.text.map((part) => (typeof part === 'string' ? part : part.text)).join('')
|
|
836
|
+
: String(node.text ?? '')))
|
|
837
|
+
.join('\n');
|
|
838
|
+
assert.ok(content.length >= 2);
|
|
839
|
+
assert.ok(content.every((node) => node.alignment === 'center'));
|
|
840
|
+
assert.match(combined, /234234/);
|
|
841
|
+
assert.match(combined, /1111/);
|
|
842
|
+
});
|
|
843
|
+
});
|
|
844
|
+
function extractPdfCellText(cell) {
|
|
845
|
+
if (typeof cell === 'string')
|
|
846
|
+
return cell;
|
|
847
|
+
if (Array.isArray(cell.stack)) {
|
|
848
|
+
return cell.stack.map((node) => extractPdfCellText(node)).join('\n');
|
|
849
|
+
}
|
|
850
|
+
const text = cell.text;
|
|
851
|
+
if (Array.isArray(text)) {
|
|
852
|
+
return text.map((part) => (typeof part === 'string' ? part : part.text)).join('');
|
|
853
|
+
}
|
|
854
|
+
return String(text ?? '');
|
|
855
|
+
}
|
|
856
|
+
function findPdfBoldPart(cell) {
|
|
857
|
+
if (typeof cell === 'string' || !cell)
|
|
858
|
+
return null;
|
|
859
|
+
if (Array.isArray(cell.stack)) {
|
|
860
|
+
for (const node of cell.stack) {
|
|
861
|
+
const found = findPdfBoldPart(node);
|
|
862
|
+
if (found)
|
|
863
|
+
return found;
|
|
864
|
+
}
|
|
865
|
+
return null;
|
|
866
|
+
}
|
|
867
|
+
const text = cell.text;
|
|
868
|
+
if (!Array.isArray(text))
|
|
869
|
+
return text?.bold ? text : null;
|
|
870
|
+
return text.find((part) => typeof part === 'object' && part.bold) ?? null;
|
|
871
|
+
}
|
|
872
|
+
function collectPageBreaks(nodes) {
|
|
873
|
+
let count = 0;
|
|
874
|
+
for (const node of nodes ?? []) {
|
|
875
|
+
if (node?.pageBreak)
|
|
876
|
+
count += 1;
|
|
877
|
+
if (node?.stack)
|
|
878
|
+
count += collectPageBreaks(node.stack);
|
|
879
|
+
if (node?.columns) {
|
|
880
|
+
for (const col of node.columns) {
|
|
881
|
+
count += collectPageBreaks(col.stack);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
return count;
|
|
886
|
+
}
|
|
887
|
+
function collectNodesWithStyle(nodes, style) {
|
|
888
|
+
const results = [];
|
|
889
|
+
function walk(chunkNodes) {
|
|
890
|
+
for (const node of chunkNodes ?? []) {
|
|
891
|
+
if (node?.style === style)
|
|
892
|
+
results.push(node);
|
|
893
|
+
if (node?.stack)
|
|
894
|
+
walk(node.stack);
|
|
895
|
+
if (node?.columns) {
|
|
896
|
+
for (const col of node.columns) {
|
|
897
|
+
walk(col.stack);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
walk(nodes);
|
|
903
|
+
return results;
|
|
904
|
+
}
|
|
905
|
+
function collectCanvasLineNodes(nodes) {
|
|
906
|
+
const results = [];
|
|
907
|
+
function walk(chunkNodes) {
|
|
908
|
+
for (const node of chunkNodes ?? []) {
|
|
909
|
+
if (Array.isArray(node?.canvas))
|
|
910
|
+
results.push(node);
|
|
911
|
+
if (node?.stack)
|
|
912
|
+
walk(node.stack);
|
|
913
|
+
if (node?.columns) {
|
|
914
|
+
for (const col of node.columns) {
|
|
915
|
+
walk(col.stack);
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
walk(nodes);
|
|
921
|
+
return results;
|
|
922
|
+
}
|
|
923
|
+
function findTextInPdfContent(nodes, text) {
|
|
924
|
+
for (const node of nodes ?? []) {
|
|
925
|
+
if (typeof node?.text === 'string' && node.text.includes(text))
|
|
926
|
+
return true;
|
|
927
|
+
if (Array.isArray(node?.text)) {
|
|
928
|
+
const joined = node.text
|
|
929
|
+
.map((part) => (typeof part === 'string' ? part : part.text ?? ''))
|
|
930
|
+
.join('');
|
|
931
|
+
if (joined.includes(text))
|
|
932
|
+
return true;
|
|
933
|
+
}
|
|
934
|
+
if (node?.stack && findTextInPdfContent(node.stack, text))
|
|
935
|
+
return true;
|
|
936
|
+
if (node?.table?.body && findTextInPdfContent(node.table.body.flat(), text))
|
|
937
|
+
return true;
|
|
938
|
+
if (node?.columns) {
|
|
939
|
+
for (const col of node.columns) {
|
|
940
|
+
if (findTextInPdfContent(col.stack, text))
|
|
941
|
+
return true;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
return false;
|
|
946
|
+
}
|
|
947
|
+
describe('shouldUseLegacyPdfExport', () => {
|
|
948
|
+
it('returns false for single-instance repeatable section (JSON renderer uses a page header)', () => {
|
|
949
|
+
const tableId = 'items_table';
|
|
950
|
+
const template = {
|
|
951
|
+
kind: 'template',
|
|
952
|
+
version: 1,
|
|
953
|
+
time: Date.now(),
|
|
954
|
+
fieldSchemas: {
|
|
955
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
956
|
+
lifeAnamnesis: { type: 'tree', name: 'Life history', label: 'Life history' },
|
|
957
|
+
[tableId]: {
|
|
958
|
+
type: 'table',
|
|
959
|
+
name: 'Results',
|
|
960
|
+
label: 'Results',
|
|
961
|
+
columns: [
|
|
962
|
+
{ key: 'col1', label: 'Column 1' },
|
|
963
|
+
{ key: 'col2', label: 'Column 2' },
|
|
964
|
+
],
|
|
965
|
+
cellType: 'text',
|
|
966
|
+
},
|
|
967
|
+
},
|
|
968
|
+
blocks: [
|
|
969
|
+
{
|
|
970
|
+
type: 'documentSection',
|
|
971
|
+
data: {
|
|
972
|
+
name: 'Anamnesis',
|
|
973
|
+
label: 'Anamnesis',
|
|
974
|
+
repeatable: true,
|
|
975
|
+
segments: [
|
|
976
|
+
{ type: 'text', content: 'Complaints: ' },
|
|
977
|
+
{ type: 'field', id: 'complaints' },
|
|
978
|
+
{ type: 'text', content: '.\nLife history: ' },
|
|
979
|
+
{ type: 'field', id: 'lifeAnamnesis' },
|
|
980
|
+
{ type: 'text', content: '.' },
|
|
981
|
+
],
|
|
982
|
+
fieldValues: {
|
|
983
|
+
complaints: ['Tearing'],
|
|
984
|
+
lifeAnamnesis: ['Unremarkable'],
|
|
985
|
+
},
|
|
986
|
+
},
|
|
987
|
+
},
|
|
988
|
+
{
|
|
989
|
+
type: 'documentSection',
|
|
990
|
+
data: {
|
|
991
|
+
name: 'items',
|
|
992
|
+
label: 'Items',
|
|
993
|
+
segments: [{ type: 'table', id: tableId, rows: [{ key: 'row1', label: '' }] }],
|
|
994
|
+
fieldValues: { [cellFieldId(tableId, 'row1', 'col1')]: '111' },
|
|
995
|
+
},
|
|
996
|
+
},
|
|
997
|
+
],
|
|
998
|
+
};
|
|
999
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1000
|
+
kind: 'field',
|
|
1001
|
+
version: 2,
|
|
1002
|
+
time: Date.now(),
|
|
1003
|
+
sections: {
|
|
1004
|
+
Anamnesis: {
|
|
1005
|
+
Complaints: ['Tearing'],
|
|
1006
|
+
'Life history': ['Unremarkable'],
|
|
1007
|
+
},
|
|
1008
|
+
items: {
|
|
1009
|
+
Results: [{ col1: '111' }],
|
|
1010
|
+
},
|
|
1011
|
+
},
|
|
1012
|
+
});
|
|
1013
|
+
assert.equal(shouldUseLegacyPdfExport(doc), false);
|
|
1014
|
+
// Legacy renderer still produces the correct page header for repeatable sections.
|
|
1015
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
1016
|
+
assert.ok(headerContainsText(docDefinition, 'Anamnesis'));
|
|
1017
|
+
assert.ok(headerContainsText(docDefinition, 'Tearing'));
|
|
1018
|
+
assert.ok(headerContainsText(docDefinition, 'Unremarkable'));
|
|
1019
|
+
assert.ok(headerContainsText(docDefinition, 'Complaints:'));
|
|
1020
|
+
assert.ok(headerContainsText(docDefinition, 'Life history:'));
|
|
1021
|
+
});
|
|
1022
|
+
it('returns false for non-repeatable mammology-style sections', () => {
|
|
1023
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), {
|
|
1024
|
+
kind: 'field',
|
|
1025
|
+
version: 2,
|
|
1026
|
+
time: Date.now(),
|
|
1027
|
+
sections: {
|
|
1028
|
+
Anamnesis: { Complaints: ['Tearing'] },
|
|
1029
|
+
},
|
|
1030
|
+
});
|
|
1031
|
+
assert.equal(shouldUseLegacyPdfExport(doc), false);
|
|
1032
|
+
});
|
|
1033
|
+
});
|
|
1034
|
+
describe('renderDocumentToPdfContent multipage', () => {
|
|
1035
|
+
it('repeats a marked section once per document instance inline (title on each when Show on each page)', () => {
|
|
1036
|
+
const template = makeTemplate();
|
|
1037
|
+
template.blocks.unshift({
|
|
1038
|
+
type: 'documentSection',
|
|
1039
|
+
data: {
|
|
1040
|
+
label: 'Patient',
|
|
1041
|
+
name: 'Patient',
|
|
1042
|
+
segments: [
|
|
1043
|
+
{ type: 'text', content: 'Patient: ' },
|
|
1044
|
+
{ type: 'field', id: 'patient_name', placeholder: 'Name' },
|
|
1045
|
+
],
|
|
1046
|
+
fieldValues: { patient_name: '' },
|
|
1047
|
+
},
|
|
1048
|
+
});
|
|
1049
|
+
template.blocks.push({
|
|
1050
|
+
type: 'documentSection',
|
|
1051
|
+
data: {
|
|
1052
|
+
label: 'Items',
|
|
1053
|
+
name: 'Items',
|
|
1054
|
+
repeatable: true,
|
|
1055
|
+
borderBottom: true,
|
|
1056
|
+
segments: [
|
|
1057
|
+
{ type: 'text', content: 'Item: ' },
|
|
1058
|
+
{ type: 'field', id: 'items_name', placeholder: 'Name' },
|
|
1059
|
+
],
|
|
1060
|
+
fieldValues: { items_name: '' },
|
|
1061
|
+
},
|
|
1062
|
+
});
|
|
1063
|
+
template.blocks.push({
|
|
1064
|
+
type: 'documentSection',
|
|
1065
|
+
data: {
|
|
1066
|
+
label: 'Signature',
|
|
1067
|
+
name: 'Signature',
|
|
1068
|
+
segments: [{ type: 'text', content: 'Signed' }],
|
|
1069
|
+
fieldValues: {},
|
|
1070
|
+
},
|
|
1071
|
+
});
|
|
1072
|
+
template.fieldSchemas.patient_name = { type: 'text', name: 'Name', label: 'Name' };
|
|
1073
|
+
template.fieldSchemas.items_name = { type: 'text', name: 'Name', label: 'Name' };
|
|
1074
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1075
|
+
kind: 'field',
|
|
1076
|
+
version: 2,
|
|
1077
|
+
time: Date.now(),
|
|
1078
|
+
sections: {
|
|
1079
|
+
Patient: { Name: 'John Doe' },
|
|
1080
|
+
Examination: makeDocument().sections.Examination,
|
|
1081
|
+
Items: [{ Name: 'Alpha' }, { Name: 'Beta' }, { Name: 'Gamma' }],
|
|
1082
|
+
Signature: {},
|
|
1083
|
+
},
|
|
1084
|
+
});
|
|
1085
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
1086
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
1087
|
+
defaultFont: 'Roboto',
|
|
1088
|
+
fieldValueStyle: {},
|
|
1089
|
+
});
|
|
1090
|
+
// Match HTML preview: continuous flow, title on every "Show on each page" row.
|
|
1091
|
+
assert.equal(collectPageBreaks(content), 0);
|
|
1092
|
+
const itemTitles = collectNodesWithStyle(content, 'sectionHeader').filter((node) => node.text === 'Items');
|
|
1093
|
+
assert.equal(itemTitles.length, 3);
|
|
1094
|
+
assert.equal(findTextInPdfContent(content, 'John Doe'), true);
|
|
1095
|
+
assert.equal(findTextInPdfContent(content, 'Alpha'), true);
|
|
1096
|
+
assert.equal(findTextInPdfContent(content, 'Beta'), true);
|
|
1097
|
+
assert.equal(findTextInPdfContent(content, 'Gamma'), true);
|
|
1098
|
+
assert.equal(findTextInPdfContent(content, 'Signed'), true);
|
|
1099
|
+
// borderBottom on each of the 3 repeated rows
|
|
1100
|
+
const borderRules = collectCanvasLineNodes(content);
|
|
1101
|
+
assert.equal(borderRules.length, 3);
|
|
1102
|
+
});
|
|
1103
|
+
it('page-breaks between rows when eachRowOnNewPage is set', () => {
|
|
1104
|
+
const template = makeTemplate();
|
|
1105
|
+
template.blocks.push({
|
|
1106
|
+
type: 'documentSection',
|
|
1107
|
+
data: {
|
|
1108
|
+
label: 'Items',
|
|
1109
|
+
name: 'Items',
|
|
1110
|
+
repeatable: true,
|
|
1111
|
+
eachRowOnNewPage: true,
|
|
1112
|
+
borderBottom: true,
|
|
1113
|
+
segments: [
|
|
1114
|
+
{ type: 'text', content: 'Item: ' },
|
|
1115
|
+
{ type: 'field', id: 'items_name', placeholder: 'Name' },
|
|
1116
|
+
],
|
|
1117
|
+
fieldValues: { items_name: '' },
|
|
1118
|
+
},
|
|
1119
|
+
});
|
|
1120
|
+
template.fieldSchemas.items_name = { type: 'text', name: 'Name', label: 'Name' };
|
|
1121
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1122
|
+
kind: 'field',
|
|
1123
|
+
version: 2,
|
|
1124
|
+
time: Date.now(),
|
|
1125
|
+
sections: {
|
|
1126
|
+
Examination: makeDocument().sections.Examination,
|
|
1127
|
+
Items: [{ Name: 'Alpha' }, { Name: 'Beta' }, { Name: 'Gamma' }],
|
|
1128
|
+
},
|
|
1129
|
+
});
|
|
1130
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
1131
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
1132
|
+
defaultFont: 'Roboto',
|
|
1133
|
+
fieldValueStyle: {},
|
|
1134
|
+
});
|
|
1135
|
+
assert.equal(collectPageBreaks(content), 2);
|
|
1136
|
+
const itemTitles = collectNodesWithStyle(content, 'sectionHeader').filter((node) => node.text === 'Items');
|
|
1137
|
+
assert.equal(itemTitles.length, 3);
|
|
1138
|
+
assert.equal(findTextInPdfContent(content, 'Alpha'), true);
|
|
1139
|
+
assert.equal(findTextInPdfContent(content, 'Beta'), true);
|
|
1140
|
+
assert.equal(findTextInPdfContent(content, 'Gamma'), true);
|
|
1141
|
+
});
|
|
1142
|
+
it('marks section stacks unbreakable when keepOnOnePage is set', () => {
|
|
1143
|
+
const template = makeTemplate();
|
|
1144
|
+
template.blocks[0].data.keepOnOnePage = true;
|
|
1145
|
+
template.blocks[0].data.hideTitleInPreview = false;
|
|
1146
|
+
const doc = mergeTemplateAndDocument(template, makeDocument());
|
|
1147
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
1148
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
1149
|
+
defaultFont: 'Roboto',
|
|
1150
|
+
fieldValueStyle: {},
|
|
1151
|
+
});
|
|
1152
|
+
const unbreakableStacks = content.filter((node) => Array.isArray(node.stack) && node.unbreakable === true);
|
|
1153
|
+
assert.ok(unbreakableStacks.length >= 1);
|
|
1154
|
+
assert.equal(findTextInPdfContent(content, 'Stable findings'), true);
|
|
1155
|
+
});
|
|
1156
|
+
it('does not mark non-repeatable section stacks unbreakable by default', () => {
|
|
1157
|
+
const doc = mergeTemplateAndDocument(makeTemplate(), makeDocument());
|
|
1158
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
1159
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
1160
|
+
defaultFont: 'Roboto',
|
|
1161
|
+
fieldValueStyle: {},
|
|
1162
|
+
});
|
|
1163
|
+
const unbreakableStacks = content.filter((node) => Array.isArray(node.stack) && node.unbreakable === true);
|
|
1164
|
+
assert.equal(unbreakableStacks.length, 0);
|
|
1165
|
+
});
|
|
1166
|
+
it('does not leave a blank line when an optional empty field sits between text', () => {
|
|
1167
|
+
const doc = {
|
|
1168
|
+
fieldSchemas: {
|
|
1169
|
+
items_note: { type: 'text', name: 'Note', label: 'Note' },
|
|
1170
|
+
items_name: { type: 'text', name: 'Name', label: 'Name' },
|
|
1171
|
+
},
|
|
1172
|
+
blocks: [
|
|
1173
|
+
{
|
|
1174
|
+
type: 'documentSection',
|
|
1175
|
+
data: {
|
|
1176
|
+
label: 'Letter',
|
|
1177
|
+
name: 'Letter',
|
|
1178
|
+
repeatable: true,
|
|
1179
|
+
hideTitleInPreview: true,
|
|
1180
|
+
segments: [
|
|
1181
|
+
{ type: 'text', content: 'main\n' },
|
|
1182
|
+
{ type: 'field', id: 'items_note', placeholder: 'Note' },
|
|
1183
|
+
{ type: 'text', content: '\namount: ' },
|
|
1184
|
+
{ type: 'field', id: 'items_name', placeholder: 'Name' },
|
|
1185
|
+
],
|
|
1186
|
+
fieldValues: { items_note: 'kept', items_name: '2' },
|
|
1187
|
+
},
|
|
1188
|
+
},
|
|
1189
|
+
],
|
|
1190
|
+
repeatableSectionInstances: {
|
|
1191
|
+
Letter: [
|
|
1192
|
+
{ Note: 'kept', Name: '2' },
|
|
1193
|
+
{ Note: '', Name: '5' },
|
|
1194
|
+
],
|
|
1195
|
+
},
|
|
1196
|
+
};
|
|
1197
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
1198
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
1199
|
+
defaultFont: 'Roboto',
|
|
1200
|
+
fieldValueStyle: {},
|
|
1201
|
+
});
|
|
1202
|
+
const texts = content.map((node) => {
|
|
1203
|
+
const walk = (n) => {
|
|
1204
|
+
if (!n)
|
|
1205
|
+
return '';
|
|
1206
|
+
if (typeof n.text === 'string')
|
|
1207
|
+
return n.text;
|
|
1208
|
+
if (Array.isArray(n.text)) {
|
|
1209
|
+
return n.text.map((p) => (typeof p === 'string' ? p : String(p?.text ?? ''))).join('');
|
|
1210
|
+
}
|
|
1211
|
+
if (Array.isArray(n.stack))
|
|
1212
|
+
return n.stack.map(walk).join('');
|
|
1213
|
+
return '';
|
|
1214
|
+
};
|
|
1215
|
+
return walk(node);
|
|
1216
|
+
});
|
|
1217
|
+
assert.match(texts[0] ?? '', /^main\nkept\namount: 2$/);
|
|
1218
|
+
// Empty note must not become main\n\namount (extra blank line).
|
|
1219
|
+
assert.match(texts[1] ?? '', /^main\namount: 5$/);
|
|
1220
|
+
});
|
|
1221
|
+
it('renders adjacent vision table rows in one flow without forced page breaks', () => {
|
|
1222
|
+
const tableId = 'visionTable';
|
|
1223
|
+
const template = {
|
|
1224
|
+
kind: 'template',
|
|
1225
|
+
version: 1,
|
|
1226
|
+
time: Date.now(),
|
|
1227
|
+
fieldSchemas: {
|
|
1228
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
1229
|
+
[tableId]: {
|
|
1230
|
+
type: 'table',
|
|
1231
|
+
name: 'Visual acuity',
|
|
1232
|
+
label: 'Visual acuity',
|
|
1233
|
+
columns: [
|
|
1234
|
+
{ key: 'vis', label: 'vis' },
|
|
1235
|
+
{ key: 'sph', label: 'Sph' },
|
|
1236
|
+
],
|
|
1237
|
+
rows: [{ key: 'od', label: 'OD' }, { key: 'os', label: 'OS' }],
|
|
1238
|
+
cellType: 'choice',
|
|
1239
|
+
},
|
|
1240
|
+
exam_notes: { type: 'text', name: 'Notes', label: 'Notes' },
|
|
1241
|
+
},
|
|
1242
|
+
blocks: [
|
|
1243
|
+
{
|
|
1244
|
+
type: 'documentSection',
|
|
1245
|
+
data: {
|
|
1246
|
+
name: 'Anamnesis',
|
|
1247
|
+
label: 'Anamnesis',
|
|
1248
|
+
repeatable: true,
|
|
1249
|
+
segments: [{ type: 'field', id: 'complaints' }],
|
|
1250
|
+
fieldValues: { complaints: [] },
|
|
1251
|
+
},
|
|
1252
|
+
},
|
|
1253
|
+
{
|
|
1254
|
+
type: 'visionTable',
|
|
1255
|
+
data: { fieldId: tableId, cells: {} },
|
|
1256
|
+
},
|
|
1257
|
+
{
|
|
1258
|
+
type: 'documentSection',
|
|
1259
|
+
data: {
|
|
1260
|
+
name: 'Examination',
|
|
1261
|
+
label: 'Examination',
|
|
1262
|
+
segments: [{ type: 'field', id: 'exam_notes' }],
|
|
1263
|
+
fieldValues: { exam_notes: '' },
|
|
1264
|
+
},
|
|
1265
|
+
},
|
|
1266
|
+
],
|
|
1267
|
+
};
|
|
1268
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1269
|
+
kind: 'field',
|
|
1270
|
+
version: 2,
|
|
1271
|
+
time: Date.now(),
|
|
1272
|
+
sections: {
|
|
1273
|
+
Anamnesis: { Complaints: ['Tearing'] },
|
|
1274
|
+
Examination: { Notes: 'OK' },
|
|
1275
|
+
},
|
|
1276
|
+
});
|
|
1277
|
+
for (const [rowKey, vis] of [['od', '0.8'], ['os', '0.9']]) {
|
|
1278
|
+
doc.blocks[1].data.cells[cellFieldId(tableId, rowKey, 'vis')] = vis;
|
|
1279
|
+
}
|
|
1280
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
1281
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
1282
|
+
defaultFont: 'Roboto',
|
|
1283
|
+
fieldValueStyle: {},
|
|
1284
|
+
});
|
|
1285
|
+
assert.equal(collectPageBreaks(content), 0);
|
|
1286
|
+
assert.equal(findTextInPdfContent(content, 'Anamnesis'), false);
|
|
1287
|
+
assert.equal(findTextInPdfContent(content, '0.8'), true);
|
|
1288
|
+
assert.equal(findTextInPdfContent(content, '0.9'), true);
|
|
1289
|
+
assert.equal(findTextInPdfContent(content, 'OK'), true);
|
|
1290
|
+
});
|
|
1291
|
+
it('paginates a multi-row table naturally without forced page breaks', () => {
|
|
1292
|
+
const tableId = 'untitled_results';
|
|
1293
|
+
const template = {
|
|
1294
|
+
kind: 'template',
|
|
1295
|
+
version: 1,
|
|
1296
|
+
time: Date.now(),
|
|
1297
|
+
fieldSchemas: {
|
|
1298
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
1299
|
+
[tableId]: {
|
|
1300
|
+
type: 'table',
|
|
1301
|
+
name: 'Results',
|
|
1302
|
+
label: 'Results',
|
|
1303
|
+
columns: [{ key: 'col1', label: 'Column 1' }],
|
|
1304
|
+
rows: [{ key: 'row1', label: '' }],
|
|
1305
|
+
cellType: 'text',
|
|
1306
|
+
},
|
|
1307
|
+
exam_notes: { type: 'text', name: 'Notes', label: 'Notes' },
|
|
1308
|
+
},
|
|
1309
|
+
blocks: [
|
|
1310
|
+
{
|
|
1311
|
+
type: 'documentSection',
|
|
1312
|
+
data: {
|
|
1313
|
+
name: 'Anamnesis',
|
|
1314
|
+
label: 'Anamnesis',
|
|
1315
|
+
repeatable: true,
|
|
1316
|
+
segments: [
|
|
1317
|
+
{ type: 'text', content: 'Complaints: ' },
|
|
1318
|
+
{ type: 'field', id: 'complaints' },
|
|
1319
|
+
],
|
|
1320
|
+
fieldValues: { complaints: [] },
|
|
1321
|
+
},
|
|
1322
|
+
},
|
|
1323
|
+
{
|
|
1324
|
+
type: 'documentSection',
|
|
1325
|
+
data: {
|
|
1326
|
+
name: 'Untitled',
|
|
1327
|
+
label: 'Untitled section',
|
|
1328
|
+
segments: [{ type: 'table', id: tableId, rows: [{ key: 'row1', label: '' }] }],
|
|
1329
|
+
fieldValues: {},
|
|
1330
|
+
},
|
|
1331
|
+
},
|
|
1332
|
+
{
|
|
1333
|
+
type: 'documentSection',
|
|
1334
|
+
data: {
|
|
1335
|
+
name: 'Examination',
|
|
1336
|
+
label: 'Examination',
|
|
1337
|
+
segments: [{ type: 'field', id: 'exam_notes' }],
|
|
1338
|
+
fieldValues: { exam_notes: '' },
|
|
1339
|
+
},
|
|
1340
|
+
},
|
|
1341
|
+
],
|
|
1342
|
+
};
|
|
1343
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1344
|
+
kind: 'field',
|
|
1345
|
+
version: 2,
|
|
1346
|
+
time: Date.now(),
|
|
1347
|
+
sections: {
|
|
1348
|
+
Anamnesis: { Complaints: ['Tearing'] },
|
|
1349
|
+
Untitled: {
|
|
1350
|
+
Results: [{ col1: 'row-alpha' }, { col1: 'row-beta' }],
|
|
1351
|
+
},
|
|
1352
|
+
Examination: { Notes: 'OK' },
|
|
1353
|
+
},
|
|
1354
|
+
});
|
|
1355
|
+
const content = renderDocumentToPdfContent(doc, {
|
|
1356
|
+
resolveFontName: (name) => name ?? 'Roboto',
|
|
1357
|
+
defaultFont: 'Roboto',
|
|
1358
|
+
fieldValueStyle: {},
|
|
1359
|
+
});
|
|
1360
|
+
assert.equal(collectPageBreaks(content), 0);
|
|
1361
|
+
assert.equal(findTextInPdfContent(content, 'Anamnesis'), false);
|
|
1362
|
+
assert.equal(findTextInPdfContent(content, 'Tearing'), false);
|
|
1363
|
+
assert.equal(findTextInPdfContent(content, 'row-alpha'), true);
|
|
1364
|
+
assert.equal(findTextInPdfContent(content, 'row-beta'), true);
|
|
1365
|
+
assert.equal(findTextInPdfContent(content, 'OK'), true);
|
|
1366
|
+
});
|
|
1367
|
+
it('renders repeatable inline table chunks with section title in the page header', () => {
|
|
1368
|
+
const tableId = 'items_table';
|
|
1369
|
+
const template = {
|
|
1370
|
+
kind: 'template',
|
|
1371
|
+
version: 1,
|
|
1372
|
+
time: Date.now(),
|
|
1373
|
+
fieldSchemas: {
|
|
1374
|
+
[tableId]: {
|
|
1375
|
+
type: 'table',
|
|
1376
|
+
name: 'Results',
|
|
1377
|
+
label: 'Results',
|
|
1378
|
+
columns: [
|
|
1379
|
+
{ key: 'col1', label: 'Column 1' },
|
|
1380
|
+
{ key: 'col2', label: 'Column 2' },
|
|
1381
|
+
],
|
|
1382
|
+
cellType: 'text',
|
|
1383
|
+
},
|
|
1384
|
+
},
|
|
1385
|
+
blocks: [
|
|
1386
|
+
{
|
|
1387
|
+
type: 'documentSection',
|
|
1388
|
+
data: {
|
|
1389
|
+
name: 'items',
|
|
1390
|
+
label: 'items',
|
|
1391
|
+
repeatable: true,
|
|
1392
|
+
segments: [{ type: 'table', id: tableId, rows: [{ key: 'row1', label: '' }] }],
|
|
1393
|
+
fieldValues: {
|
|
1394
|
+
[cellFieldId(tableId, 'row1', 'col1')]: '111',
|
|
1395
|
+
[cellFieldId(tableId, 'row1', 'col2')]: 'aaa',
|
|
1396
|
+
[cellFieldId(tableId, 'row2', 'col1')]: '222',
|
|
1397
|
+
[cellFieldId(tableId, 'row2', 'col2')]: 'bbb',
|
|
1398
|
+
},
|
|
1399
|
+
},
|
|
1400
|
+
},
|
|
1401
|
+
],
|
|
1402
|
+
};
|
|
1403
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1404
|
+
kind: 'field',
|
|
1405
|
+
version: 2,
|
|
1406
|
+
time: Date.now(),
|
|
1407
|
+
sections: {
|
|
1408
|
+
items: {
|
|
1409
|
+
Results: [
|
|
1410
|
+
{ col1: '111', col2: 'aaa' },
|
|
1411
|
+
{ col1: '222', col2: 'bbb' },
|
|
1412
|
+
],
|
|
1413
|
+
},
|
|
1414
|
+
},
|
|
1415
|
+
});
|
|
1416
|
+
const content = renderDocumentToPdfContent(doc, defaultRenderOptions);
|
|
1417
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
1418
|
+
assert.equal(collectPageBreaks(content), 0);
|
|
1419
|
+
assert.ok(headerContainsText(docDefinition, 'items'));
|
|
1420
|
+
const tables = findAllPdfTables(content);
|
|
1421
|
+
assert.equal(tables.length, 1);
|
|
1422
|
+
assert.equal(tables[0].table.headerRows, 1);
|
|
1423
|
+
assert.equal(tables[0].table.body[0][0].text, 'Column 1');
|
|
1424
|
+
assert.equal(tables[0].table.body.length, 3);
|
|
1425
|
+
});
|
|
1426
|
+
it('renders a long repeatable inline table as one pdfmake table in page header mode', () => {
|
|
1427
|
+
const tableId = 'items_table';
|
|
1428
|
+
const template = {
|
|
1429
|
+
kind: 'template',
|
|
1430
|
+
version: 1,
|
|
1431
|
+
time: Date.now(),
|
|
1432
|
+
pageSetup: { format: 'a4', margin: 120 },
|
|
1433
|
+
fieldSchemas: {
|
|
1434
|
+
[tableId]: {
|
|
1435
|
+
type: 'table',
|
|
1436
|
+
name: 'Results',
|
|
1437
|
+
label: 'Results',
|
|
1438
|
+
columns: [{ key: 'col1', label: 'Column 1' }],
|
|
1439
|
+
cellType: 'text',
|
|
1440
|
+
},
|
|
1441
|
+
},
|
|
1442
|
+
blocks: [
|
|
1443
|
+
{
|
|
1444
|
+
type: 'documentSection',
|
|
1445
|
+
data: {
|
|
1446
|
+
name: 'items',
|
|
1447
|
+
label: 'items',
|
|
1448
|
+
repeatable: true,
|
|
1449
|
+
segments: [{ type: 'table', id: tableId, rows: [{ key: 'row1', label: '' }] }],
|
|
1450
|
+
fieldValues: Object.fromEntries(Array.from({ length: 12 }, (_, index) => [
|
|
1451
|
+
cellFieldId(tableId, `row${index + 1}`, 'col1'),
|
|
1452
|
+
`value-${index + 1}`,
|
|
1453
|
+
])),
|
|
1454
|
+
},
|
|
1455
|
+
},
|
|
1456
|
+
],
|
|
1457
|
+
};
|
|
1458
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1459
|
+
kind: 'field',
|
|
1460
|
+
version: 2,
|
|
1461
|
+
time: Date.now(),
|
|
1462
|
+
sections: {
|
|
1463
|
+
items: {
|
|
1464
|
+
Results: Array.from({ length: 12 }, (_, index) => ({
|
|
1465
|
+
col1: `value-${index + 1}`,
|
|
1466
|
+
})),
|
|
1467
|
+
},
|
|
1468
|
+
},
|
|
1469
|
+
});
|
|
1470
|
+
const content = renderDocumentToPdfContent(doc, defaultRenderOptions);
|
|
1471
|
+
const tables = findAllPdfTables(content);
|
|
1472
|
+
assert.equal(tables.length, 1);
|
|
1473
|
+
assert.equal(tables[0].table.headerRows, 1);
|
|
1474
|
+
assert.equal(tables[0].table.body.length, 13);
|
|
1475
|
+
assert.equal(collectPageBreaks(content), 0);
|
|
1476
|
+
});
|
|
1477
|
+
it('renders an adjacent items table after prior sections in page header mode', () => {
|
|
1478
|
+
const tableId = 'items_table';
|
|
1479
|
+
const template = {
|
|
1480
|
+
kind: 'template',
|
|
1481
|
+
version: 1,
|
|
1482
|
+
time: Date.now(),
|
|
1483
|
+
fieldSchemas: {
|
|
1484
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
1485
|
+
[tableId]: {
|
|
1486
|
+
type: 'table',
|
|
1487
|
+
name: 'Results',
|
|
1488
|
+
label: 'Results',
|
|
1489
|
+
columns: [{ key: 'col1', label: 'Column 1' }],
|
|
1490
|
+
cellType: 'text',
|
|
1491
|
+
},
|
|
1492
|
+
},
|
|
1493
|
+
blocks: [
|
|
1494
|
+
{
|
|
1495
|
+
type: 'documentSection',
|
|
1496
|
+
data: {
|
|
1497
|
+
name: 'Anamnesis',
|
|
1498
|
+
label: 'Anamnesis',
|
|
1499
|
+
segments: [
|
|
1500
|
+
{ type: 'text', content: 'Complaints: ' },
|
|
1501
|
+
{ type: 'field', id: 'complaints' },
|
|
1502
|
+
],
|
|
1503
|
+
fieldValues: { complaints: ['Tearing'] },
|
|
1504
|
+
},
|
|
1505
|
+
},
|
|
1506
|
+
{
|
|
1507
|
+
type: 'documentSection',
|
|
1508
|
+
data: {
|
|
1509
|
+
name: 'items',
|
|
1510
|
+
label: 'items',
|
|
1511
|
+
repeatable: true,
|
|
1512
|
+
segments: [{ type: 'table', id: tableId, rows: [{ key: 'row1', label: '' }] }],
|
|
1513
|
+
fieldValues: Object.fromEntries(Array.from({ length: 24 }, (_, index) => [
|
|
1514
|
+
cellFieldId(tableId, `row${index + 1}`, 'col1'),
|
|
1515
|
+
`value-${index + 1}`,
|
|
1516
|
+
])),
|
|
1517
|
+
},
|
|
1518
|
+
},
|
|
1519
|
+
],
|
|
1520
|
+
};
|
|
1521
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1522
|
+
kind: 'field',
|
|
1523
|
+
version: 2,
|
|
1524
|
+
time: Date.now(),
|
|
1525
|
+
sections: {
|
|
1526
|
+
Anamnesis: { Complaints: ['Tearing'] },
|
|
1527
|
+
items: {
|
|
1528
|
+
Results: Array.from({ length: 24 }, (_, index) => ({
|
|
1529
|
+
col1: `value-${index + 1}`,
|
|
1530
|
+
})),
|
|
1531
|
+
},
|
|
1532
|
+
},
|
|
1533
|
+
});
|
|
1534
|
+
const content = renderDocumentToPdfContent(doc, defaultRenderOptions);
|
|
1535
|
+
const tables = findAllPdfTables(content);
|
|
1536
|
+
assert.equal(tables.length, 1);
|
|
1537
|
+
assert.equal(tables[0].table.headerRows, 1);
|
|
1538
|
+
assert.equal(tables[0].table.body[0][0].text, 'Column 1');
|
|
1539
|
+
assert.equal(tables[0].table.body.length, 25);
|
|
1540
|
+
});
|
|
1541
|
+
it('puts repeatable section content in the page header and table chunks in the body', () => {
|
|
1542
|
+
const tableId = 'items_table';
|
|
1543
|
+
const rowCount = 24;
|
|
1544
|
+
const template = {
|
|
1545
|
+
kind: 'template',
|
|
1546
|
+
version: 1,
|
|
1547
|
+
time: Date.now(),
|
|
1548
|
+
fieldSchemas: {
|
|
1549
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
1550
|
+
lifeAnamnesis: { type: 'tree', name: 'Life history', label: 'Life history' },
|
|
1551
|
+
[tableId]: {
|
|
1552
|
+
type: 'table',
|
|
1553
|
+
name: 'Results',
|
|
1554
|
+
label: 'Results',
|
|
1555
|
+
columns: [
|
|
1556
|
+
{ key: 'col1', label: 'Column 1' },
|
|
1557
|
+
{ key: 'col2', label: 'Column 2' },
|
|
1558
|
+
],
|
|
1559
|
+
cellType: 'text',
|
|
1560
|
+
},
|
|
1561
|
+
},
|
|
1562
|
+
blocks: [
|
|
1563
|
+
{
|
|
1564
|
+
type: 'documentSection',
|
|
1565
|
+
data: {
|
|
1566
|
+
name: 'Anamnesis',
|
|
1567
|
+
label: 'Anamnesis',
|
|
1568
|
+
repeatable: true,
|
|
1569
|
+
segments: [
|
|
1570
|
+
{ type: 'text', content: 'Complaints: ' },
|
|
1571
|
+
{ type: 'field', id: 'complaints' },
|
|
1572
|
+
{ type: 'text', content: '.\nLife history: ' },
|
|
1573
|
+
{ type: 'field', id: 'lifeAnamnesis' },
|
|
1574
|
+
{ type: 'text', content: '.' },
|
|
1575
|
+
],
|
|
1576
|
+
fieldValues: {
|
|
1577
|
+
complaints: ['Tearing'],
|
|
1578
|
+
lifeAnamnesis: ['Unremarkable'],
|
|
1579
|
+
},
|
|
1580
|
+
},
|
|
1581
|
+
},
|
|
1582
|
+
{
|
|
1583
|
+
type: 'documentSection',
|
|
1584
|
+
data: {
|
|
1585
|
+
name: 'items',
|
|
1586
|
+
label: 'Items',
|
|
1587
|
+
segments: [{ type: 'table', id: tableId, rows: [{ key: 'row1', label: '' }] }],
|
|
1588
|
+
fieldValues: Object.fromEntries(Array.from({ length: rowCount }, (_, index) => [
|
|
1589
|
+
cellFieldId(tableId, `row${index + 1}`, 'col1'),
|
|
1590
|
+
`value-${index + 1}`,
|
|
1591
|
+
])),
|
|
1592
|
+
},
|
|
1593
|
+
},
|
|
1594
|
+
{
|
|
1595
|
+
type: 'documentSection',
|
|
1596
|
+
data: {
|
|
1597
|
+
name: 'Diagnosis',
|
|
1598
|
+
label: 'Diagnosis',
|
|
1599
|
+
segments: [{ type: 'text', content: 'Diagnosis note' }],
|
|
1600
|
+
fieldValues: {},
|
|
1601
|
+
},
|
|
1602
|
+
},
|
|
1603
|
+
],
|
|
1604
|
+
};
|
|
1605
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1606
|
+
kind: 'field',
|
|
1607
|
+
version: 2,
|
|
1608
|
+
time: Date.now(),
|
|
1609
|
+
sections: {
|
|
1610
|
+
Anamnesis: {
|
|
1611
|
+
Complaints: ['Tearing'],
|
|
1612
|
+
'Life history': ['Unremarkable'],
|
|
1613
|
+
},
|
|
1614
|
+
items: {
|
|
1615
|
+
Results: Array.from({ length: rowCount }, (_, index) => ({
|
|
1616
|
+
col1: `value-${index + 1}`,
|
|
1617
|
+
})),
|
|
1618
|
+
},
|
|
1619
|
+
Diagnosis: {},
|
|
1620
|
+
},
|
|
1621
|
+
});
|
|
1622
|
+
const content = renderDocumentToPdfContent(doc, defaultRenderOptions);
|
|
1623
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
1624
|
+
assert.ok(headerContainsText(docDefinition, 'Anamnesis'));
|
|
1625
|
+
assert.ok(headerContainsText(docDefinition, 'Tearing'));
|
|
1626
|
+
assert.ok(headerContainsText(docDefinition, 'Unremarkable'));
|
|
1627
|
+
assert.ok(headerContainsText(docDefinition, 'Complaints:'));
|
|
1628
|
+
assert.ok(headerContainsText(docDefinition, 'Life history:'));
|
|
1629
|
+
const tables = findAllPdfTables(content);
|
|
1630
|
+
assert.equal(tables.length, 1);
|
|
1631
|
+
assert.equal(tables[0].table.headerRows, 1);
|
|
1632
|
+
assert.equal(tables[0].table.body[0][0].text, 'Column 1');
|
|
1633
|
+
assert.equal(tables[0].table.body.length, rowCount + 1);
|
|
1634
|
+
// Items title is page-1 header companion content (with the stolen table in the body).
|
|
1635
|
+
assert.ok(headerContainsText(docDefinition, 'Items') || findTextInPdfContent(content, 'Items'), 'Items section title should appear in the PDF');
|
|
1636
|
+
assert.equal(collectPageBreaks(content), 0);
|
|
1637
|
+
assert.equal(findTextInPdfContent(content, 'Diagnosis note'), true);
|
|
1638
|
+
const standaloneItemsSections = content.filter((node) => {
|
|
1639
|
+
if (!Array.isArray(node.stack))
|
|
1640
|
+
return false;
|
|
1641
|
+
if (node.unbreakable || node.pageBreak === 'before')
|
|
1642
|
+
return false;
|
|
1643
|
+
const headers = node.stack.filter((child) => child?.style === 'sectionHeader');
|
|
1644
|
+
return headers.length === 1 && headers[0]?.text === 'Items';
|
|
1645
|
+
});
|
|
1646
|
+
assert.equal(standaloneItemsSections.length, 0);
|
|
1647
|
+
});
|
|
1648
|
+
it('keeps companion section leading prose above the adjacent table', () => {
|
|
1649
|
+
const itemsTableId = 'items_table';
|
|
1650
|
+
const template = {
|
|
1651
|
+
kind: 'template',
|
|
1652
|
+
version: 1,
|
|
1653
|
+
time: Date.now(),
|
|
1654
|
+
fieldSchemas: {
|
|
1655
|
+
price_group_name: { type: 'text', name: 'Name', label: 'Name' },
|
|
1656
|
+
item_group: { type: 'text', name: 'Group', label: 'Group' },
|
|
1657
|
+
[itemsTableId]: {
|
|
1658
|
+
type: 'table',
|
|
1659
|
+
name: 'Items',
|
|
1660
|
+
label: 'Items',
|
|
1661
|
+
columns: [
|
|
1662
|
+
{ key: 'item', label: 'Item' },
|
|
1663
|
+
{ key: 'price', label: 'Price' },
|
|
1664
|
+
],
|
|
1665
|
+
cellType: 'text',
|
|
1666
|
+
},
|
|
1667
|
+
},
|
|
1668
|
+
blocks: [
|
|
1669
|
+
{
|
|
1670
|
+
type: 'documentSection',
|
|
1671
|
+
data: {
|
|
1672
|
+
name: 'Price list',
|
|
1673
|
+
label: 'Price list',
|
|
1674
|
+
hideTitleInPreview: true,
|
|
1675
|
+
repeatable: true,
|
|
1676
|
+
segments: [
|
|
1677
|
+
{
|
|
1678
|
+
type: 'text',
|
|
1679
|
+
html: '<div class="document-align document-align--center" style="text-align: center"><b>PRICE LIST</b></div>',
|
|
1680
|
+
},
|
|
1681
|
+
{ type: 'text', content: 'Price group: ' },
|
|
1682
|
+
{ type: 'field', id: 'price_group_name' },
|
|
1683
|
+
],
|
|
1684
|
+
fieldValues: { price_group_name: 'sample_name' },
|
|
1685
|
+
},
|
|
1686
|
+
},
|
|
1687
|
+
{
|
|
1688
|
+
type: 'documentSection',
|
|
1689
|
+
data: {
|
|
1690
|
+
name: 'items',
|
|
1691
|
+
label: 'items',
|
|
1692
|
+
hideTitleInPreview: true,
|
|
1693
|
+
segments: [
|
|
1694
|
+
{ type: 'text', content: 'Item group ' },
|
|
1695
|
+
{ type: 'field', id: 'item_group' },
|
|
1696
|
+
{
|
|
1697
|
+
type: 'table',
|
|
1698
|
+
id: itemsTableId,
|
|
1699
|
+
rows: [{ key: 'row1', label: '' }],
|
|
1700
|
+
},
|
|
1701
|
+
],
|
|
1702
|
+
fieldValues: {
|
|
1703
|
+
item_group: 'sample_name / sample_name',
|
|
1704
|
+
[cellFieldId(itemsTableId, 'row1', 'item')]: 'sample_description',
|
|
1705
|
+
[cellFieldId(itemsTableId, 'row1', 'price')]: '1',
|
|
1706
|
+
},
|
|
1707
|
+
},
|
|
1708
|
+
},
|
|
1709
|
+
],
|
|
1710
|
+
};
|
|
1711
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1712
|
+
kind: 'field',
|
|
1713
|
+
version: 2,
|
|
1714
|
+
time: Date.now(),
|
|
1715
|
+
sections: {
|
|
1716
|
+
'Price list': { Name: 'sample_name' },
|
|
1717
|
+
items: {
|
|
1718
|
+
Group: 'sample_name / sample_name',
|
|
1719
|
+
Items: [{ item: 'sample_description', price: '1' }],
|
|
1720
|
+
},
|
|
1721
|
+
},
|
|
1722
|
+
});
|
|
1723
|
+
const content = renderDocumentToPdfContent(doc, defaultRenderOptions);
|
|
1724
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
1725
|
+
assert.ok(findTextInPdfContent(content, 'sample_description'), 'table cell should appear in body');
|
|
1726
|
+
// Companion leading lives in the page-1 header (same flow as Price group), not the body.
|
|
1727
|
+
assert.equal(findTextInPdfContent(content, 'Item group'), false, 'Item group must not sit in the body below a header-band gap');
|
|
1728
|
+
assert.ok(headerContainsText(docDefinition, 'PRICE LIST'));
|
|
1729
|
+
assert.ok(headerContainsText(docDefinition, 'Price group:'));
|
|
1730
|
+
assert.ok(headerContainsText(docDefinition, 'Item group'));
|
|
1731
|
+
assert.ok(headerContainsText(docDefinition, 'sample_name / sample_name'));
|
|
1732
|
+
const repeatableHeader = buildRepeatableSectionPageHeader(doc, defaultRenderOptions);
|
|
1733
|
+
assert.ok(repeatableHeader);
|
|
1734
|
+
assert.ok(nodeContainsText({ stack: repeatableHeader.firstPageStack }, 'Item group'), 'page-1 header stack includes Item group after Price group');
|
|
1735
|
+
assert.equal(nodeContainsText({ stack: repeatableHeader.stack }, 'Item group'), false, 'continuation-page header must not repeat Item group');
|
|
1736
|
+
if (typeof docDefinition.header === 'function') {
|
|
1737
|
+
assert.equal(nodeContainsText(docDefinition.header(2), 'Item group'), false, 'page 2 header omits companion leading prose');
|
|
1738
|
+
}
|
|
1739
|
+
});
|
|
1740
|
+
it('reserves enough header height for long wrapped Anamnesis prose', () => {
|
|
1741
|
+
const tableId = 'items_table';
|
|
1742
|
+
const longComplaints = [
|
|
1743
|
+
'Vision disturbance decreased acuity',
|
|
1744
|
+
'Tearing',
|
|
1745
|
+
'Redness JUST test , no more.',
|
|
1746
|
+
];
|
|
1747
|
+
const template = {
|
|
1748
|
+
kind: 'template',
|
|
1749
|
+
version: 1,
|
|
1750
|
+
time: Date.now(),
|
|
1751
|
+
fieldSchemas: {
|
|
1752
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
1753
|
+
lifeAnamnesis: { type: 'tree', name: 'Life history', label: 'Life history' },
|
|
1754
|
+
[tableId]: {
|
|
1755
|
+
type: 'table',
|
|
1756
|
+
name: 'Results',
|
|
1757
|
+
label: 'Results',
|
|
1758
|
+
columns: [
|
|
1759
|
+
{ key: 'col1', label: 'Column 1' },
|
|
1760
|
+
{ key: 'col2', label: 'Column 2' },
|
|
1761
|
+
],
|
|
1762
|
+
cellType: 'text',
|
|
1763
|
+
},
|
|
1764
|
+
},
|
|
1765
|
+
blocks: [
|
|
1766
|
+
{
|
|
1767
|
+
type: 'documentSection',
|
|
1768
|
+
data: {
|
|
1769
|
+
name: 'Anamnesis',
|
|
1770
|
+
label: 'Anamnesis',
|
|
1771
|
+
repeatable: true,
|
|
1772
|
+
segments: [
|
|
1773
|
+
{ type: 'text', content: 'Complaints: ' },
|
|
1774
|
+
{ type: 'field', id: 'complaints' },
|
|
1775
|
+
{ type: 'text', content: '.\nLife history: ' },
|
|
1776
|
+
{ type: 'field', id: 'lifeAnamnesis' },
|
|
1777
|
+
{ type: 'text', content: '.' },
|
|
1778
|
+
],
|
|
1779
|
+
fieldValues: {
|
|
1780
|
+
complaints: longComplaints,
|
|
1781
|
+
lifeAnamnesis: ['Chronic conditions diabetes mellitus', 'Unremarkable'],
|
|
1782
|
+
},
|
|
1783
|
+
},
|
|
1784
|
+
},
|
|
1785
|
+
{
|
|
1786
|
+
type: 'documentSection',
|
|
1787
|
+
data: {
|
|
1788
|
+
name: 'items',
|
|
1789
|
+
label: 'Items',
|
|
1790
|
+
segments: [{ type: 'table', id: tableId, rows: [{ key: 'row1', label: '' }] }],
|
|
1791
|
+
fieldValues: {},
|
|
1792
|
+
},
|
|
1793
|
+
},
|
|
1794
|
+
],
|
|
1795
|
+
};
|
|
1796
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1797
|
+
kind: 'field',
|
|
1798
|
+
version: 2,
|
|
1799
|
+
time: Date.now(),
|
|
1800
|
+
sections: {
|
|
1801
|
+
Anamnesis: {
|
|
1802
|
+
Complaints: longComplaints,
|
|
1803
|
+
'Life history': ['Chronic conditions diabetes mellitus', 'Unremarkable'],
|
|
1804
|
+
},
|
|
1805
|
+
items: { Results: [{ col1: '111' }] },
|
|
1806
|
+
},
|
|
1807
|
+
});
|
|
1808
|
+
const renderOptions = { ...defaultRenderOptions, pageSetup: { format: 'a4', margin: 15 } };
|
|
1809
|
+
const repeatableHeader = buildRepeatableSectionPageHeader(doc, renderOptions);
|
|
1810
|
+
assert.ok(repeatableHeader);
|
|
1811
|
+
assert.equal(findHeaderBodyBlocks(repeatableHeader.stack).length, 1);
|
|
1812
|
+
const pageSetup = {
|
|
1813
|
+
format: 'a4',
|
|
1814
|
+
margin: 15,
|
|
1815
|
+
header: { height: repeatableHeader.heightMm, fromRepeatableSection: true },
|
|
1816
|
+
};
|
|
1817
|
+
const estimatedPt = estimatePdfContentHeightPt(repeatableHeader.firstPageStack, pageSetup);
|
|
1818
|
+
assert.ok(estimatedPt <= mmToPt(repeatableHeader.heightMm));
|
|
1819
|
+
assert.equal(repeatableHeader.heightPt, computeRepeatableHeaderBandHeightPt(repeatableHeader.firstPageStack, pageSetup));
|
|
1820
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, renderOptions);
|
|
1821
|
+
assert.ok(headerContainsText(docDefinition, 'Life history:'));
|
|
1822
|
+
assert.ok(headerContainsText(docDefinition, 'Chronic conditions diabetes mellitus'));
|
|
1823
|
+
assert.ok(headerContainsText(docDefinition, 'Unremarkable'));
|
|
1824
|
+
});
|
|
1825
|
+
it('does not over-reserve empty space after a short Show-on-each-page header', () => {
|
|
1826
|
+
const template = {
|
|
1827
|
+
kind: 'template',
|
|
1828
|
+
version: 1,
|
|
1829
|
+
time: Date.now(),
|
|
1830
|
+
fieldSchemas: {
|
|
1831
|
+
price_group_name: { type: 'text', name: 'Name', label: 'Name' },
|
|
1832
|
+
item_name: { type: 'text', name: 'Item', label: 'Item' },
|
|
1833
|
+
},
|
|
1834
|
+
blocks: [
|
|
1835
|
+
{
|
|
1836
|
+
type: 'documentSection',
|
|
1837
|
+
data: {
|
|
1838
|
+
name: 'header',
|
|
1839
|
+
label: 'Price list',
|
|
1840
|
+
repeatable: true,
|
|
1841
|
+
segments: [
|
|
1842
|
+
{ type: 'text', content: 'Price group: ' },
|
|
1843
|
+
{ type: 'field', id: 'price_group_name', placeholder: 'Name' },
|
|
1844
|
+
],
|
|
1845
|
+
fieldValues: { price_group_name: 'Diller' },
|
|
1846
|
+
},
|
|
1847
|
+
},
|
|
1848
|
+
{
|
|
1849
|
+
type: 'documentSection',
|
|
1850
|
+
data: {
|
|
1851
|
+
name: 'items',
|
|
1852
|
+
label: 'items',
|
|
1853
|
+
hideTitleInPreview: true,
|
|
1854
|
+
segments: [
|
|
1855
|
+
{ type: 'text', content: 'Item group: ' },
|
|
1856
|
+
{ type: 'field', id: 'item_name', placeholder: 'Item' },
|
|
1857
|
+
],
|
|
1858
|
+
fieldValues: { item_name: '' },
|
|
1859
|
+
},
|
|
1860
|
+
},
|
|
1861
|
+
],
|
|
1862
|
+
};
|
|
1863
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1864
|
+
kind: 'field',
|
|
1865
|
+
version: 2,
|
|
1866
|
+
time: Date.now(),
|
|
1867
|
+
sections: {
|
|
1868
|
+
header: { Name: 'Diller' },
|
|
1869
|
+
items: [{ Item: 'Product' }, { Item: 'Product / Apparel' }],
|
|
1870
|
+
},
|
|
1871
|
+
});
|
|
1872
|
+
const renderOptions = { ...defaultRenderOptions, pageSetup: { format: 'a4', margin: 15 } };
|
|
1873
|
+
const repeatableHeader = buildRepeatableSectionPageHeader(doc, renderOptions);
|
|
1874
|
+
assert.ok(repeatableHeader);
|
|
1875
|
+
const contentPt = estimatePdfContentHeightPt(repeatableHeader.stack, {
|
|
1876
|
+
format: 'a4',
|
|
1877
|
+
margin: 15,
|
|
1878
|
+
header: { height: repeatableHeader.heightMm, fromRepeatableSection: true },
|
|
1879
|
+
});
|
|
1880
|
+
// Reserved band may include a small anti-clip slack, but must not leave
|
|
1881
|
+
// multiple blank lines after a short Show-on-each-page header.
|
|
1882
|
+
const reservedExtraPt = repeatableHeader.heightPt - contentPt;
|
|
1883
|
+
assert.ok(reservedExtraPt <= HEADER_LINE_SLACK_PT + HEADER_HEIGHT_BUFFER_PT + 0.01);
|
|
1884
|
+
assert.ok(reservedExtraPt < 12);
|
|
1885
|
+
});
|
|
1886
|
+
it('reserves header height for trailing prose lines like 123 and 456', () => {
|
|
1887
|
+
const template = {
|
|
1888
|
+
kind: 'template',
|
|
1889
|
+
version: 1,
|
|
1890
|
+
time: Date.now(),
|
|
1891
|
+
fieldSchemas: {
|
|
1892
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
1893
|
+
lifeAnamnesis: { type: 'tree', name: 'Life history', label: 'Life history' },
|
|
1894
|
+
},
|
|
1895
|
+
blocks: [
|
|
1896
|
+
{
|
|
1897
|
+
type: 'documentSection',
|
|
1898
|
+
data: {
|
|
1899
|
+
name: 'Anamnesis',
|
|
1900
|
+
label: 'Anamnesis',
|
|
1901
|
+
repeatable: true,
|
|
1902
|
+
segments: [
|
|
1903
|
+
{ type: 'text', content: 'Complaints: ' },
|
|
1904
|
+
{ type: 'field', id: 'complaints' },
|
|
1905
|
+
{ type: 'text', content: '.\nLife history: ' },
|
|
1906
|
+
{ type: 'field', id: 'lifeAnamnesis' },
|
|
1907
|
+
{ type: 'text', content: '.\n123\n456' },
|
|
1908
|
+
],
|
|
1909
|
+
fieldValues: {
|
|
1910
|
+
complaints: ['Tearing'],
|
|
1911
|
+
lifeAnamnesis: ['Unremarkable'],
|
|
1912
|
+
},
|
|
1913
|
+
},
|
|
1914
|
+
},
|
|
1915
|
+
],
|
|
1916
|
+
};
|
|
1917
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1918
|
+
kind: 'field',
|
|
1919
|
+
version: 2,
|
|
1920
|
+
time: Date.now(),
|
|
1921
|
+
sections: {
|
|
1922
|
+
Anamnesis: {
|
|
1923
|
+
Complaints: ['Tearing'],
|
|
1924
|
+
'Life history': ['Unremarkable'],
|
|
1925
|
+
},
|
|
1926
|
+
},
|
|
1927
|
+
});
|
|
1928
|
+
const renderOptions = { ...defaultRenderOptions, pageSetup: { format: 'a4', margin: 15 } };
|
|
1929
|
+
const repeatableHeader = buildRepeatableSectionPageHeader(doc, renderOptions);
|
|
1930
|
+
assert.ok(repeatableHeader);
|
|
1931
|
+
const pageSetup = { format: 'a4', margin: 15, header: { height: repeatableHeader.heightMm, fromRepeatableSection: true } };
|
|
1932
|
+
const prose = nodeTextJoined(findHeaderBodyBlocks(repeatableHeader.stack)[0]);
|
|
1933
|
+
assert.ok(prose.includes('123'));
|
|
1934
|
+
assert.ok(prose.includes('456'));
|
|
1935
|
+
assert.ok(repeatableHeader.heightPt
|
|
1936
|
+
> estimatePdfContentHeightPt(repeatableHeader.stack, pageSetup) + HEADER_LINE_SLACK_PT);
|
|
1937
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, renderOptions);
|
|
1938
|
+
assert.ok(headerContainsText(docDefinition, '123'));
|
|
1939
|
+
assert.ok(headerContainsText(docDefinition, '456'));
|
|
1940
|
+
});
|
|
1941
|
+
it('merges label and field value into the same header prose block', () => {
|
|
1942
|
+
const template = {
|
|
1943
|
+
kind: 'template',
|
|
1944
|
+
version: 1,
|
|
1945
|
+
time: Date.now(),
|
|
1946
|
+
fieldSchemas: {
|
|
1947
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
1948
|
+
lifeAnamnesis: { type: 'tree', name: 'Life history', label: 'Life history' },
|
|
1949
|
+
},
|
|
1950
|
+
blocks: [
|
|
1951
|
+
{
|
|
1952
|
+
type: 'documentSection',
|
|
1953
|
+
data: {
|
|
1954
|
+
name: 'Anamnesis',
|
|
1955
|
+
label: 'Anamnesis',
|
|
1956
|
+
repeatable: true,
|
|
1957
|
+
segments: [
|
|
1958
|
+
{ type: 'text', content: 'Complaints: ' },
|
|
1959
|
+
{ type: 'field', id: 'complaints' },
|
|
1960
|
+
{ type: 'text', content: '.\nLife history: ' },
|
|
1961
|
+
{ type: 'field', id: 'lifeAnamnesis' },
|
|
1962
|
+
{ type: 'text', content: '.' },
|
|
1963
|
+
],
|
|
1964
|
+
fieldValues: {
|
|
1965
|
+
complaints: ['Tearing', 'Redness'],
|
|
1966
|
+
lifeAnamnesis: ['Unremarkable'],
|
|
1967
|
+
},
|
|
1968
|
+
},
|
|
1969
|
+
},
|
|
1970
|
+
],
|
|
1971
|
+
};
|
|
1972
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
1973
|
+
kind: 'field',
|
|
1974
|
+
version: 2,
|
|
1975
|
+
time: Date.now(),
|
|
1976
|
+
sections: {
|
|
1977
|
+
Anamnesis: {
|
|
1978
|
+
Complaints: ['Tearing', 'Redness'],
|
|
1979
|
+
'Life history': ['Unremarkable'],
|
|
1980
|
+
},
|
|
1981
|
+
},
|
|
1982
|
+
});
|
|
1983
|
+
const repeatableHeader = buildRepeatableSectionPageHeader(doc, defaultRenderOptions);
|
|
1984
|
+
assert.ok(repeatableHeader);
|
|
1985
|
+
const bodyBlocks = findHeaderBodyBlocks(repeatableHeader.stack);
|
|
1986
|
+
assert.equal(bodyBlocks.length, 1);
|
|
1987
|
+
const prose = nodeTextJoined(bodyBlocks[0]);
|
|
1988
|
+
assert.ok(prose.includes('Complaints:'));
|
|
1989
|
+
assert.ok(prose.includes('Tearing'));
|
|
1990
|
+
assert.ok(prose.includes('Redness'));
|
|
1991
|
+
assert.ok(prose.includes('Life history:'));
|
|
1992
|
+
assert.ok(prose.includes('Unremarkable'));
|
|
1993
|
+
});
|
|
1994
|
+
it('merges clinical diagnosis label value and punctuation into one prose block', () => {
|
|
1995
|
+
const doc = {
|
|
1996
|
+
kind: 'document',
|
|
1997
|
+
version: 2,
|
|
1998
|
+
time: Date.now(),
|
|
1999
|
+
fieldSchemas: {
|
|
2000
|
+
clinicalDiagnosis: { type: 'choice', name: 'Clinical diagnosis', label: 'Clinical diagnosis' },
|
|
2001
|
+
},
|
|
2002
|
+
blocks: [],
|
|
2003
|
+
};
|
|
2004
|
+
const ctx = createPdfRenderContext(doc, {
|
|
2005
|
+
...defaultRenderOptions,
|
|
2006
|
+
fieldSchemas: doc.fieldSchemas,
|
|
2007
|
+
});
|
|
2008
|
+
ctx.fieldValues = { clinicalDiagnosis: 'Conjunctivitis' };
|
|
2009
|
+
const blocks = renderSegmentsToPdfContent([
|
|
2010
|
+
{ type: 'text', content: '.\nClinical diagnosis: ' },
|
|
2011
|
+
{ type: 'field', id: 'clinicalDiagnosis' },
|
|
2012
|
+
{ type: 'text', content: '.' },
|
|
2013
|
+
], ctx);
|
|
2014
|
+
assert.equal(blocks.length, 1);
|
|
2015
|
+
assert.equal(nodeTextJoined(blocks[0]), '.\nClinical diagnosis: Conjunctivitis.');
|
|
2016
|
+
const fieldPart = blocks[0].text.find((part) => typeof part === 'object' && String(part.text ?? '').includes('Conjunctivitis'));
|
|
2017
|
+
assert.equal(fieldPart?.decoration, undefined);
|
|
2018
|
+
assert.equal(fieldPart?.decorationColor, undefined);
|
|
2019
|
+
const labelPart = blocks[0].text.find((part) => typeof part === 'object' && String(part.text ?? '').includes('Clinical diagnosis'));
|
|
2020
|
+
assert.equal(labelPart?.font, fieldPart?.font);
|
|
2021
|
+
assert.equal(labelPart?.font, 'Roboto');
|
|
2022
|
+
});
|
|
2023
|
+
it('does not apply fieldHighlight underline decoration in PDF export', () => {
|
|
2024
|
+
const doc = {
|
|
2025
|
+
kind: 'document',
|
|
2026
|
+
version: 2,
|
|
2027
|
+
time: Date.now(),
|
|
2028
|
+
pageSetup: { fieldHighlight: { color: '#7c3aed' } },
|
|
2029
|
+
fieldSchemas: {
|
|
2030
|
+
clinicalDiagnosis: { type: 'choice', name: 'Clinical diagnosis', label: 'Clinical diagnosis' },
|
|
2031
|
+
},
|
|
2032
|
+
blocks: [],
|
|
2033
|
+
};
|
|
2034
|
+
const ctx = createPdfRenderContext(doc, defaultRenderOptions);
|
|
2035
|
+
ctx.fieldValues = { clinicalDiagnosis: 'Conjunctivitis' };
|
|
2036
|
+
const blocks = renderSegmentsToPdfContent([
|
|
2037
|
+
{ type: 'field', id: 'clinicalDiagnosis' },
|
|
2038
|
+
], ctx);
|
|
2039
|
+
const fieldPart = blocks[0].text.find((part) => typeof part === 'object' && String(part.text ?? '').includes('Conjunctivitis'));
|
|
2040
|
+
assert.equal(fieldPart?.decoration, undefined);
|
|
2041
|
+
assert.equal(fieldPart?.decorationColor, undefined);
|
|
2042
|
+
});
|
|
2043
|
+
it('renders child field segments as nested repeater PDF content', () => {
|
|
2044
|
+
const doc = {
|
|
2045
|
+
kind: 'document',
|
|
2046
|
+
version: 2,
|
|
2047
|
+
time: Date.now(),
|
|
2048
|
+
fieldSchemas: {
|
|
2049
|
+
addressField: {
|
|
2050
|
+
type: 'child',
|
|
2051
|
+
label: 'address',
|
|
2052
|
+
name: 'address',
|
|
2053
|
+
fieldSchemas: {},
|
|
2054
|
+
template: {
|
|
2055
|
+
fieldSchemas: {
|
|
2056
|
+
city: { type: 'text', name: 'City', label: 'City' },
|
|
2057
|
+
address: { type: 'text', name: 'Address', label: 'Address' },
|
|
2058
|
+
},
|
|
2059
|
+
blocks: [
|
|
2060
|
+
{
|
|
2061
|
+
type: 'documentSection',
|
|
2062
|
+
data: {
|
|
2063
|
+
label: '',
|
|
2064
|
+
segments: [
|
|
2065
|
+
{ type: 'field', id: 'city' },
|
|
2066
|
+
{ type: 'field', id: 'address' },
|
|
2067
|
+
],
|
|
2068
|
+
fieldValues: {},
|
|
2069
|
+
},
|
|
2070
|
+
},
|
|
2071
|
+
],
|
|
2072
|
+
},
|
|
2073
|
+
},
|
|
2074
|
+
},
|
|
2075
|
+
blocks: [],
|
|
2076
|
+
};
|
|
2077
|
+
const ctx = createPdfRenderContext(doc, defaultRenderOptions);
|
|
2078
|
+
ctx.fieldValues = {
|
|
2079
|
+
addressField: { city: 'milan', address: 'via per arogno 4' },
|
|
2080
|
+
};
|
|
2081
|
+
const blocks = renderSegmentsToPdfContent([
|
|
2082
|
+
{ type: 'child', id: 'addressField' },
|
|
2083
|
+
], ctx);
|
|
2084
|
+
assert.ok(blocks.length >= 1);
|
|
2085
|
+
const text = blocks.map((node) => nodeTextJoined(node)).join('\n');
|
|
2086
|
+
assert.match(text, /address:/);
|
|
2087
|
+
assert.match(text, /milan/);
|
|
2088
|
+
assert.match(text, /via per arogno 4/);
|
|
2089
|
+
});
|
|
2090
|
+
it('omits empty child field segments when hideEmptyValues is true', () => {
|
|
2091
|
+
const doc = {
|
|
2092
|
+
kind: 'document',
|
|
2093
|
+
version: 2,
|
|
2094
|
+
time: Date.now(),
|
|
2095
|
+
fieldSchemas: {
|
|
2096
|
+
addressField: {
|
|
2097
|
+
type: 'child',
|
|
2098
|
+
label: 'address',
|
|
2099
|
+
name: 'address',
|
|
2100
|
+
fieldSchemas: {},
|
|
2101
|
+
template: {
|
|
2102
|
+
fieldSchemas: {
|
|
2103
|
+
city: { type: 'text', name: 'City', label: 'City' },
|
|
2104
|
+
},
|
|
2105
|
+
blocks: [
|
|
2106
|
+
{
|
|
2107
|
+
type: 'documentSection',
|
|
2108
|
+
data: {
|
|
2109
|
+
label: '',
|
|
2110
|
+
segments: [{ type: 'field', id: 'city' }],
|
|
2111
|
+
fieldValues: {},
|
|
2112
|
+
},
|
|
2113
|
+
},
|
|
2114
|
+
],
|
|
2115
|
+
},
|
|
2116
|
+
},
|
|
2117
|
+
},
|
|
2118
|
+
blocks: [],
|
|
2119
|
+
};
|
|
2120
|
+
const ctx = createPdfRenderContext(doc, { ...defaultRenderOptions, hideEmptyValues: true });
|
|
2121
|
+
ctx.fieldValues = { addressField: {} };
|
|
2122
|
+
const blocks = renderSegmentsToPdfContent([
|
|
2123
|
+
{ type: 'field', id: 'addressField' },
|
|
2124
|
+
], ctx);
|
|
2125
|
+
assert.equal(blocks.length, 0);
|
|
2126
|
+
});
|
|
2127
|
+
it('omits section header nodes when section label is empty', () => {
|
|
2128
|
+
assert.equal(buildRepeatableSectionTitleNode(''), null);
|
|
2129
|
+
assert.equal(buildRepeatableSectionTitleNode(' '), null);
|
|
2130
|
+
const doc = {
|
|
2131
|
+
kind: 'document',
|
|
2132
|
+
version: 2,
|
|
2133
|
+
time: Date.now(),
|
|
2134
|
+
fieldSchemas: {},
|
|
2135
|
+
blocks: [
|
|
2136
|
+
{
|
|
2137
|
+
type: 'documentSection',
|
|
2138
|
+
data: {
|
|
2139
|
+
label: '',
|
|
2140
|
+
segments: [{ type: 'text', content: 'Section body only.' }],
|
|
2141
|
+
fieldValues: {},
|
|
2142
|
+
},
|
|
2143
|
+
},
|
|
2144
|
+
],
|
|
2145
|
+
};
|
|
2146
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
2147
|
+
const def = docDefinition;
|
|
2148
|
+
const headers = collectNodesWithStyle(def.content, 'sectionHeader');
|
|
2149
|
+
assert.equal(headers.length, 0);
|
|
2150
|
+
assert.ok(chunkContainsText({ stack: def.content }, 'Section body only.'));
|
|
2151
|
+
});
|
|
2152
|
+
it('omits section header nodes when hideTitleInPreview is true', () => {
|
|
2153
|
+
const doc = {
|
|
2154
|
+
kind: 'document',
|
|
2155
|
+
version: 2,
|
|
2156
|
+
time: Date.now(),
|
|
2157
|
+
fieldSchemas: {},
|
|
2158
|
+
blocks: [
|
|
2159
|
+
{
|
|
2160
|
+
type: 'documentSection',
|
|
2161
|
+
data: {
|
|
2162
|
+
label: 'Order Details',
|
|
2163
|
+
hideTitleInPreview: true,
|
|
2164
|
+
segments: [{ type: 'text', content: 'GoldenDent' }],
|
|
2165
|
+
fieldValues: {},
|
|
2166
|
+
},
|
|
2167
|
+
},
|
|
2168
|
+
{
|
|
2169
|
+
type: 'documentSection',
|
|
2170
|
+
data: {
|
|
2171
|
+
label: 'Addresses',
|
|
2172
|
+
hideTitleInPreview: false,
|
|
2173
|
+
segments: [{ type: 'text', content: 'Payment Address' }],
|
|
2174
|
+
fieldValues: {},
|
|
2175
|
+
},
|
|
2176
|
+
},
|
|
2177
|
+
],
|
|
2178
|
+
};
|
|
2179
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
2180
|
+
const titleHeaders = collectNodesWithStyle(docDefinition.content, 'sectionHeader');
|
|
2181
|
+
assert.deepEqual(titleHeaders.map((node) => node.text), ['Addresses']);
|
|
2182
|
+
assert.ok(chunkContainsText({ stack: docDefinition.content }, 'GoldenDent'));
|
|
2183
|
+
});
|
|
2184
|
+
it('omits section header nodes when hideTitleInPreview is the string true', () => {
|
|
2185
|
+
const doc = {
|
|
2186
|
+
kind: 'document',
|
|
2187
|
+
version: 2,
|
|
2188
|
+
time: Date.now(),
|
|
2189
|
+
fieldSchemas: {},
|
|
2190
|
+
blocks: [
|
|
2191
|
+
{
|
|
2192
|
+
type: 'documentSection',
|
|
2193
|
+
data: {
|
|
2194
|
+
label: 'Header',
|
|
2195
|
+
hideTitleInPreview: 'true',
|
|
2196
|
+
segments: [{ type: 'text', content: 'Invoice body' }],
|
|
2197
|
+
fieldValues: {},
|
|
2198
|
+
},
|
|
2199
|
+
},
|
|
2200
|
+
],
|
|
2201
|
+
};
|
|
2202
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
2203
|
+
const titleHeaders = collectNodesWithStyle(docDefinition.content, 'sectionHeader');
|
|
2204
|
+
assert.equal(titleHeaders.length, 0);
|
|
2205
|
+
assert.ok(chunkContainsText({ stack: docDefinition.content }, 'Invoice body'));
|
|
2206
|
+
});
|
|
2207
|
+
it('formats date fields with the default display format instead of ISO', () => {
|
|
2208
|
+
const doc = {
|
|
2209
|
+
kind: 'document',
|
|
2210
|
+
version: 2,
|
|
2211
|
+
time: Date.now(),
|
|
2212
|
+
fieldSchemas: {
|
|
2213
|
+
order_date: { type: 'date', label: 'Date', name: 'Date', dateFormat: 'dd/mm/yyyy' },
|
|
2214
|
+
},
|
|
2215
|
+
blocks: [
|
|
2216
|
+
{
|
|
2217
|
+
type: 'documentSection',
|
|
2218
|
+
data: {
|
|
2219
|
+
label: 'Header',
|
|
2220
|
+
hideTitleInPreview: true,
|
|
2221
|
+
segments: [{ type: 'field', id: 'order_date' }],
|
|
2222
|
+
fieldValues: { order_date: '2026-08-19' },
|
|
2223
|
+
},
|
|
2224
|
+
},
|
|
2225
|
+
],
|
|
2226
|
+
};
|
|
2227
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
2228
|
+
assert.ok(chunkContainsText({ stack: docDefinition.content }, '19/08/2026'));
|
|
2229
|
+
assert.equal(chunkContainsText({ stack: docDefinition.content }, '2026-08-19'), false);
|
|
2230
|
+
});
|
|
2231
|
+
it('keeps newline characters inline within header prose blocks', () => {
|
|
2232
|
+
const doc = {
|
|
2233
|
+
kind: 'document',
|
|
2234
|
+
version: 2,
|
|
2235
|
+
time: Date.now(),
|
|
2236
|
+
fieldSchemas: {},
|
|
2237
|
+
blocks: [],
|
|
2238
|
+
};
|
|
2239
|
+
const ctx = createPdfRenderContext(doc, defaultRenderOptions);
|
|
2240
|
+
const blocks = renderSegmentsToPdfProseBlocks([
|
|
2241
|
+
{ type: 'text', content: 'Line one.\nLine two.' },
|
|
2242
|
+
], ctx);
|
|
2243
|
+
assert.equal(blocks.length, 1);
|
|
2244
|
+
assert.equal(nodeTextJoined(blocks[0]), 'Line one.\nLine two.');
|
|
2245
|
+
const textParts = /** @type {Array<string | Record<string, unknown>>} */ (blocks[0].text);
|
|
2246
|
+
assert.ok(textParts.some((part) => {
|
|
2247
|
+
const text = typeof part === 'string' ? part : String(part.text ?? '');
|
|
2248
|
+
return text.includes('\n');
|
|
2249
|
+
}));
|
|
2250
|
+
assert.ok(!textParts.some((part) => typeof part === 'object' && part.lineBreak === true));
|
|
2251
|
+
});
|
|
2252
|
+
it('renders mammology plain-content row separators in PDF prose blocks', () => {
|
|
2253
|
+
const doc = {
|
|
2254
|
+
kind: 'document',
|
|
2255
|
+
version: 2,
|
|
2256
|
+
time: Date.now(),
|
|
2257
|
+
fieldSchemas: {
|
|
2258
|
+
statusLocalis: { type: 'tree', name: 'ST.LOCALIS', label: 'ST.LOCALIS' },
|
|
2259
|
+
focalPalpation: { type: 'tree', name: 'Focal palpation', label: 'Вогнещевої патології пальпаторно' },
|
|
2260
|
+
},
|
|
2261
|
+
blocks: [],
|
|
2262
|
+
};
|
|
2263
|
+
const ctx = createPdfRenderContext(doc, defaultRenderOptions);
|
|
2264
|
+
ctx.fieldValues = { statusLocalis: ['дольчата'], focalPalpation: ['до 1 см'] };
|
|
2265
|
+
const blocks = renderSegmentsToPdfProseBlocks([
|
|
2266
|
+
{ type: 'text', content: 'ST.LOCALIS: ' },
|
|
2267
|
+
{ type: 'field', id: 'statusLocalis' },
|
|
2268
|
+
{ type: 'text', content: '.\nВогнещевої патології пальпаторно: ' },
|
|
2269
|
+
{ type: 'field', id: 'focalPalpation' },
|
|
2270
|
+
], ctx);
|
|
2271
|
+
assert.equal(blocks.length, 1);
|
|
2272
|
+
const prose = nodeTextJoined(blocks[0]);
|
|
2273
|
+
assert.doesNotMatch(prose, /дольчата\.Вогнещевої/);
|
|
2274
|
+
assert.match(prose, /ST\.LOCALIS:/);
|
|
2275
|
+
assert.match(prose, /дольчата/);
|
|
2276
|
+
assert.match(prose, /Вогнещевої патології пальпаторно:/);
|
|
2277
|
+
const textParts = /** @type {Array<string | Record<string, unknown>>} */ (blocks[0].text);
|
|
2278
|
+
assert.ok(textParts.some((part) => {
|
|
2279
|
+
const text = typeof part === 'string' ? part : String(part.text ?? '');
|
|
2280
|
+
return text.includes('\n');
|
|
2281
|
+
}));
|
|
2282
|
+
assertAllPartsUseFont(textParts);
|
|
2283
|
+
});
|
|
2284
|
+
it('exports mammology section with inline font on title and prose labels', () => {
|
|
2285
|
+
const doc = {
|
|
2286
|
+
kind: 'document',
|
|
2287
|
+
version: 2,
|
|
2288
|
+
time: Date.now(),
|
|
2289
|
+
fieldSchemas: {
|
|
2290
|
+
statusLocalis: { type: 'tree', name: 'ST.LOCALIS', label: 'ST.LOCALIS' },
|
|
2291
|
+
},
|
|
2292
|
+
blocks: [
|
|
2293
|
+
{
|
|
2294
|
+
type: 'documentSection',
|
|
2295
|
+
data: {
|
|
2296
|
+
label: 'Огляд',
|
|
2297
|
+
segments: [
|
|
2298
|
+
{ type: 'text', content: 'ST.LOCALIS: ' },
|
|
2299
|
+
{ type: 'field', id: 'statusLocalis' },
|
|
2300
|
+
],
|
|
2301
|
+
fieldValues: { statusLocalis: ['дольчата'] },
|
|
2302
|
+
},
|
|
2303
|
+
},
|
|
2304
|
+
],
|
|
2305
|
+
};
|
|
2306
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, {});
|
|
2307
|
+
const def = docDefinition;
|
|
2308
|
+
assert.equal(def.defaultStyle.font, 'Roboto');
|
|
2309
|
+
const sectionStack = def.content[0].stack;
|
|
2310
|
+
assert.equal(sectionStack[0].font, 'Roboto');
|
|
2311
|
+
assert.equal(sectionStack[0].bold, true);
|
|
2312
|
+
const proseParts = sectionStack[1].text;
|
|
2313
|
+
assertAllPartsUseFont(proseParts);
|
|
2314
|
+
const labelPart = proseParts.find((part) => String(part.text ?? '').includes('ST.LOCALIS'));
|
|
2315
|
+
const fieldPart = proseParts.find((part) => String(part.text ?? '').includes('дольчата'));
|
|
2316
|
+
assert.equal(labelPart?.font, fieldPart?.font);
|
|
2317
|
+
});
|
|
2318
|
+
it('renders mammology row separators and bold labels in PDF prose blocks', () => {
|
|
2319
|
+
const doc = {
|
|
2320
|
+
kind: 'document',
|
|
2321
|
+
version: 2,
|
|
2322
|
+
time: Date.now(),
|
|
2323
|
+
fieldSchemas: {},
|
|
2324
|
+
blocks: [],
|
|
2325
|
+
};
|
|
2326
|
+
const ctx = createPdfRenderContext(doc, defaultRenderOptions);
|
|
2327
|
+
ctx.fieldValues = { statusLocalis: 'дольчата', focalPalpation: 'до 1 см' };
|
|
2328
|
+
const blocks = renderSegmentsToPdfProseBlocks([
|
|
2329
|
+
{ type: 'text', html: '<b>ST.LOCALIS: </b>' },
|
|
2330
|
+
{ type: 'field', id: 'statusLocalis' },
|
|
2331
|
+
{ type: 'text', html: '<b>.\nВогнещевої патології пальпаторно: </b>' },
|
|
2332
|
+
{ type: 'field', id: 'focalPalpation' },
|
|
2333
|
+
], ctx);
|
|
2334
|
+
assert.equal(blocks.length, 1);
|
|
2335
|
+
const prose = nodeTextJoined(blocks[0]);
|
|
2336
|
+
assert.doesNotMatch(prose, /дольчата\.Вогнещевої/);
|
|
2337
|
+
assert.match(prose, /ST\.LOCALIS:/);
|
|
2338
|
+
assert.match(prose, /дольчата/);
|
|
2339
|
+
assert.match(prose, /Вогнещевої патології пальпаторно:/);
|
|
2340
|
+
const textParts = /** @type {Array<string | Record<string, unknown>>} */ (blocks[0].text);
|
|
2341
|
+
assert.ok(textParts.some((part) => typeof part === 'object' && part.bold === true));
|
|
2342
|
+
assert.ok(textParts.some((part) => typeof part === 'object' && part.bold === false && String(part.text ?? '').includes('дольчата')));
|
|
2343
|
+
assert.ok(textParts.some((part) => typeof part === 'object' && part.bold === false && String(part.text ?? '').includes('до 1 см')));
|
|
2344
|
+
});
|
|
2345
|
+
it('stampExplicitPdfBold leaves unmixed paragraphs unchanged', () => {
|
|
2346
|
+
const parts = ['plain text', { text: 'more text', fontSize: 15 }];
|
|
2347
|
+
assert.deepEqual(stampExplicitPdfBold(parts), parts);
|
|
2348
|
+
});
|
|
2349
|
+
it('stampExplicitPdfBold sets bold:false on non-bold parts in mixed paragraphs', () => {
|
|
2350
|
+
const stamped = stampExplicitPdfBold([
|
|
2351
|
+
{ text: 'Label: ', bold: true },
|
|
2352
|
+
'value text',
|
|
2353
|
+
]);
|
|
2354
|
+
assert.equal(stamped[0].bold, true);
|
|
2355
|
+
assert.equal(stamped[1].bold, false);
|
|
2356
|
+
assert.equal(stamped[1].text, 'value text');
|
|
2357
|
+
});
|
|
2358
|
+
it('ensurePdfPartFonts stamps font on string fragments after bold stamping', () => {
|
|
2359
|
+
const stamped = finalizePdfInlineParts([
|
|
2360
|
+
{ text: 'Label: ', bold: true, font: 'dejavu' },
|
|
2361
|
+
'value text',
|
|
2362
|
+
], { font: 'dejavu' });
|
|
2363
|
+
assert.equal(stamped[1].font, 'dejavu');
|
|
2364
|
+
assert.equal(stamped[1].bold, false);
|
|
2365
|
+
});
|
|
2366
|
+
it('buildPdfSectionTitleNode inlines section header font', () => {
|
|
2367
|
+
const node = buildPdfSectionTitleNode('Огляд', { font: 'dejavu', bold: true });
|
|
2368
|
+
assert.equal(node?.font, 'dejavu');
|
|
2369
|
+
assert.equal(node?.bold, true);
|
|
2370
|
+
assert.equal(node?.style, 'sectionHeader');
|
|
2371
|
+
assert.deepEqual(node?.margin, [0, 0, 0, 6]);
|
|
2372
|
+
});
|
|
2373
|
+
it('static bold labels and field values share the same font in mixed prose', () => {
|
|
2374
|
+
const doc = {
|
|
2375
|
+
kind: 'document',
|
|
2376
|
+
version: 2,
|
|
2377
|
+
time: Date.now(),
|
|
2378
|
+
fieldSchemas: {
|
|
2379
|
+
statusLocalis: { type: 'tree', name: 'ST.LOCALIS', label: 'ST.LOCALIS' },
|
|
2380
|
+
},
|
|
2381
|
+
blocks: [],
|
|
2382
|
+
};
|
|
2383
|
+
const ctx = createPdfRenderContext(doc, defaultRenderOptions);
|
|
2384
|
+
ctx.fieldValues = { statusLocalis: ['дольчата'] };
|
|
2385
|
+
const blocks = renderSegmentsToPdfProseBlocks([
|
|
2386
|
+
{ type: 'text', html: '<b>ST.LOCALIS: </b>' },
|
|
2387
|
+
{ type: 'field', id: 'statusLocalis' },
|
|
2388
|
+
], ctx);
|
|
2389
|
+
assert.equal(blocks.length, 1);
|
|
2390
|
+
const textParts = /** @type {Array<Record<string, unknown>>} */ (blocks[0].text);
|
|
2391
|
+
const labelPart = textParts.find((part) => String(part.text ?? '').includes('ST.LOCALIS'));
|
|
2392
|
+
const fieldPart = textParts.find((part) => String(part.text ?? '').includes('дольчата'));
|
|
2393
|
+
assert.equal(labelPart?.font, 'Roboto');
|
|
2394
|
+
assert.equal(fieldPart?.font, 'Roboto');
|
|
2395
|
+
assert.equal(labelPart?.font, fieldPart?.font);
|
|
2396
|
+
});
|
|
2397
|
+
it('keeps intentional bold on field values in mixed prose paragraphs', () => {
|
|
2398
|
+
const doc = {
|
|
2399
|
+
kind: 'document',
|
|
2400
|
+
version: 2,
|
|
2401
|
+
time: Date.now(),
|
|
2402
|
+
fieldSchemas: {
|
|
2403
|
+
statusLocalis: { type: 'tree', name: 'ST.LOCALIS', label: 'ST.LOCALIS' },
|
|
2404
|
+
diagnosis: {
|
|
2405
|
+
type: 'list',
|
|
2406
|
+
name: 'Diagnosis',
|
|
2407
|
+
label: 'ДІАГНОЗ',
|
|
2408
|
+
displayStyle: { fontWeight: 'bold' },
|
|
2409
|
+
},
|
|
2410
|
+
},
|
|
2411
|
+
blocks: [],
|
|
2412
|
+
};
|
|
2413
|
+
const ctx = createPdfRenderContext(doc, defaultRenderOptions);
|
|
2414
|
+
ctx.fieldValues = { statusLocalis: ['дольчата'], diagnosis: ['Мастопатія'] };
|
|
2415
|
+
const blocks = renderSegmentsToPdfProseBlocks([
|
|
2416
|
+
{ type: 'text', html: '<b>ST.LOCALIS: </b>' },
|
|
2417
|
+
{ type: 'field', id: 'statusLocalis' },
|
|
2418
|
+
{ type: 'text', html: '<b>.\nДІАГНОЗ: </b>' },
|
|
2419
|
+
{ type: 'field', id: 'diagnosis' },
|
|
2420
|
+
], ctx);
|
|
2421
|
+
assert.equal(blocks.length, 1);
|
|
2422
|
+
const textParts = /** @type {Array<Record<string, unknown>>} */ (blocks[0].text);
|
|
2423
|
+
assert.ok(textParts.some((part) => part.bold === true && String(part.text ?? '').includes('ST.LOCALIS')));
|
|
2424
|
+
assert.ok(textParts.some((part) => part.bold === false && String(part.text ?? '').includes('дольчата')));
|
|
2425
|
+
assert.ok(textParts.some((part) => part.bold === true && String(part.text ?? '').includes('Мастопатія')));
|
|
2426
|
+
});
|
|
2427
|
+
it('keeps bold styling on html row separators with embedded newlines', () => {
|
|
2428
|
+
const blocks = htmlToPdfBlocks('<b>.\nLabel: </b>');
|
|
2429
|
+
assert.equal(blocks.length, 1);
|
|
2430
|
+
const parts = blocks[0].parts;
|
|
2431
|
+
assert.equal(parts.length, 1);
|
|
2432
|
+
assert.equal(parts[0].bold, true);
|
|
2433
|
+
assert.match(String(parts[0].text ?? ''), /\.\nLabel:/);
|
|
2434
|
+
});
|
|
2435
|
+
it('applies segment alignment to centered h2 html blocks', () => {
|
|
2436
|
+
const doc = {
|
|
2437
|
+
kind: 'document',
|
|
2438
|
+
version: 2,
|
|
2439
|
+
time: Date.now(),
|
|
2440
|
+
fieldSchemas: {},
|
|
2441
|
+
blocks: [],
|
|
2442
|
+
};
|
|
2443
|
+
const ctx = createPdfRenderContext(doc, defaultRenderOptions);
|
|
2444
|
+
const blocks = renderSegmentsToPdfProseBlocks([
|
|
2445
|
+
{ type: 'text', html: '<h2>Рекомендовано</h2>', align: 'center' },
|
|
2446
|
+
], ctx);
|
|
2447
|
+
assert.equal(blocks.length, 1);
|
|
2448
|
+
assert.equal(blocks[0].alignment, 'center');
|
|
2449
|
+
assert.equal(blocks[0].text[0].fontSize, 16.2);
|
|
2450
|
+
assert.match(nodeTextJoined(blocks[0]), /Рекомендовано/);
|
|
2451
|
+
});
|
|
2452
|
+
it('centers h2 inside aligned html wrapper', () => {
|
|
2453
|
+
const blocks = htmlToPdfBlocks('<div class="document-align document-align--center" style="text-align: center"><h2>Рекомендовано</h2></div>');
|
|
2454
|
+
assert.equal(blocks.length, 1);
|
|
2455
|
+
assert.equal(blocks[0].alignment, 'center');
|
|
2456
|
+
assert.match(nodeTextJoined({ text: blocks[0].parts }), /Рекомендовано/);
|
|
2457
|
+
});
|
|
2458
|
+
it('treats inline html as prose parts and block html as separate nodes', () => {
|
|
2459
|
+
assert.equal(isBlockLevelHtml('<b>Label: </b>'), false);
|
|
2460
|
+
assert.equal(isBlockLevelHtml('<h2>Title</h2>'), true);
|
|
2461
|
+
});
|
|
2462
|
+
it('converts plain text newlines to pdfmake line breaks', () => {
|
|
2463
|
+
const parts = plainTextToPdfText('Line one.\nLine two.');
|
|
2464
|
+
assert.deepEqual(parts, ['Line one.\nLine two.']);
|
|
2465
|
+
});
|
|
2466
|
+
it('converts html newlines to br before pdf block parsing', () => {
|
|
2467
|
+
assert.equal(plainTextNewlinesToBrHtml('<b>.\nLabel: </b>'), '<b>.<br>Label: </b>');
|
|
2468
|
+
const blocks = htmlToPdfBlocks('<b>.\nLabel: </b>');
|
|
2469
|
+
assert.equal(blocks.length, 1);
|
|
2470
|
+
assert.match(nodeTextJoined(blocks[0]), /\.\s*Label:/);
|
|
2471
|
+
assert.doesNotMatch(nodeTextJoined(blocks[0]), /\.Label:/);
|
|
2472
|
+
assert.equal(blocks[0].parts.length, 1);
|
|
2473
|
+
assert.equal(blocks[0].parts[0].bold, true);
|
|
2474
|
+
assert.match(String(blocks[0].parts[0].text ?? ''), /\n/);
|
|
2475
|
+
});
|
|
2476
|
+
it('paginates an adjacent vision table with repeatable section content in the page header', () => {
|
|
2477
|
+
const tableId = 'visionTable';
|
|
2478
|
+
const template = {
|
|
2479
|
+
kind: 'template',
|
|
2480
|
+
version: 1,
|
|
2481
|
+
time: Date.now(),
|
|
2482
|
+
pageSetup: { format: 'a4', margin: 120 },
|
|
2483
|
+
fieldSchemas: {
|
|
2484
|
+
complaints: { type: 'tree', name: 'Complaints', label: 'Complaints' },
|
|
2485
|
+
[tableId]: {
|
|
2486
|
+
type: 'table',
|
|
2487
|
+
name: 'Visual acuity',
|
|
2488
|
+
label: 'Visual acuity',
|
|
2489
|
+
columns: [
|
|
2490
|
+
{ key: 'vis', label: 'vis' },
|
|
2491
|
+
{ key: 'sph', label: 'Sph' },
|
|
2492
|
+
],
|
|
2493
|
+
rows: Array.from({ length: 12 }, (_, index) => ({
|
|
2494
|
+
key: `row${index + 1}`,
|
|
2495
|
+
label: `Row ${index + 1}`,
|
|
2496
|
+
})),
|
|
2497
|
+
cellType: 'text',
|
|
2498
|
+
},
|
|
2499
|
+
},
|
|
2500
|
+
blocks: [
|
|
2501
|
+
{
|
|
2502
|
+
type: 'documentSection',
|
|
2503
|
+
data: {
|
|
2504
|
+
name: 'Anamnesis',
|
|
2505
|
+
label: 'Anamnesis',
|
|
2506
|
+
repeatable: true,
|
|
2507
|
+
segments: [{ type: 'field', id: 'complaints' }],
|
|
2508
|
+
fieldValues: { complaints: ['Tearing'] },
|
|
2509
|
+
},
|
|
2510
|
+
},
|
|
2511
|
+
{
|
|
2512
|
+
type: 'visionTable',
|
|
2513
|
+
data: { fieldId: tableId, cells: {} },
|
|
2514
|
+
},
|
|
2515
|
+
],
|
|
2516
|
+
};
|
|
2517
|
+
const doc = mergeTemplateAndDocument(template, {
|
|
2518
|
+
kind: 'field',
|
|
2519
|
+
version: 2,
|
|
2520
|
+
time: Date.now(),
|
|
2521
|
+
sections: {
|
|
2522
|
+
Anamnesis: { Complaints: ['Tearing'] },
|
|
2523
|
+
},
|
|
2524
|
+
});
|
|
2525
|
+
for (let index = 0; index < 12; index += 1) {
|
|
2526
|
+
doc.blocks[1].data.cells[cellFieldId(tableId, `row${index + 1}`, 'vis')] = `value-${index + 1}`;
|
|
2527
|
+
}
|
|
2528
|
+
const content = renderDocumentToPdfContent(doc, defaultRenderOptions);
|
|
2529
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
2530
|
+
assert.ok(headerContainsText(docDefinition, 'Anamnesis'));
|
|
2531
|
+
assert.ok(headerContainsText(docDefinition, 'Tearing'));
|
|
2532
|
+
const tables = findAllPdfTables(content);
|
|
2533
|
+
assert.equal(tables.length, 1);
|
|
2534
|
+
assert.equal(tables[0].table.headerRows, 1);
|
|
2535
|
+
assert.equal(tables[0].table.body[0][0].text, 'vis');
|
|
2536
|
+
assert.equal(tables[0].table.body.length, 13);
|
|
2537
|
+
assert.equal(collectPageBreaks(content), 0);
|
|
2538
|
+
assert.equal(content.some((node) => node?.table), true);
|
|
2539
|
+
});
|
|
2540
|
+
});
|
|
2541
|
+
describe('generatePdfFromTemplate', () => {
|
|
2542
|
+
it('returns a PDF buffer with text content', async () => {
|
|
2543
|
+
const buffer = await generatePdfFromTemplate({
|
|
2544
|
+
template: makeTemplate(),
|
|
2545
|
+
document: makeDocument(),
|
|
2546
|
+
pageSetup: { title: 'Document summary' },
|
|
2547
|
+
});
|
|
2548
|
+
assert.ok(Buffer.isBuffer(buffer));
|
|
2549
|
+
assert.ok(buffer.length > 500);
|
|
2550
|
+
assert.equal(buffer.subarray(0, 4).toString('ascii'), '%PDF');
|
|
2551
|
+
});
|
|
2552
|
+
it('defaults pageSetup from the template', () => {
|
|
2553
|
+
const template = makeTemplate();
|
|
2554
|
+
template.pageSetup = { format: 'letter', title: 'Saved title' };
|
|
2555
|
+
const { docDefinition } = renderDocumentToPdfDefinition(mergeTemplateAndDocument(template, makeDocument()), {});
|
|
2556
|
+
const def = docDefinition;
|
|
2557
|
+
assert.equal(def.pageSize, resolvePageSize('letter'));
|
|
2558
|
+
assert.equal(def.content[0].text, 'Saved title');
|
|
2559
|
+
});
|
|
2560
|
+
});
|
|
2561
|
+
describe('generateDocumentPdf', () => {
|
|
2562
|
+
it('generates a larger PDF when Cyrillic text is present', async () => {
|
|
2563
|
+
const template = makeTemplate();
|
|
2564
|
+
template.blocks[0].data.segments.unshift({
|
|
2565
|
+
type: 'text',
|
|
2566
|
+
content: 'Осмотр: ',
|
|
2567
|
+
});
|
|
2568
|
+
const doc = mergeTemplateAndDocument(template, makeDocument());
|
|
2569
|
+
const buffer = await generateDocumentPdf(doc);
|
|
2570
|
+
assert.ok(buffer.length > 500);
|
|
2571
|
+
assert.equal(buffer.subarray(0, 4).toString('ascii'), '%PDF');
|
|
2572
|
+
});
|
|
2573
|
+
it('omits hidden section titles from the JSON API renderer', () => {
|
|
2574
|
+
const doc = {
|
|
2575
|
+
kind: 'document',
|
|
2576
|
+
version: 2,
|
|
2577
|
+
time: Date.now(),
|
|
2578
|
+
fieldSchemas: {},
|
|
2579
|
+
blocks: [
|
|
2580
|
+
{
|
|
2581
|
+
type: 'documentSection',
|
|
2582
|
+
data: {
|
|
2583
|
+
label: 'Order Details',
|
|
2584
|
+
hideTitleInPreview: true,
|
|
2585
|
+
segments: [
|
|
2586
|
+
{ type: 'text', content: 'Date Added: ' },
|
|
2587
|
+
{ type: 'text', content: '2026-07-10' },
|
|
2588
|
+
],
|
|
2589
|
+
fieldValues: {},
|
|
2590
|
+
},
|
|
2591
|
+
},
|
|
2592
|
+
{
|
|
2593
|
+
type: 'documentSection',
|
|
2594
|
+
data: {
|
|
2595
|
+
label: 'Addresses',
|
|
2596
|
+
hideTitleInPreview: false,
|
|
2597
|
+
segments: [{ type: 'text', content: 'Payment Address' }],
|
|
2598
|
+
fieldValues: {},
|
|
2599
|
+
},
|
|
2600
|
+
},
|
|
2601
|
+
],
|
|
2602
|
+
};
|
|
2603
|
+
const { docDefinition } = renderDocumentToPdfDefinition(doc, defaultRenderOptions);
|
|
2604
|
+
const text = (docDefinition.content ?? [])
|
|
2605
|
+
.map((node) => nodeTextJoined(node))
|
|
2606
|
+
.join('\n');
|
|
2607
|
+
const titleHeaders = collectNodesWithStyle(docDefinition.content, 'sectionHeader');
|
|
2608
|
+
assert.doesNotMatch(text, /Order Details/);
|
|
2609
|
+
assert.match(text, /Addresses/);
|
|
2610
|
+
assert.match(text, /Payment Address/);
|
|
2611
|
+
assert.deepEqual(titleHeaders.map((node) => node.text), ['Addresses']);
|
|
2612
|
+
});
|
|
2613
|
+
});
|
|
2614
|
+
//# sourceMappingURL=pdf-renderer.test.js.map
|