Skip to content

Commit 251e260

Browse files
committed
feat: support raw object or raw string for text
1 parent d5753d9 commit 251e260

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

demo/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pdfeasy.add([
2222
{ raw: 'A simple pdf', text: { font: 'Roboto' }},
2323
{ lineBreak: {} },
2424
{ raw: 'using...', text: {}, position: { x: 250, y: 0 } },
25-
{ raw: 'hm...', text: {} },
25+
{ raw: 'hm...' },
26+
'aaaaa...',
2627
{ pageBreak: {} },
2728
{ raw: 'pdfeasy!', text: {} },
2829
{ raw: 'a first in list...', list: { style: 'counter' } },

packages/core/src/resolvers.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ export const resolveContent = async (
5151
globals: InternalGlobals,
5252
run: RunOptionsBase
5353
) => {
54+
const addOnlyStringText = async (str: string) => {
55+
await app.text(str, {
56+
indent: defaults.text.indent,
57+
align: defaults.text.align,
58+
paragraphGap: defaults.text.paragraphMargin,
59+
lineGap: defaults.text.lineHeight,
60+
destination: defaults.text.destination,
61+
goTo: defaults.text.go,
62+
})
63+
}
64+
65+
if (typeof content === 'string') {
66+
await addOnlyStringText(content)
67+
68+
return
69+
}
70+
5471
const possibleLastPos = globals.__LAST_POSITION__
5572

5673
let position = content?.position
@@ -65,7 +82,13 @@ export const resolveContent = async (
6582
const addStack = async () => {
6683
const stack = content.stack as Content[]
6784

68-
await stack.forEach((entity) => {
85+
await stack.forEach(async (entity) => {
86+
if (typeof entity === 'string') {
87+
await addOnlyStringText(entity)
88+
89+
return
90+
}
91+
6992
if (!entity.text || !entity.raw) return
7093

7194
const isLast = stack.length - 1 === stack.indexOf(entity)
@@ -359,6 +382,25 @@ export const resolveContent = async (
359382
return
360383
}
361384

385+
if (
386+
!content.stack &&
387+
!content.text &&
388+
!content.image &&
389+
!content.svg &&
390+
!content.lineBreak &&
391+
!content.pageBreak &&
392+
!content.table &&
393+
!content.form &&
394+
!content.checkbox &&
395+
!content.position &&
396+
!content.qrcode &&
397+
content.raw
398+
) {
399+
addSimpleText()
400+
401+
return
402+
}
403+
362404
if (content.stack) await addStack()
363405
if (content.text) await addText()
364406
if (content.image || content.svg) await addImage()

packages/core/src/runner.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,20 @@ export class PDFEasy {
112112
}
113113

114114
private posUpdateContent = (content: Content) => {
115+
if (typeof content === 'string') {
116+
this.globals.__LAST_CONTENT__ = { raw: content, text: {} }
117+
this.globals.__LAST_POSITION__ = null
118+
119+
return
120+
}
121+
115122
this.globals.__LAST_CONTENT__ = content
116123
this.globals.__LAST_POSITION__ = content.position || null
117124
}
118125

119126
private getType = (content: Content): ItemType => {
127+
if (typeof content === 'string') return 'paragraph'
128+
120129
if (content.checkbox) return 'checkbox'
121130
if (content.list) return 'list'
122131
if (content.lineBreak) return 'line-break'

packages/core/src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export interface InternalGlobals {
177177
__BACKGROUND_RAW__: string
178178
}
179179
__LAST_TYPE__: [ItemType, number]
180-
__LAST_CONTENT__: Record<any, any>
180+
__LAST_CONTENT__: Record<any, any> | string
181181
__LAST_POSITION__: { x: number; y: number } | null
182182
}
183183

@@ -294,7 +294,7 @@ export interface ContentFormulary<T extends ContentFormularyType> {
294294
: never
295295
}
296296

297-
export interface Content {
297+
export interface ContentObject {
298298
raw?: string
299299
position?: { x: number; y: number }
300300
stack?: Content[]
@@ -310,6 +310,8 @@ export interface Content {
310310
qrcode?: ContentQRCode
311311
}
312312

313+
export type Content = ContentObject | string
314+
313315
export interface PDFEasyDefaults {
314316
text: DefaultsText
315317
lineBreak: DefaultsLineBreak

0 commit comments

Comments
 (0)