vg-print 1.0.20 → 1.0.201

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/README.md CHANGED
@@ -1,252 +1,282 @@
1
- # 打印设计器插件(Vue3)
1
+ # 打印设计器组件库使用说明书
2
2
 
3
- 可视化打印设计器,基于 `hiprint` 封装,支持组件式与函数式调用,提供 `Designer`、`Header`、`Preview`、`DragBox` 组件与常用 API。包名:`vg-print`。
3
+ 本项目已改造为可发布的 Vue3 组件库,提供 `Designer`、`Header`、`Preview`、`DragBox` 四个 UI 组件,并同时暴露 `hiprint` 工具能力,满足“可视化设计 + 打印/导出/直接打印”的完整链路。
4
4
 
5
5
  ## 安装
6
6
 
7
- - 安装依赖:`npm i vg-print`
8
- - 引入样式:在应用入口引入 `dist/style.css`
9
- - 注入打印锁样式:调用 `injectPrintLock()`
7
+ ```bash
8
+ npm i vg-print
9
+ ```
10
+
11
+ 必须在宿主站点的 `index.html` 引入打印样式,以保证浏览器打印版式稳定:
12
+
13
+ ```html
14
+ <link rel="stylesheet" type="text/css" media="print" href="/print-lock.css" />
15
+ ```
16
+
17
+ 或使用 CDN:
10
18
 
11
- ```js
19
+ ```html
20
+ <link rel="stylesheet" type="text/css" media="print" href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css" />
21
+ ```
22
+
23
+ ## 快速开始
24
+
25
+ ```ts
26
+ // main.ts
12
27
  import { createApp } from 'vue'
13
28
  import App from './App.vue'
14
- import 'vg-print/style.css'
15
- import { hiPrintPlugin, injectPrintLock } from 'vg-print'
29
+ import HiprintDesigner from 'vg-print'
30
+ import 'vg-print/dist/style.css'
16
31
 
17
32
  const app = createApp(App)
18
- app.use(hiPrintPlugin)
19
- injectPrintLock()
33
+ app.use(HiprintDesigner, { name: '$hiPrint', autoConnect: true })
20
34
  app.mount('#app')
21
35
  ```
22
36
 
23
- ## 快速上手
24
-
25
37
  ```vue
26
38
  <template>
27
- <Designer :template="template" @onDesigned="onDesigned">
39
+ <Designer
40
+ :template="template"
41
+ :designOptions="{ grid: true, activePanel: true }"
42
+ :providers="providers"
43
+ :providerMap="{ container: '.hiprintEpContainer', value: 'defaultModule' }"
44
+ :plugins="plugins"
45
+ authKey=""
46
+ @onDesigned="onDesigned"
47
+ >
28
48
  <template #header>
29
- <Header title="sv-print" />
49
+ <Header :title="'sv-print'" :menuList="['预览']" @preview="openPreview" />
50
+ </template>
51
+ <template #other>
52
+ <DragBox target="SVPrint" drag-key="test" drag-style="width:200px;height:200px;left:238px;top:95px" :show="true">
53
+ <template #iconTitle>
54
+ <i class="svicon sv-print"></i><span>自定义拖拽框</span>
55
+ </template>
56
+ <template #body>
57
+ <div>自定义拖拽框内容</div>
58
+ </template>
59
+ </DragBox>
30
60
  </template>
31
61
  </Designer>
32
- </template>
33
62
 
63
+ <Preview ref="previewRef" :modalShow="false" />
64
+ </template>
34
65
  <script setup>
35
66
  import { ref } from 'vue'
36
- import { Designer, Header } from 'vg-print'
37
- import templateJson from './template.json'
67
+ import { defaultElementTypeProvider } from 'vg-print'
68
+ import { Designer, Header, Preview, DragBox } from 'vg-print'
38
69
 
39
- const template = ref(templateJson)
70
+ const template = ref({})
71
+ const previewRef = ref(null)
40
72
  const onDesigned = (e) => {
41
- const { hiprint, designerUtils } = e.detail
42
- const tpl = designerUtils.printTemplate
73
+ // e.detail.hiprint / e.detail.designerUtils.printTemplate
74
+ }
75
+ const openPreview = () => {
76
+ const tpl = previewRef.value.getTarget()
77
+ previewRef.value.init(tpl, { name: 'i不简' })
78
+ previewRef.value.show()
43
79
  }
44
- </script>
45
- ```
46
-
47
- ### 完整示例(含 providers、plugins、Preview、DragBox)
48
-
49
- ```vue
50
- <template>
51
- <div style="width: 100vw; height: 97vh;">
52
- <Designer
53
- :template="template"
54
- :printData="printData"
55
- :providers="providers"
56
- :providerMap="providerMap"
57
- :plugins="plugins"
58
- :designOptions="{ grid: true, activePanel: true }"
59
- :lang="'cn'"
60
- authKey=""
61
- @onDesigned="onDesigned"
62
- >
63
- <template #header>
64
- <Header title="vg-print" :menuList="['预览','导出']" @preview="openPreview" />
65
- </template>
66
- <template #other>
67
- <DragBox target="SVPrint" drag-key="test" drag-style="width:200px;height:200px;left:238px;top:95px" :show="true">
68
- <template #iconTitle>
69
- <span>自定义拖拽框</span>
70
- </template>
71
- <template #body>
72
- <div>扩展内容</div>
73
- </template>
74
- </DragBox>
75
- </template>
76
- </Designer>
77
-
78
- <Preview ref="previewRef" />
79
- </div>
80
- <button @click="openPreview">显示预览</button>
81
- <button @click="exportPdf">导出PDF</button>
82
- </template>
83
-
84
- <script setup>
85
- import { ref } from 'vue'
86
- import { Designer, Header, Preview, DragBox, hiprint } from 'vg-print'
87
- import provider from './provider.js'
88
- import templateJson from './template.json'
89
-
90
- const template = ref(templateJson)
91
- const printData = ref({ name: '测试' })
92
- const providers = ref([provider])
93
- const providerMap = ref({ container: '.hiprintEpContainer', value: 'defaultModule' })
94
- const plugins = ref([])
95
- const previewRef = ref(null)
96
- let tpl = null
97
80
 
98
- const onDesigned = (e) => { tpl = e.detail.designerUtils.printTemplate }
99
- const openPreview = () => { previewRef.value.init(tpl, printData.value); previewRef.value.show() }
100
- const exportPdf = () => { tpl && tpl.toPdf(printData.value, '导出', {}) }
81
+ const providers = [new (defaultElementTypeProvider())()]
82
+ const plugins = []
101
83
  </script>
102
84
  ```
103
85
 
104
- ## 组件与 API
86
+ ## 组件与属性
105
87
 
106
88
  ### Designer
107
89
 
108
- - 基础参数:
109
- - `template: object` 模板 json 或模板对象
110
- - `printData: object` 设计预览数据
111
- - `designOptions: object` 设计参数,默认 `{ grid: true, activePanel: true }`
112
- - `providers: array` 自定义可拖拽元素 provider 列表
113
- - `provider: function` provider 工厂,默认内置 `defaultElementTypeProvider`
114
- - `providerMap: object` `{ container: '.hiprintEpContainer', value: 'defaultModule' }`
115
- - `plugins: array` 插件注册列表
116
- - `authKey: string` 授权 key
117
- - `lang: string` 语言
118
- - 事件:
119
- - `onDesigned(e)` 返回 `{ hiprint, designerUtils }`,包含 `designerUtils.printTemplate`
120
- - 插槽:
121
- - `header` 顶部区域
122
- - `other` 扩展区域
123
-
124
- 示例:
90
+ - `template` object 模板 json/对象
91
+ - `printData` object 预览/打印数据
92
+ - `templateKey` string 本地缓存键(`localStorage`)
93
+ - `title` string 默认导出名称
94
+ - `designOptions` object 设计参数:`{ grid: true, activePanel: true }`
95
+ - `paperList` string[] 纸张列表(A/B 系列)
96
+ - `config` object 统一配置(同 `hiprint.setConfig`)
97
+ - `plugins` object[] 插件注册(扩展打印元素)
98
+ - `authKey` string 授权 key(无则体验版水印)
99
+ - `providers` object[] 可拖拽元素 provider 列表
100
+ - `providerMap` object 构建容器与键 `{ container: '.hiprintEpContainer', value: 'defaultModule' }`
125
101
 
126
- ```vue
127
- <Designer :template="template" :plugins="plugins" :providerMap="{ container: '.hiprintEpContainer', value: 'defaultModule' }" @onDesigned="onDesigned" />
128
- ```
102
+ 事件与插槽:
103
+
104
+ - 事件:`onDesigned({ detail: { hiprint, designerUtils } })`,其中 `designerUtils.printTemplate` 为当前模板实例
105
+ - 插槽:`#header` 顶部区域;`#other` 其他扩展区域
129
106
 
130
107
  ### Header
131
108
 
132
- - 参数:
133
- - `logoHtml: string` 左侧 logo html
134
- - `title: string` 标题
135
- - `reEle: boolean|string` 是否重建元素
136
- - `eleList: array` 元素列表
137
- - `reMenu: boolean|string` 是否重建菜单
138
- - `menuList: array` 菜单列表
139
- - `onPreviewClick: function` 预览点击回调,返回 `true` 阻止默认
140
- - 事件:
141
- - `preview` 默认预览点击事件
142
- - 插槽:
143
- - `headerLeft` 左侧内容
144
- - `headerCenter` 中间内容
145
- - `headerRight` 右侧内容
109
+ - `logoHtml` string 左侧 logo html
110
+ - `title` string 左侧标题
111
+ - `reEle` boolean 是否重建元素(true 则以 `eleList` 为准)
112
+ - `eleList` string[] 元素列表
113
+ - `reMenu` boolean 是否重建菜单(true 则以 `menuList` 为准)
114
+ - `menuList` string[] 菜单列表
115
+ - `onPreviewClick(e): boolean` 预览点击;返回 `true` 阻止默认行为
116
+
117
+ 插槽:`#headerLeft`、`#headerCenter`、`#headerRight`
146
118
 
147
119
  ### Preview
148
120
 
149
- - 参数:
150
- - `printTemplate: object|string` 模板对象或 json
151
- - `printData: object` 打印数据
152
- - `printerList: array` 打印机列表
153
- - `selectedPrinter: string` 默认打印机
154
- - `showPdf: boolean` 是否显示导出 PDF 按钮
155
- - `showImg: boolean` 是否显示导出图片按钮
156
- - `showPrint2: boolean` 是否显示直接打印按钮
157
- - `modalShow: boolean` 是否显示
158
- - 方法:
159
- - `init(template, data)` 初始化模板与数据
160
- - `show()` 显示预览并渲染
161
- - `getTarget()` 返回内部模板对象
162
- - `print()` 浏览器打印
163
- - `toPdf()` 导出 PDF
164
- - `toImage()` 导出图片
165
- - `directPrint()` 客户端直接打印
166
-
167
- 示例:
121
+ - `printTemplate` 模板 json/对象或 `hiprint.PrintTemplate` 实例
122
+ - `printData` json 打印数据
123
+ - `printerList` array 打印机列表
124
+ - `selectedPrinter` string 默认打印机名称
125
+ - `showPdf` boolean 显示导出 PDF
126
+ - `showImg` boolean 显示导出图片
127
+ - `showPrint2` boolean 显示直接打印
128
+ - `modalShow` boolean 是否显示
168
129
 
169
- ```vue
170
- <template>
171
- <button @click="testPreview">显示预览</button>
172
- <Preview ref="previewRef" />
173
- </template>
174
- <script setup>
175
- import { ref } from 'vue'
176
- import { Preview } from 'vg-print'
177
- import templateJson from './template.json'
130
+ 方法:`getTarget()`、`init(tpl, data)`、`show()`、`print()`、`toPdf()`、`toImage(options)`、`directPrint()`
178
131
 
179
- const previewRef = ref(null)
180
- const testPreview = () => {
181
- previewRef.value.init(templateJson, { name: 'i不简' })
182
- previewRef.value.show()
183
- }
184
- </script>
132
+ ### DragBox
133
+
134
+ - `target` string 容器 Id 或 ClassName
135
+ - `dragKey` string 拖拽标识
136
+ - `dragStyle` string 默认位置/大小(`left/top/width/height` 内联样式字符串)
137
+ - `mode` string `default|top|left|bottom|right|fixed`
138
+ - `show` boolean 是否显示
139
+
140
+ 插槽:`#iconTitle`、`#body`
141
+
142
+ ## 插件注册与扩展
143
+
144
+ 注册第三方元素插件(如 bwip-js 条码):
145
+
146
+ ```bash
147
+ npm i @sv-print/plugin-ele-bwip-js
185
148
  ```
186
149
 
187
- ### DragBox
150
+ ```ts
151
+ import { hiprint } from 'vg-print'
152
+ import pluginEleBwip from '@sv-print/plugin-ele-bwip-js'
188
153
 
189
- - 参数:
190
- - `target: string` 目标容器 Id 或 ClassName(配合 `Designer` 的 `other` 插槽使用)
191
- - `dragKey: string` 拖拽标识
192
- - `dragStyle: string` 初始位置大小,如 `width:200px;height:200px;left:238px;top:95px`
193
- - `mode: string` `default|top|left|bottom|right|fixed`
194
- - `show: boolean` 是否显示
195
- - 插槽:
196
- - `iconTitle` 头部图标与标题
197
- - `body` 内容
154
+ hiprint.register({ authKey: '', plugins: [ pluginEleBwip({}) ] })
155
+ ```
198
156
 
199
- 示例:
157
+ 也可在 `Designer` 组件通过 `:plugins` 传入:
200
158
 
201
159
  ```vue
202
- <Designer>
203
- <template #other>
204
- <DragBox target="SVPrint" drag-key="test" drag-style="width:200px;height:200px;left:238px;top:95px" :show="true">
205
- <template #iconTitle>
206
- <span>自定义拖拽框</span>
207
- </template>
208
- <template #body>
209
- <div>内容</div>
210
- </template>
211
- </DragBox>
212
- </template>
213
- <template #header>
214
- <Header title="sv-print" />
215
- </template>
216
- </Designer>
160
+ <Designer :plugins="[pluginEleBwip({})]" authKey="" />
217
161
  ```
218
162
 
219
- ## 函数式 API
163
+ 配置设计器 UI:
220
164
 
221
- ```js
222
- import { print, print2, defaultElementTypeProvider } from 'vg-print'
165
+ ```ts
166
+ import { hiprint } from 'vg-print'
223
167
 
224
- const tpl = print(defaultElementTypeProvider, templateJson, { data: {} })
225
- const tpl2 = print2(defaultElementTypeProvider, templateJson, { data: {} })
168
+ hiprint.setConfig({
169
+ showAdsorbLine: true,
170
+ showPosition: true,
171
+ showSizeBox: true,
172
+ })
226
173
  ```
227
174
 
228
- ## 可拖拽元素 Provider
175
+ 构建可拖拽元素 provider:
229
176
 
230
- ```js
177
+ ```ts
231
178
  import { hiprint } from 'vg-print'
232
179
 
233
- export default function(options){
234
- var addElementTypes = function(context){
180
+ const provider = function(options) {
181
+ const addElementTypes = function(context) {
235
182
  context.removePrintElementTypes('ibujianModule')
236
183
  context.addPrintElementTypes('ibujianModule', [
237
- new hiprint.PrintElementTypeGroup('i不简',[{
238
- tid:'ibujianModule.text', type:'text', title:'文本', options:{ title:'文本2', field:'text', testData:'123' }
239
- }])
184
+ new hiprint.PrintElementTypeGroup('i不简', [
185
+ {
186
+ tid: 'ibujianModule.text',
187
+ type: 'text',
188
+ title: '文本',
189
+ options: { title: '文本2', field: 'text', testData: '123' }
190
+ }
191
+ ])
240
192
  ])
241
193
  }
242
194
  return { addElementTypes }
243
195
  }
196
+
197
+ // 传入 Designer
198
+ <Designer :providers="[provider]" :providerMap="{ container: '.hiprintEpContainer', value: 'ibujianModule' }" />
244
199
  ```
245
200
 
246
- `Designer` 中传入 `providers` 或使用 `providerMap` 构建左侧容器。
201
+ 可选:自定义 provider 容器 UI 渲染(`providerMap.render`):
202
+
203
+ ```ts
204
+ const providerMap = {
205
+ container: '.hiprintEpContainer',
206
+ value: 'defaultModule',
207
+ render: (list) => {
208
+ const $ = globalThis.$
209
+ const container = $('<div class="hiprint-printElement-type"></div>')
210
+ list.forEach(function(item) {
211
+ const box = $(`<div class="draggable-ele-group"><span class="title">${ item.name }</span><div class="list"></div></div>`)
212
+ const typeList = box.find('.list')
213
+ item.printElementTypes.forEach(function(t) {
214
+ typeList.append(`<div class="draggable-ele" draggable="true" data-tid="${t.tid}">${t.title}</div>`)
215
+ })
216
+ container.append(box)
217
+ })
218
+ $(providerMap.container).empty().append(container)
219
+ }
220
+ }
221
+ ```
222
+
223
+ ## 全局方法与案例
224
+
225
+ 安装时会挂载全局:`src/lib/index.js:9` 与 `src/index.js:34`
226
+
227
+ - 全局对象:`this.$hiPrint`(或安装时自定义的 `name`)
228
+ - 浏览器打印:`this.$print(provider, templateJson, printData, options)`
229
+ - 直接打印:`this.$print2(provider, templateJson, printData, options)`(需客户端)
230
+ - 连接控制:`autoConnect(cb)` / `disAutoConnect()`
231
+
232
+ 浏览器打印:
233
+
234
+ ```ts
235
+ import { defaultElementTypeProvider } from 'vg-print'
236
+ this.$print(defaultElementTypeProvider, templateJson, { name: 'i不简' }, { importCss: true })
237
+ ```
238
+
239
+ 直接打印(指定客户端与打印机):
240
+
241
+ ```ts
242
+ const clientId = 'AlBaUCNs3AIMFPLZAAAh'
243
+ const client = this.$hiPrint.hiwebSocket.clients[clientId]
244
+ const printer = client.printerList[0]
245
+
246
+ this.$print2(undefined, templateJson, { name: 'i不简' }, {
247
+ client: clientId,
248
+ printer: printer.name,
249
+ title: '打印预览'
250
+ })
251
+ ```
252
+
253
+ 导出 PDF / 图片:
254
+
255
+ ```ts
256
+ const tpl = new this.$hiPrint.PrintTemplate({ template: templateJson })
257
+ tpl.toPdf({ name: 'i不简' }, '导出PDF')
258
+ await tpl.toImage({ name: 'i不简' }, { isDownload: true, type: 'image/jpeg', pixelRatio: 2 })
259
+ ```
260
+
261
+ ## 常见问题
262
+
263
+ - 必须引入 `print-lock.css`,否则浏览器打印样式会错乱
264
+ - 直接打印需安装并连接 `electron-hiprint` 客户端,线上使用需 `https` 以避免跨域连接失败
265
+ - 模板资源(图片等)建议使用绝对可访问链接或 Base64
266
+ - 纸张列表可参考 A/B 系列(来自 sv-print 文档)
267
+ - 可通过 `hiprint.setConfig` 与组件插槽定制 UI/交互
268
+
269
+ ## 打包与发布
270
+
271
+ - 运行库打包:`npm run build:lib`,产物:`dist/hiprint-designer.es.js`、`dist/hiprint-designer.umd.js`、`dist/style.css`
272
+ - `package.json` 已配置 `main/module/exports` 指向库产物
273
+ - 首次发布:`npm config set registry=https://registry.npmjs.org`、`npm adduser`、`npm publish`
247
274
 
248
- ## 注意事项
275
+ ## 代码位置索引
249
276
 
250
- - 需要在入口引入库样式,并调用 `injectPrintLock()` 注入打印锁样式。
251
- - 依赖 `jquery` 与 `element-plus`;请在应用中安装并初始化 UI 库。
252
- - 若使用客户端直连打印,请确保客户端组件可用并已连接。
277
+ - 组件安装入口:`src/lib/index.js:9`
278
+ - 全局插件与方法:`src/index.js:34`
279
+ - 设计器:`src/components/Designer.vue:16`
280
+ - 预览:`src/components/Preview.vue:1`
281
+ - 头部:`src/components/Header.vue:1`
282
+ - 拖拽盒:`src/components/DragBox.vue:1`
@@ -0,0 +1,15 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>vue-plugin-hiprint-demo</title>
7
+ <link rel="icon" href="/favicon.ico" />
8
+ <link rel="stylesheet" type="text/css" media="print" href="/css/print-lock.css" />
9
+ <script type="module" crossorigin src="/static/js/index-DaZYVOmF.js"></script>
10
+ <link rel="stylesheet" crossorigin href="/static/css/index-BQgwTBxP.css">
11
+ </head>
12
+ <body>
13
+ <div id="app"></div>
14
+ </body>
15
+ </html>