z-certificate-editor 1.0.0 → 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 z-certificate-editor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,7 +1,110 @@
1
- # 证书编辑器 (Certificate Editor)
1
+ # z-certificate-editor
2
2
 
3
3
  一个功能强大的基于 Vue 3 的可视化证书编辑器,支持拖拽编辑、系统字段关联、模板管理、图片/二维码处理等功能。
4
4
 
5
+ ## 📦 安装
6
+
7
+ ```bash
8
+ npm install z-certificate-editor
9
+ ```
10
+
11
+ 或使用 yarn:
12
+
13
+ ```bash
14
+ yarn add z-certificate-editor
15
+ ```
16
+
17
+ 或使用 pnpm:
18
+
19
+ ```bash
20
+ pnpm add z-certificate-editor
21
+ ```
22
+
23
+ ## 🚀 快速开始
24
+
25
+ ### 基础使用
26
+
27
+ ```vue
28
+ <template>
29
+ <CertificateEditor @save="handleSave" />
30
+ </template>
31
+
32
+ <script setup>
33
+ import { CertificateEditor } from 'z-certificate-editor'
34
+ import 'z-certificate-editor/dist/style.css'
35
+
36
+ const handleSave = (certificateData) => {
37
+ console.log('证书配置数据:', certificateData)
38
+ }
39
+ </script>
40
+ ```
41
+
42
+ ### 使用自定义系统字段
43
+
44
+ ```vue
45
+ <template>
46
+ <CertificateEditor
47
+ :system-fields="customSystemFields"
48
+ @save="handleSave"
49
+ />
50
+ </template>
51
+
52
+ <script setup>
53
+ import { CertificateEditor, SYSTEM_FIELDS } from 'z-certificate-editor'
54
+ import 'z-certificate-editor/dist/style.css'
55
+
56
+ const customSystemFields = [
57
+ {
58
+ key: 'studentId',
59
+ label: '学号',
60
+ placeholder: '{{学号}}',
61
+ category: '学生信息',
62
+ description: '学生的学号'
63
+ },
64
+ ...SYSTEM_FIELDS
65
+ ]
66
+
67
+ const handleSave = (certificateData) => {
68
+ console.log('证书配置数据:', certificateData)
69
+ }
70
+ </script>
71
+ ```
72
+
73
+ ### 通过组件引用获取数据
74
+
75
+ ```vue
76
+ <template>
77
+ <CertificateEditor
78
+ ref="certificateEditorRef"
79
+ @save="handleSave"
80
+ />
81
+ <button @click="getCurrentData">获取当前配置</button>
82
+ </template>
83
+
84
+ <script setup>
85
+ import { ref } from 'vue'
86
+ import { CertificateEditor } from 'z-certificate-editor'
87
+ import 'z-certificate-editor/dist/style.css'
88
+
89
+ const certificateEditorRef = ref(null)
90
+
91
+ const handleSave = (certificateData) => {
92
+ console.log('保存的数据:', certificateData)
93
+ }
94
+
95
+ const getCurrentData = () => {
96
+ if (certificateEditorRef.value) {
97
+ const data = certificateEditorRef.value.getCertificateData()
98
+ console.log('当前配置:', data)
99
+ }
100
+ }
101
+ </script>
102
+ ```
103
+
104
+ ## 📖 完整文档
105
+
106
+ 以下是完整的项目文档和使用说明。
107
+
5
108
  ## ✨ 功能特性
6
109
 
7
110
  ### 核心功能
@@ -11,6 +114,7 @@
11
114
  - 🖼️ **图片元素**: 支持本地上传、URL输入、关联系统字段(如用户头像)
12
115
  - 🔲 **二维码元素**: 支持文本内容、URL输入、关联系统字段(如证书编号、身份证号)
13
116
  - 🎯 **系统字段关联**: 动态关联系统字段,实现证书内容的自动化填充
117
+ - ⚙️ **自定义系统字段**: 支持通过 props 传入自定义系统字段配置,灵活适配不同业务场景
14
118
  - 📐 **精确定位**: 支持精确的位置(X、Y)和尺寸(宽度、高度)设置
15
119
  - 🎨 **丰富的文本格式**: 字体大小、字体族、粗体、斜体、下划线、颜色、对齐方式、行高等
16
120
 
@@ -60,78 +164,12 @@ drawcert/
60
164
  └── README.md
61
165
  ```
62
166
 
63
- ## 📦 作为 npm 包使用
64
-
65
- ### 安装
66
-
67
- ```bash
68
- npm install z-certificate-editor
69
- ```
70
-
71
- ### 在 Vue 3 项目中使用
72
-
73
- ```vue
74
- <template>
75
- <CertificateEditor />
76
- </template>
77
-
78
- <script setup>
79
- import CertificateEditor from 'z-certificate-editor'
80
- import 'z-certificate-editor/style.css'
81
- </script>
82
- ```
83
-
84
- ### 按需导入组件
85
-
86
- ```vue
87
- <template>
88
- <div>
89
- <DesignPanel @add-element="handleAddElement" />
90
- <CanvasArea :elements="elements" />
91
- <PropertiesPanel :element="selectedElement" />
92
- </div>
93
- </template>
94
-
95
- <script setup>
96
- import {
97
- DesignPanel,
98
- CanvasArea,
99
- PropertiesPanel
100
- } from 'z-certificate-editor'
101
- import 'z-certificate-editor/style.css'
102
- </script>
103
- ```
104
-
105
- ### 使用工具函数
106
-
107
- ```javascript
108
- import {
109
- SYSTEM_FIELDS,
110
- replaceFields,
111
- extractFields,
112
- PRESET_TEMPLATES
113
- } from 'z-certificate-editor'
114
-
115
- // 使用系统字段
116
- console.log(SYSTEM_FIELDS)
117
-
118
- // 替换字段占位符
119
- const text = '{{姓名}}({{性别称呼}})'
120
- const fieldData = { name: '张三', gender: '先生' }
121
- const result = replaceFields(text, fieldData)
122
- // 结果: '张三(先生)'
123
-
124
- // 提取字段
125
- const fields = extractFields(text)
126
- // 结果: [{ key: 'name', label: '姓名', ... }, { key: 'gender', label: '性别称呼', ... }]
127
- ```
128
-
129
- ## 🚀 本地开发
167
+ ## 🚀 安装和运行
130
168
 
131
169
  ### 环境要求
132
170
 
133
- - Node.js >= 16.0.0
134
- - npm >= 7.0.0
171
+ - Node.js >= 22.21.0
172
+ - npm >= 11.6.2
135
173
 
136
174
  ### 安装依赖
137
175
 
@@ -147,16 +185,13 @@ npm run dev
147
185
 
148
186
  启动后访问 `http://localhost:3000`(或终端显示的地址)
149
187
 
150
- ### 构建 npm 包
188
+ ### 构建生产版本
151
189
 
152
190
  ```bash
153
191
  npm run build
154
192
  ```
155
193
 
156
- 构建产物将输出到 `dist` 目录,包含:
157
- - `z-certificate-editor.es.js` - ES 模块版本
158
- - `z-certificate-editor.umd.js` - UMD 版本
159
- - `style.css` - 样式文件
194
+ 构建产物将输出到 `dist` 目录
160
195
 
161
196
  ### 预览生产版本
162
197
 
@@ -164,26 +199,63 @@ npm run build
164
199
  npm run preview
165
200
  ```
166
201
 
167
- ## 📤 发布到 npm
202
+ ## 📖 使用说明
168
203
 
169
- 1. 确保已登录 npm:
170
- ```bash
171
- npm login
172
- ```
204
+ ### 自定义系统字段
173
205
 
174
- 2. 更新版本号(可选):
175
- ```bash
176
- npm version patch # 或 minor, major
177
- ```
206
+ `CertificateEditor` 组件支持通过 `systemFields` prop 传入自定义系统字段配置,实现灵活的字段管理。
178
207
 
179
- 3. 发布:
180
- ```bash
181
- npm publish
208
+ #### 在 App.vue 中使用
209
+
210
+ ```vue
211
+ <template>
212
+ <CertificateEditor :system-fields="customSystemFields" />
213
+ </template>
214
+
215
+ <script setup>
216
+ import { ref } from 'vue'
217
+ import CertificateEditor from './components/CertificateEditor.vue'
218
+ import { SYSTEM_FIELDS } from './utils/fieldManager.js'
219
+
220
+ // 定义自定义系统字段
221
+ const customSystemFields = ref([
222
+ // 添加自定义字段
223
+ {
224
+ key: 'studentId',
225
+ label: '学号',
226
+ placeholder: '{{学号}}',
227
+ category: '学生信息',
228
+ description: '学生的学号'
229
+ },
230
+ {
231
+ key: 'className',
232
+ label: '班级名称',
233
+ placeholder: '{{班级名称}}',
234
+ category: '学生信息',
235
+ description: '学生所在班级'
236
+ },
237
+ // 保留默认字段
238
+ ...SYSTEM_FIELDS
239
+ ])
240
+ </script>
182
241
  ```
183
242
 
184
- 注意:发布前会自动执行 `npm run build` 构建包。
243
+ #### 字段对象格式
185
244
 
186
- ## 📖 使用说明
245
+ 每个系统字段对象必须包含以下属性:
246
+
247
+ - `key` (String, 必需): 字段的唯一标识符,用于在代码中引用
248
+ - `label` (String, 必需): 字段的显示名称,显示在界面上
249
+ - `placeholder` (String, 必需): 字段的占位符格式,如 `{{姓名}}`
250
+ - `category` (String, 可选): 字段的分类,用于分组显示
251
+ - `description` (String, 可选): 字段的描述信息
252
+
253
+ #### 注意事项
254
+
255
+ - 如果不传入 `systemFields` prop,组件将使用默认的 `SYSTEM_FIELDS`
256
+ - 自定义字段的 `key` 必须唯一,避免与默认字段冲突
257
+ - 字段的 `placeholder` 格式必须为 `{{字段名}}` 的形式
258
+ - 自定义字段会自动在字段选择对话框、属性面板等位置显示
187
259
 
188
260
  ### 基本操作流程
189
261
 
@@ -319,7 +391,8 @@ npm publish
319
391
 
320
392
  - 点击顶部工具栏的"保存"按钮
321
393
  - 系统会将当前证书配置(模板、元素列表、字段数据)输出到浏览器控制台
322
- - 打开浏览器开发者工具(F12)查看保存的数据
394
+ - 同时触发 `save` 事件,将数据传递给父组件
395
+ - 父组件可以通过事件处理函数接收保存的数据
323
396
 
324
397
  **保存的数据结构**:
325
398
  ```json
@@ -352,6 +425,113 @@ npm publish
352
425
  }
353
426
  ```
354
427
 
428
+ #### 在父组件中获取保存的数据
429
+
430
+ 有两种方式可以在父组件中获取证书配置数据:
431
+
432
+ **方式一:通过事件监听(推荐)**
433
+
434
+ 当用户点击保存按钮时,组件会触发 `save` 事件,父组件可以通过监听该事件获取数据:
435
+
436
+ ```vue
437
+ <template>
438
+ <CertificateEditor
439
+ :system-fields="customSystemFields"
440
+ @save="handleSave"
441
+ />
442
+ </template>
443
+
444
+ <script setup>
445
+ import { ref } from 'vue'
446
+ import CertificateEditor from './components/CertificateEditor.vue'
447
+
448
+ const handleSave = (certificateData) => {
449
+ console.log('接收到保存的数据:', certificateData)
450
+
451
+ // 在这里可以处理保存的数据,例如:
452
+ // - 发送到服务器
453
+ // - 保存到本地存储
454
+ // - 进行数据验证
455
+ // 等等...
456
+ }
457
+ </script>
458
+ ```
459
+
460
+ **方式二:通过组件方法主动获取**
461
+
462
+ 父组件可以通过组件引用调用 `getCertificateData()` 方法,随时获取当前的证书配置数据(无需点击保存按钮):
463
+
464
+ ```vue
465
+ <template>
466
+ <CertificateEditor
467
+ ref="certificateEditorRef"
468
+ :system-fields="customSystemFields"
469
+ />
470
+ <button @click="getData">获取当前配置</button>
471
+ </template>
472
+
473
+ <script setup>
474
+ import { ref } from 'vue'
475
+ import CertificateEditor from './components/CertificateEditor.vue'
476
+
477
+ const certificateEditorRef = ref(null)
478
+
479
+ const getData = () => {
480
+ if (certificateEditorRef.value) {
481
+ const data = certificateEditorRef.value.getCertificateData()
482
+ console.log('当前证书配置:', data)
483
+ // 处理数据...
484
+ }
485
+ }
486
+ </script>
487
+ ```
488
+
489
+ **完整示例**:
490
+
491
+ ```vue
492
+ <template>
493
+ <CertificateEditor
494
+ ref="certificateEditorRef"
495
+ :system-fields="customSystemFields"
496
+ @save="handleSave"
497
+ />
498
+ <button @click="getCurrentData">获取当前配置</button>
499
+ </template>
500
+
501
+ <script setup>
502
+ import { ref } from 'vue'
503
+ import CertificateEditor from './components/CertificateEditor.vue'
504
+ import { SYSTEM_FIELDS } from './utils/fieldManager.js'
505
+
506
+ const customSystemFields = ref([...SYSTEM_FIELDS])
507
+ const certificateEditorRef = ref(null)
508
+
509
+ // 方式一:监听保存事件
510
+ const handleSave = (certificateData) => {
511
+ console.log('用户点击保存,数据:', certificateData)
512
+
513
+ // 示例:保存到本地存储
514
+ localStorage.setItem('certificateConfig', JSON.stringify(certificateData))
515
+
516
+ // 示例:发送到服务器
517
+ // fetch('/api/certificate/save', {
518
+ // method: 'POST',
519
+ // headers: { 'Content-Type': 'application/json' },
520
+ // body: JSON.stringify(certificateData)
521
+ // })
522
+ }
523
+
524
+ // 方式二:主动获取数据
525
+ const getCurrentData = () => {
526
+ if (certificateEditorRef.value) {
527
+ const data = certificateEditorRef.value.getCertificateData()
528
+ console.log('当前配置数据:', data)
529
+ return data
530
+ }
531
+ }
532
+ </script>
533
+ ```
534
+
355
535
  ## 🧩 组件说明
356
536
 
357
537
  ### CertificateEditor
@@ -363,6 +543,43 @@ npm publish
363
543
  - 协调各子组件的通信
364
544
  - 提供预览和保存功能
365
545
 
546
+ **Props**:
547
+ - `systemFields` (Array, 可选): 自定义系统字段配置数组
548
+ - 如果不传入,将使用默认的 `SYSTEM_FIELDS`
549
+ - 字段对象格式:
550
+ ```javascript
551
+ {
552
+ key: '字段唯一标识',
553
+ label: '字段显示名称',
554
+ placeholder: '{{占位符}}',
555
+ category: '字段分类',
556
+ description: '字段描述'
557
+ }
558
+ ```
559
+ - 示例:
560
+ ```javascript
561
+ const customFields = [
562
+ {
563
+ key: 'customField',
564
+ label: '自定义字段',
565
+ placeholder: '{{自定义字段}}',
566
+ category: '其他',
567
+ description: '这是一个自定义字段'
568
+ },
569
+ ...SYSTEM_FIELDS // 可以结合默认字段使用
570
+ ]
571
+ ```
572
+
573
+ **Events**:
574
+ - `save`: 当用户点击保存按钮时触发
575
+ - 参数:`certificateData` (Object) - 证书配置数据对象
576
+ - 数据结构包含:`template`、`elements`、`fieldData`、`timestamp`
577
+
578
+ **Exposed Methods**:
579
+ - `getCertificateData()`: 获取当前证书配置数据
580
+ - 返回:`Object` - 证书配置数据对象
581
+ - 使用方式:通过组件引用调用 `certificateEditorRef.value.getCertificateData()`
582
+
366
583
  ### DesignPanel
367
584
 
368
585
  左侧设计面板,提供元素添加和模板管理功能。
@@ -392,6 +609,7 @@ npm publish
392
609
  - `selectedElement`: Object | null - 当前选中的元素
393
610
  - `fieldData`: Object - 字段数据对象
394
611
  - `template`: Object - 当前选中的模板
612
+ - `systemFields`: Array - 系统字段配置数组(从 CertificateEditor 传递)
395
613
 
396
614
  **Events**:
397
615
  - `select-element`: 选择元素时触发
@@ -409,6 +627,7 @@ npm publish
409
627
 
410
628
  **Props**:
411
629
  - `element`: Object - 当前选中的元素
630
+ - `systemFields`: Array - 系统字段配置数组(从 CertificateEditor 传递)
412
631
 
413
632
  **Events**:
414
633
  - `update-element`: 更新元素时触发
@@ -437,6 +656,13 @@ npm publish
437
656
  - 支持搜索字段
438
657
  - 插入字段占位符到文本内容
439
658
 
659
+ **Props**:
660
+ - `visible`: Boolean - 对话框显示状态
661
+ - `currentText`: String - 当前文本内容
662
+ - `selectionStart`: Number - 文本选择起始位置
663
+ - `selectionEnd`: Number - 文本选择结束位置
664
+ - `systemFields`: Array - 系统字段配置数组(从 PropertiesPanel 传递)
665
+
440
666
  ### CertificatePreview
441
667
 
442
668
  证书预览组件,以模态框形式显示最终证书效果。
@@ -446,6 +672,13 @@ npm publish
446
672
  - 自动替换所有字段占位符为实际数据
447
673
  - 支持关闭预览
448
674
 
675
+ **Props**:
676
+ - `visible`: Boolean - 预览对话框显示状态
677
+ - `elements`: Array - 元素列表
678
+ - `template`: Object - 当前选中的模板
679
+ - `fieldData`: Object - 字段数据对象
680
+ - `systemFields`: Array - 系统字段配置数组(从 CertificateEditor 传递)
681
+
449
682
  ## 🛠️ 技术栈
450
683
 
451
684
  - **Vue 3**: 使用 Composition API 进行组件开发
@@ -453,6 +686,71 @@ npm publish
453
686
  - **原生 CSS**: 无第三方CSS框架,纯原生样式
454
687
  - **Canvas/HTML**: 基于HTML和CSS的渲染,非Canvas绘制
455
688
 
689
+ ## 🔌 API 参考
690
+
691
+ ### CertificateEditor Props
692
+
693
+ | 属性名 | 类型 | 默认值 | 说明 |
694
+ |--------|------|--------|------|
695
+ | `systemFields` | `Array` | `SYSTEM_FIELDS` | 自定义系统字段配置数组 |
696
+
697
+ ### CertificateEditor Events
698
+
699
+ | 事件名 | 参数 | 说明 |
700
+ |--------|------|------|
701
+ | `save` | `certificateData: Object` | 当用户点击保存按钮时触发,传递证书配置数据 |
702
+
703
+ ### CertificateEditor Exposed Methods
704
+
705
+ | 方法名 | 参数 | 返回值 | 说明 |
706
+ |--------|------|--------|------|
707
+ | `getCertificateData()` | - | `Object` | 获取当前证书配置数据,无需点击保存按钮 |
708
+
709
+ ### 系统字段对象格式
710
+
711
+ ```typescript
712
+ interface SystemField {
713
+ key: string // 字段唯一标识
714
+ label: string // 字段显示名称
715
+ placeholder: string // 占位符格式,如 "{{姓名}}"
716
+ category?: string // 字段分类(可选)
717
+ description?: string // 字段描述(可选)
718
+ }
719
+ ```
720
+
721
+ ### 证书配置数据格式
722
+
723
+ ```typescript
724
+ interface CertificateData {
725
+ template: {
726
+ id: string
727
+ name: string
728
+ type: 'vertical' | 'horizontal'
729
+ background: string
730
+ width: number
731
+ height: number
732
+ }
733
+ elements: Array<{
734
+ id: number
735
+ type: 'text' | 'longtext' | 'image' | 'qrcode'
736
+ x: number
737
+ y: number
738
+ width: number
739
+ height?: number
740
+ content?: string
741
+ style?: object
742
+ imageSource?: 'url' | 'field'
743
+ imageUrl?: string
744
+ imageField?: string
745
+ qrcodeSource?: 'url' | 'field'
746
+ qrcodeContent?: string
747
+ qrcodeField?: string
748
+ }>
749
+ fieldData: Record<string, any>
750
+ timestamp: string
751
+ }
752
+ ```
753
+
456
754
  ## 📋 系统字段列表
457
755
 
458
756
  系统内置了丰富的字段类型,按类别组织:
@@ -509,6 +807,7 @@ npm publish
509
807
  - [x] 图片元素(本地上传、URL、关联字段)
510
808
  - [x] 二维码元素(文本内容、URL、关联字段)
511
809
  - [x] 系统字段关联
810
+ - [x] 自定义系统字段支持(通过 props 传入)
512
811
  - [x] 预设模板管理
513
812
  - [x] 自定义模板上传
514
813
  - [x] 字段数据管理
@@ -528,10 +827,24 @@ npm publish
528
827
  - [ ] 模板收藏和分类
529
828
  - [ ] 响应式设计适配
530
829
 
830
+
531
831
  ## 📄 许可证
532
832
 
533
833
  MIT
534
834
 
835
+ ## 📝 版本历史
836
+
837
+ ### 1.0.3
838
+ - 初始版本发布
839
+ - 支持可视化证书编辑
840
+ - 支持系统字段关联
841
+ - 支持模板管理
842
+ - 支持图片和二维码元素
843
+ - 支持保存和预览功能
844
+ - 支持自定义系统字段
845
+ - 支持通过事件和方法获取保存的数据
846
+ - 发布为 npm 包,支持 ES 模块和 UMD 格式
847
+
535
848
  ## 👨‍💻 开发说明
536
849
 
537
850
  ### 核心概念