azs-doc-editor 1.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.
package/README.md ADDED
@@ -0,0 +1,407 @@
1
+ # Vue Document Editor
2
+
3
+ 一个功能完整的 Vue 3 文档编辑器组件,支持 DOCX 和 XLSX 文件的转换和编辑。
4
+
5
+ ## 特性
6
+
7
+ - 📄 支持 DOCX 文件转换和编辑(基于 Tiptap)
8
+ - 📊 支持 XLSX 文件转换和编辑(基于 Luckysheet)
9
+ - 🎨 丰富的编辑功能(格式化、表格、图片等)
10
+ - 💾 灵活的保存机制
11
+ - 🔌 易于集成到任何 Vue 3 项目
12
+
13
+ ## 安装
14
+
15
+ ```bash
16
+ npm install vue-document-editor
17
+ ```
18
+
19
+ ## 前置要求
20
+
21
+ ### Luckysheet 资源
22
+
23
+ 如果需要使用 Excel 编辑功能,需要在 `index.html` 中引入 Luckysheet 资源:
24
+
25
+ ```html
26
+ <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/luckysheet@latest/dist/plugins/css/pluginsCss.css' />
27
+ <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/luckysheet@latest/dist/plugins/plugins.css' />
28
+ <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/luckysheet@latest/dist/css/luckysheet.css' />
29
+ <script src="https://cdn.jsdelivr.net/npm/luckysheet@latest/dist/plugins/js/plugin.js"></script>
30
+ <script src="https://cdn.jsdelivr.net/npm/luckysheet@latest/dist/luckysheet.umd.js"></script>
31
+ ```
32
+
33
+ ## 使用
34
+
35
+ ### 全局注册
36
+
37
+ ```typescript
38
+ import { createApp } from 'vue'
39
+ import DocumentConverter from 'vue-document-converter'
40
+ import 'vue-document-converter/style.css'
41
+
42
+ const app = createApp(App)
43
+ app.use(DocumentConverter)
44
+ ```
45
+
46
+ ### 基础使用(仅转换)
47
+
48
+ ```vue
49
+ <template>
50
+ <DocumentConverter
51
+ :file-url="fileUrl"
52
+ :file-type="fileType"
53
+ @success="handleSuccess"
54
+ @error="handleError"
55
+ >
56
+ <template #loading>
57
+ <div>自定义加载中...</div>
58
+ </template>
59
+ <template #error="{ error }">
60
+ <div>错误: {{ error }}</div>
61
+ </template>
62
+ </DocumentConverter>
63
+ </template>
64
+
65
+ <script setup lang="ts">
66
+ import { ref } from 'vue'
67
+ import { DocumentConverter, type ConversionResult } from 'vue-document-converter'
68
+ import 'vue-document-converter/style.css'
69
+
70
+ const fileUrl = ref('https://example.com/document.docx')
71
+ const fileType = ref<'docx' | 'xlsx'>('docx')
72
+
73
+ function handleSuccess(result: ConversionResult) {
74
+ console.log('转换成功:', result)
75
+ // result.type: 'docx' | 'xlsx'
76
+ // result.content: 转换后的内容
77
+ // result.fileName: 文件名
78
+ }
79
+
80
+ function handleError(error: string) {
81
+ console.error('转换失败:', error)
82
+ }
83
+ </script>
84
+ ```
85
+
86
+ #### 2. 使用 Word 编辑器
87
+
88
+ ```vue
89
+ <template>
90
+ <WordEditor
91
+ :content="htmlContent"
92
+ :editable="true"
93
+ @save="handleSave"
94
+ @update="handleUpdate"
95
+ />
96
+ </template>
97
+
98
+ <script setup lang="ts">
99
+ import { ref } from 'vue'
100
+ import { WordEditor } from 'vue-document-editor'
101
+ import 'vue-document-editor/style.css'
102
+
103
+ const htmlContent = ref('<p>Hello World</p>')
104
+
105
+ function handleSave(content: string) {
106
+ console.log('保存内容:', content)
107
+ // 调用 API 保存
108
+ }
109
+
110
+ function handleUpdate(content: string) {
111
+ // 实时更新
112
+ htmlContent.value = content
113
+ }
114
+ </script>
115
+ ```
116
+
117
+ #### 3. 使用 Excel 编辑器
118
+
119
+ ```vue
120
+ <template>
121
+ <ExcelEditor
122
+ :content="sheets"
123
+ :title="title"
124
+ :editable="true"
125
+ @save="handleSave"
126
+ />
127
+ </template>
128
+
129
+ <script setup lang="ts">
130
+ import { ref } from 'vue'
131
+ import { ExcelEditor } from 'vue-document-editor'
132
+ import 'vue-document-editor/style.css'
133
+
134
+ const sheets = ref([])
135
+ const title = ref('我的表格')
136
+
137
+ function handleSave(content: any[]) {
138
+ console.log('保存表格:', content)
139
+ // 调用 API 保存
140
+ }
141
+ </script>
142
+ ```
143
+
144
+ ## 完整示例:在你的项目中使用
145
+
146
+ ```vue
147
+ <!-- pages/document-editor.vue -->
148
+ <template>
149
+ <DocumentEditorPage
150
+ :save-handler="saveDocument"
151
+ :load-handler="loadDocument"
152
+ @saved="handleSaved"
153
+ @error="handleError"
154
+ />
155
+ </template>
156
+
157
+ <script setup lang="ts">
158
+ import { DocumentEditorPage, type SaveHandler } from 'vue-document-editor'
159
+ import { useRouter } from 'vue-router'
160
+ import 'vue-document-editor/style.css'
161
+
162
+ const router = useRouter()
163
+
164
+ const saveDocument: SaveHandler = async (result) => {
165
+ // 调用你的 API
166
+ const response = await fetch('/api/documents', {
167
+ method: 'POST',
168
+ headers: { 'Content-Type': 'application/json' },
169
+ body: JSON.stringify({
170
+ type: result.type,
171
+ title: result.fileName,
172
+ content: result.content
173
+ })
174
+ })
175
+ const data = await response.json()
176
+ return { _id: data.id }
177
+ }
178
+
179
+ async function loadDocument(id: string) {
180
+ const response = await fetch(`/api/documents/${id}`)
181
+ const data = await response.json()
182
+ return {
183
+ type: data.type,
184
+ content: data.content,
185
+ title: data.title
186
+ }
187
+ }
188
+
189
+ function handleSaved({ _id, result }) {
190
+ // 保存成功,可以跳转
191
+ router.push(`/documents/${_id}`)
192
+ }
193
+
194
+ function handleError(error: string) {
195
+ console.error('错误:', error)
196
+ }
197
+ </script>
198
+ ```
199
+
200
+ ## 路由配置
201
+
202
+ ```typescript
203
+ // router.ts
204
+ const routes = [
205
+ {
206
+ path: '/document/convert',
207
+ component: () => import('./pages/document-editor.vue'),
208
+ // 访问: /document/convert?fileUrl=xxx&fileType=docx
209
+ },
210
+ {
211
+ path: '/document/edit/:id',
212
+ component: () => import('./pages/document-editor.vue'),
213
+ // 访问: /document/edit/123
214
+ }
215
+ ]
216
+ ```
217
+
218
+ ```vue
219
+ <template>
220
+ <DocumentConverter
221
+ :file-url="fileUrl"
222
+ :file-type="fileType"
223
+ :save-handler="saveDocument"
224
+ @saved="handleSaved"
225
+ @error="handleError"
226
+ />
227
+ </template>
228
+
229
+ <script setup lang="ts">
230
+ import { ref } from 'vue'
231
+ import { useRouter } from 'vue-router'
232
+ import { DocumentConverter, type ConversionResult, type SaveHandler } from 'vue-document-converter'
233
+ import 'vue-document-converter/style.css'
234
+
235
+ const router = useRouter()
236
+ const fileUrl = ref('https://example.com/document.docx')
237
+ const fileType = ref<'docx' | 'xlsx'>('docx')
238
+
239
+ // 自定义保存逻辑
240
+ const saveDocument: SaveHandler = async (result) => {
241
+ // 调用你的 API 保存文档
242
+ const response = await fetch('/api/documents', {
243
+ method: 'POST',
244
+ headers: { 'Content-Type': 'application/json' },
245
+ body: JSON.stringify({
246
+ type: result.type,
247
+ title: result.fileName,
248
+ content: result.content
249
+ })
250
+ })
251
+ const data = await response.json()
252
+ return { _id: data.id }
253
+ }
254
+
255
+ // 保存成功后跳转
256
+ function handleSaved({ _id, result }) {
257
+ if (result.type === 'docx') {
258
+ router.push(`/word-edit?id=${_id}`)
259
+ } else if (result.type === 'xlsx') {
260
+ router.push(`/excel-edit?id=${_id}`)
261
+ }
262
+ }
263
+
264
+ function handleError(error: string) {
265
+ console.error('转换失败:', error)
266
+ }
267
+ </script>
268
+ ```
269
+
270
+ ### 手动触发转换
271
+
272
+ ```vue
273
+ <template>
274
+ <DocumentConverter
275
+ ref="converterRef"
276
+ :auto-convert="false"
277
+ />
278
+ <button @click="startConvert">开始转换</button>
279
+ </template>
280
+
281
+ <script setup lang="ts">
282
+ import { ref } from 'vue'
283
+ import { DocumentConverter } from 'vue-document-converter'
284
+
285
+ const converterRef = ref()
286
+
287
+ function startConvert() {
288
+ converterRef.value?.convert('https://example.com/file.xlsx', 'xlsx')
289
+ }
290
+ </script>
291
+ ```
292
+
293
+ ## API
294
+
295
+ ### DocumentEditorPage
296
+
297
+ 完整的文档编辑器页面组件。
298
+
299
+ #### Props
300
+
301
+ | 属性 | 类型 | 默认值 | 说明 |
302
+ |------|------|--------|------|
303
+ | fileUrl | string | - | 文件 URL(用于转换新文件) |
304
+ | fileType | 'docx' \| 'xlsx' | - | 文件类型 |
305
+ | documentId | string | - | 文档 ID(用于加载已存在的文档) |
306
+ | editable | boolean | true | 是否可编辑 |
307
+ | loadHandler | (id: string) => Promise<Document> | - | 加载文档的处理函数 |
308
+ | saveHandler | SaveHandler | - | 保存文档的处理函数 |
309
+
310
+ #### Events
311
+
312
+ | 事件 | 参数 | 说明 |
313
+ |------|------|------|
314
+ | saved | { _id: string, result: ConversionResult } | 保存成功时触发 |
315
+ | error | string | 发生错误时触发 |
316
+ | loaded | { type, content, title } | 文档加载成功时触发 |
317
+
318
+ ### WordEditor
319
+
320
+ Word 文档编辑器组件(基于 Tiptap)。
321
+
322
+ #### Props
323
+
324
+ | 属性 | 类型 | 默认值 | 说明 |
325
+ |------|------|--------|------|
326
+ | content | string | - | HTML 内容 |
327
+ | editable | boolean | true | 是否可编辑 |
328
+
329
+ #### Events
330
+
331
+ | 事件 | 参数 | 说明 |
332
+ |------|------|------|
333
+ | save | string | 点击保存按钮时触发 |
334
+ | update | string | 内容更新时触发 |
335
+
336
+ #### Methods
337
+
338
+ | 方法 | 参数 | 返回值 | 说明 |
339
+ |------|------|--------|------|
340
+ | getContent | - | string | 获取当前内容 |
341
+ | setContent | content: string | - | 设置内容 |
342
+
343
+ ### ExcelEditor
344
+
345
+ Excel 表格编辑器组件(基于 Luckysheet)。
346
+
347
+ #### Props
348
+
349
+ | 属性 | 类型 | 默认值 | 说明 |
350
+ |------|------|--------|------|
351
+ | content | any[] | [] | Luckysheet 格式的表格数据 |
352
+ | title | string | '未命名表格' | 表格标题 |
353
+ | editable | boolean | true | 是否可编辑 |
354
+
355
+ #### Events
356
+
357
+ | 事件 | 参数 | 说明 |
358
+ |------|------|------|
359
+ | save | any[] | 点击保存按钮时触发 |
360
+ | update | any[] | 内容更新时触发 |
361
+
362
+ #### Methods
363
+
364
+ | 方法 | 参数 | 返回值 | 说明 |
365
+ |------|------|--------|------|
366
+ | getContent | - | any[] | 获取当前内容 |
367
+
368
+ ### DocumentConverter
369
+
370
+ 文件转换组件(不包含编辑功能)。
371
+
372
+ | 属性 | 类型 | 默认值 | 说明 |
373
+ |------|------|--------|------|
374
+ | fileUrl | string | - | 文件 URL |
375
+ | fileType | 'docx' \| 'xlsx' | - | 文件类型 |
376
+ | autoConvert | boolean | true | 是否自动转换 |
377
+ | loadingText | string | '加载中...' | 加载提示文本 |
378
+ | successText | string | '转换成功' | 成功提示文本 |
379
+ | saveHandler | SaveHandler | - | 保存处理函数,提供后会自动保存 |
380
+ | autoRedirect | boolean | false | 是否自动跳转(需配合 saveHandler) |
381
+
382
+ ## Events
383
+
384
+ | 事件 | 参数 | 说明 |
385
+ |------|------|------|
386
+ | success | ConversionResult | 转换成功时触发 |
387
+ | error | string | 转换失败时触发 |
388
+ | loading | boolean | 加载状态变化时触发 |
389
+ | saved | { _id: string, result: ConversionResult } | 保存成功时触发(需提供 saveHandler) |
390
+
391
+ ## Slots
392
+
393
+ | 插槽 | 作用域 | 说明 |
394
+ |------|--------|------|
395
+ | loading | - | 自定义加载状态 |
396
+ | error | { error: string } | 自定义错误显示 |
397
+ | success | { result: ConversionResult } | 自定义成功显示 |
398
+
399
+ ## Methods
400
+
401
+ | 方法 | 参数 | 说明 |
402
+ |------|------|------|
403
+ | convert | (fileUrl: string, fileType: 'docx' \| 'xlsx') | 手动触发转换 |
404
+
405
+ ## License
406
+
407
+ MIT