diff --git a/README.md b/README.md index 38b964d..e031807 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ## Installing ```shell -npm install xe-utils vxe-table vxe-table-plugin-export-pdf jspdf +npm install xe-utils vxe-table@next vxe-table-plugin-export-pdf@next jspdf ``` ```javascript diff --git a/gulpfile.js b/gulpfile.js index a8e8cf3..5462bef 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -29,7 +29,8 @@ gulp.task('build_commonjs', function () { gulp.task('build_umd', function () { return gulp.src(['depend.ts', 'index.ts']) .pipe(ts(tsconfig.compilerOptions)) - .pipe(replace('import XEUtils from \'xe-utils/ctor\';', 'import XEUtils from \'xe-utils\';')) + .pipe(replace(`from 'xe-utils/ctor';`, `from 'xe-utils';`)) + .pipe(replace(`from 'vxe-table/lib/vxe-table';`, `from 'vxe-table';`)) .pipe(babel({ moduleId: pack.name, presets: [ @@ -39,6 +40,7 @@ gulp.task('build_umd', function () { ['@babel/transform-modules-umd', { globals: { [pack.name]: exportModuleName, + 'vxe-table': 'VXETable', 'xe-utils': 'XEUtils', jspdf: 'jspdf' }, @@ -46,8 +48,6 @@ gulp.task('build_umd', function () { }] ] })) - .pipe(replace('global.jspdf', 'global.jsPDF || global.jspdf')) - .pipe(replace(`global.${exportModuleName} = mod.exports;`, `global.${exportModuleName} = mod.exports.default;`)) .pipe(rename({ basename: 'index', suffix: '.umd', diff --git a/index.ts b/index.ts index 4cfef39..f73425e 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,6 @@ import XEUtils from 'xe-utils/ctor' import { - VXETableByVueProperty, - VXETableInstance, + VXETableCore, VxeTableConstructor, VxeTablePropTypes, VxeTableDefines, @@ -9,6 +8,8 @@ import { } from 'vxe-table/lib/vxe-table' import jsPDF from 'jspdf' +let vxetable: VXETableCore + declare module 'vxe-table/lib/vxe-table' { namespace VxeTablePropTypes { interface ExportConfig { @@ -17,13 +18,6 @@ declare module 'vxe-table/lib/vxe-table' { } } -declare global { - interface Window { - jspdf: any; - jsPDF: any; - } -} - interface VXETablePluginExportPDFFonts { fontName: string; fontStyle?: 'normal'; @@ -36,7 +30,6 @@ interface VXETablePluginExportPDFOptions { beforeMethod?: Function; } -const isWin = typeof window !== 'undefined' const globalOptions: VXETablePluginExportPDFOptions = {} const globalFonts: { [key: string]: any } = {} @@ -45,7 +38,7 @@ function getCellText (cellValue: any) { } function getFooterCellValue ($table: VxeTableConstructor, opts: VxeTablePropTypes.ExportConfig, rows: any[], column: VxeTableDefines.ColumnInfo) { - const cellValue = XEUtils.toString(rows[$table.getVTColumnIndex(column)]) + const cellValue = XEUtils.toValueString(rows[$table.getVTColumnIndex(column)]) return getCellText(cellValue) } @@ -57,11 +50,10 @@ function getFooterData (opts: VxeTablePropTypes.ExportConfig, footerData: any[][ function exportPDF (params: VxeGlobalInterceptorHandles.InterceptorExportParams) { const { fonts, beforeMethod } = globalOptions const { $table, options, columns, datas } = params - const { props, instance } = $table + const { props } = $table const { treeConfig } = props const { computeTreeOpts } = $table.getComputeMaps() 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 @@ -72,7 +64,7 @@ function exportPDF (params: VxeGlobalInterceptorHandles.InterceptorExportParams) const { type, filename, isHeader, isFooter, original } = options const footList: { [key: string]: any }[] = [] const headers: any[] = columns.map((column) => { - const title = XEUtils.toString(original ? column.property : column.getTitle()) + const title = XEUtils.toValueString(original ? column.property : column.getTitle()) const width = column.renderWidth / ratio colWidth += width return { @@ -125,9 +117,9 @@ function exportPDF (params: VxeGlobalInterceptorHandles.InterceptorExportParams) return } if (options.sheetName) { - const title = XEUtils.toString(options.sheetName) + const title = XEUtils.toValueString(options.sheetName) const textWidth = doc.getTextWidth(title) - doc.text(XEUtils.toString(title), (pdfWidth - textWidth) / 2, dY / 2 + 2) + doc.text(title, (pdfWidth - textWidth) / 2, dY / 2 + 2) } // 转换数据 doc.table(dX, dY, rowList.concat(footList), headers, { @@ -138,12 +130,12 @@ function exportPDF (params: VxeGlobalInterceptorHandles.InterceptorExportParams) // 导出 pdf doc.save(`${filename}.${type}`) if (showMsg) { - modal.close(msgKey) - modal.message({ message: t('vxe.table.expSuccess'), status: 'success' }) + vxetable.modal.close(msgKey) + vxetable.modal.message({ message: vxetable.t('vxe.table.expSuccess'), status: 'success' }) } } if (showMsg) { - modal.message({ id: msgKey, message: t('vxe.table.expLoading'), status: 'loading', duration: -1 }) + vxetable.modal.message({ id: msgKey, message: vxetable.t('vxe.table.expLoading'), status: 'loading', duration: -1 }) } checkFont(fontConf).then(() => { if (showMsg) { @@ -179,35 +171,32 @@ function handleExportEvent (params: VxeGlobalInterceptorHandles.InterceptorExpor } } -function setup (options: VXETablePluginExportPDFOptions) { - const { fonts } = Object.assign(globalOptions, options) - if (fonts) { - if (isWin) { - if (!window.jsPDF) { - window.jsPDF = window.jspdf ? window.jspdf.jsPDF : jsPDF - } - } - } +function pluginSetup (options: VXETablePluginExportPDFOptions) { + Object.assign(globalOptions, options) } /** * 基于 vxe-table 表格的增强插件,支持导出 pdf 格式 */ export const VXETablePluginExportPDF = { - setup, - install (vxetable: VXETableInstance, options?: VXETablePluginExportPDFOptions) { - vxetable.setup({ + setup: pluginSetup, + install (vxetablecore: VXETableCore, options?: VXETablePluginExportPDFOptions) { + const { setup, interceptor } = vxetablecore + + vxetable = vxetablecore + + setup({ export: { types: { pdf: 1 } } }) - vxetable.interceptor.mixin({ + interceptor.mixin({ 'event.export': handleExportEvent }) if (options) { - setup(options) + pluginSetup(options) } } } diff --git a/package.json b/package.json index b4fe0f5..c8da7a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vxe-table-plugin-export-pdf", - "version": "3.0.0-beta.0", + "version": "3.0.0-beta.3", "description": "基于 vxe-table 的表格插件,支持导出 pdf 格式", "scripts": { "lib": "gulp build" @@ -51,8 +51,8 @@ "prettier": "^2.1.2", "typescript": "^4.0.5", "vue": "^3.0.6", - "vxe-table": "^4.0.0-beta.15", - "xe-utils": "^3.1.8" + "vxe-table": "^4.0.0-beta.18", + "xe-utils": "^3.1.11" }, "peerDependencies": { "vxe-table": ">= 4" diff --git a/tsconfig.json b/tsconfig.json index 5a00f0d..8bac054 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "strict": true, "moduleResolution": "node", "noImplicitAny": true, + "allowSyntheticDefaultImports": true, "target": "esnext", "lib": [ "esnext",