yh-hiprint 2.5.1 → 2.5.3

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare module "yh-hiprint";
1
+ declare module 'yh-hiprint';
2
2
 
3
3
  /** JQuery 简化dom操作的库 */
4
4
  export declare const jquery: JQuery;
@@ -12,7 +12,6 @@ export declare const zIndex;
12
12
  /** hiprint 侧边栏配置 */
13
13
  export declare const panel;
14
14
 
15
- export declare const hiprint;
16
15
  export declare const defaultElementTypeProvider: (options: any) => {
17
16
  addElementTypes: (context) => void;
18
17
  };
@@ -64,4 +63,315 @@ export declare interface HiprintOption {
64
63
  isCustom: boolean;
65
64
  }
66
65
 
67
- export declare const hiprint: (HiprintOption) => void;
66
+ //#region Hiprint 类型文件
67
+
68
+
69
+ /** 打印元素唯一标识 */
70
+ type Tid = string;
71
+
72
+ export interface Field {
73
+ field: string;
74
+ text: string;
75
+ }
76
+
77
+ export interface TextPrintElementType {
78
+ printElementType: {
79
+ tid: Tid;
80
+ title: string;
81
+ field: string;
82
+ data: string;
83
+ formatter: (title,value,options,templateData,target) => string;
84
+ styler: (value,options,target,templateData,) => CSSStyleDeclaration;
85
+ onRendered: (target,options,paperTarget) => void;
86
+ fields: Field[],
87
+ },
88
+ options: {
89
+ /**
90
+ * 标题
91
+ * field 存在则
92
+ */
93
+ title: string;
94
+ /** 字段名称,从数据源获取数据的属性名称 */
95
+ field: string;
96
+ /** 测试数据 设计时使用 */
97
+ testData: string;
98
+ /** 字体名称 */
99
+ fontFamily: string;
100
+ /** 字体大小 */
101
+ fontSize: string;
102
+ /** 字体粗细 */
103
+ fontWeight: string;
104
+ /** 字体间距 */
105
+ letterSpacing: number;
106
+ /** 字体颜色 */
107
+ color: string;
108
+ /** 文本描述 */
109
+ textDecoration: string;
110
+ /** 文字水平对齐 */
111
+ textAlign: 'left'|'center'|'right';
112
+ /** 行高 数字带单位 例如 18pt */
113
+ lineHeight: string;
114
+ /**
115
+ * 文本类型
116
+ * text - 纯文本
117
+ * barcode - 条形码
118
+ * qrcode - 二维码
119
+ */
120
+ textType: 'text' | 'barcode' | 'qrcode';
121
+ /** 条形码格式 */
122
+ barcodeMode: string;
123
+ /** 因此标题 */
124
+ hideTitle: boolean;
125
+ /**
126
+ * 页眉/页脚/固定定位等元素显示规则
127
+ * first - 首页显示
128
+ * odd - 奇数页显示
129
+ */
130
+ showInPage: 'first' | 'odd' | 'even' | 'last';
131
+ /** 固定定位 */
132
+ fixed: boolean;
133
+ /** 打印设计时拖拽方向 */
134
+ axis: 'v' | 'h';
135
+ /** 旋转角度 */
136
+ transform: number;
137
+ }
138
+ }
139
+
140
+ export interface ImagePrintElementType {
141
+ printElementType: {
142
+ tid: string;
143
+ field: string;
144
+ data: string;
145
+ formatter: () => void;
146
+ styler: () => void;
147
+ onRendered: () => void;
148
+ fields: Field[];
149
+ },
150
+ options: {
151
+ title: string;
152
+ field: string;
153
+ src: string;
154
+ showInPage: string;
155
+ fixed: boolean;
156
+ axis: string;
157
+ transform: number;
158
+ }
159
+ }
160
+
161
+ export interface HlinePrintElementType {
162
+ printElementType: {
163
+ tid: string;
164
+ onRendered: () => void;
165
+ },
166
+ options: {
167
+ borderColor: string;
168
+ borderWidth: string;
169
+ showInPage: string;
170
+ fixed: boolean;
171
+ axis: string;
172
+ transform: number;
173
+ }
174
+ }
175
+ export interface VlinePrintElementType {
176
+ printElementType: {
177
+ tid: string;
178
+ onRendered: () => void;
179
+ },
180
+ options: {
181
+ borderColor: string;
182
+ borderWidth: string;
183
+ showInPage: string;
184
+ fixed: boolean;
185
+ axis: string;
186
+ transform: number;
187
+ }
188
+ }
189
+ export interface RectPrintElementType {
190
+ printElementType: {
191
+ tid: string;
192
+ onRendered: () => void;
193
+ },
194
+ options: {
195
+ borderColor: string;
196
+ borderWidth: string;
197
+ showInPage: string;
198
+ fixed: boolean;
199
+ axis: string;
200
+ transform: number;
201
+ }
202
+ }
203
+ export interface OvalPrintElementType {
204
+ printElementType: {
205
+ tid: string;
206
+ onRendered: () => void;
207
+ },
208
+ options: {
209
+ borderColor: string;
210
+ borderWidth: string;
211
+ showInPage: string;
212
+ fixed: boolean;
213
+ axis: string;
214
+ transform: number;
215
+ }
216
+ }
217
+ export interface HlinePrintElementType {
218
+ printElementType: {
219
+ tid: string;
220
+ onRendered: () => void;
221
+ },
222
+ options: {
223
+ borderColor: string;
224
+ borderWidth: string;
225
+ showInPage: string;
226
+ fixed: boolean;
227
+ axis: string;
228
+ transform: number;
229
+ }
230
+ }
231
+
232
+ export interface TableColumn {
233
+ width: number;
234
+ title: string;
235
+ field: string;
236
+ fixed: boolean;
237
+ rowspan: number;
238
+ colspan: number;
239
+ align: string;
240
+ halign: string;
241
+ formater: () => void;
242
+ styler: () => void;
243
+ }
244
+
245
+ export interface TablePrintElementType {
246
+ printElementType: {
247
+ tid: string;
248
+ title: string;
249
+ field: string;
250
+ columns: TableColumn[]
251
+ styler: () => void;
252
+ rowStyler: () => void;
253
+ formatter: () => void;
254
+ groupFields: any[];
255
+ groupFormatter: () => void;
256
+ fotterFormatter: () => void;
257
+ gridColumnsFooterFormatter: () => void;
258
+ groupFooterFormatter: () => void;
259
+ onRendered: () => void;
260
+ fields: Field[];
261
+ },
262
+ options: {
263
+ field: string;
264
+ fontFamily: string;
265
+ fontSize: number;
266
+ lineHeight: number;
267
+ textAlign: 'left'|'center'|'right';
268
+ tableBorder: 'noBorder' | 'border';
269
+ tableHeaderBorder: 'noBorder'|'border'|'topBorder'|'bottomBorder'|'topBottomBorder';
270
+ tableHeaderCellBorder: 'noBorder'|'border';
271
+ tableHeaderRowHeight: number;
272
+ tableHeaderBackground: string;
273
+ tableHeaderFontSize: number;
274
+ tableHeaderFontWeight: string;
275
+ tableBodyCellBorder: string;
276
+ tableBodyRowBorder: string;
277
+ tableBodyRowHeight: number;
278
+ axis: 'v' | 'h';
279
+ lHeight: number;
280
+ }
281
+ }
282
+ export interface LongTextPrintElementType {
283
+ printElementType: {
284
+ tid: string;
285
+ title: string;
286
+ field: string;
287
+ data: string;
288
+ formatter: () => void;
289
+ styler: () => void;
290
+ onRendered: () => void;
291
+ fields: Field[];
292
+ },
293
+ options: {
294
+ title: string;
295
+ field: string;
296
+ testData: string;
297
+ fontFamily: string;
298
+ fontSize: number;
299
+ fontWeight: string;
300
+ letterSpacing: number;
301
+ color: string;
302
+ textDecoration: string;
303
+ textAlign: string;
304
+ lineHeight: number;
305
+ hideTitle: boolean;
306
+ longTextIndent: number;
307
+ showInPage: string;
308
+ fixed: boolean;
309
+ leftSpaceRemoved: boolean;
310
+ axis: string;
311
+ lHeight: number;
312
+ transform: number;
313
+ }
314
+ }
315
+ export interface HTMLPrintElementType {
316
+ printElementType: {
317
+ tid: string;
318
+ title: string;
319
+ field: string;
320
+ data: string;
321
+ formatter: () => void;
322
+ styler: () => void;
323
+ onRendered: () => void;
324
+ },
325
+ options: {
326
+ title: string;
327
+ field: string;
328
+ testData: string;
329
+ fontFamily: string;
330
+ showInPage: string;
331
+ fixed: boolean;
332
+ axis: string;
333
+ transform: number;
334
+ }
335
+ }
336
+
337
+ /** Hiprint 打印面板对象 */
338
+ export interface PrintPanel {
339
+ addPrintText: (options?) => void;
340
+ addPrintLongText: () => void;
341
+ }
342
+
343
+ /** Hprint 打印面板创建配置对象 */
344
+ export interface PrintPanelOptions {
345
+ /** 宽度 */
346
+ width: number;
347
+ /** 高度 */
348
+ height: number;
349
+ /** 页脚高度 */
350
+ paperFotter: number;
351
+ /** 页眉高度 */
352
+ paperHeader: number;
353
+ }
354
+
355
+ export interface PrintTemplateData {
356
+
357
+ }
358
+
359
+ export interface PrintTemplateOptions {
360
+ template: PrintTemplateData
361
+ }
362
+
363
+ export interface PrintTemplate {
364
+ addPrintPanel:(options:PrintPanelOptions) => void;
365
+ design: () => void;
366
+ }
367
+
368
+ export declare const hiprint = {
369
+ /** 初始化 Hiprint */
370
+ init:() => viod,
371
+ /** 创建打印模板对象 */
372
+ PrintTemplate: {
373
+ new (options: PrintTemplateOptions): PrintTemplate;
374
+ }
375
+ }
376
+
377
+ //#endregion
package/index.js CHANGED
@@ -1,11 +1,14 @@
1
+ // @ts-check
1
2
  import './libs/jquery';
2
- export {hiprint, defaultElementTypeProvider, print, print2, usePaper, useScale, useDataSource} from './hooks/useHiprint';
3
- export {default as fontSize} from './font-size';
4
- export {default as scale} from './scale';
5
- export {default as zIndex} from './z-index';
6
- export {default as panel} from './panel';
3
+ export { hiprint, defaultElementTypeProvider, print, print2, usePaper, useScale, useDataSource } from './hooks/useHiprint';
4
+ export { default as fontSize } from './font-size';
5
+ export { default as scale } from './scale';
6
+ export { default as zIndex } from './z-index';
7
+ export { default as panel } from './panel';
8
+ import { getPrintTemplate } from 'yh-hiprint/libs/index.js';
9
+ import { ElMessageBox, ElLoading } from 'element-plus';
7
10
 
8
- export function cLog(string, isError = false) {
11
+ export function cLog (string, isError = false) {
9
12
  if (isError) {
10
13
  console.error('%cyhHiprint:%c', 'font-size: 16px;font-weight: bold;color: #00ffff', 'font-size: 16px;font-weight: bold;color: #ccccc', string);
11
14
  } else {
@@ -13,9 +16,10 @@ export function cLog(string, isError = false) {
13
16
  }
14
17
  }
15
18
 
16
- const hiprint = ({code, params, data, isCustom}) => {
19
+ const hiprintFun = async ({ code, params, data, isCustom, returnHtml }) => {
17
20
  let height = document.documentElement.clientHeight;
18
- let width = (document.documentElement.clientWidth - 1200) / 2;
21
+ let width = 1200;
22
+ let left = (document.documentElement.clientWidth - 1200) / 2;
19
23
  // 转换数组
20
24
  let paramData = params;
21
25
  if (!Array.isArray(params)) {
@@ -31,11 +35,70 @@ const hiprint = ({code, params, data, isCustom}) => {
31
35
  if (isCustom) {
32
36
  url += `&isCustom=${isCustom ? '1' : '0'}`;
33
37
  }
34
- let windowOpen = window.open(url, 'hiprintWindow', `height=${height}, width=1200, top=20, left=${width}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
38
+ if (returnHtml) {
39
+ url += `&returnHtml=${returnHtml ? '1' : '0'}`;
40
+ }
41
+ let windowOpen = window.open(url, 'hiprintWindow', `height=${height}, width=${width}, top=20, left=${left}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
42
+
43
+ return new Promise((resolve, reject) => {
44
+ if (!windowOpen) {
45
+ reject("预览窗口渲染失败!");
46
+ }
47
+ function printHTMLHandler (e) {
48
+ let { type, data } = e.data;
49
+ if (type === "sendPrintHTML" && data) {
50
+ resolve(data);
51
+ }
52
+ window.removeEventListener('message', printHTMLHandler);
53
+ }
54
+ window.addEventListener('message', printHTMLHandler);
55
+ });
35
56
  };
57
+
58
+ const silentHiprint = async ({ code, params = {}, data }) => {
59
+ if (!code || !data) {
60
+ ElMessageBox.alert("请正确的传递模板编号和打印数据", '提示');
61
+ return false;
62
+ }
63
+ let loading = null;
64
+ try {
65
+ loading = ElLoading.service({
66
+ text: "正在获取打印模板"
67
+ });
68
+ let res = await getPrintTemplate(code, params);
69
+ if (res.status === 200 && res.data && res.data.json) {
70
+ let json = res.data.json;
71
+ let hiprintTemplate = new hiprint.PrintTemplate({ template: JSON.parse(json) });
72
+ let html = hiprintTemplate.getHtml(data);
73
+ if (html[0]) {
74
+ return html[0].innerHTML;
75
+ } else {
76
+ ElMessageBox.alert("打印内容渲染失败", '提示');
77
+ }
78
+ } else {
79
+ ElMessageBox.alert(res.message || "获取打印模板失败", '提示');
80
+ }
81
+ } catch (error) {
82
+ if (loading) {
83
+ loading.close();
84
+ loading = null;
85
+ }
86
+ console.error(error);
87
+ } finally {
88
+ if (loading) {
89
+ loading.close();
90
+ loading = null;
91
+ }
92
+ }
93
+
94
+ };
95
+
96
+ window.silentHiprint = silentHiprint;
97
+
36
98
  export default {
37
- install(app, {router, pinia, isAdmin}) {
38
- app.provide('$hiprint', hiprint);
99
+ install (app, { router, pinia, isAdmin }) {
100
+ app.provide('$hiprint', hiprintFun);
101
+ app.provide('$silentHiprint', silentHiprint);
39
102
  if (router) {
40
103
  router.addRoute('Index', {
41
104
  path: 'hiprint/designer',
@@ -0,0 +1,42 @@
1
+ import * as babelCore from "@babel/core";
2
+ import parser from "@babel/parser";
3
+
4
+ // ✅ 只导入实际用到的插件
5
+ import pluginTransformArrowFunctions from "@babel/plugin-transform-arrow-functions";
6
+ import pluginTransformBlockScoping from "@babel/plugin-transform-block-scoping";
7
+ import pluginTransformTemplateLiterals from "@babel/plugin-transform-template-literals";
8
+ import pluginTransformShorthandProperties from "@babel/plugin-transform-shorthand-properties";
9
+ import pluginTransformComputedProperties from "@babel/plugin-transform-computed-properties";
10
+
11
+ /**
12
+ * 将 ES6 代码转换为 ES5
13
+ * @param {string} code - 用户输入的 ES6+ 代码
14
+ * @returns {string} - 编译后的 ES5 代码
15
+ */
16
+ export default function compileES6toES5 (code) {
17
+ try {
18
+ // 1. 解析为 AST
19
+ const ast = parser.parse(code, {
20
+ sourceType: "script",
21
+ plugins: [],
22
+ });
23
+
24
+ // 2. 转换 AST → ES5(内部自动调用 generator)
25
+ const result = babelCore.transformFromAstSync(ast, code, {
26
+ plugins: [
27
+ pluginTransformArrowFunctions,
28
+ pluginTransformBlockScoping,
29
+ pluginTransformTemplateLiterals,
30
+ pluginTransformShorthandProperties,
31
+ pluginTransformComputedProperties,
32
+ ],
33
+ sourceMaps: false,
34
+ ast: false, // ✅ 如果你不需要返回 AST,设为 false 更轻量
35
+ });
36
+
37
+ return result.code;
38
+ } catch (err) {
39
+ console.error("Babel 编译错误:", err);
40
+ throw new Error("编译失败:" + (err.message || err));
41
+ }
42
+ }
@@ -1,82 +1,82 @@
1
1
  export default function (hiprint) {
2
2
  return function (options) {
3
3
  var addElementTypes = function (context) {
4
- context.removePrintElementTypes("defaultModule");
5
- context.addPrintElementTypes("defaultModule", [
6
- new hiprint.PrintElementTypeGroup("常规", [
4
+ context.removePrintElementTypes('defaultModule');
5
+ context.addPrintElementTypes('defaultModule', [
6
+ new hiprint.PrintElementTypeGroup('常规', [
7
7
  {
8
- tid: "defaultModule.text",
9
- title: "文本",
10
- data: "",
11
- type: "text",
8
+ tid: 'defaultModule.text',
9
+ title: '文本',
10
+ data: '',
11
+ type: 'text',
12
12
  },
13
13
  {
14
- tid: "defaultModule.textVal",
15
- title: "值(文本)",
16
- data: "",
17
- type: "text",
14
+ tid: 'defaultModule.textVal',
15
+ title: '值(文本)',
16
+ data: '',
17
+ type: 'text',
18
18
  options: {
19
19
  hideTitle: true,
20
20
  },
21
21
  },
22
22
  {
23
- tid: "defaultModule.barcode",
24
- title: "条形码",
25
- data: "",
26
- type: "text",
23
+ tid: 'defaultModule.barcode',
24
+ title: '条形码',
25
+ data: '',
26
+ type: 'text',
27
27
  options: {
28
- field: "barcode",
29
- testData: "lkforntend",
28
+ field: 'barcode',
29
+ testData: 'lkforntend',
30
30
  height: 32,
31
31
  fontSize: 12,
32
32
  lineHeight: 18,
33
- textType: "barcode",
33
+ textType: 'barcode',
34
34
  },
35
35
  },
36
36
  {
37
- tid: "defaultModule.qrcode",
38
- title: "二维码",
39
- data: "",
40
- type: "text",
37
+ tid: 'defaultModule.qrcode',
38
+ title: '二维码',
39
+ data: '',
40
+ type: 'text',
41
41
  options: {
42
- testData: "力控远海-技术中心-前端",
43
- field: "qrcode",
42
+ testData: '力控元海-技术中心-前端',
43
+ field: 'qrcode',
44
44
  width: 50,
45
45
  height: 50,
46
- textType: "qrcode",
46
+ textType: 'qrcode',
47
47
  },
48
48
  },
49
49
  {
50
- tid: "defaultModule.image",
51
- title: "图片",
52
- data: "",
53
- type: "image",
50
+ tid: 'defaultModule.image',
51
+ title: '图片',
52
+ data: '',
53
+ type: 'image',
54
54
  },
55
55
  {
56
- tid: "defaultModule.longText",
57
- title: "长文",
58
- data: "155123456789",
59
- type: "longText",
56
+ tid: 'defaultModule.longText',
57
+ title: '长文',
58
+ data: '155123456789',
59
+ type: 'longText',
60
60
  },
61
61
  {
62
- tid: "defaultModule.table",
63
- field: "table",
64
- title: "表格",
65
- type: "table",
62
+ tid: 'defaultModule.table',
63
+ field: 'table',
64
+ title: '表格',
65
+ type: 'table',
66
66
  columns: [
67
67
  [
68
68
  {
69
- title: "",
69
+ title: '',
70
70
  width: 100,
71
71
  },
72
72
  {
73
- title: "",
73
+ title: '',
74
74
  width: 100,
75
75
  },
76
76
  ],
77
77
  ],
78
78
  editable: true,
79
- tableBorder: "border",
79
+ tableBorder: 'border',
80
80
  columnDisplayEditable: true, //列显示是否能编辑
81
81
  columnDisplayIndexEditable: true, //列顺序显示是否能编辑
82
82
  columnTitleEditable: true, //列标题是否能编辑
@@ -91,61 +91,61 @@ export default function (hiprint) {
91
91
  isEnableMergeCell: true, //合并单元格
92
92
  },
93
93
  {
94
- tid: "defaultModule.emptyTable",
95
- field: "table",
96
- title: "空白表格",
97
- type: "table",
94
+ tid: 'defaultModule.emptyTable',
95
+ field: 'table',
96
+ title: '空白表格',
97
+ type: 'table',
98
98
  columns: [
99
99
  [
100
100
  {
101
- title: "",
102
- field: "",
101
+ title: '',
102
+ field: '',
103
103
  width: 100,
104
104
  },
105
105
  {
106
- title: "",
107
- field: "",
106
+ title: '',
107
+ field: '',
108
108
  width: 100,
109
109
  },
110
110
  ],
111
111
  ],
112
112
  },
113
113
  {
114
- tid: "defaultModule.html",
115
- title: "html",
114
+ tid: 'defaultModule.html',
115
+ title: 'html',
116
116
  formatter: function (data, options) {
117
- return "<div></div>";
117
+ return '<div></div>';
118
118
  },
119
- type: "html",
119
+ type: 'html',
120
120
  },
121
121
  {
122
- tid: "defaultModule.customText",
123
- title: "自定义文本",
124
- customText: "自定义文本",
122
+ tid: 'defaultModule.customText',
123
+ title: '自定义文本',
124
+ customText: '自定义文本',
125
125
  custom: true,
126
- type: "text",
126
+ type: 'text',
127
127
  },
128
128
  ]),
129
- new hiprint.PrintElementTypeGroup("辅助", [
129
+ new hiprint.PrintElementTypeGroup('辅助', [
130
130
  {
131
- tid: "defaultModule.hline",
132
- title: "横线",
133
- type: "hline",
131
+ tid: 'defaultModule.hline',
132
+ title: '横线',
133
+ type: 'hline',
134
134
  },
135
135
  {
136
- tid: "defaultModule.vline",
137
- title: "竖线",
138
- type: "vline",
136
+ tid: 'defaultModule.vline',
137
+ title: '竖线',
138
+ type: 'vline',
139
139
  },
140
140
  {
141
- tid: "defaultModule.rect",
142
- title: "矩形",
143
- type: "rect",
141
+ tid: 'defaultModule.rect',
142
+ title: '矩形',
143
+ type: 'rect',
144
144
  },
145
145
  {
146
- tid: "defaultModule.oval",
147
- title: "椭圆",
148
- type: "oval",
146
+ tid: 'defaultModule.oval',
147
+ title: '椭圆',
148
+ type: 'oval',
149
149
  },
150
150
  ]),
151
151
  ]);