vg-print 1.0.208 → 1.0.209

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