Skip to content

Commit

Permalink
重构v4版本
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Mar 8, 2021
1 parent 0f43ce5 commit 5fdaa87
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -39,15 +40,14 @@ gulp.task('build_umd', function () {
['@babel/transform-modules-umd', {
globals: {
[pack.name]: exportModuleName,
'vxe-table': 'VXETable',
'xe-utils': 'XEUtils',
jspdf: 'jspdf'
},
exactGlobals: true
}]
]
}))
.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',
Expand Down
55 changes: 22 additions & 33 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import XEUtils from 'xe-utils/ctor'
import {
VXETableByVueProperty,
VXETableInstance,
VXETableCore,
VxeTableConstructor,
VxeTablePropTypes,
VxeTableDefines,
VxeGlobalInterceptorHandles
} from 'vxe-table/lib/vxe-table'
import jsPDF from 'jspdf'

let vxetable: VXETableCore

declare module 'vxe-table/lib/vxe-table' {
namespace VxeTablePropTypes {
interface ExportConfig {
Expand All @@ -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';
Expand All @@ -36,7 +30,6 @@ interface VXETablePluginExportPDFOptions {
beforeMethod?: Function;
}

const isWin = typeof window !== 'undefined'
const globalOptions: VXETablePluginExportPDFOptions = {}
const globalFonts: { [key: string]: any } = {}

Expand All @@ -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)
}

Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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, {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"strict": true,
"moduleResolution": "node",
"noImplicitAny": true,
"allowSyntheticDefaultImports": true,
"target": "esnext",
"lib": [
"esnext",
Expand Down

0 comments on commit 5fdaa87

Please sign in to comment.