yh-hiprint 2.5.2 → 3.0.0

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,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
+ }
@@ -0,0 +1,55 @@
1
+ // @ts-ignore
2
+ import axios from '@/libs/api.request';
3
+ import {ElMessage, ElMessageBox} from 'element-plus';
4
+ import {hiprint} from 'yh-hiprint';
5
+
6
+ export default async function (code) {
7
+ let {
8
+ data: {list, json},
9
+ } = await axios.request({
10
+ url: `/printTemplate/data/${code}`,
11
+ method: 'post',
12
+ type: 'json',
13
+ data: [
14
+ {
15
+ code: '50101820',
16
+ },
17
+ ],
18
+ });
19
+ if (json) {
20
+ if (Array.isArray(list) && list.length > 0) {
21
+ list = list.map((item) => {
22
+ let printData = {};
23
+ let datas = Object.entries(item);
24
+ datas.forEach((arr) => {
25
+ if (Array.isArray(arr[1])) {
26
+ printData[arr[0]] = arr[1];
27
+ } else {
28
+ if (arr[1] !== null && arr[1] !== undefined) {
29
+ let itemsEntries = Object.entries(arr[1]);
30
+ itemsEntries.forEach((cArr) => {
31
+ printData[`$${arr[0]}[${cArr[0]}]`] = cArr[1];
32
+ });
33
+ }
34
+ }
35
+ });
36
+ return printData;
37
+ });
38
+ } else {
39
+ list = [];
40
+ }
41
+ const htmlStr = new hiprint.PrintTemplate({template: JSON.parse(json)}).getHtml(list)[0].innerHTML;
42
+ ElMessageBox.alert(htmlStr, '打印预览', {
43
+ customClass: 'print-preview-dialog',
44
+ dangerouslyUseHTMLString: true,
45
+ closeOnClickModal: true,
46
+ closeOnPressEscape: true,
47
+ closeOnHashChange: true,
48
+ showConfirmButton: false,
49
+ });
50
+ } else {
51
+ ElMessage.warning({
52
+ message: '模板配置不存在,请检查',
53
+ });
54
+ }
55
+ }
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "yh-hiprint",
3
- "version": "2.5.2",
4
- "description": "Hiprint for Vue3 by NoahLiu in ForceCon in Hunan Changesha",
5
- "main": "index.js",
6
- "types": "index.d.ts",
3
+ "version": "3.0.0",
4
+ "description": "Hiprint for Vue3",
5
+ "main": "index.ts",
7
6
  "scripts": {
8
7
  "start": "npm run pub:aliyun && npm run pub:npm",
9
8
  "pub:aliyun": "npm publish --registry https://packages.aliyun.com/60765e0161a945067837bb5f/npm/npm-registry/ --no-git-checks",
@@ -0,0 +1,152 @@
1
+ import {HlinePrintElementType, ImagePrintElementType, PrintElementTypes, TextPrintElementType, VlinePrintElementType, RectPrintElementType, OvalPrintElementType, TablePrintElementType, LongTextPrintElementType, BarcodePrintElementType, QRCodePrintElementType, HTMLPrintElementType} from '../types';
2
+
3
+ export const defaultTextElement: TextPrintElementType = {
4
+ printElementType: {
5
+ title: '文本',
6
+ type: PrintElementTypes.TEXT,
7
+ },
8
+ options: {
9
+ textType: 'text',
10
+ title: '文本',
11
+ },
12
+ };
13
+
14
+ export const defaultTextValElement: TextPrintElementType = {
15
+ printElementType: {
16
+ title: '值(文本)',
17
+ type: PrintElementTypes.TEXT,
18
+ },
19
+ options: {
20
+ textType: 'text',
21
+ title: '值(文本)',
22
+ },
23
+ };
24
+
25
+ export const defaultImageElement: ImagePrintElementType = {
26
+ printElementType: {
27
+ title: '图片',
28
+ type: PrintElementTypes.IMAGE,
29
+ },
30
+ options: {
31
+ title: '图片',
32
+ },
33
+ };
34
+
35
+ export const defaultHlineElement: HlinePrintElementType = {
36
+ printElementType: {
37
+ title: '横线',
38
+ type: PrintElementTypes.HLINE,
39
+ },
40
+ options: {
41
+ title: '横线',
42
+ },
43
+ };
44
+
45
+ export const defaultVlineElement: VlinePrintElementType = {
46
+ printElementType: {
47
+ title: '竖线',
48
+ type: PrintElementTypes.VLINE,
49
+ },
50
+ options: {
51
+ title: '竖线',
52
+ },
53
+ };
54
+
55
+ export const defaultReactElement: RectPrintElementType = {
56
+ printElementType: {
57
+ title: '矩形',
58
+ type: PrintElementTypes.RECT,
59
+ },
60
+ options: {
61
+ title: '矩形',
62
+ },
63
+ };
64
+
65
+ export const defaultOvalElement: OvalPrintElementType = {
66
+ printElementType: {
67
+ title: '圆形',
68
+ type: PrintElementTypes.OVAL,
69
+ },
70
+ options: {
71
+ title: '圆形',
72
+ },
73
+ };
74
+
75
+ export const defaultTableElement: TablePrintElementType = {
76
+ printElementType: {
77
+ title: '表格',
78
+ type: PrintElementTypes.TABLE,
79
+ columns: [
80
+ [
81
+ {
82
+ title: '列1',
83
+ width: 100,
84
+ rowspan: 1,
85
+ colspan: 1,
86
+ },
87
+ {
88
+ title: '列2',
89
+ width: 100,
90
+ rowspan: 1,
91
+ colspan: 1,
92
+ },
93
+ ],
94
+ ],
95
+ },
96
+ options: {
97
+ title: '表格',
98
+ },
99
+ };
100
+
101
+ export const defaultemptyTableElement: TablePrintElementType = {
102
+ printElementType: {
103
+ title: '空白表格',
104
+ type: PrintElementTypes.TABLE,
105
+ columns: [],
106
+ },
107
+ options: {
108
+ title: '空白表格',
109
+ },
110
+ };
111
+
112
+ export const defaultLongTextElement: LongTextPrintElementType = {
113
+ printElementType: {
114
+ title: '长文本',
115
+ type: PrintElementTypes.LONGTEXT,
116
+ },
117
+ options: {
118
+ title: '长文本',
119
+ },
120
+ };
121
+
122
+ export const defaultHTMLElement: HTMLPrintElementType = {
123
+ printElementType: {
124
+ title: 'HTML',
125
+ type: PrintElementTypes.HTML,
126
+ },
127
+ options: {
128
+ title: 'HTML',
129
+ },
130
+ };
131
+
132
+ export const defaultQRCodeElement: QRCodePrintElementType = {
133
+ printElementType: {
134
+ title: '二维码',
135
+ type: PrintElementTypes.TEXT,
136
+ },
137
+ options: {
138
+ title: '二维码',
139
+ textType: 'qrcode',
140
+ },
141
+ };
142
+
143
+ export const defaultBarcodeElement: BarcodePrintElementType = {
144
+ printElementType: {
145
+ title: '条形码',
146
+ type: PrintElementTypes.TEXT,
147
+ },
148
+ options: {
149
+ title: '条形码',
150
+ textType: 'barcode',
151
+ },
152
+ };
@@ -0,0 +1,75 @@
1
+ import {computed, ref} from 'vue';
2
+ import {HiprintPrintOption, PrintElementType, PrintElementTypes, PrintPanelOptions, TextPrintElementType} from 'yh-hiprint/types';
3
+ import {defineStore} from 'pinia';
4
+
5
+ export default defineStore('useHiprintStore', () => {
6
+ const printElements = ref<PrintElementType[]>([]);
7
+ const panelOption = ref<PrintPanelOptions>();
8
+
9
+ const activeElements = ref<PrintElementType[]>([]);
10
+
11
+ const activeElement = computed<PrintElementType | null>(() => {
12
+ if (activeElements.value.length && activeElements.value.length === 1) {
13
+ return activeElements.value[0];
14
+ } else {
15
+ return null;
16
+ }
17
+ });
18
+
19
+ const panelType = computed<PrintElementTypes>(() => {
20
+ if (activeElement.value === null) {
21
+ return PrintElementTypes.PANEL;
22
+ } else {
23
+ if (activeElement.value.printElementType.type === PrintElementTypes.TEXT) {
24
+ if ((activeElement.value as TextPrintElementType).options.textType === 'qrcode') {
25
+ return PrintElementTypes.QRCODE;
26
+ } else if ((activeElement.value as TextPrintElementType).options.textType === 'barcode') {
27
+ return PrintElementTypes.BARCODE;
28
+ } else {
29
+ return PrintElementTypes.TEXT;
30
+ }
31
+ } else {
32
+ switch (activeElement.value.printElementType.type) {
33
+ case PrintElementTypes.IMAGE:
34
+ return PrintElementTypes.IMAGE;
35
+ case PrintElementTypes.TABLE:
36
+ return PrintElementTypes.TABLE;
37
+ case PrintElementTypes.HLINE:
38
+ return PrintElementTypes.HLINE;
39
+ case PrintElementTypes.VLINE:
40
+ return PrintElementTypes.VLINE;
41
+ case PrintElementTypes.RECT:
42
+ return PrintElementTypes.RECT;
43
+ case PrintElementTypes.OVAL:
44
+ return PrintElementTypes.OVAL;
45
+ case PrintElementTypes.LONGTEXT:
46
+ return PrintElementTypes.LONGTEXT;
47
+ case PrintElementTypes.HTML:
48
+ return PrintElementTypes.HTML;
49
+ default:
50
+ return PrintElementTypes.PANEL;
51
+ }
52
+ }
53
+ }
54
+ });
55
+
56
+ const hiprontPrintOption = computed<string>(() => {
57
+ return JSON.stringify({
58
+ panels: [
59
+ {
60
+ ...panelOption.value,
61
+ printElements: printElements.value,
62
+ },
63
+ ],
64
+ });
65
+ });
66
+
67
+ return {
68
+ hiprontPrintOption,
69
+ panelType,
70
+ panelOption,
71
+ printElements,
72
+ activeElement,
73
+ activeElements,
74
+ };
75
+ });