bridgerte 0.9.13 → 0.9.14

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,7 +1,7 @@
1
- # bridgerte
2
-
3
- ![BridgeRTE logo](https://gitee.com/wangchuyi1122/bridgerte/raw/master/packages/bridgerte/assets/bridgerte-logo-small.png)
4
-
1
+ # bridgerte
2
+
3
+ ![BridgeRTE logo](https://gitee.com/wangchuyi1122/bridgerte/raw/master/packages/bridgerte/assets/bridgerte-logo-small.png)
4
+
5
5
  `bridgerte` 是 BridgeRTE 对外发布给业务项目使用的唯一 npm 包。它面向 Web、PC、H5、
6
6
  React Native WebView 和 Flutter WebView 场景,内置 Lexical 编辑器、DOM toolbar/tabbar、
7
7
  参数面板、上传入口、WebView bridge 协议和跨端菜单 schema。
@@ -15,30 +15,30 @@ Lexical 相关包,也不需要直接依赖仓库里的 `@bridgerte/*` 内部
15
15
  - DOM 编辑器:直接在 Web、PC、H5 页面里创建富文本编辑器。
16
16
  - 编辑器和菜单分离:editor 负责内容,toolbar/tabbar 由业务按布局单独挂载。
17
17
  - WebView / Native:WebView 内运行编辑器,RN/Flutter 原生侧自己渲染菜单并通过 bridge 发命令。
18
-
19
- ## 安装
20
-
21
- ```bash
22
- pnpm add bridgerte
23
- ```
24
-
25
- 使用 npm:
26
-
27
- ```bash
28
- npm install bridgerte
29
- ```
30
-
31
- DOM 编辑器必须显式导入样式:
32
-
33
- ```ts
34
- import 'bridgerte/style.css';
35
- ```
36
-
37
- ## 推荐导入方式
38
-
39
- 推荐按能力从 subpath 导入,方便业务打包器按使用边界 tree-shaking:
40
-
41
- ```ts
18
+
19
+ ## 安装
20
+
21
+ ```bash
22
+ pnpm add bridgerte
23
+ ```
24
+
25
+ 使用 npm:
26
+
27
+ ```bash
28
+ npm install bridgerte
29
+ ```
30
+
31
+ DOM 编辑器必须显式导入样式:
32
+
33
+ ```ts
34
+ import 'bridgerte/style.css';
35
+ ```
36
+
37
+ ## 推荐导入方式
38
+
39
+ 推荐按能力从 subpath 导入,方便业务打包器按使用边界 tree-shaking:
40
+
41
+ ```ts
42
42
  import {
43
43
  createRichTextEditor,
44
44
  createRichTextToolbar,
@@ -49,189 +49,189 @@ import { BRIDGERTE_CONTENT_VERSION, isEditorContentEmpty } from 'bridgerte/core'
49
49
  import { isBridgeMessage } from 'bridgerte/bridge';
50
50
  import { defaultMenuSchema, resolveToolbarMenu } from 'bridgerte/native-spec';
51
51
  import 'bridgerte/style.css';
52
- ```
53
-
54
- 入口说明:
55
-
56
- - `bridgerte/dom`:DOM 编辑器、独立 toolbar/tabbar、WebView runtime 的 DOM 实现。
57
- - `bridgerte/webview`:WebView 页面内的 bridge runtime。
52
+ ```
53
+
54
+ 入口说明:
55
+
56
+ - `bridgerte/dom`:DOM 编辑器、独立 toolbar/tabbar、WebView runtime 的 DOM 实现。
57
+ - `bridgerte/webview`:WebView 页面内的 bridge runtime。
58
58
  - `bridgerte/core`:内容模型、命令、上传、菜单、参数面板和 `EditorAPI` 类型。
59
- - `bridgerte/bridge`:WebView 双向消息类型、默认事件节流配置和消息判断工具。
60
- - `bridgerte/native-spec`:RN/Flutter 原生菜单 schema、toolbar 解析和命令状态匹配工具。
61
- - `bridgerte/style.css`:DOM 默认样式。
62
- - `bridgerte`:聚合入口,适合迁移期或小型项目;生产示例优先使用 subpath。
63
-
64
- ## 快速开始
65
-
66
- 页面准备两个容器:一个给 toolbar/tabbar,一个给编辑器内容区。`createRichTextEditor()`
67
- 只创建 editor;菜单必须用 `createRichTextToolbar()` 单独挂载,并绑定同一个 `EditorAPI`。
68
-
69
- ```html
70
- <div id="toolbar"></div>
71
- <div id="editor"></div>
72
- ```
73
-
74
- ```ts
75
- import { createRichTextEditor, createRichTextToolbar } from 'bridgerte/dom';
76
- import 'bridgerte/style.css';
77
-
78
- const editorContainer = document.querySelector<HTMLElement>('#editor');
79
- const toolbarContainer = document.querySelector<HTMLElement>('#toolbar');
80
-
81
- if (!editorContainer) throw new Error('editor container not found');
82
- if (!toolbarContainer) throw new Error('toolbar container not found');
83
-
84
- const editor = createRichTextEditor(editorContainer, {
85
- placeholder: '开始输入',
86
- onContentChange(change) {
87
- counter.textContent = `${change.plainTextLength}/${change.maxLength ?? '∞'}`;
88
- saveButton.disabled = !change.dirty;
89
- }
90
- });
91
-
92
- const toolbar = createRichTextToolbar(toolbarContainer, {
93
- editor,
94
- placement: 'top'
95
- });
96
-
97
- toolbar.update();
98
-
99
- window.addEventListener('beforeunload', () => {
100
- toolbar.destroy();
101
- editor.destroy();
102
- });
103
- ```
104
-
105
- `placement: 'top' | 'bottom'` 只影响菜单语义、默认样式状态和无障碍命名;DOM 放在哪里、
106
- 是否吸顶、吸底或跟随键盘,由业务自己的布局决定。销毁时建议先销毁 toolbar,再销毁 editor。
107
-
108
- WebView / RN / Flutter 也是同一心智:WebView 内的 editor 只提供内容和命令能力;原生菜单先读取
109
- ready payload 中的 `menuSchema`,再订阅后续 `editor.commandStateChange` 自行渲染,并通过
110
- bridge 发送命令。
111
-
112
- ## 基础配置
113
-
114
- ```ts
115
- import { createRichTextEditor } from 'bridgerte/dom';
116
- import type { EditorContent } from 'bridgerte/core';
117
-
118
- const initialValue: Partial<EditorContent> = {
119
- html: '<p>Hello BridgeRTE</p>',
120
- plainText: 'Hello BridgeRTE'
121
- };
122
-
123
- const editor = createRichTextEditor(container, {
124
- value: initialValue,
125
- readonly: false,
126
- placeholder: '写点什么',
127
- maxLength: 10000,
128
- keyboardShortcuts: true,
129
- onReady(api) {
130
- api.focus();
131
- },
132
- onError(error) {
133
- reportError(error);
134
- },
135
- onFocus() {
136
- console.log('focus');
137
- },
138
- onBlur() {
139
- console.log('blur');
140
- }
141
- });
142
- ```
143
-
144
- 常用选项:
145
-
146
- - `value`:初始内容,传 `Partial<EditorContent>`。
147
- - `readonly`:只读状态。
148
- - `placeholder`:空态提示。
149
- - `maxLength`:最大纯文本长度。
150
- - `keyboardShortcuts`:是否启用 DOM 基础快捷键,默认关闭。
151
- - `onReady(api)`:编辑器初始化完成后回传 `EditorAPI`。
152
- - `onContentChange(change)`:高频轻量内容摘要。
153
- - `onChange(content)`:兼容旧项目的完整内容回调,大文档不建议逐字依赖。
154
- - `onError(error)`:运行时错误。
155
- - `onFocus()` / `onBlur()`:焦点变化。
156
-
157
- Deprecated 兼容字段:
158
-
159
- - `toolbarMode`:历史字段,当前在 `createRichTextEditor()` 中是 no-op,不再创建或控制 DOM
160
- toolbar/tabbar。
161
- - `toolbarConfig`:历史 editor 字段,当前不再影响 editor 创建。菜单显示结构请传给
162
- `createRichTextToolbar()`。
163
-
164
- ## 内容读写
165
-
166
- 主动读取完整内容:
167
-
168
- ```ts
169
- const content = editor.getContent();
170
-
171
- console.log(content.version);
172
- console.log(content.html);
173
- console.log(content.json);
174
- console.log(content.plainText);
175
- console.log(content.assets);
176
- ```
177
-
178
- 写入内容:
179
-
180
- ```ts
181
- editor.setContent({
182
- html: '<h1>标题</h1><p>正文</p>',
183
- plainText: '标题\n正文'
184
- });
185
- ```
186
-
187
- 高频变化使用轻量摘要:
188
-
189
- ```ts
190
- createRichTextEditor(container, {
191
- maxLength: 5000,
192
- onContentChange(change) {
193
- saveButton.disabled = !change.dirty;
194
- counter.textContent = `${change.plainTextLength}/${change.maxLength ?? '∞'}`;
195
- warning.hidden = !change.isOverMaxLength;
196
- }
197
- });
198
- ```
199
-
200
- 重要边界:
201
-
202
- - `onContentChange` 不携带完整 `html`、`json`、`plainText`,适合驱动 dirty、字数和保存按钮。
203
- - 保存、提交、离开页面确认时,再主动调用 `getContent()`。
204
- - `onChange(content)` 会回传完整内容,主要用于兼容旧项目;10w 内容场景不要逐字依赖它保存。
205
- - WebView 高频 `editor.contentChange` 也只应依赖轻量摘要;完整内容通过 `requestContent` 获取。
206
-
207
- ## EditorAPI
208
-
209
- ```ts
210
- editor.focus();
211
- editor.blur();
212
- editor.setReadonly(true);
213
-
214
- editor.executeCommand({ type: 'format.bold' });
215
-
216
- const states = editor.getCommandStates();
217
- const unsubscribe = editor.subscribeCommandStateChange((nextStates) => {
218
- renderToolbarState(nextStates);
219
- });
220
-
221
- unsubscribe();
222
- editor.destroy();
223
- ```
224
-
225
- `EditorAPI` 方法:
226
-
59
+ - `bridgerte/bridge`:WebView 双向消息类型、默认事件节流配置和消息判断工具。
60
+ - `bridgerte/native-spec`:RN/Flutter 原生菜单 schema、toolbar 解析和命令状态匹配工具。
61
+ - `bridgerte/style.css`:DOM 默认样式。
62
+ - `bridgerte`:聚合入口,适合迁移期或小型项目;生产示例优先使用 subpath。
63
+
64
+ ## 快速开始
65
+
66
+ 页面准备两个容器:一个给 toolbar/tabbar,一个给编辑器内容区。`createRichTextEditor()`
67
+ 只创建 editor;菜单必须用 `createRichTextToolbar()` 单独挂载,并绑定同一个 `EditorAPI`。
68
+
69
+ ```html
70
+ <div id="toolbar"></div>
71
+ <div id="editor"></div>
72
+ ```
73
+
74
+ ```ts
75
+ import { createRichTextEditor, createRichTextToolbar } from 'bridgerte/dom';
76
+ import 'bridgerte/style.css';
77
+
78
+ const editorContainer = document.querySelector<HTMLElement>('#editor');
79
+ const toolbarContainer = document.querySelector<HTMLElement>('#toolbar');
80
+
81
+ if (!editorContainer) throw new Error('editor container not found');
82
+ if (!toolbarContainer) throw new Error('toolbar container not found');
83
+
84
+ const editor = createRichTextEditor(editorContainer, {
85
+ placeholder: '开始输入',
86
+ onContentChange(change) {
87
+ counter.textContent = `${change.plainTextLength}/${change.maxLength ?? '∞'}`;
88
+ saveButton.disabled = !change.dirty;
89
+ }
90
+ });
91
+
92
+ const toolbar = createRichTextToolbar(toolbarContainer, {
93
+ editor,
94
+ placement: 'top'
95
+ });
96
+
97
+ toolbar.update();
98
+
99
+ window.addEventListener('beforeunload', () => {
100
+ toolbar.destroy();
101
+ editor.destroy();
102
+ });
103
+ ```
104
+
105
+ `placement: 'top' | 'bottom'` 只影响菜单语义、默认样式状态和无障碍命名;DOM 放在哪里、
106
+ 是否吸顶、吸底或跟随键盘,由业务自己的布局决定。销毁时建议先销毁 toolbar,再销毁 editor。
107
+
108
+ WebView / RN / Flutter 也是同一心智:WebView 内的 editor 只提供内容和命令能力;原生菜单先读取
109
+ ready payload 中的 `menuSchema`,再订阅后续 `editor.commandStateChange` 自行渲染,并通过
110
+ bridge 发送命令。
111
+
112
+ ## 基础配置
113
+
114
+ ```ts
115
+ import { createRichTextEditor } from 'bridgerte/dom';
116
+ import type { EditorContent } from 'bridgerte/core';
117
+
118
+ const initialValue: Partial<EditorContent> = {
119
+ html: '<p>Hello BridgeRTE</p>',
120
+ plainText: 'Hello BridgeRTE'
121
+ };
122
+
123
+ const editor = createRichTextEditor(container, {
124
+ value: initialValue,
125
+ readonly: false,
126
+ placeholder: '写点什么',
127
+ maxLength: 10000,
128
+ keyboardShortcuts: true,
129
+ onReady(api) {
130
+ api.focus();
131
+ },
132
+ onError(error) {
133
+ reportError(error);
134
+ },
135
+ onFocus() {
136
+ console.log('focus');
137
+ },
138
+ onBlur() {
139
+ console.log('blur');
140
+ }
141
+ });
142
+ ```
143
+
144
+ 常用选项:
145
+
146
+ - `value`:初始内容,传 `Partial<EditorContent>`。
147
+ - `readonly`:只读状态。
148
+ - `placeholder`:空态提示。
149
+ - `maxLength`:最大纯文本长度。
150
+ - `keyboardShortcuts`:是否启用 DOM 基础快捷键,默认关闭。
151
+ - `onReady(api)`:编辑器初始化完成后回传 `EditorAPI`。
152
+ - `onContentChange(change)`:高频轻量内容摘要。
153
+ - `onChange(content)`:兼容旧项目的完整内容回调,大文档不建议逐字依赖。
154
+ - `onError(error)`:运行时错误。
155
+ - `onFocus()` / `onBlur()`:焦点变化。
156
+
157
+ Deprecated 兼容字段:
158
+
159
+ - `toolbarMode`:历史字段,当前在 `createRichTextEditor()` 中是 no-op,不再创建或控制 DOM
160
+ toolbar/tabbar。
161
+ - `toolbarConfig`:历史 editor 字段,当前不再影响 editor 创建。菜单显示结构请传给
162
+ `createRichTextToolbar()`。
163
+
164
+ ## 内容读写
165
+
166
+ 主动读取完整内容:
167
+
168
+ ```ts
169
+ const content = editor.getContent();
170
+
171
+ console.log(content.version);
172
+ console.log(content.html);
173
+ console.log(content.json);
174
+ console.log(content.plainText);
175
+ console.log(content.assets);
176
+ ```
177
+
178
+ 写入内容:
179
+
180
+ ```ts
181
+ editor.setContent({
182
+ html: '<h1>标题</h1><p>正文</p>',
183
+ plainText: '标题\n正文'
184
+ });
185
+ ```
186
+
187
+ 高频变化使用轻量摘要:
188
+
189
+ ```ts
190
+ createRichTextEditor(container, {
191
+ maxLength: 5000,
192
+ onContentChange(change) {
193
+ saveButton.disabled = !change.dirty;
194
+ counter.textContent = `${change.plainTextLength}/${change.maxLength ?? '∞'}`;
195
+ warning.hidden = !change.isOverMaxLength;
196
+ }
197
+ });
198
+ ```
199
+
200
+ 重要边界:
201
+
202
+ - `onContentChange` 不携带完整 `html`、`json`、`plainText`,适合驱动 dirty、字数和保存按钮。
203
+ - 保存、提交、离开页面确认时,再主动调用 `getContent()`。
204
+ - `onChange(content)` 会回传完整内容,主要用于兼容旧项目;10w 内容场景不要逐字依赖它保存。
205
+ - WebView 高频 `editor.contentChange` 也只应依赖轻量摘要;完整内容通过 `requestContent` 获取。
206
+
207
+ ## EditorAPI
208
+
209
+ ```ts
210
+ editor.focus();
211
+ editor.blur();
212
+ editor.setReadonly(true);
213
+
214
+ editor.executeCommand({ type: 'format.bold' });
215
+
216
+ const states = editor.getCommandStates();
217
+ const unsubscribe = editor.subscribeCommandStateChange((nextStates) => {
218
+ renderToolbarState(nextStates);
219
+ });
220
+
221
+ unsubscribe();
222
+ editor.destroy();
223
+ ```
224
+
225
+ `EditorAPI` 方法:
226
+
227
227
  - `getContent()`:读取完整内容。
228
228
  - `setContent(content)`:写入内容。
229
- - `executeCommand(command)`:执行命令。
230
- - `requestPayloadPanel(request)`:打开参数面板请求,供自绘菜单复用。
231
- - `getCommandStates()`:读取当前命令状态。
232
- - `subscribeCommandStateChange(listener)`:订阅命令状态变化。
233
- - `setReadonly(readonly)`:切换只读。
234
- - `focus()` / `blur()`:焦点控制。
229
+ - `executeCommand(command)`:执行命令。
230
+ - `requestPayloadPanel(request)`:打开参数面板请求,供自绘菜单复用。
231
+ - `getCommandStates()`:读取当前命令状态。
232
+ - `subscribeCommandStateChange(listener)`:订阅命令状态变化。
233
+ - `setReadonly(readonly)`:切换只读。
234
+ - `focus()` / `blur()`:焦点控制。
235
235
  - `destroy()`:销毁实例。
236
236
 
237
237
  保存、提交或离开确认前可以用 `isEditorContentEmpty()` 判断富文本是否为空。它暴露在
@@ -258,122 +258,122 @@ submitButton.disabled = !hasMeaningfulHtmlContent(html);
258
258
  ```
259
259
 
260
260
  ## 命令 API
261
-
262
- 所有菜单最终都会落到 `EditorCommand`。业务也可以直接调用命令 API。
263
-
264
- 文本格式:
265
-
266
- ```ts
267
- editor.executeCommand({ type: 'format.bold' });
268
- editor.executeCommand({ type: 'format.italic' });
269
- editor.executeCommand({ type: 'format.underline' });
270
- editor.executeCommand({ type: 'format.strike' });
271
- editor.executeCommand({ type: 'format.inlineCode' });
272
- editor.executeCommand({ type: 'format.superscript' });
273
- editor.executeCommand({ type: 'format.subscript' });
274
- editor.executeCommand({ type: 'format.clear' });
275
- editor.executeCommand({ type: 'format.color', value: '#1677ff' });
276
- editor.executeCommand({ type: 'format.backgroundColor', value: '#e8f3ff' });
277
- editor.executeCommand({ type: 'format.fontSize', value: '18px' });
278
- editor.executeCommand({ type: 'format.fontFamily', value: 'Arial' });
279
- editor.executeCommand({ type: 'format.lineHeight', value: '1.75' });
280
- ```
281
-
282
- 段落、列表、对齐:
283
-
284
- ```ts
285
- editor.executeCommand({ type: 'block.paragraph' });
286
- editor.executeCommand({ type: 'block.heading', level: 1 });
287
- editor.executeCommand({ type: 'block.quote' });
288
- editor.executeCommand({ type: 'block.divider' });
289
- editor.executeCommand({ type: 'block.code', language: 'typescript' });
290
- editor.executeCommand({ type: 'block.setCodeLanguage', language: 'json' });
291
-
292
- editor.executeCommand({ type: 'list.ordered' });
293
- editor.executeCommand({ type: 'list.unordered' });
294
- editor.executeCommand({ type: 'list.todo' });
295
-
296
- editor.executeCommand({ type: 'align', value: 'left' });
297
- editor.executeCommand({ type: 'align', value: 'center' });
298
- editor.executeCommand({ type: 'align', value: 'right' });
299
- editor.executeCommand({ type: 'align', value: 'justify' });
300
- editor.executeCommand({ type: 'indent.increase' });
301
- editor.executeCommand({ type: 'indent.decrease' });
302
- ```
303
-
304
- 链接、表格、媒体、历史:
305
-
306
- ```ts
307
- editor.executeCommand({ type: 'link.set', href: 'https://example.com', text: 'Example' });
308
- editor.executeCommand({ type: 'link.unset' });
309
- editor.executeCommand({ type: 'link.open' });
310
-
311
- editor.executeCommand({ type: 'table.insert', rows: 3, cols: 4 });
312
- editor.executeCommand({ type: 'table.insertRow', direction: 'after', count: 1 });
313
- editor.executeCommand({ type: 'table.insertColumn', direction: 'after', count: 1 });
314
- editor.executeCommand({ type: 'table.deleteRow' });
315
- editor.executeCommand({ type: 'table.deleteColumn' });
316
- editor.executeCommand({ type: 'table.delete' });
317
-
318
- editor.executeCommand({
319
- type: 'media.insertImage',
320
- url: 'https://example.com/image.png',
321
- alt: '示例图片',
322
- width: 1200,
323
- height: 800,
324
- displayWidthPercent: 50,
325
- align: 'center'
326
- });
327
-
328
- editor.executeCommand({
329
- type: 'media.insertVideo',
330
- url: 'https://example.com/video.mp4',
331
- poster: 'https://example.com/poster.png',
332
- displayWidthPercent: 100
333
- });
334
-
335
- editor.executeCommand({ type: 'history.undo' });
336
- editor.executeCommand({ type: 'history.redo' });
337
- editor.executeCommand({ type: 'fullscreen.toggle' });
338
- editor.executeCommand({ type: 'content.clear' });
339
- ```
340
-
341
- 默认 toolbar 不提供图片/视频 URL 插入入口,但保留 `media.insertImage` 和
342
- `media.insertVideo` 命令。主动链接编辑也不是当前默认内置重点,业务可以通过自定义菜单调用
343
- `link.*` 命令。
344
-
345
- ## Toolbar / Tabbar 菜单配置
346
-
347
- DOM toolbar/tabbar 只通过 `createRichTextToolbar()` 创建。它使用同一套菜单 schema/config,
348
- 并通过传入的 `EditorAPI` 订阅状态、执行命令。
349
-
350
- ```ts
351
- const toolbar = createRichTextToolbar(toolbarContainer, {
352
- editor,
353
- placement: 'bottom',
354
- toolbarConfig: {
355
- toolbarKeys: [
356
- 'bold',
357
- 'italic',
358
- 'underline',
359
- '|',
360
- 'heading-1',
361
- 'quote',
362
- '|',
363
- {
364
- key: 'history',
365
- title: '历史',
366
- icon: 'history',
367
- menuKeys: ['undo', 'redo']
368
- }
369
- ],
370
- excludeKeys: ['quote']
371
- }
372
- });
373
-
374
- toolbar.update();
375
- ```
376
-
261
+
262
+ 所有菜单最终都会落到 `EditorCommand`。业务也可以直接调用命令 API。
263
+
264
+ 文本格式:
265
+
266
+ ```ts
267
+ editor.executeCommand({ type: 'format.bold' });
268
+ editor.executeCommand({ type: 'format.italic' });
269
+ editor.executeCommand({ type: 'format.underline' });
270
+ editor.executeCommand({ type: 'format.strike' });
271
+ editor.executeCommand({ type: 'format.inlineCode' });
272
+ editor.executeCommand({ type: 'format.superscript' });
273
+ editor.executeCommand({ type: 'format.subscript' });
274
+ editor.executeCommand({ type: 'format.clear' });
275
+ editor.executeCommand({ type: 'format.color', value: '#1677ff' });
276
+ editor.executeCommand({ type: 'format.backgroundColor', value: '#e8f3ff' });
277
+ editor.executeCommand({ type: 'format.fontSize', value: '18px' });
278
+ editor.executeCommand({ type: 'format.fontFamily', value: 'Arial' });
279
+ editor.executeCommand({ type: 'format.lineHeight', value: '1.75' });
280
+ ```
281
+
282
+ 段落、列表、对齐:
283
+
284
+ ```ts
285
+ editor.executeCommand({ type: 'block.paragraph' });
286
+ editor.executeCommand({ type: 'block.heading', level: 1 });
287
+ editor.executeCommand({ type: 'block.quote' });
288
+ editor.executeCommand({ type: 'block.divider' });
289
+ editor.executeCommand({ type: 'block.code', language: 'typescript' });
290
+ editor.executeCommand({ type: 'block.setCodeLanguage', language: 'json' });
291
+
292
+ editor.executeCommand({ type: 'list.ordered' });
293
+ editor.executeCommand({ type: 'list.unordered' });
294
+ editor.executeCommand({ type: 'list.todo' });
295
+
296
+ editor.executeCommand({ type: 'align', value: 'left' });
297
+ editor.executeCommand({ type: 'align', value: 'center' });
298
+ editor.executeCommand({ type: 'align', value: 'right' });
299
+ editor.executeCommand({ type: 'align', value: 'justify' });
300
+ editor.executeCommand({ type: 'indent.increase' });
301
+ editor.executeCommand({ type: 'indent.decrease' });
302
+ ```
303
+
304
+ 链接、表格、媒体、历史:
305
+
306
+ ```ts
307
+ editor.executeCommand({ type: 'link.set', href: 'https://example.com', text: 'Example' });
308
+ editor.executeCommand({ type: 'link.unset' });
309
+ editor.executeCommand({ type: 'link.open' });
310
+
311
+ editor.executeCommand({ type: 'table.insert', rows: 3, cols: 4 });
312
+ editor.executeCommand({ type: 'table.insertRow', direction: 'after', count: 1 });
313
+ editor.executeCommand({ type: 'table.insertColumn', direction: 'after', count: 1 });
314
+ editor.executeCommand({ type: 'table.deleteRow' });
315
+ editor.executeCommand({ type: 'table.deleteColumn' });
316
+ editor.executeCommand({ type: 'table.delete' });
317
+
318
+ editor.executeCommand({
319
+ type: 'media.insertImage',
320
+ url: 'https://example.com/image.png',
321
+ alt: '示例图片',
322
+ width: 1200,
323
+ height: 800,
324
+ displayWidthPercent: 50,
325
+ align: 'center'
326
+ });
327
+
328
+ editor.executeCommand({
329
+ type: 'media.insertVideo',
330
+ url: 'https://example.com/video.mp4',
331
+ poster: 'https://example.com/poster.png',
332
+ displayWidthPercent: 100
333
+ });
334
+
335
+ editor.executeCommand({ type: 'history.undo' });
336
+ editor.executeCommand({ type: 'history.redo' });
337
+ editor.executeCommand({ type: 'fullscreen.toggle' });
338
+ editor.executeCommand({ type: 'content.clear' });
339
+ ```
340
+
341
+ 默认 toolbar 不提供图片/视频 URL 插入入口,但保留 `media.insertImage` 和
342
+ `media.insertVideo` 命令。主动链接编辑也不是当前默认内置重点,业务可以通过自定义菜单调用
343
+ `link.*` 命令。
344
+
345
+ ## Toolbar / Tabbar 菜单配置
346
+
347
+ DOM toolbar/tabbar 只通过 `createRichTextToolbar()` 创建。它使用同一套菜单 schema/config,
348
+ 并通过传入的 `EditorAPI` 订阅状态、执行命令。
349
+
350
+ ```ts
351
+ const toolbar = createRichTextToolbar(toolbarContainer, {
352
+ editor,
353
+ placement: 'bottom',
354
+ toolbarConfig: {
355
+ toolbarKeys: [
356
+ 'bold',
357
+ 'italic',
358
+ 'underline',
359
+ '|',
360
+ 'heading-1',
361
+ 'quote',
362
+ '|',
363
+ {
364
+ key: 'history',
365
+ title: '历史',
366
+ icon: 'history',
367
+ menuKeys: ['undo', 'redo']
368
+ }
369
+ ],
370
+ excludeKeys: ['quote']
371
+ }
372
+ });
373
+
374
+ toolbar.update();
375
+ ```
376
+
377
377
  `toolbarConfig` 规则:
378
378
 
379
379
  - `toolbarKeys`:完整控制显示顺序。
@@ -410,192 +410,192 @@ createRichTextToolbar(toolbarContainer, {
410
410
 
411
411
  ```ts
412
412
  const toolbar = createRichTextToolbar(toolbarContainer, {
413
- editor,
414
- placement: 'top',
415
- toolbarConfig: {
416
- toolbarKeys: ['bold', 'italic', '|', 'undo', 'redo']
417
- }
418
- });
419
-
420
- toolbar.update();
421
- ```
422
-
423
- ## 自定义菜单、Icon 和文案
424
-
425
- ```ts
426
- import { createRichTextEditor, createRichTextToolbar } from 'bridgerte/dom';
427
- import { defaultMenuSchema, type MenuItem } from 'bridgerte/native-spec';
428
-
413
+ editor,
414
+ placement: 'top',
415
+ toolbarConfig: {
416
+ toolbarKeys: ['bold', 'italic', '|', 'undo', 'redo']
417
+ }
418
+ });
419
+
420
+ toolbar.update();
421
+ ```
422
+
423
+ ## 自定义菜单、Icon 和文案
424
+
425
+ ```ts
426
+ import { createRichTextEditor, createRichTextToolbar } from 'bridgerte/dom';
427
+ import { defaultMenuSchema, type MenuItem } from 'bridgerte/native-spec';
428
+
429
429
  const customMenu: MenuItem = {
430
430
  id: 'custom-clear',
431
431
  command: { type: 'content.clear' },
432
432
  label: '清空',
433
433
  icon: 'custom-clear'
434
434
  };
435
-
436
- const menuSchema = [...defaultMenuSchema, customMenu];
437
- const icons = {
438
- 'custom-clear': '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M4 6h16"/></svg>'
439
- };
440
- const menuLabels = {
441
- 'custom-clear': '清空文档',
442
- bold: '加粗文本'
443
- };
444
-
445
- const editor = createRichTextEditor(editorContainer, {
446
- menuSchema,
447
- icons,
448
- menuLabels
449
- });
450
-
451
- const toolbar = createRichTextToolbar(toolbarContainer, {
452
- editor,
453
- menuSchema,
454
- toolbarConfig: {
455
- insertKeys: {
456
- index: 0,
457
- keys: ['custom-clear', '|']
458
- }
459
- },
460
- icons,
461
- menuLabels
462
- });
463
-
464
- toolbar.update();
465
- ```
466
-
467
- 稳定约束:
468
-
469
- - `MenuItem.id` 是菜单配置使用的稳定 key。
470
- - `MenuItem.icon` 是稳定 icon key,不是 SVG 字符串。
471
- - 业务覆盖 icon 只能通过 `icons` map。
472
- - 业务覆盖文案只能通过 `menuLabels`。
473
- - `menuLabels` 影响按钮文本、tooltip 和 `aria-label`,不改变命令语义。
474
- - 缺失 icon 时,DOM 菜单使用 label 文本兜底。
475
-
476
- ## Hoverbar
477
-
478
- 选中文本 hoverbar 默认开启,复用 `menuSchema`、`icons` 和 `menuLabels`。它有独立的
479
- `hoverbarConfig`,可以和 toolbar 展示不同菜单。
480
-
481
- ```ts
482
- createRichTextEditor(container, {
483
- hoverbarConfig: {
484
- toolbarKeys: [
485
- 'bold',
486
- 'italic',
487
- '|',
488
- 'color',
489
- 'background-color',
490
- '|',
491
- 'font-size',
492
- 'line-height'
493
- ]
494
- }
495
- });
496
- ```
497
-
498
- 关闭内置 hoverbar:
499
-
500
- ```ts
501
- createRichTextEditor(container, {
502
- floatingMenus: {
503
- hoverbar: false
504
- }
505
- });
506
- ```
507
-
508
- 关闭后只是不显示 DOM hoverbar,不影响底层命令 API。H5/WebView 或品牌化项目可以关闭内置
509
- hoverbar 后自绘选区菜单。
510
-
511
- ## 参数面板
512
-
513
- 颜色、背景色、字号、字体、行高、表格和代码语言都通过 `PayloadPanelSchema` 描述候选项。
514
- DOM 默认 UI 和业务自绘 request 使用同一份 schema。
515
-
516
- ```ts
517
- createRichTextEditor(container, {
518
- payloadPanelConfig: {
519
- 'font-size': {
520
- fields: {
521
- value: {
522
- includeValues: ['16px', '20px', '24px'],
523
- optionLabels: {
524
- '16px': '正文',
525
- '24px': '标题'
526
- }
527
- }
528
- }
529
- },
530
- color: {
531
- fields: {
532
- value: {
533
- options: [
534
- { label: '品牌蓝', value: '#1677ff' },
535
- { label: '危险红', value: '#ff4d4f' }
536
- ]
537
- }
538
- }
539
- },
540
- table: {
541
- fields: {
542
- rows: { defaultValue: '2', max: 6 },
543
- cols: { defaultValue: '3', max: 6 }
544
- }
545
- },
546
- 'code-block-language': {
547
- fields: {
548
- language: {
549
- includeValues: ['plain', 'javascript', 'typescript', 'json']
550
- }
551
- }
552
- }
553
- },
554
- onPayloadPanelRequest(request) {
555
- if (request.panel.id === 'color') {
556
- renderColorPanel(request);
557
- return true;
558
- }
559
- }
560
- });
561
- ```
562
-
563
- 自绘接管规则:
564
-
565
- ```ts
566
- createRichTextEditor(container, {
567
- onPayloadPanelRequest(request) {
568
- renderPanel({
569
- title: request.panel.title,
570
- fields: request.panel.fields,
571
- currentValues: request.currentValues,
572
- readonly: request.readonly,
573
- submit: request.submit,
574
- cancel: request.cancel
575
- });
576
-
577
- return true;
578
- }
579
- });
580
- ```
581
-
582
- `onPayloadPanelRequest` 返回 `true` 表示业务接管渲染,DOM 默认面板不会显示。业务自绘完成后:
583
-
584
- ```ts
585
- request.submit({ value: '#1677ff' });
586
- request.cancel();
587
- ```
588
-
589
- readonly 下 request 会带 `readonly: true`,自绘层应展示只读态;DOM 默认面板不会打开。
590
-
591
- 代码块语言也可以直接传完整 schema:
592
-
593
- ```ts
594
- createRichTextEditor(container, {
595
- codeBlockLanguagePanel: {
596
- id: 'code-block-language',
597
- title: '代码语言',
598
- fields: [
435
+
436
+ const menuSchema = [...defaultMenuSchema, customMenu];
437
+ const icons = {
438
+ 'custom-clear': '<svg aria-hidden="true" viewBox="0 0 24 24"><path d="M4 6h16"/></svg>'
439
+ };
440
+ const menuLabels = {
441
+ 'custom-clear': '清空文档',
442
+ bold: '加粗文本'
443
+ };
444
+
445
+ const editor = createRichTextEditor(editorContainer, {
446
+ menuSchema,
447
+ icons,
448
+ menuLabels
449
+ });
450
+
451
+ const toolbar = createRichTextToolbar(toolbarContainer, {
452
+ editor,
453
+ menuSchema,
454
+ toolbarConfig: {
455
+ insertKeys: {
456
+ index: 0,
457
+ keys: ['custom-clear', '|']
458
+ }
459
+ },
460
+ icons,
461
+ menuLabels
462
+ });
463
+
464
+ toolbar.update();
465
+ ```
466
+
467
+ 稳定约束:
468
+
469
+ - `MenuItem.id` 是菜单配置使用的稳定 key。
470
+ - `MenuItem.icon` 是稳定 icon key,不是 SVG 字符串。
471
+ - 业务覆盖 icon 只能通过 `icons` map。
472
+ - 业务覆盖文案只能通过 `menuLabels`。
473
+ - `menuLabels` 影响按钮文本、tooltip 和 `aria-label`,不改变命令语义。
474
+ - 缺失 icon 时,DOM 菜单使用 label 文本兜底。
475
+
476
+ ## Hoverbar
477
+
478
+ 选中文本 hoverbar 默认开启,复用 `menuSchema`、`icons` 和 `menuLabels`。它有独立的
479
+ `hoverbarConfig`,可以和 toolbar 展示不同菜单。
480
+
481
+ ```ts
482
+ createRichTextEditor(container, {
483
+ hoverbarConfig: {
484
+ toolbarKeys: [
485
+ 'bold',
486
+ 'italic',
487
+ '|',
488
+ 'color',
489
+ 'background-color',
490
+ '|',
491
+ 'font-size',
492
+ 'line-height'
493
+ ]
494
+ }
495
+ });
496
+ ```
497
+
498
+ 关闭内置 hoverbar:
499
+
500
+ ```ts
501
+ createRichTextEditor(container, {
502
+ floatingMenus: {
503
+ hoverbar: false
504
+ }
505
+ });
506
+ ```
507
+
508
+ 关闭后只是不显示 DOM hoverbar,不影响底层命令 API。H5/WebView 或品牌化项目可以关闭内置
509
+ hoverbar 后自绘选区菜单。
510
+
511
+ ## 参数面板
512
+
513
+ 颜色、背景色、字号、字体、行高、表格和代码语言都通过 `PayloadPanelSchema` 描述候选项。
514
+ DOM 默认 UI 和业务自绘 request 使用同一份 schema。
515
+
516
+ ```ts
517
+ createRichTextEditor(container, {
518
+ payloadPanelConfig: {
519
+ 'font-size': {
520
+ fields: {
521
+ value: {
522
+ includeValues: ['16px', '20px', '24px'],
523
+ optionLabels: {
524
+ '16px': '正文',
525
+ '24px': '标题'
526
+ }
527
+ }
528
+ }
529
+ },
530
+ color: {
531
+ fields: {
532
+ value: {
533
+ options: [
534
+ { label: '品牌蓝', value: '#1677ff' },
535
+ { label: '危险红', value: '#ff4d4f' }
536
+ ]
537
+ }
538
+ }
539
+ },
540
+ table: {
541
+ fields: {
542
+ rows: { defaultValue: '2', max: 6 },
543
+ cols: { defaultValue: '3', max: 6 }
544
+ }
545
+ },
546
+ 'code-block-language': {
547
+ fields: {
548
+ language: {
549
+ includeValues: ['plain', 'javascript', 'typescript', 'json']
550
+ }
551
+ }
552
+ }
553
+ },
554
+ onPayloadPanelRequest(request) {
555
+ if (request.panel.id === 'color') {
556
+ renderColorPanel(request);
557
+ return true;
558
+ }
559
+ }
560
+ });
561
+ ```
562
+
563
+ 自绘接管规则:
564
+
565
+ ```ts
566
+ createRichTextEditor(container, {
567
+ onPayloadPanelRequest(request) {
568
+ renderPanel({
569
+ title: request.panel.title,
570
+ fields: request.panel.fields,
571
+ currentValues: request.currentValues,
572
+ readonly: request.readonly,
573
+ submit: request.submit,
574
+ cancel: request.cancel
575
+ });
576
+
577
+ return true;
578
+ }
579
+ });
580
+ ```
581
+
582
+ `onPayloadPanelRequest` 返回 `true` 表示业务接管渲染,DOM 默认面板不会显示。业务自绘完成后:
583
+
584
+ ```ts
585
+ request.submit({ value: '#1677ff' });
586
+ request.cancel();
587
+ ```
588
+
589
+ readonly 下 request 会带 `readonly: true`,自绘层应展示只读态;DOM 默认面板不会打开。
590
+
591
+ 代码块语言也可以直接传完整 schema:
592
+
593
+ ```ts
594
+ createRichTextEditor(container, {
595
+ codeBlockLanguagePanel: {
596
+ id: 'code-block-language',
597
+ title: '代码语言',
598
+ fields: [
599
599
  {
600
600
  type: 'select',
601
601
  name: 'language',
@@ -624,136 +624,136 @@ Vue、XML、YAML。粘贴 fenced code 时也会识别常见 alias,
624
624
  `yml` 到 `yaml`。
625
625
 
626
626
  ## 图片、视频和上传
627
-
628
- BridgeRTE 不内置上传后端。图片/视频上传必须由业务实现 `uploadAdapter`。
629
-
630
- ```ts
631
- import { createRichTextEditor } from 'bridgerte/dom';
632
- import type { UploadAdapter } from 'bridgerte/core';
633
-
634
- const uploadBlob = async (url: string, file: unknown, signal?: AbortSignal) => {
635
- const formData = new FormData();
636
-
637
- if (file instanceof Blob) formData.append('file', file);
638
-
639
- const response = await fetch(url, {
640
- method: 'POST',
641
- body: formData,
642
- signal
643
- });
644
-
645
- return await response.json() as {
646
- url: string;
647
- width?: number;
648
- height?: number;
649
- poster?: string;
650
- };
651
- };
652
-
653
- const uploadAdapter: UploadAdapter = {
654
- async uploadImage(file, context) {
655
- return await uploadBlob('/api/upload-image', file.data, context.signal as AbortSignal);
656
- },
657
- async uploadVideo(file, context) {
658
- return await uploadBlob('/api/upload-video', file.data, context.signal as AbortSignal);
659
- }
660
- };
661
-
662
- createRichTextEditor(container, {
663
- uploadAdapter,
664
- mediaDefaultWidthPercent: 50
665
- });
666
- ```
667
-
668
- 媒体能力:
669
-
670
- - 默认 toolbar 提供本地上传图片/视频入口。
671
- - URL 图片/视频插入使用 `media.insertImage` / `media.insertVideo` 命令。
672
- - 图片/视频加载前会显示占位和 loading 状态,资源加载完成后再显示真实媒体。
673
- - 上传失败时可以重试或删除。
674
- - 成功态 controls 支持左/中/右对齐、`20%`、`50%`、`100%` 显示比例和删除。
675
- - `mediaDefaultWidthPercent` 可设为 `20 | 50 | 100`,默认 `50`。
676
-
677
- 配置媒体 controls:
678
-
679
- ```ts
680
- createRichTextEditor(container, {
681
- mediaControlsConfig: {
682
- toolbarKeys: [
683
- 'media-align-left',
684
- 'media-align-center',
685
- 'media-align-right',
686
- '|',
687
- 'media-remove'
688
- ]
689
- },
690
- menuLabels: {
691
- 'media-remove': '删除媒体'
692
- }
693
- });
694
- ```
695
-
696
- `media-resize-20`、`media-resize-50`、`media-resize-100` 是基础尺寸能力,配置中误删时会自动补回。
697
- 如果业务完全自绘媒体 controls,也需要提供等价的 `20%`、`50%`、`100%` 尺寸能力。
698
-
699
- ## Mention
700
-
701
- `@` mention 默认开启。`mentionProvider` 负责返回候选数据,展示字段由
702
- `mentionMenuConfig` 控制。
703
-
704
- ```ts
705
- import { createRichTextEditor } from 'bridgerte/dom';
706
- import type { MentionItem } from 'bridgerte/core';
707
-
708
- const mentionProvider = async (query: string): Promise<MentionItem[]> => {
709
- const response = await fetch(`/api/members?q=${encodeURIComponent(query)}`);
710
- return await response.json() as MentionItem[];
711
- };
712
-
713
- createRichTextEditor(container, {
714
- mentionProvider,
715
- mentionMenuConfig: {
716
- labelField: 'data.displayName',
717
- descriptionField: 'data.role',
718
- avatarField: 'data.avatarUrl',
719
- showAvatar: true,
720
- showDescription: true,
721
- loadingText: '搜索成员中',
722
- emptyText: '没有匹配成员',
723
- errorText: '成员加载失败'
724
- }
725
- });
726
- ```
727
-
728
- 自绘 mention 菜单:
729
-
730
- ```ts
731
- createRichTextEditor(container, {
732
- mentionProvider,
733
- onMentionMenuRequest(request) {
734
- renderMentionPopover(request);
735
- return true;
736
- }
737
- });
738
- ```
739
-
740
- 关闭 mention trigger:
741
-
742
- ```ts
743
- createRichTextEditor(container, {
744
- floatingMenus: {
745
- mention: false
746
- }
747
- });
748
- ```
749
-
750
- 关闭后不会监听 `@query`,也不会请求 provider;业务仍可主动执行 `mention.insert` 命令。
751
-
752
- ## Slash Command
753
-
754
- `/` slash command 默认从菜单 schema 里选择常用结构命令。
755
-
756
- ```ts
627
+
628
+ BridgeRTE 不内置上传后端。图片/视频上传必须由业务实现 `uploadAdapter`。
629
+
630
+ ```ts
631
+ import { createRichTextEditor } from 'bridgerte/dom';
632
+ import type { UploadAdapter } from 'bridgerte/core';
633
+
634
+ const uploadBlob = async (url: string, file: unknown, signal?: AbortSignal) => {
635
+ const formData = new FormData();
636
+
637
+ if (file instanceof Blob) formData.append('file', file);
638
+
639
+ const response = await fetch(url, {
640
+ method: 'POST',
641
+ body: formData,
642
+ signal
643
+ });
644
+
645
+ return await response.json() as {
646
+ url: string;
647
+ width?: number;
648
+ height?: number;
649
+ poster?: string;
650
+ };
651
+ };
652
+
653
+ const uploadAdapter: UploadAdapter = {
654
+ async uploadImage(file, context) {
655
+ return await uploadBlob('/api/upload-image', file.data, context.signal as AbortSignal);
656
+ },
657
+ async uploadVideo(file, context) {
658
+ return await uploadBlob('/api/upload-video', file.data, context.signal as AbortSignal);
659
+ }
660
+ };
661
+
662
+ createRichTextEditor(container, {
663
+ uploadAdapter,
664
+ mediaDefaultWidthPercent: 50
665
+ });
666
+ ```
667
+
668
+ 媒体能力:
669
+
670
+ - 默认 toolbar 提供本地上传图片/视频入口。
671
+ - URL 图片/视频插入使用 `media.insertImage` / `media.insertVideo` 命令。
672
+ - 图片/视频加载前会显示占位和 loading 状态,资源加载完成后再显示真实媒体。
673
+ - 上传失败时可以重试或删除。
674
+ - 成功态 controls 支持左/中/右对齐、`20%`、`50%`、`100%` 显示比例和删除。
675
+ - `mediaDefaultWidthPercent` 可设为 `20 | 50 | 100`,默认 `50`。
676
+
677
+ 配置媒体 controls:
678
+
679
+ ```ts
680
+ createRichTextEditor(container, {
681
+ mediaControlsConfig: {
682
+ toolbarKeys: [
683
+ 'media-align-left',
684
+ 'media-align-center',
685
+ 'media-align-right',
686
+ '|',
687
+ 'media-remove'
688
+ ]
689
+ },
690
+ menuLabels: {
691
+ 'media-remove': '删除媒体'
692
+ }
693
+ });
694
+ ```
695
+
696
+ `media-resize-20`、`media-resize-50`、`media-resize-100` 是基础尺寸能力,配置中误删时会自动补回。
697
+ 如果业务完全自绘媒体 controls,也需要提供等价的 `20%`、`50%`、`100%` 尺寸能力。
698
+
699
+ ## Mention
700
+
701
+ `@` mention 默认开启。`mentionProvider` 负责返回候选数据,展示字段由
702
+ `mentionMenuConfig` 控制。
703
+
704
+ ```ts
705
+ import { createRichTextEditor } from 'bridgerte/dom';
706
+ import type { MentionItem } from 'bridgerte/core';
707
+
708
+ const mentionProvider = async (query: string): Promise<MentionItem[]> => {
709
+ const response = await fetch(`/api/members?q=${encodeURIComponent(query)}`);
710
+ return await response.json() as MentionItem[];
711
+ };
712
+
713
+ createRichTextEditor(container, {
714
+ mentionProvider,
715
+ mentionMenuConfig: {
716
+ labelField: 'data.displayName',
717
+ descriptionField: 'data.role',
718
+ avatarField: 'data.avatarUrl',
719
+ showAvatar: true,
720
+ showDescription: true,
721
+ loadingText: '搜索成员中',
722
+ emptyText: '没有匹配成员',
723
+ errorText: '成员加载失败'
724
+ }
725
+ });
726
+ ```
727
+
728
+ 自绘 mention 菜单:
729
+
730
+ ```ts
731
+ createRichTextEditor(container, {
732
+ mentionProvider,
733
+ onMentionMenuRequest(request) {
734
+ renderMentionPopover(request);
735
+ return true;
736
+ }
737
+ });
738
+ ```
739
+
740
+ 关闭 mention trigger:
741
+
742
+ ```ts
743
+ createRichTextEditor(container, {
744
+ floatingMenus: {
745
+ mention: false
746
+ }
747
+ });
748
+ ```
749
+
750
+ 关闭后不会监听 `@query`,也不会请求 provider;业务仍可主动执行 `mention.insert` 命令。
751
+
752
+ ## Slash Command
753
+
754
+ `/` slash command 默认从菜单 schema 里选择常用结构命令。
755
+
756
+ ```ts
757
757
  createRichTextEditor(container, {
758
758
  slashCommandConfig: {
759
759
  toolbarKeys: ['heading-1', 'heading-2', '|', 'quote', 'code-block', 'table']
@@ -782,28 +782,28 @@ createRichTextEditor(container, {
782
782
 
783
783
  ```ts
784
784
  import type { SlashCommandItem } from 'bridgerte/core';
785
-
786
- const slashCommandProvider = async (query: string): Promise<SlashCommandItem[]> => [
787
- {
788
- id: 'insert-template',
789
- label: '插入模板',
790
- description: `按 ${query} 搜索模板`,
791
- icon: 'template',
792
- command: { type: 'block.quote' }
793
- }
794
- ];
795
-
796
- createRichTextEditor(container, {
797
- slashCommandConfig: {
798
- toolbarKeys: ['quote', 'table']
799
- },
800
- slashCommandProvider
801
- });
802
- ```
803
-
804
- 语义说明:
805
-
806
- - 只传 `slashCommandProvider` 时,provider 候选替换默认 slash 列表。
785
+
786
+ const slashCommandProvider = async (query: string): Promise<SlashCommandItem[]> => [
787
+ {
788
+ id: 'insert-template',
789
+ label: '插入模板',
790
+ description: `按 ${query} 搜索模板`,
791
+ icon: 'template',
792
+ command: { type: 'block.quote' }
793
+ }
794
+ ];
795
+
796
+ createRichTextEditor(container, {
797
+ slashCommandConfig: {
798
+ toolbarKeys: ['quote', 'table']
799
+ },
800
+ slashCommandProvider
801
+ });
802
+ ```
803
+
804
+ 语义说明:
805
+
806
+ - 只传 `slashCommandProvider` 时,provider 候选替换默认 slash 列表。
807
807
  - 传入 `slashCommandConfig` 或自定义 `menuSchema` 后,provider 作为动态候选追加。
808
808
  - provider 失败时,如果静态候选可用,仍显示静态候选。
809
809
  - 表格这类需要参数的命令会继续走参数面板。
@@ -811,211 +811,211 @@ createRichTextEditor(container, {
811
811
  的语义一致;未知 key 会被忽略,首尾或连续分割线会在解析阶段清理。
812
812
 
813
813
  自绘 slash command:
814
-
815
- ```ts
816
- createRichTextEditor(container, {
817
- onSlashCommandMenuRequest(request) {
818
- renderSlashPopover(request);
819
- return true;
820
- }
821
- });
822
- ```
823
-
824
- 关闭 slash trigger:
825
-
826
- ```ts
827
- createRichTextEditor(container, {
828
- floatingMenus: {
829
- slash: false
830
- }
831
- });
832
- ```
833
-
834
- ## WebView
835
-
836
- WebView 页面内使用 `createWebViewBridgeRuntime()` 接 RN/Flutter 外壳消息。
837
-
838
- ```ts
839
- import { createWebViewBridgeRuntime } from 'bridgerte/webview';
840
- import 'bridgerte/style.css';
841
-
842
- const runtime = createWebViewBridgeRuntime({
843
- container,
844
- transport: {
845
- postMessage(message) {
846
- window.ReactNativeWebView?.postMessage(JSON.stringify(message));
847
- },
848
- addMessageListener(listener) {
849
- const handleMessage = (event: MessageEvent) => {
850
- listener(JSON.parse(String(event.data)));
851
- };
852
-
853
- window.addEventListener('message', handleMessage);
854
-
855
- return () => {
856
- window.removeEventListener('message', handleMessage);
857
- };
858
- }
859
- }
860
- });
861
-
862
- window.addEventListener('beforeunload', () => {
863
- runtime.destroy();
864
- });
865
- ```
866
-
867
- 也可以不提供 `addMessageListener`,由业务手动转发消息:
868
-
869
- ```ts
870
- runtime.receive(messageFromNative);
871
- ```
872
-
873
- 原生侧发给编辑器:
874
-
875
- - `editor.init`
876
- - `editor.executeCommand`
877
- - `editor.setContent`
878
- - `editor.setReadonly`
879
- - `editor.requestContent`
880
- - `editor.payloadPanelResolved`
881
- - `editor.payloadPanelCanceled`
882
- - `editor.uploadResolved`
883
- - `editor.uploadRejected`
884
-
885
- 编辑器发给原生侧:
886
-
887
- - `editor.ready`
888
- - `editor.content`
889
- - `editor.contentChange`
890
- - `editor.commandStateChange`
891
- - `editor.payloadPanelRequest`
892
- - `editor.uploadRequest`
893
- - `editor.heightChange`
894
- - `editor.error`
895
-
896
- 内容消息边界:
897
-
898
- - 高频自动 `editor.contentChange` 只传轻量摘要。
899
- - 完整 `EditorContent` 通过原生侧发送 `editor.requestContent` 获取。
900
- - `requestContent` 的响应消息是 `editor.content`。
901
- - 兼容期也会同步发送旧 `editor.contentChange` 完整响应。
902
- - bridge 不传 File、Blob、base64 或大体积二进制文件。
903
-
904
- ## RN / Flutter 原生菜单
905
-
906
- RN/Flutter 原生侧可以读取 `bridgerte/native-spec` 来渲染自己的菜单:
907
-
908
- ```ts
909
- import {
910
- defaultMenuSchema,
911
- defaultToolbarConfig,
912
- resolveToolbarMenu,
913
- isMenuItemCommandState
914
- } from 'bridgerte/native-spec';
915
-
916
- const toolbarItems = resolveToolbarMenu(defaultToolbarConfig, defaultMenuSchema);
917
- ```
918
-
919
- 渲染规则:
920
-
921
- - `MenuItem.id` 是配置和状态匹配的稳定 key。
922
- - `MenuItem.command` 是完整命令,点击后可以直接发给 WebView。
923
- - `MenuItem.icon` 是稳定 icon key,RN/Flutter 映射到自己的原生图标。
924
- - `MenuItem.icon` 不是 SVG 字符串。
925
- - `payloadPanel` 描述需要原生侧补齐的参数。
926
- - 命令状态可用 `isMenuItemCommandState(item, state)` 匹配。
927
-
928
- 原生菜单不复用 DOM CSS;WebView 内编辑器样式通过 `--bridgerte-*` CSS Variables 覆盖。
929
-
930
- ## 样式和主题
931
-
932
- 导入默认样式:
933
-
934
- ```ts
935
- import 'bridgerte/style.css';
936
- ```
937
-
938
- 覆盖主题变量:
939
-
940
- ```css
941
- .editor-shell {
942
- --bridgerte-color-primary: #1677ff;
943
- --bridgerte-color-text: #1f2329;
944
- --bridgerte-color-text-muted: #86909c;
945
- --bridgerte-color-bg: #ffffff;
946
- --bridgerte-color-panel: #ffffff;
947
- --bridgerte-color-border: #e5e6eb;
948
- --bridgerte-color-active-bg: #e8f3ff;
949
- --bridgerte-color-placeholder: #b7bcc5;
950
- --bridgerte-shadow-panel: 0 12px 32px rgb(15 23 42 / 14%);
951
- --bridgerte-font-size: 15px;
952
- --bridgerte-line-height: 1.7;
953
- --bridgerte-radius: 8px;
954
- --bridgerte-toolbar-height: 42px;
955
- --bridgerte-control-height: 32px;
956
- --bridgerte-editor-padding: 12px;
957
- }
958
- ```
959
-
960
- 常用变量:
961
-
962
- - `--bridgerte-color-primary`
963
- - `--bridgerte-color-text`
964
- - `--bridgerte-color-text-muted`
965
- - `--bridgerte-color-bg`
966
- - `--bridgerte-color-panel`
967
- - `--bridgerte-color-border`
968
- - `--bridgerte-color-active-bg`
969
- - `--bridgerte-color-placeholder`
970
- - `--bridgerte-color-danger`
971
- - `--bridgerte-shadow-panel`
972
- - `--bridgerte-font-size`
973
- - `--bridgerte-line-height`
974
- - `--bridgerte-radius`
975
- - `--bridgerte-toolbar-height`
976
- - `--bridgerte-control-height`
977
- - `--bridgerte-hoverbar-button-size`
978
- - `--bridgerte-editor-padding`
979
-
980
- PC/H5 边界:
981
-
982
- - DOM 默认样式只服务 Web/PC/H5。
983
- - RN/Flutter 原生菜单不复用 DOM CSS。
984
- - H5 触屏端不依赖 hover tooltip。
985
- - 业务可以在外层容器覆盖 CSS Variables 实现品牌主题。
986
-
987
- ## 性能建议
988
-
989
- BridgeRTE 按 10w 字符级内容设计输入路径:
990
-
991
- - 不要在每次输入时调用 `getContent()`。
992
- - 不要对 10w 内容逐字依赖 `onChange` 完整保存。
993
- - 高频 UI 状态使用 `onContentChange` 摘要。
994
- - 保存、提交或离开页面确认时再调用 `getContent()`。
995
- - WebView 高频 `editor.contentChange` 只依赖摘要。
996
- - WebView 完整内容通过 `editor.requestContent` 主动获取。
997
- - bridge 不传 base64、File、Blob 或二进制大文件。
998
-
999
- 推荐保存:
1000
-
1001
- ```ts
1002
- const save = async () => {
1003
- const content = editor.getContent();
1004
-
1005
- await fetch('/api/document', {
1006
- method: 'POST',
1007
- headers: {
1008
- 'content-type': 'application/json'
1009
- },
1010
- body: JSON.stringify(content)
1011
- });
1012
- };
1013
- ```
1014
-
1015
- ## 当前边界
1016
-
1017
- - 业务项目只安装 `bridgerte`,不需要安装 Lexical。
1018
- - 默认 toolbar 不提供图片/视频 URL 插入入口;业务用命令 API 自定义入口。
1019
- - 主动链接编辑不作为默认内置入口;业务用 `link.*` 命令自定义入口。
1020
- - BridgeRTE 不内置上传后端,上传必须由业务实现 `uploadAdapter`。
1021
- - WebView bridge 不传大体积二进制和 base64 文件。
814
+
815
+ ```ts
816
+ createRichTextEditor(container, {
817
+ onSlashCommandMenuRequest(request) {
818
+ renderSlashPopover(request);
819
+ return true;
820
+ }
821
+ });
822
+ ```
823
+
824
+ 关闭 slash trigger:
825
+
826
+ ```ts
827
+ createRichTextEditor(container, {
828
+ floatingMenus: {
829
+ slash: false
830
+ }
831
+ });
832
+ ```
833
+
834
+ ## WebView
835
+
836
+ WebView 页面内使用 `createWebViewBridgeRuntime()` 接 RN/Flutter 外壳消息。
837
+
838
+ ```ts
839
+ import { createWebViewBridgeRuntime } from 'bridgerte/webview';
840
+ import 'bridgerte/style.css';
841
+
842
+ const runtime = createWebViewBridgeRuntime({
843
+ container,
844
+ transport: {
845
+ postMessage(message) {
846
+ window.ReactNativeWebView?.postMessage(JSON.stringify(message));
847
+ },
848
+ addMessageListener(listener) {
849
+ const handleMessage = (event: MessageEvent) => {
850
+ listener(JSON.parse(String(event.data)));
851
+ };
852
+
853
+ window.addEventListener('message', handleMessage);
854
+
855
+ return () => {
856
+ window.removeEventListener('message', handleMessage);
857
+ };
858
+ }
859
+ }
860
+ });
861
+
862
+ window.addEventListener('beforeunload', () => {
863
+ runtime.destroy();
864
+ });
865
+ ```
866
+
867
+ 也可以不提供 `addMessageListener`,由业务手动转发消息:
868
+
869
+ ```ts
870
+ runtime.receive(messageFromNative);
871
+ ```
872
+
873
+ 原生侧发给编辑器:
874
+
875
+ - `editor.init`
876
+ - `editor.executeCommand`
877
+ - `editor.setContent`
878
+ - `editor.setReadonly`
879
+ - `editor.requestContent`
880
+ - `editor.payloadPanelResolved`
881
+ - `editor.payloadPanelCanceled`
882
+ - `editor.uploadResolved`
883
+ - `editor.uploadRejected`
884
+
885
+ 编辑器发给原生侧:
886
+
887
+ - `editor.ready`
888
+ - `editor.content`
889
+ - `editor.contentChange`
890
+ - `editor.commandStateChange`
891
+ - `editor.payloadPanelRequest`
892
+ - `editor.uploadRequest`
893
+ - `editor.heightChange`
894
+ - `editor.error`
895
+
896
+ 内容消息边界:
897
+
898
+ - 高频自动 `editor.contentChange` 只传轻量摘要。
899
+ - 完整 `EditorContent` 通过原生侧发送 `editor.requestContent` 获取。
900
+ - `requestContent` 的响应消息是 `editor.content`。
901
+ - 兼容期也会同步发送旧 `editor.contentChange` 完整响应。
902
+ - bridge 不传 File、Blob、base64 或大体积二进制文件。
903
+
904
+ ## RN / Flutter 原生菜单
905
+
906
+ RN/Flutter 原生侧可以读取 `bridgerte/native-spec` 来渲染自己的菜单:
907
+
908
+ ```ts
909
+ import {
910
+ defaultMenuSchema,
911
+ defaultToolbarConfig,
912
+ resolveToolbarMenu,
913
+ isMenuItemCommandState
914
+ } from 'bridgerte/native-spec';
915
+
916
+ const toolbarItems = resolveToolbarMenu(defaultToolbarConfig, defaultMenuSchema);
917
+ ```
918
+
919
+ 渲染规则:
920
+
921
+ - `MenuItem.id` 是配置和状态匹配的稳定 key。
922
+ - `MenuItem.command` 是完整命令,点击后可以直接发给 WebView。
923
+ - `MenuItem.icon` 是稳定 icon key,RN/Flutter 映射到自己的原生图标。
924
+ - `MenuItem.icon` 不是 SVG 字符串。
925
+ - `payloadPanel` 描述需要原生侧补齐的参数。
926
+ - 命令状态可用 `isMenuItemCommandState(item, state)` 匹配。
927
+
928
+ 原生菜单不复用 DOM CSS;WebView 内编辑器样式通过 `--bridgerte-*` CSS Variables 覆盖。
929
+
930
+ ## 样式和主题
931
+
932
+ 导入默认样式:
933
+
934
+ ```ts
935
+ import 'bridgerte/style.css';
936
+ ```
937
+
938
+ 覆盖主题变量:
939
+
940
+ ```css
941
+ .editor-shell {
942
+ --bridgerte-color-primary: #1677ff;
943
+ --bridgerte-color-text: #1f2329;
944
+ --bridgerte-color-text-muted: #86909c;
945
+ --bridgerte-color-bg: #ffffff;
946
+ --bridgerte-color-panel: #ffffff;
947
+ --bridgerte-color-border: #e5e6eb;
948
+ --bridgerte-color-active-bg: #e8f3ff;
949
+ --bridgerte-color-placeholder: #b7bcc5;
950
+ --bridgerte-shadow-panel: 0 12px 32px rgb(15 23 42 / 14%);
951
+ --bridgerte-font-size: 15px;
952
+ --bridgerte-line-height: 1.7;
953
+ --bridgerte-radius: 8px;
954
+ --bridgerte-toolbar-height: 42px;
955
+ --bridgerte-control-height: 32px;
956
+ --bridgerte-editor-padding: 12px;
957
+ }
958
+ ```
959
+
960
+ 常用变量:
961
+
962
+ - `--bridgerte-color-primary`
963
+ - `--bridgerte-color-text`
964
+ - `--bridgerte-color-text-muted`
965
+ - `--bridgerte-color-bg`
966
+ - `--bridgerte-color-panel`
967
+ - `--bridgerte-color-border`
968
+ - `--bridgerte-color-active-bg`
969
+ - `--bridgerte-color-placeholder`
970
+ - `--bridgerte-color-danger`
971
+ - `--bridgerte-shadow-panel`
972
+ - `--bridgerte-font-size`
973
+ - `--bridgerte-line-height`
974
+ - `--bridgerte-radius`
975
+ - `--bridgerte-toolbar-height`
976
+ - `--bridgerte-control-height`
977
+ - `--bridgerte-hoverbar-button-size`
978
+ - `--bridgerte-editor-padding`
979
+
980
+ PC/H5 边界:
981
+
982
+ - DOM 默认样式只服务 Web/PC/H5。
983
+ - RN/Flutter 原生菜单不复用 DOM CSS。
984
+ - H5 触屏端不依赖 hover tooltip。
985
+ - 业务可以在外层容器覆盖 CSS Variables 实现品牌主题。
986
+
987
+ ## 性能建议
988
+
989
+ BridgeRTE 按 10w 字符级内容设计输入路径:
990
+
991
+ - 不要在每次输入时调用 `getContent()`。
992
+ - 不要对 10w 内容逐字依赖 `onChange` 完整保存。
993
+ - 高频 UI 状态使用 `onContentChange` 摘要。
994
+ - 保存、提交或离开页面确认时再调用 `getContent()`。
995
+ - WebView 高频 `editor.contentChange` 只依赖摘要。
996
+ - WebView 完整内容通过 `editor.requestContent` 主动获取。
997
+ - bridge 不传 base64、File、Blob 或二进制大文件。
998
+
999
+ 推荐保存:
1000
+
1001
+ ```ts
1002
+ const save = async () => {
1003
+ const content = editor.getContent();
1004
+
1005
+ await fetch('/api/document', {
1006
+ method: 'POST',
1007
+ headers: {
1008
+ 'content-type': 'application/json'
1009
+ },
1010
+ body: JSON.stringify(content)
1011
+ });
1012
+ };
1013
+ ```
1014
+
1015
+ ## 当前边界
1016
+
1017
+ - 业务项目只安装 `bridgerte`,不需要安装 Lexical。
1018
+ - 默认 toolbar 不提供图片/视频 URL 插入入口;业务用命令 API 自定义入口。
1019
+ - 主动链接编辑不作为默认内置入口;业务用 `link.*` 命令自定义入口。
1020
+ - BridgeRTE 不内置上传后端,上传必须由业务实现 `uploadAdapter`。
1021
+ - WebView bridge 不传大体积二进制和 base64 文件。