@mario9/tiptap-editor 1.0.2 → 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.
Files changed (2) hide show
  1. package/README.md +103 -1
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -57,6 +57,57 @@ const content = ref('')
57
57
  </template>
58
58
  ```
59
59
 
60
+ ## 常见场景
61
+
62
+ ### 最小化配置
63
+
64
+ 只引入用到的 feature,未引入的模块不会进入消费方 bundle:
65
+
66
+ ```vue
67
+ <TiptapEditor
68
+ v-model="content"
69
+ :features="[UndoRedoFeature, TextStyleFeature]"
70
+ />
71
+ ```
72
+
73
+ ### 只读模式
74
+
75
+ ```vue
76
+ <TiptapEditor v-model="content" :readonly="true" />
77
+ ```
78
+
79
+ `readonly` 时工具栏隐藏,编辑器不可编辑,图片控件仅保留下载,数学公式不可点击编辑。
80
+
81
+ ### 获取 / 设置内容
82
+
83
+ `v-model` 绑定的是 HTML 字符串,直接读写即可:
84
+
85
+ ```typescript
86
+ const content = ref('<p>初始内容</p>')
87
+
88
+ // 读取当前内容
89
+ console.log(content.value)
90
+
91
+ // 程序化设置内容
92
+ content.value = '<h1>新标题</h1><p>新内容</p>'
93
+ ```
94
+
95
+ ### 工具栏顺序
96
+
97
+ `features` 数组的顺序即工具栏按钮的顺序,用 `SeparatorFeature` 插入分隔符:
98
+
99
+ ```typescript
100
+ :features="[
101
+ UndoRedoFeature, // 撤销重做
102
+ SeparatorFeature, // ——
103
+ TextStyleFeature, // 粗体/斜体/下划线/删除线/链接
104
+ TextAlignFeature, // 对齐
105
+ SeparatorFeature, // ——
106
+ TableFeature, // 表格
107
+ ImageFeature, // 图片
108
+ ]"
109
+ ```
110
+
60
111
  ## Props
61
112
 
62
113
  | 属性 | 类型 | 默认值 | 说明 |
@@ -102,7 +153,9 @@ const myUpload: UploadFn = async (file) => {
102
153
 
103
154
  ## 自定义 Feature Plugin
104
155
 
105
- 实现 `FeaturePlugin` 接口即可创建自定义功能插件:
156
+ 实现 `FeaturePlugin` 接口即可创建自定义功能插件。
157
+
158
+ ### 基础示例
106
159
 
107
160
  ```typescript
108
161
  import type { FeaturePlugin } from '@mario9/tiptap-editor'
@@ -118,6 +171,55 @@ export const MyFeature: FeaturePlugin = {
118
171
  }
119
172
  ```
120
173
 
174
+ ### 含浮层和状态共享的完整示例
175
+
176
+ 如果 feature 需要一个持久渲染的浮层(如对话框),并且工具栏按钮需要触发它,使用 `controlComponent` + `ctx.provide`:
177
+
178
+ ```typescript
179
+ import { ref, defineComponent, h } from 'vue'
180
+ import type { FeaturePlugin } from '@mario9/tiptap-editor'
181
+ import MyExtension from './MyExtension'
182
+ import MyToolbarButton from './MyToolbarButton.vue'
183
+ import MyControlPanel from './MyControlPanel.vue'
184
+
185
+ export const MyFeature: FeaturePlugin = {
186
+ name: 'my-feature',
187
+
188
+ install(ctx) {
189
+ // per-instance 状态(多个编辑器实例互不干扰)
190
+ const isOpen = ref(false)
191
+ const openPanel = () => { isOpen.value = true }
192
+
193
+ // 通过 provide 共享给工具栏按钮
194
+ ctx.provide('openMyPanel', openPanel)
195
+
196
+ // 闭包组件,捕获 per-instance 状态
197
+ const ControlWrapper = defineComponent({
198
+ setup: () => () => h(MyControlPanel, {
199
+ visible: isOpen.value,
200
+ 'onUpdate:visible': (v: boolean) => { isOpen.value = v },
201
+ }),
202
+ })
203
+
204
+ return {
205
+ extensions: [MyExtension],
206
+ controlComponent: ControlWrapper, // 渲染在编辑器内容区之后
207
+ }
208
+ },
209
+
210
+ toolbarComponent: MyToolbarButton,
211
+ }
212
+ ```
213
+
214
+ 工具栏按钮通过 `inject` 获取共享函数:
215
+
216
+ ```typescript
217
+ // MyToolbarButton.vue
218
+ import { inject } from 'vue'
219
+
220
+ const openMyPanel = inject<() => void>('openMyPanel')
221
+ ```
222
+
121
223
  `install()` 接收 `PluginInstallContext`(含 `readonly`、`provide`、`upload`),返回 `{ extensions, controlComponent? }`。
122
224
 
123
225
  ## 内置功能(始终启用)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mario9/tiptap-editor",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "基于 Tiptap + Vue 3 的富文本编辑器组件。",
5
5
  "type": "module",
6
6
  "main": "./dist/tiptap-editor.umd.cjs",