diff --git a/gulpfile.js b/gulpfile.js index 53b8c41..9213430 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -7,19 +7,14 @@ const replace = require('gulp-replace') const sourcemaps = require('gulp-sourcemaps') const ts = require('gulp-typescript') const pack = require('./package.json') +const tsconfig = require('./tsconfig.json') const exportModuleName = 'VXETablePluginExportPDF' gulp.task('build_commonjs', function () { return gulp.src(['depend.ts', 'index.ts']) .pipe(sourcemaps.init()) - .pipe(ts({ - strict: true, - moduleResolution: 'node', - noImplicitAny: true, - target: 'es6', - lib: ['dom', 'es6'] - })) + .pipe(ts(tsconfig.compilerOptions)) .pipe(babel({ presets: ['@babel/env'] })) @@ -33,13 +28,7 @@ gulp.task('build_commonjs', function () { gulp.task('build_umd', function () { return gulp.src(['depend.ts', 'index.ts']) - .pipe(ts({ - strict: true, - moduleResolution: 'node', - noImplicitAny: true, - target: 'es6', - lib: ['dom', 'es6'] - })) + .pipe(ts(tsconfig.compilerOptions)) .pipe(replace(`import XEUtils from 'xe-utils/ctor';`, `import XEUtils from 'xe-utils';`)) .pipe(babel({ moduleId: pack.name, diff --git a/index.ts b/index.ts index 500135d..cf18a76 100644 --- a/index.ts +++ b/index.ts @@ -1,15 +1,43 @@ /* eslint-disable no-unused-vars */ import XEUtils from 'xe-utils/ctor' import { - VXETable, - Table, - InterceptorExportParams, - ColumnConfig, - ExportOptons + VXETableByVueProperty, + VXETableInstance, + VxeTableConstructor, + VxeTablePropTypes, + VxeTableDefines, + VxeGlobalInterceptorHandles } from 'vxe-table/lib/vxe-table' import jsPDF from 'jspdf' + +declare module 'vxe-table/lib/vxe-table' { + namespace VxeTablePropTypes { + interface ExportConfig { + fontName?: string; + } + } +} + +declare global { + interface Window { + jspdf: any; + jsPDF: any; + } +} /* eslint-enable no-unused-vars */ +interface VXETablePluginExportPDFFonts { + fontName: string; + fontStyle?: 'normal'; + fontUrl: string; +} + +interface VXETablePluginExportPDFOptions { + fontName?: string; + fonts?: VXETablePluginExportPDFFonts[]; + beforeMethod?: Function; +} + const isWin = typeof window !== 'undefined' const globalOptions: VXETablePluginExportPDFOptions = {} const globalFonts: { [key: string]: any } = {} @@ -18,21 +46,24 @@ function getCellText (cellValue: any) { return cellValue || ' ' } -function getFooterCellValue ($table: Table, opts: ExportOptons, rows: any[], column: ColumnConfig) { - const cellValue = XEUtils.toString(rows[$table.$getColumnIndex(column)]) +function getFooterCellValue ($table: VxeTableConstructor, opts: VxeTablePropTypes.ExportConfig, rows: any[], column: VxeTableDefines.ColumnInfo) { + const cellValue = XEUtils.toString(rows[$table.getVTColumnIndex(column)]) return getCellText(cellValue) } -function getFooterData (opts: ExportOptons, footerData: any[][]) { +function getFooterData (opts: VxeTablePropTypes.ExportConfig, footerData: any[][]) { const { footerFilterMethod } = opts return footerFilterMethod ? footerData.filter((items, index) => footerFilterMethod({ items, $rowIndex: index })) : footerData } -function exportPDF (params: InterceptorExportParams) { +function exportPDF (params: VxeGlobalInterceptorHandles.InterceptorExportParams) { const { fonts, beforeMethod } = globalOptions const { $table, options, columns, datas } = params - const { $vxe, treeConfig, treeOpts } = $table - const { modal, t } = $vxe + const { props, instance, computeMaps } = $table + const { treeConfig } = props + const { computeTreeOpts } = computeMaps + const treeOpts = computeTreeOpts.value + const { modal, t } = instance.appContext.config.globalProperties.$vxe as VXETableByVueProperty const dX = 7 const dY = 15.8 const ratio = 3.78 @@ -52,12 +83,12 @@ function exportPDF (params: InterceptorExportParams) { width } }) - let offsetWidth = (colWidth - Math.floor(pdfWidth + dX * 2 * ratio)) / headers.length + const offsetWidth = (colWidth - Math.floor(pdfWidth + dX * 2 * ratio)) / headers.length headers.forEach((column) => { column.width = column.width - offsetWidth }) - let rowList: { [key: string]: any }[] = datas.map((row) => { - const item: { [key: string]: any } = {} + const rowList: any[] = datas.map((row) => { + const item: any = {} columns.forEach((column) => { item[column.id] = getCellText(treeConfig && column.treeNode ? (' '.repeat(row._level * treeOpts.indent / 8) + row[column.id]) : row[column.id]) }) @@ -67,7 +98,7 @@ function exportPDF (params: InterceptorExportParams) { const { footerData } = $table.getTableData() const footers = getFooterData(options, footerData) footers.forEach(rows => { - const item: { [key: string]: any } = {} + const item: any = {} columns.forEach((column) => { item[column.id] = getFooterCellValue($table, options, rows, column) }) @@ -143,32 +174,13 @@ function checkFont (fontConf?: VXETablePluginExportPDFFonts | null | undefined) return Promise.resolve() } -function handleExportEvent (params: InterceptorExportParams) { +function handleExportEvent (params: VxeGlobalInterceptorHandles.InterceptorExportParams) { if (params.options.type === 'pdf') { exportPDF(params) return false } } -interface VXETablePluginExportPDFFonts { - fontName: string; - fontStyle?: 'normal'; - fontUrl: string; -} - -interface VXETablePluginExportPDFOptions { - fontName?: string; - fonts?: VXETablePluginExportPDFFonts[]; - beforeMethod?: Function; -} - -declare global { - interface Window { - jspdf: any; - jsPDF: any; - } -} - function setup (options: VXETablePluginExportPDFOptions) { const { fonts } = Object.assign(globalOptions, options) if (fonts) { @@ -185,7 +197,7 @@ function setup (options: VXETablePluginExportPDFOptions) { */ export const VXETablePluginExportPDF = { setup, - install (vxetable: typeof VXETable, options?: VXETablePluginExportPDFOptions) { + install (vxetable: VXETableInstance, options?: VXETablePluginExportPDFOptions) { vxetable.setup({ export: { types: { @@ -202,7 +214,7 @@ export const VXETablePluginExportPDF = { } } -if (isWin && window.VXETable) { +if (typeof window !== 'undefined' && window.VXETable) { window.VXETable.use(VXETablePluginExportPDF) } diff --git a/package.json b/package.json index 8d661c2..f618531 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vxe-table-plugin-export-pdf", - "version": "1.8.8", + "version": "3.0.0-alpha.0", "description": "基于 vxe-table 的表格插件,支持导出 pdf 格式", "scripts": { "lib": "gulp build" @@ -18,22 +18,22 @@ "style": "dist/style.css", "typings": "index.ts", "devDependencies": { - "@babel/core": "^7.4.4", - "@babel/plugin-transform-runtime": "^7.4.4", - "@babel/preset-env": "^7.4.4", - "@babel/runtime": "^7.4.4", - "@typescript-eslint/eslint-plugin": "^2.3.1", - "@typescript-eslint/parser": "^2.3.1", - "del": "^5.1.0", - "eslint": "^5.15.1", - "eslint-config-prettier": "^6.3.0", - "eslint-config-standard": "^12.0.0", + "@babel/core": "^7.12.3", + "@babel/plugin-transform-runtime": "^7.12.1", + "@babel/preset-env": "^7.12.1", + "@babel/runtime": "^7.12.5", + "@typescript-eslint/eslint-plugin": "^4.6.1", + "@typescript-eslint/parser": "^4.6.1", + "del": "^6.0.0", + "eslint": "^7.13.0", + "eslint-config-prettier": "^6.15.0", + "eslint-config-standard": "^16.0.1", "eslint-friendly-formatter": "^4.0.1", - "eslint-plugin-import": "^2.16.0", - "eslint-plugin-node": "^8.0.1", - "eslint-plugin-prettier": "^3.1.1", - "eslint-plugin-promise": "^4.0.1", - "eslint-plugin-standard": "^4.0.0", + "eslint-plugin-import": "^2.22.1", + "eslint-plugin-node": "^11.1.0", + "eslint-plugin-prettier": "^3.1.4", + "eslint-plugin-promise": "^4.2.1", + "eslint-plugin-standard": "^4.0.2", "eslint-plugin-typescript": "^0.14.0", "gulp": "^4.0.2", "gulp-autoprefixer": "^7.0.1", @@ -46,16 +46,16 @@ "gulp-sourcemaps": "^2.6.5", "gulp-typescript": "^5.0.1", "gulp-uglify": "^3.0.2", - "jspdf": "^2.1.0", - "markdown-doctest": "^0.9.1", - "prettier": "^1.18.2", - "typescript": "^3.9.3", - "vue": "^2.6.11", - "vxe-table": "^2.9.25", - "xe-utils": "^2.7.13" + "jspdf": "^2.1.1", + "markdown-doctest": "^1.1.0", + "prettier": "^2.1.2", + "typescript": "^4.0.5", + "vue": "^3.0.2", + "vxe-table": "^4.0.0-alpha.1", + "xe-utils": "^3.0.1" }, "peerDependencies": { - "vxe-table": ">= 2.9.25", + "vxe-table": ">= 4", "jspdf": ">= 2.1.0" }, "repository": { diff --git a/tsconfig.json b/tsconfig.json index 59f58b4..5a00f0d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,10 +7,11 @@ "strict": true, "moduleResolution": "node", "noImplicitAny": true, - "target": "es6", + "target": "esnext", "lib": [ + "esnext", "dom", - "es6" + "dom.iterable" ] } } \ No newline at end of file