@idevs/corelib 0.0.97 → 1.0.1
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/CHANGELOG.md +743 -0
- package/README.md +221 -24
- package/dist/editors/checkboxButtonEditor.d.ts +3 -1
- package/dist/editors/checkboxButtonEditor.d.ts.map +1 -0
- package/dist/editors/checkboxButtonEditor.js +28 -18
- package/dist/editors/checkboxButtonEditor.js.map +1 -1
- package/dist/editors/dateMonthEditor.d.ts +3 -1
- package/dist/editors/dateMonthEditor.d.ts.map +1 -0
- package/dist/editors/dateMonthEditor.js +14 -7
- package/dist/editors/dateMonthEditor.js.map +1 -1
- package/dist/editors/index.d.ts +2 -1
- package/dist/editors/index.d.ts.map +1 -0
- package/dist/editors/index.js +18 -2
- package/dist/editors/index.js.map +1 -1
- package/dist/formatters/formatters.d.ts +2 -1
- package/dist/formatters/formatters.d.ts.map +1 -0
- package/dist/formatters/formatters.js +35 -23
- package/dist/formatters/formatters.js.map +1 -1
- package/dist/formatters/index.d.ts +2 -1
- package/dist/formatters/index.d.ts.map +1 -0
- package/dist/formatters/index.js +17 -1
- package/dist/formatters/index.js.map +1 -1
- package/dist/globals/index.d.ts +17 -10
- package/dist/globals/index.d.ts.map +1 -0
- package/dist/globals/index.js +65 -105
- package/dist/globals/index.js.map +1 -1
- package/dist/helpers/custom-fonts.d.ts +2 -1
- package/dist/helpers/custom-fonts.d.ts.map +1 -0
- package/dist/helpers/custom-fonts.js +4 -1
- package/dist/helpers/custom-fonts.js.map +1 -1
- package/dist/helpers/dialogHelper.d.ts +6 -9
- package/dist/helpers/dialogHelper.d.ts.map +1 -0
- package/dist/helpers/dialogHelper.js +17 -10
- package/dist/helpers/dialogHelper.js.map +1 -1
- package/dist/helpers/excelExportHelper.d.ts +2 -1
- package/dist/helpers/excelExportHelper.d.ts.map +1 -0
- package/dist/helpers/excelExportHelper.js +7 -4
- package/dist/helpers/excelExportHelper.js.map +1 -1
- package/dist/helpers/gridHelper.d.ts +2 -1
- package/dist/helpers/gridHelper.d.ts.map +1 -0
- package/dist/helpers/gridHelper.js +14 -9
- package/dist/helpers/gridHelper.js.map +1 -1
- package/dist/helpers/index.d.ts +2 -1
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/index.js +20 -4
- package/dist/helpers/index.js.map +1 -1
- package/dist/helpers/pdfExportHelper.d.ts +2 -1
- package/dist/helpers/pdfExportHelper.d.ts.map +1 -0
- package/dist/helpers/pdfExportHelper.js +83 -17
- package/dist/helpers/pdfExportHelper.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -5
- package/dist/index.js.map +1 -1
- package/dist/types/common.d.ts +12 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/common.js +3 -0
- package/dist/types/common.js.map +1 -0
- package/dist/types/export.d.ts +50 -0
- package/dist/types/export.d.ts.map +1 -0
- package/dist/types/export.js +24 -0
- package/dist/types/export.js.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +19 -0
- package/dist/types/index.js.map +1 -0
- package/dist/ui/DropdownToolButton.d.ts +2 -1
- package/dist/ui/DropdownToolButton.d.ts.map +1 -0
- package/dist/ui/DropdownToolButton.js +15 -11
- package/dist/ui/DropdownToolButton.js.map +1 -1
- package/dist/ui/ToggleToolButton.d.ts +2 -1
- package/dist/ui/ToggleToolButton.d.ts.map +1 -0
- package/dist/ui/ToggleToolButton.js +7 -3
- package/dist/ui/ToggleToolButton.js.map +1 -1
- package/dist/ui/index.d.ts +2 -1
- package/dist/ui/index.d.ts.map +1 -0
- package/dist/ui/index.js +18 -2
- package/dist/ui/index.js.map +1 -1
- package/dist/utils/date.d.ts +16 -0
- package/dist/utils/date.d.ts.map +1 -0
- package/dist/utils/date.js +85 -0
- package/dist/utils/date.js.map +1 -0
- package/dist/utils/dom.d.ts +13 -0
- package/dist/utils/dom.d.ts.map +1 -0
- package/dist/utils/dom.js +78 -0
- package/dist/utils/dom.js.map +1 -0
- package/dist/utils/format.d.ts +22 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/format.js +100 -0
- package/dist/utils/format.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +48 -29
- package/dist/ui/ToolDropdownButton.d.ts +0 -49
- package/dist/ui/ToolDropdownButton.js +0 -230
- package/dist/ui/ToolDropdownButton.js.map +0 -1
- package/src/editors/checkboxButtonEditor.ts +0 -184
- package/src/editors/dateMonthEditor.ts +0 -47
- package/src/editors/index.ts +0 -2
- package/src/formatters/formatters.ts +0 -185
- package/src/formatters/index.ts +0 -1
- package/src/globals/index.ts +0 -385
- package/src/helpers/custom-fonts.d.ts +0 -5
- package/src/helpers/custom-fonts.ts +0 -18
- package/src/helpers/dialogHelper.ts +0 -143
- package/src/helpers/excelExportHelper.ts +0 -29
- package/src/helpers/gridHelper.ts +0 -107
- package/src/helpers/html2pdf.d.ts +0 -19
- package/src/helpers/index.ts +0 -4
- package/src/helpers/pdfExportHelper.ts +0 -179
- package/src/index.ts +0 -5
- package/src/tsconfig.json +0 -29
- package/src/ui/DropdownToolButton.ts +0 -305
- package/src/ui/ToggleToolButton.ts +0 -71
- package/src/ui/index.ts +0 -2
- /package/{css → dist/css}/idevs.dropdown.css +0 -0
- /package/{css → dist/css}/idevs.print.css +0 -0
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { StringEditor, ToolButton } from '@serenity-is/corelib'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Use this function within updateInterface()
|
|
5
|
-
* @param targets
|
|
6
|
-
* @constructor
|
|
7
|
-
*/
|
|
8
|
-
export function RemoveSelect2ClearButton(...targets: string[]): void {
|
|
9
|
-
for (let i = 0; i < targets.length; i++) {
|
|
10
|
-
$(`.${targets[i]} .select2-search-choice-close`)?.remove()
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function EmailValidator(value: string): string | null {
|
|
15
|
-
// eslint-disable-next-line no-useless-escape
|
|
16
|
-
const filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
|
|
17
|
-
return filter.test(value) ? null : 'Invalid email address'
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function NumberOnly(e: Event): boolean {
|
|
21
|
-
const ev = e as KeyboardEvent
|
|
22
|
-
if ('0123456789'.includes(ev.key)) {
|
|
23
|
-
return true
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
e.preventDefault()
|
|
27
|
-
return false
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Use GetDialogHelper function within function initToolbar()
|
|
32
|
-
* @param helper
|
|
33
|
-
* @constructor
|
|
34
|
-
*/
|
|
35
|
-
export function GetDialogHelper(helper: DialogHelper): DialogHelper {
|
|
36
|
-
if (helper) {
|
|
37
|
-
return helper
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return new DialogHelper()
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export class DialogHelper {
|
|
44
|
-
/**
|
|
45
|
-
* Use this function within function getToolbarButtons()
|
|
46
|
-
* @param buttons
|
|
47
|
-
*/
|
|
48
|
-
public getToolbarButtons(buttons: ToolButton[]): ToolButton[] {
|
|
49
|
-
const applyButton = buttons.find(b => b.cssClass === 'apply-changes-button')
|
|
50
|
-
if (applyButton) {
|
|
51
|
-
applyButton.visible = false
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return buttons
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Use this function within constructor
|
|
59
|
-
* @param targets
|
|
60
|
-
*/
|
|
61
|
-
public inputNumberOnly(...targets: StringEditor[]): void {
|
|
62
|
-
for (let i = 0; i < targets.length; i++) {
|
|
63
|
-
targets[i].element.on('keypress', NumberOnly)
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
private NumberOnly(e: Event): boolean {
|
|
68
|
-
const ev = e as KeyboardEvent
|
|
69
|
-
if ('0123456789'.includes(ev.key)) {
|
|
70
|
-
return true
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
e.preventDefault()
|
|
74
|
-
return false
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public enabledEditItem<T>(form: T, ...targets: string[]): void {
|
|
78
|
-
const frm = form as { [key: string]: object }
|
|
79
|
-
for (let i = 0; i < targets.length; i++) {
|
|
80
|
-
if (frm[targets[i]].constructor.name == 'LookupEditor') {
|
|
81
|
-
;(frm[targets[i]] as HTMLInputElement).readOnly = false
|
|
82
|
-
} else {
|
|
83
|
-
$(`input[name="${targets[i]}"]`).prop('disabled', false).removeClass('readonly')
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public disabledEditItem<T>(form: T, ...targets: string[]): void {
|
|
89
|
-
const frm = form as { [key: string]: object }
|
|
90
|
-
for (let i = 0; i < targets.length; i++) {
|
|
91
|
-
if (!frm[targets[i]]) {
|
|
92
|
-
continue
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (frm[targets[i]].constructor.name == 'LookupEditor') {
|
|
96
|
-
;(frm[targets[i]] as HTMLInputElement).readOnly = true
|
|
97
|
-
} else {
|
|
98
|
-
$(`input[name="${targets[i]}"]`).prop('disabled', true).addClass('readonly')
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
public disabledAndClearEditItem<T>(form: T, ...targets: string[]): void {
|
|
104
|
-
const frm = form as { [key: string]: object }
|
|
105
|
-
for (let i = 0; i < targets.length; i++) {
|
|
106
|
-
const className = frm[targets[i]].constructor.name
|
|
107
|
-
if (className == 'LookupEditor') {
|
|
108
|
-
;(frm[targets[i]] as HTMLInputElement).readOnly = true
|
|
109
|
-
} else {
|
|
110
|
-
$(`input[name="${targets[i]}"]`).val('')
|
|
111
|
-
$(`input[name="${targets[i]}"]`).addClass('readonly').prop('disabled', true)
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Use this function within function onDialogOpen()
|
|
118
|
-
* Put after super.onDialogOpen() line
|
|
119
|
-
* @param dialog
|
|
120
|
-
* @param options
|
|
121
|
-
*/
|
|
122
|
-
public setDialogSize(dialog: object, options?: IDialogSize) {
|
|
123
|
-
let name = dialog.constructor.name
|
|
124
|
-
name = `.s-${name}`
|
|
125
|
-
const optionH = options?.height || 0
|
|
126
|
-
const titleH = $(`${name} .ui-dialog-titlebar`).innerHeight() || 0
|
|
127
|
-
const dialogH = $(`${name} .s-TemplatedDialog`).css('height', '').innerHeight() || 0
|
|
128
|
-
const h = optionH > 0 ? optionH : titleH + dialogH + 2
|
|
129
|
-
const w = options?.width ?? 420
|
|
130
|
-
$(name)
|
|
131
|
-
.css({ width: `${w}px`, height: `${h}px` })
|
|
132
|
-
.position({
|
|
133
|
-
of: window,
|
|
134
|
-
my: 'center center',
|
|
135
|
-
at: 'center center',
|
|
136
|
-
})
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export type IDialogSize = {
|
|
141
|
-
width?: number
|
|
142
|
-
height?: number
|
|
143
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { deepClone, postToService } from '@serenity-is/corelib'
|
|
2
|
-
import { IdevsExportOptions, IdevsExportRequest } from '../globals'
|
|
3
|
-
|
|
4
|
-
export function doExportExcel(options: IdevsExportOptions): void {
|
|
5
|
-
const grid = options.grid
|
|
6
|
-
const request = deepClone(grid.getView().params) as IdevsExportRequest
|
|
7
|
-
request.Take = 0
|
|
8
|
-
request.Skip = 0
|
|
9
|
-
const sortBy = grid.getView().sortBy
|
|
10
|
-
if (sortBy) {
|
|
11
|
-
request.Sort = sortBy
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
request.ExportColumns = []
|
|
15
|
-
const columns = grid.getGrid().getColumns()
|
|
16
|
-
for (const column of columns) {
|
|
17
|
-
request.ExportColumns.push(column.id || column.field || '')
|
|
18
|
-
}
|
|
19
|
-
request.ExportColumns = request.ExportColumns.filter(column => !!column)
|
|
20
|
-
request.viewName = options.viewName
|
|
21
|
-
request.companyName = options.companyName
|
|
22
|
-
request.reportName = options.reportName
|
|
23
|
-
request.selectionRange = options.selectionRange
|
|
24
|
-
request.conditionRange = options.conditionRange
|
|
25
|
-
request.logo = options.logo
|
|
26
|
-
request.entity = options.entity
|
|
27
|
-
|
|
28
|
-
postToService({ service: options.service, request: request, target: '_blank' })
|
|
29
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { Column } from '@serenity-is/sleekgrid'
|
|
2
|
-
import { indexOf, tryFirst } from '@serenity-is/corelib'
|
|
3
|
-
import { EntityGrid, ToolButton } from '@serenity-is/corelib'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Use GetGridHelper within function layout()
|
|
7
|
-
* Put the line before super.layout()
|
|
8
|
-
* In case of master/detail form use this function within function getButtons() or getColumns()
|
|
9
|
-
* @param helper
|
|
10
|
-
* @constructor
|
|
11
|
-
*/
|
|
12
|
-
export function GetGridHelper(helper: GridHelper): GridHelper {
|
|
13
|
-
if (helper) {
|
|
14
|
-
return helper
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return new GridHelper()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class GridHelper {
|
|
21
|
-
/**
|
|
22
|
-
* Use this function within function getColumns()
|
|
23
|
-
* @param columns
|
|
24
|
-
* @param removeColumns
|
|
25
|
-
*/
|
|
26
|
-
public getColumns(columns: Column[], ...removeColumns: string[]): Column[] {
|
|
27
|
-
for (const column of removeColumns) {
|
|
28
|
-
columns.splice(
|
|
29
|
-
indexOf(columns, c => c.field === column),
|
|
30
|
-
1,
|
|
31
|
-
)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const id = tryFirst(columns, c => c.field === 'Id')
|
|
35
|
-
if (id) {
|
|
36
|
-
columns.splice(
|
|
37
|
-
indexOf(columns, c => c.field === 'Id'),
|
|
38
|
-
1,
|
|
39
|
-
)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return columns
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Use this function within function getButtons()
|
|
47
|
-
* @param buttons
|
|
48
|
-
* @param removeButtons
|
|
49
|
-
*/
|
|
50
|
-
public getButtons(buttons: ToolButton[], ...removeButtons: string[]): ToolButton[] {
|
|
51
|
-
for (const btn of removeButtons) {
|
|
52
|
-
const id = indexOf(buttons, x => x.cssClass == btn)
|
|
53
|
-
if (id >= 0) {
|
|
54
|
-
buttons.splice(id, 1)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return buttons
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Use this function within updateInterface method
|
|
63
|
-
* Put below super.updateInterface() line
|
|
64
|
-
* @param columns
|
|
65
|
-
*/
|
|
66
|
-
public setHeaderAlignment(...columns: IHeaderAlignment[]): void {
|
|
67
|
-
for (let c = 0; c < columns.length; c++) {
|
|
68
|
-
const column: IHeaderAlignment = columns[c]
|
|
69
|
-
const header = document.querySelector(`.slick-header-columns div[id$="${column.column}"] .slick-column-name`)
|
|
70
|
-
if (header) {
|
|
71
|
-
header.classList.add('text-center')
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Use this function within constructor
|
|
78
|
-
* @param grid
|
|
79
|
-
* @param options
|
|
80
|
-
*/
|
|
81
|
-
public initGrid<TItem, TOptions>(grid: EntityGrid<TItem, TOptions>, options?: IInitGridOptions): void {
|
|
82
|
-
// Set auto column width
|
|
83
|
-
if (options?.autoColumnWidth ?? true) {
|
|
84
|
-
const g = grid.getGrid()
|
|
85
|
-
g.autosizeColumns()
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// get dialog mode
|
|
89
|
-
grid.openDialogsAsPanel = (options?.dialogMode ?? true) == false
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export type IInitGridOptions = {
|
|
94
|
-
autoColumnWidth?: boolean
|
|
95
|
-
dialogMode?: boolean
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export type IHeaderAlignment = {
|
|
99
|
-
column: string
|
|
100
|
-
textAlign: HeaderTextAlignments
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export enum HeaderTextAlignments {
|
|
104
|
-
Left = 0,
|
|
105
|
-
Center = 1,
|
|
106
|
-
Right = 2,
|
|
107
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
declare module 'html2pdf.js' {
|
|
2
|
-
type Html2PdfOptions = {
|
|
3
|
-
margin?: number | [number, number, number, number]
|
|
4
|
-
filename?: string
|
|
5
|
-
image?: { type: string; quality: number }
|
|
6
|
-
html2canvas?: { scale: number; logging: boolean; dpi: number; letterRendering: boolean }
|
|
7
|
-
jsPDF?: { unit: string; format: string | number[]; orientation: string }
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type Html2Pdf = {
|
|
11
|
-
from(element: HTMLElement | string): Html2Pdf
|
|
12
|
-
set(options: Html2PdfOptions): Html2Pdf
|
|
13
|
-
toPdf(): Html2Pdf
|
|
14
|
-
get(type: 'pdf'): Promise<jsPDF>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const html2pdf: () => Html2Pdf
|
|
18
|
-
export = html2pdf
|
|
19
|
-
}
|
package/src/helpers/index.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { deepClone, postToService } from '@serenity-is/corelib'
|
|
2
|
-
import { IdevsExportOptions, IdevsExportRequest } from '../globals'
|
|
3
|
-
import html2pdf from 'html2pdf.js'
|
|
4
|
-
import { jsPDF } from 'jspdf'
|
|
5
|
-
import pdfMake from 'pdfmake/build/pdfmake'
|
|
6
|
-
import { vfsFonts } from './custom-fonts'
|
|
7
|
-
import htmlToPdfmake from 'html-to-pdfmake'
|
|
8
|
-
import { Margins, PageOrientation, PageSize, Style, TDocumentDefinitions } from 'pdfmake/interfaces'
|
|
9
|
-
|
|
10
|
-
export function doExportPdf(options: IdevsExportOptions): void {
|
|
11
|
-
const grid = options.grid
|
|
12
|
-
const request = deepClone(grid.getView().params) as IdevsExportRequest
|
|
13
|
-
request.Take = 0
|
|
14
|
-
request.Skip = 0
|
|
15
|
-
const sortBy = grid.getView().sortBy
|
|
16
|
-
if (sortBy) {
|
|
17
|
-
request.Sort = sortBy
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
request.ExportColumns = []
|
|
21
|
-
const columns = grid.getGrid().getColumns()
|
|
22
|
-
for (const column of columns) {
|
|
23
|
-
request.ExportColumns.push(column.id || column.field || '')
|
|
24
|
-
}
|
|
25
|
-
request.ExportColumns = request.ExportColumns.filter(column => !!column)
|
|
26
|
-
request.viewName = options.viewName
|
|
27
|
-
request.companyName = options.companyName
|
|
28
|
-
request.reportName = options.reportName
|
|
29
|
-
request.selectionRange = options.selectionRange
|
|
30
|
-
request.conditionRange = options.conditionRange
|
|
31
|
-
request.logo = options.logo
|
|
32
|
-
request.entity = options.entity
|
|
33
|
-
|
|
34
|
-
postToService({ service: options.service, request: request, target: '_blank' })
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export type generatePdfOption = {
|
|
38
|
-
unit?: string
|
|
39
|
-
format?: string
|
|
40
|
-
orientation?: string
|
|
41
|
-
margin?: number
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function generatePdf(content: string, option?: generatePdfOption): void {
|
|
45
|
-
const margin = option?.margin ?? 10
|
|
46
|
-
const unit = option?.unit ?? 'mm'
|
|
47
|
-
const format = option?.format ?? 'a4'
|
|
48
|
-
const orientation = option?.orientation ?? 'portrait'
|
|
49
|
-
if (content) {
|
|
50
|
-
html2pdf()
|
|
51
|
-
.from(content)
|
|
52
|
-
.set({
|
|
53
|
-
margin: margin,
|
|
54
|
-
filename: 'filename.pdf',
|
|
55
|
-
image: { type: 'jpeg', quality: 0.98 },
|
|
56
|
-
html2canvas: { scale: 2, logging: true, dpi: 192, letterRendering: true },
|
|
57
|
-
jsPDF: { unit: unit, format: format, orientation: orientation },
|
|
58
|
-
})
|
|
59
|
-
.toPdf()
|
|
60
|
-
.get('pdf')
|
|
61
|
-
.then((pdf: jsPDF) => {
|
|
62
|
-
const blob = pdf.output('blob')
|
|
63
|
-
const url = URL.createObjectURL(blob)
|
|
64
|
-
window.open(url, '_blank')
|
|
65
|
-
})
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function makePdf(html: string, options?: PageOptions): Promise<string> {
|
|
70
|
-
return new Promise((resolve, reject) => {
|
|
71
|
-
if (!html) {
|
|
72
|
-
reject('No html to make a pdf')
|
|
73
|
-
} else {
|
|
74
|
-
pdfMake.vfs = vfsFonts
|
|
75
|
-
pdfMake.fonts = {
|
|
76
|
-
Roboto: {
|
|
77
|
-
normal: 'Roboto-Regular.ttf',
|
|
78
|
-
bold: 'Roboto-Medium.ttf',
|
|
79
|
-
italics: 'Roboto-Italic.ttf',
|
|
80
|
-
bolditalics: 'Roboto-MediumItalic.ttf',
|
|
81
|
-
},
|
|
82
|
-
THSarabun: {
|
|
83
|
-
normal: 'THSarabun.ttf',
|
|
84
|
-
bold: 'THSarabun Bold.ttf',
|
|
85
|
-
italics: 'THSarabun Italic.ttf',
|
|
86
|
-
bolditalics: 'THSarabun Bold Italic.ttf',
|
|
87
|
-
},
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
-
const def: any = htmlToPdfmake(html, { tableAutoSize: true })
|
|
92
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
93
|
-
let main = def.find((el: any) => el['nodeName'] == 'MAIN')
|
|
94
|
-
if (!main) {
|
|
95
|
-
return
|
|
96
|
-
}
|
|
97
|
-
main = removeEmptyTextNodes(main)
|
|
98
|
-
|
|
99
|
-
const defStyle = options?.defaultStyle || {
|
|
100
|
-
font: 'THSarabun',
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const margin = options?.pageMargins ?? 40
|
|
104
|
-
const pageSize = options?.pageSize ?? 'A4'
|
|
105
|
-
const pageOrientation = options?.pageOrientation ?? 'portrait'
|
|
106
|
-
|
|
107
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
108
|
-
let header: any = def.find((el: any) => el['nodeName'] == 'HEADER')
|
|
109
|
-
header = removeEmptyTextNodes(header)
|
|
110
|
-
const hd = header ? JSON.stringify(header) : undefined
|
|
111
|
-
|
|
112
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
113
|
-
let footer = def.find((el: any) => el['nodeName'] == 'FOOTER')
|
|
114
|
-
if (footer) {
|
|
115
|
-
footer = removeEmptyTextNodes(footer)
|
|
116
|
-
}
|
|
117
|
-
const ft = footer ? JSON.stringify(footer) : undefined
|
|
118
|
-
|
|
119
|
-
const content: TDocumentDefinitions = {
|
|
120
|
-
content: main,
|
|
121
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
122
|
-
header: function (currentPage, pageCount, pageSize) {
|
|
123
|
-
if (!hd) {
|
|
124
|
-
return ''
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const h = hd.replace('{{pageNo}}', currentPage.toString()).replace('{{totalPages}}', pageCount.toString())
|
|
128
|
-
return JSON.parse(h)
|
|
129
|
-
},
|
|
130
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
131
|
-
footer: function (currentPage, pageCount, pageSize) {
|
|
132
|
-
if (!ft) {
|
|
133
|
-
return ''
|
|
134
|
-
}
|
|
135
|
-
const f = ft.replace('{{pageNo}}', currentPage.toString()).replace('{{totalPages}}', pageCount.toString())
|
|
136
|
-
return JSON.parse(f)
|
|
137
|
-
},
|
|
138
|
-
pageSize: pageSize,
|
|
139
|
-
pageOrientation: pageOrientation,
|
|
140
|
-
pageMargins: margin,
|
|
141
|
-
defaultStyle: defStyle,
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
pdfMake.createPdf(content).open()
|
|
145
|
-
|
|
146
|
-
resolve('Make pdf successfully')
|
|
147
|
-
}
|
|
148
|
-
})
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export type PageOptions = {
|
|
152
|
-
defaultStyle?: Style | undefined
|
|
153
|
-
pageSize?: PageSize | undefined
|
|
154
|
-
pageOrientation?: PageOrientation | undefined
|
|
155
|
-
pageMargins?: Margins | undefined
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
159
|
-
const removeEmptyTextNodes = (obj: any): any => {
|
|
160
|
-
// If obj is an array, iterate through each element and filter
|
|
161
|
-
if (Array.isArray(obj)) {
|
|
162
|
-
return obj
|
|
163
|
-
.map(removeEmptyTextNodes) // Recursively apply the function
|
|
164
|
-
.filter(item => !(item && item.text === ' ')) // Remove elements where text is " "
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// If obj is an object, check each key
|
|
168
|
-
if (typeof obj === 'object' && obj !== null) {
|
|
169
|
-
// eslint-disable-next-line prefer-const
|
|
170
|
-
for (let key in obj) {
|
|
171
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
172
|
-
if (obj.hasOwnProperty(key)) {
|
|
173
|
-
obj[key] = removeEmptyTextNodes(obj[key])
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return obj
|
|
179
|
-
}
|
package/src/index.ts
DELETED
package/src/tsconfig.json
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compileOnSave": false,
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"experimentalDecorators": true,
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"declarationDir": "../dist",
|
|
7
|
-
"emitBOM": true,
|
|
8
|
-
"emitDeclarationOnly": false,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"inlineSources": true,
|
|
11
|
-
"isolatedModules": true,
|
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
|
13
|
-
"noImplicitAny": true,
|
|
14
|
-
"sourceMap": true,
|
|
15
|
-
"module": "ESNext",
|
|
16
|
-
"moduleResolution": "Node",
|
|
17
|
-
"skipLibCheck": true,
|
|
18
|
-
"types": ["jquery", "jquery.validation", "jqueryui", "toastr"],
|
|
19
|
-
"outDir": "../dist",
|
|
20
|
-
"target": "ES2020",
|
|
21
|
-
"lib": ["ES6", "ES2020", "DOM"],
|
|
22
|
-
"noEmitHelpers": false,
|
|
23
|
-
"importHelpers": false,
|
|
24
|
-
"jsx": "react-jsx",
|
|
25
|
-
"typeRoots": ["../node_modules/@types"]
|
|
26
|
-
},
|
|
27
|
-
"include": ["**/*"],
|
|
28
|
-
"exclude": ["**/*.spec.ts"]
|
|
29
|
-
}
|