gm-printer 10.13.11 → 10.13.12-alpha.2

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.
@@ -0,0 +1,312 @@
1
+ import i18next from '../../locales'
2
+ import React from 'react'
3
+ import classNames from 'classnames'
4
+ import { inject, observer, Provider } from 'mobx-react'
5
+ import PropTypes from 'prop-types'
6
+ import PrinterStore from '../printer/store'
7
+ import Page from '../printer/page'
8
+ import _ from 'lodash'
9
+ import Panel from '../printer/panel'
10
+ import Table from './components/table'
11
+ import MergePage from '../printer/merge_page'
12
+
13
+ // Header Sign Footer 相对特殊,要单独处理
14
+ const Header = props => (
15
+ <Panel {...props} name='header' placeholder={i18next.t('页眉')} />
16
+ )
17
+
18
+ const Sign = props => (
19
+ <Panel
20
+ {...props}
21
+ style={{
22
+ ...props.style,
23
+ position: 'absolute',
24
+ left: 0,
25
+ right: 0
26
+ }}
27
+ name='sign'
28
+ placeholder={i18next.t('签名')}
29
+ />
30
+ )
31
+
32
+ Sign.propTypes = {
33
+ style: PropTypes.object
34
+ }
35
+
36
+ const Footer = props => (
37
+ <Panel
38
+ {...props}
39
+ style={{
40
+ ...props.style,
41
+ position: 'absolute',
42
+ bottom: 0,
43
+ left: 0,
44
+ right: 0
45
+ }}
46
+ name='footer'
47
+ placeholder={i18next.t('页脚')}
48
+ />
49
+ )
50
+
51
+ Footer.propTypes = {
52
+ style: PropTypes.object
53
+ }
54
+
55
+ @inject('printerStore')
56
+ @observer
57
+ class Printer extends React.Component {
58
+ constructor(props) {
59
+ super(props)
60
+
61
+ // 无实际意义,辅助 onReady,见 didMount
62
+ this.state = {}
63
+
64
+ props.printerStore.init(props.config, props.data)
65
+ props.printerStore.setSelected(props.selected)
66
+ props.printerStore.setSelectedRegion(props.selectedRegion)
67
+ }
68
+
69
+ UNSAFE_componentWillReceiveProps(nextProps) {
70
+ if (nextProps.selected !== this.props.selected) {
71
+ this.props.printerStore.setSelected(nextProps.selected)
72
+ }
73
+ if (nextProps.selectedRegion !== this.props.selectedRegion) {
74
+ this.props.printerStore.setSelectedRegion(nextProps.selectedRegion)
75
+ }
76
+ }
77
+
78
+ componentDidMount() {
79
+ const { printerStore, config } = this.props
80
+ const batchPrintConfig = config.batchPrintConfig
81
+
82
+ // 连续打印不需要计算
83
+ if (batchPrintConfig !== 2) {
84
+ // didMount 代表第一次渲染完成
85
+ printerStore.setReady(true)
86
+
87
+ // 开始计算,获取各种数据
88
+ // printerStore.computedPages()
89
+ printerStore.financeComputedPages()
90
+ }
91
+ // Printer 不是立马就呈现出最终样式,有个过程。这个过程需要时间,什么 ready,不太清楚,估借 setState 来获取过程结束时刻
92
+ this.setState({}, () => {
93
+ this.props.onReady()
94
+ })
95
+ }
96
+
97
+ renderBefore() {
98
+ const { printerStore } = this.props
99
+ const { config } = printerStore
100
+
101
+ return (
102
+ <Page isFontFamily>
103
+ <Header config={config.header} pageIndex={0} />
104
+ {_.map(config.contents, (content, index) => {
105
+ switch (content.type) {
106
+ case 'table':
107
+ // eslint-disable-next-line no-case-declarations
108
+ const list = printerStore.data._table[content.dataKey]
109
+ return (
110
+ <Table
111
+ key={`contents.table.${index}`}
112
+ name={`contents.table.${index}`}
113
+ config={content}
114
+ range={{ begin: 0, end: list?.length || 0 }}
115
+ pageIndex={0}
116
+ placeholder={`${i18next.t('区域')} ${index}`}
117
+ />
118
+ )
119
+
120
+ default:
121
+ return (
122
+ <Panel
123
+ key={`contents.panel.${index}`}
124
+ name={`contents.panel.${index}`}
125
+ config={content}
126
+ pageIndex={0}
127
+ placeholder={`${i18next.t('区域')} ${index}`}
128
+ />
129
+ )
130
+ }
131
+ })}
132
+ <Sign config={config.sign} pageIndex={0} />
133
+ <Footer config={config.footer} pageIndex={0} />
134
+ </Page>
135
+ )
136
+ }
137
+
138
+ renderPage() {
139
+ const { printerStore } = this.props
140
+ const { config } = printerStore
141
+ return (
142
+ <>
143
+ {_.map(printerStore.pages, (page, i) => {
144
+ const isLastPage = i === printerStore.pages.length - 1
145
+
146
+ return (
147
+ <Page key={i} isFontFamily>
148
+ <Header config={config.header} pageIndex={i} />
149
+
150
+ {_.map(page, (panel, ii) => {
151
+ switch (panel.type) {
152
+ case 'table':
153
+ return (
154
+ <Table
155
+ key={`contents.table.${panel.index}.${ii}`}
156
+ name={`contents.table.${panel.index}`}
157
+ config={config.contents[panel.index]}
158
+ range={{
159
+ begin: panel.begin,
160
+ end: panel.end
161
+ }}
162
+ placeholder={`${i18next.t('区域')} ${panel.index}`}
163
+ pageIndex={i}
164
+ />
165
+ )
166
+
167
+ default:
168
+ return (
169
+ <Panel
170
+ key={`contents.panel.${panel.index}`}
171
+ name={`contents.panel.${panel.index}`}
172
+ config={config.contents[panel.index]}
173
+ pageIndex={i}
174
+ placeholder={`${i18next.t('区域')} ${panel.index}`}
175
+ />
176
+ )
177
+ }
178
+ })}
179
+ {isLastPage && (
180
+ <Sign
181
+ config={config.sign}
182
+ pageIndex={i}
183
+ style={{ bottom: config.footer.style.height }}
184
+ />
185
+ )}
186
+ <Footer config={config.footer} pageIndex={i} />
187
+ </Page>
188
+ )
189
+ })}
190
+ </>
191
+ )
192
+ }
193
+
194
+ renderMerge() {
195
+ const { printerStore } = this.props
196
+ const { config } = printerStore
197
+
198
+ return (
199
+ <MergePage>
200
+ <Header config={config.header} pageIndex={0} />
201
+ {_.map(config.contents, (content, index) => {
202
+ switch (content.type) {
203
+ case 'table':
204
+ // eslint-disable-next-line no-case-declarations
205
+ const list = printerStore.data._table[content.dataKey]
206
+ return (
207
+ <Table
208
+ key={`contents.table.${index}`}
209
+ name={`contents.table.${index}`}
210
+ config={content}
211
+ range={{ begin: 0, end: list?.length || 0 }}
212
+ pageIndex={0}
213
+ placeholder={`${i18next.t('区域')} ${index}`}
214
+ />
215
+ )
216
+
217
+ default:
218
+ return (
219
+ <Panel
220
+ key={`contents.panel.${index}`}
221
+ name={`contents.panel.${index}`}
222
+ config={content}
223
+ pageIndex={0}
224
+ placeholder={`${i18next.t('区域')} ${index}`}
225
+ />
226
+ )
227
+ }
228
+ })}
229
+ <Sign config={config.sign} pageIndex={0} />
230
+ <Footer config={config.footer} pageIndex={0} />
231
+ </MergePage>
232
+ )
233
+ }
234
+
235
+ doRender() {
236
+ const { printerStore, config } = this.props
237
+ // batchPrintConfig: 1 不连续打印(纸张会间断)2 连续打印(纸张连续打,不间断)
238
+ const batchPrintConfig = config.batchPrintConfig
239
+ if (batchPrintConfig === 2) {
240
+ return this.renderMerge()
241
+ } else {
242
+ // renderBefore ,拿到各种数据,哪些模块哪些内容放合适位置,切割表格
243
+ // renderPage,最终渲染打印的页面
244
+ return !printerStore.ready ? this.renderBefore() : this.renderPage()
245
+ }
246
+ }
247
+
248
+ render() {
249
+ const {
250
+ selected,
251
+ data,
252
+ config,
253
+ onReady,
254
+ selectedRegion,
255
+ printerStore,
256
+ ...rest
257
+ } = this.props
258
+
259
+ const {
260
+ size: { width },
261
+ className,
262
+ style
263
+ } = printerStore.config.page
264
+ // batchPrintConfig: 1 不连续打印(纸张会间断)2 连续打印(纸张连续打,不间断)
265
+ const batchPrintConfig = config.batchPrintConfig
266
+
267
+ return (
268
+ <div
269
+ {...rest}
270
+ className={classNames('gm-printer', className)}
271
+ style={Object.assign({}, style, {
272
+ width,
273
+ breakAfter: batchPrintConfig === 2 ? 'auto' : 'always'
274
+ })}
275
+ >
276
+ {this.doRender()}
277
+ </div>
278
+ )
279
+ }
280
+ }
281
+
282
+ Printer.propTypes = {
283
+ className: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
284
+ style: PropTypes.object,
285
+ printerStore: PropTypes.object,
286
+ selected: PropTypes.string,
287
+ selectedRegion: PropTypes.string,
288
+ data: PropTypes.object.isRequired,
289
+ config: PropTypes.object.isRequired,
290
+ onReady: PropTypes.func
291
+ }
292
+
293
+ Printer.defaultProps = {
294
+ onReady: _.noop
295
+ }
296
+
297
+ class WithStorePrinter extends React.Component {
298
+ constructor(props) {
299
+ super(props)
300
+ this.printerStore = new PrinterStore()
301
+ }
302
+
303
+ render() {
304
+ return (
305
+ <Provider printerStore={this.printerStore}>
306
+ <Printer {...this.props} />
307
+ </Provider>
308
+ )
309
+ }
310
+ }
311
+
312
+ export default WithStorePrinter
package/src/index.js CHANGED
@@ -16,7 +16,8 @@ import {
16
16
  doPrint,
17
17
  Printer,
18
18
  getCSS,
19
- getPrintContainerHTML
19
+ getPrintContainerHTML,
20
+ doBatchFinancePrint
20
21
  } from './printer'
21
22
  import { MULTI_SUFFIX, MULTI_SUFFIX3 } from './config'
22
23
 
@@ -41,5 +42,6 @@ export {
41
42
  getCSS,
42
43
  MULTI_SUFFIX,
43
44
  MULTI_SUFFIX3,
44
- getPrintContainerHTML
45
+ getPrintContainerHTML,
46
+ doBatchFinancePrint
45
47
  }
@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom'
3
3
  import Printer from './printer'
4
4
  import getCSS from './get_css'
5
5
  import BatchPrinter from './batch_printer'
6
+ import BatchFinancePrinter from '../finance_voucher/components/finance_batch_printer'
6
7
  import { isZoom } from 'gm-util'
7
8
  import { afterImgAndSvgLoaded } from '../util'
8
9
 
@@ -92,6 +93,25 @@ function toDoPrintBatch(list, isPrint = true) {
92
93
  })
93
94
  }
94
95
 
96
+ function toDoPrintFinanceBatch(list, isPrint = true) {
97
+ return new window.Promise(resolve => {
98
+ const $app = $printer.contentWindow.document.getElementById('appContainer')
99
+ ReactDOM.unmountComponentAtNode($app)
100
+ ReactDOM.render(
101
+ <BatchFinancePrinter
102
+ list={list}
103
+ onReady={() => {
104
+ afterImgAndSvgLoaded(() => {
105
+ isPrint && $printer.contentWindow.print()
106
+ resolve()
107
+ }, $app)
108
+ }}
109
+ />,
110
+ $app
111
+ )
112
+ })
113
+ }
114
+
95
115
  function doPrint({ data, config }, isTest) {
96
116
  init({ isTest })
97
117
 
@@ -107,6 +127,16 @@ function doBatchPrint(
107
127
  return toDoPrintBatch(list, extraCofnig.isPrint)
108
128
  }
109
129
 
130
+ function doBatchFinancePrint(
131
+ list,
132
+ isTest,
133
+ extraCofnig = { isPrint: true, isTipZoom: true }
134
+ ) {
135
+ init({ isTest, isTipZoom: extraCofnig.isTipZoom })
136
+
137
+ return toDoPrintFinanceBatch(list, extraCofnig.isPrint)
138
+ }
139
+
110
140
  /**
111
141
  * @description: 获取打印的html
112
142
  */
@@ -114,4 +144,4 @@ function getPrintContainerHTML() {
114
144
  return $printer.contentWindow.document.getElementsByTagName('html')[0]
115
145
  .innerHTML
116
146
  }
117
- export { doPrint, doBatchPrint, getPrintContainerHTML }
147
+ export { doPrint, doBatchPrint, getPrintContainerHTML, doBatchFinancePrint }
@@ -1,6 +1,11 @@
1
1
  import Printer from './printer'
2
2
  import BatchPrinter from './batch_printer'
3
- import { doPrint, doBatchPrint, getPrintContainerHTML } from './do_print'
3
+ import {
4
+ doPrint,
5
+ doBatchPrint,
6
+ getPrintContainerHTML,
7
+ doBatchFinancePrint
8
+ } from './do_print'
4
9
  import getCSS from './get_css'
5
10
 
6
11
  export {
@@ -9,5 +14,6 @@ export {
9
14
  Printer,
10
15
  BatchPrinter,
11
16
  getCSS,
12
- getPrintContainerHTML
17
+ getPrintContainerHTML,
18
+ doBatchFinancePrint
13
19
  }
@@ -2,6 +2,7 @@ import React from 'react'
2
2
  import PropTypes from 'prop-types'
3
3
  import { inject, observer } from 'mobx-react'
4
4
  import { getHeight } from '../util'
5
+ import classNames from 'classnames'
5
6
 
6
7
  @inject('printerStore')
7
8
  @observer
@@ -18,13 +19,14 @@ class Page extends React.Component {
18
19
  }
19
20
 
20
21
  render() {
21
- const { children, printerStore } = this.props
22
+ const { children, printerStore, isFontFamily } = this.props
22
23
  const {
23
24
  paddingTop,
24
25
  paddingRight,
25
26
  paddingBottom,
26
27
  paddingLeft
27
28
  } = printerStore.config.page.gap
29
+ console.log('isFontFamily', isFontFamily)
28
30
 
29
31
  const {
30
32
  size: { width, height },
@@ -36,7 +38,9 @@ class Page extends React.Component {
36
38
  return (
37
39
  <div
38
40
  ref={this.ref}
39
- className='gm-printer-page'
41
+ className={classNames('gm-printer-page', {
42
+ 'gm-printer-page-fontFamily': isFontFamily
43
+ })}
40
44
  style={{
41
45
  ...pageStyle,
42
46
  boxSizing: 'content-box',
@@ -60,7 +64,8 @@ class Page extends React.Component {
60
64
  }
61
65
 
62
66
  Page.propTypes = {
63
- printerStore: PropTypes.number
67
+ printerStore: PropTypes.number,
68
+ isFontFamily: PropTypes.bool
64
69
  }
65
70
 
66
71
  export default Page
@@ -299,6 +299,46 @@ class PrinterStore {
299
299
  this.pages.push(page)
300
300
  }
301
301
 
302
+ @action
303
+ financeComputedPages() {
304
+ // 当前在处理 contents 的索引
305
+ let index = 0
306
+ /** 一页承载的内容. [object, object, ...] */
307
+ let page = []
308
+ const pageFixLineNum = this.config.financeSpecialConfig.pageFixLineNum
309
+ while (index < this.config.contents.length) {
310
+ const content = this.config.contents[index]
311
+ // debugger
312
+ const table = this.tablesInfo[`contents.table.${index}`]
313
+ // 表格行的索引,用于table.slice(begin, end), 分割到不同页面中
314
+ let begin = 0
315
+ let end = 0
316
+
317
+ if (content.type === 'table') {
318
+ while (end < table.body.heights.length) {
319
+ end += pageFixLineNum
320
+ page.push({
321
+ type: 'table',
322
+ index,
323
+ begin,
324
+ end
325
+ })
326
+ this.pages.push(page)
327
+ page = []
328
+ begin = end
329
+ }
330
+ index++
331
+ } else {
332
+ page.push({
333
+ type: 'panel',
334
+ index
335
+ })
336
+ index++
337
+ }
338
+ }
339
+ // this.pages.push(page)
340
+ }
341
+
302
342
  template(text, pageIndex) {
303
343
  // 做好保护,出错就返回 text
304
344
  try {
@@ -334,6 +374,30 @@ class PrinterStore {
334
374
  }
335
375
  }
336
376
 
377
+ /**
378
+ * 配送单的定制化需求:限制表格里的字的个数,超出显示'...'
379
+ */
380
+ templateFinance(text, dataKey, index, pageIndex) {
381
+ // 做好保护,出错就返回 text
382
+ try {
383
+ const list = this.data._table[dataKey] || this.data._table.orders
384
+
385
+ let res = _.template(text, {
386
+ interpolate: /{{([\s\S]+?)}}/g
387
+ })({
388
+ ...this.data.common,
389
+ [i18next.t('列')]: list[index],
390
+ [i18next.t('当前页码')]: pageIndex + 1,
391
+ [i18next.t('页码总数')]: this.pages.length,
392
+ price: price // 提供一个价格处理函数
393
+ })
394
+ res = res.length > 9 ? `${res.slice(0, 9)}...` : res
395
+ return res
396
+ } catch (err) {
397
+ return text
398
+ }
399
+ }
400
+
337
401
  templateSpecialDetails(col, dataKey, index) {
338
402
  // 做好保护,出错就返回 text
339
403
  const { specialDetailsKey, text, detailLastColType, separator } = col
@@ -28,6 +28,10 @@
28
28
  }
29
29
  }
30
30
 
31
+ .gm-printer-page-fontFamily {
32
+ font-family:STSong
33
+ }
34
+
31
35
  // panel
32
36
  .gm-printer-panel {
33
37
  position: relative;