@formulaxjs/ckeditor5 0.2.2 → 0.3.1

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.zh-CN.md CHANGED
@@ -1,270 +1,283 @@
1
- # @formulaxjs/ckeditor5
2
-
3
- [English](./README.md) | 简体中文
4
-
5
- FormulaX 的 CKEditor 5 集成适配器。
6
-
7
- `@formulaxjs/ckeditor5` 提供了一个 CKEditor 5 插件,会以弹窗形式打开 FormulaX 编辑器,用于在 CKEditor 5 内容中插入和更新行内公式。
8
-
9
- > 状态:实验阶段。在首个稳定版本发布前,公共 API 仍可能调整。
10
-
11
- ## 功能特性
12
-
13
- - 通过 `FormulaX` 导出 CKEditor 5 插件
14
- - 基于 CKEditor 5 `componentFactory` 注册工具栏按钮
15
- - 通过 `editor.execute('formulaX')` 在代码中主动打开
16
- - 将公式作为行内 widget object 插入和更新
17
- - 支持双击编辑已有公式
18
- - 在 CKEditor 5 model 中仅持久化 LaTeX 源内容
19
- - 在 editing view 中运行时渲染 SVG
20
- - 支持在编辑器数据中对公式 markup 进行 upcast 和 downcast
21
- - 直接导出弹窗工具函数 `openFormulaXModal`
22
-
23
- ## 兼容性
24
-
25
- 该包将 CKEditor 5 声明为可选 peer dependency:
26
-
27
- ```json
28
- {
29
- "ckeditor5": ">=42 <49"
30
- }
31
- ```
32
-
33
- 当前工作空间中的 demo 使用的是 CKEditor 5 `46.1.1`。
34
-
35
- ## 安装
36
-
37
- 包发布后可使用:
38
-
39
- ```bash
40
- pnpm add @formulaxjs/ckeditor5
41
- pnpm add ckeditor5
42
- ```
43
-
44
- 在 FormulaX 工作空间内,直接使用 workspace 包:
45
-
46
- ```bash
47
- pnpm install
48
- pnpm dev:ckeditor5
49
- ```
50
-
51
- ## 基础使用
52
-
53
- 将 `FormulaX` 加入 CKEditor 5 的插件列表,并配置对应的工具栏项:
54
-
55
- ```ts
56
- import {
57
- ClassicEditor,
58
- Essentials,
59
- Paragraph,
60
- } from 'ckeditor5';
61
- import 'ckeditor5/ckeditor5.css';
62
-
63
- import { FormulaX } from '@formulaxjs/ckeditor5';
64
-
65
- await ClassicEditor.create(document.querySelector('#editor')!, {
66
- licenseKey: 'GPL',
67
- plugins: [
68
- Essentials,
69
- Paragraph,
70
- FormulaX,
71
- ],
72
- toolbar: ['formulaX'],
73
- formulaX: {
74
- toolbarText: 'FormulaX',
75
- tooltip: '插入或编辑公式',
76
- modal: {
77
- title: 'FormulaX 公式编辑器',
78
- },
79
- editor: {
80
- render: {
81
- fontsize: 40,
82
- },
83
- },
84
- },
85
- } as any);
86
- ```
87
-
88
- 之后用户可以点击 `FormulaX` 工具栏按钮插入公式。已有公式可以通过双击更新,也可以先选中后再次执行同一个命令进行更新。
89
-
90
- ## 自定义 model 名称
91
-
92
- 默认的 CKEditor 5 model 名称是 `formulaX`。
93
-
94
- 如果宿主编辑器里已经存在同名 model,可以在 `formulaX` 配置里传入自定义 `name`:
95
-
96
- ```ts
97
- await ClassicEditor.create(document.querySelector('#editor')!, {
98
- plugins: [Essentials, Paragraph, FormulaX],
99
- toolbar: ['formulaX'],
100
- formulaX: {
101
- name: 'inlineMath',
102
- },
103
- } as any);
104
- ```
105
-
106
- 如果 CKEditor 5 检测到配置的 model 名称已经被注册,插件会输出一条 `console.error`,并跳过本次注册,避免悄悄创建冲突的 schema 定义。
107
-
108
- ## 代码中主动打开
109
-
110
- 插件注册的命令名与 `buttonName` 保持一致。默认配置下可直接调用:
111
-
112
- ```ts
113
- editor.execute('formulaX');
114
- ```
115
-
116
- ## 自定义按钮名称
117
-
118
- 默认的工具栏按钮名和命令名都是 `formulaX`。
119
-
120
- 如果自定义它,需要确保 CKEditor 5 的 `toolbar` 配置与自定义名称保持一致:
121
-
122
- ```ts
123
- await ClassicEditor.create(document.querySelector('#editor')!, {
124
- plugins: [Essentials, Paragraph, FormulaX],
125
- toolbar: ['myFormulaX'],
126
- formulaX: {
127
- buttonName: 'myFormulaX',
128
- toolbarText: 'FormulaX',
129
- },
130
- } as any);
131
- ```
132
-
133
- 代码中主动打开时,也要使用同一个自定义命令名:
134
-
135
- ```ts
136
- editor.execute('myFormulaX');
137
- ```
138
-
139
- ## 持久化数据与 markup
140
-
141
- CKEditor 5 的 model 层只保存公式的 LaTeX 源内容:
142
-
143
- ```ts
144
- <formulaX latex="\\sqrt{x}" />
145
- ```
146
-
147
- 当编辑器数据 downcast 为 HTML 时,生成的公式节点默认会通过 `data-formulax="true"` 标记,并将 LaTeX 源内容保存在 `data-formulax-latex` 中:
148
-
149
- ```html
150
- <span
151
- class="formulax-math"
152
- data-formulax="true"
153
- data-formulax-latex="\\sqrt{x}"
154
- data-latex="\\sqrt{x}"
155
- contenteditable="false"
156
- role="button"
157
- style="cursor: pointer"
158
- tabindex="0"
159
- ></span>
160
- ```
161
-
162
- 编辑态中的 SVG 由持久化的 LaTeX 在运行时生成。用于渲染公式的内部 HTML 结构属于实现细节,后续可能演进。业务侧应优先依赖插件能力和导出的配置项,而不是写死完整 markup 结构。
163
-
164
- ## 配置项
165
-
166
- ```ts
167
- interface FormulaXCKEditor5Options {
168
- name?: string;
169
- buttonName?: string;
170
- toolbarText?: string;
171
- tooltip?: string;
172
- cursorStyle?: string;
173
- formulaClassName?: string;
174
- formulaAttributeName?: string;
175
- modal?: {
176
- title?: string;
177
- insertText?: string;
178
- updateText?: string;
179
- cancelText?: string;
180
- closeOnBackdrop?: boolean;
181
- };
182
- editor?: {
183
- height?: number | string;
184
- autofocus?: boolean;
185
- assets?: Partial<KityEditorAssets>;
186
- render?: {
187
- fontsize?: number;
188
- };
189
- };
190
- }
191
- ```
192
-
193
- | 配置项 | 默认值 | 说明 |
194
- | --- | --- | --- |
195
- | `name` | `formulaX` | 用于持久化公式节点的 CKEditor 5 model/schema 元素名。 |
196
- | `buttonName` | `formulaX` | CKEditor 5 的工具栏按钮名,同时也是命令名。 |
197
- | `toolbarText` | `FormulaX` | 工具栏按钮显示文本。 |
198
- | `tooltip` | `Insert or edit formula` | 工具栏按钮 tooltip。 |
199
- | `cursorStyle` | `pointer` | 应用于生成公式节点的鼠标光标样式。 |
200
- | `formulaClassName` | `formulax-math` | 生成的公式节点 CSS class。 |
201
- | `formulaAttributeName` | `data-formulax-latex` | 用于保存 LaTeX 源内容的属性。 |
202
- | `modal` | 见下方 | 弹窗文案和关闭行为。 |
203
- | `editor` | 见下方 | 内嵌 FormulaX 编辑器配置。 |
204
-
205
- ### Modal 配置
206
-
207
- | 配置项 | 默认值 | 说明 |
208
- | --- | --- | --- |
209
- | `title` | `FormulaX Editor` | 弹窗标题。 |
210
- | `insertText` | `Insert` | 插入公式时的提交按钮文本。 |
211
- | `updateText` | `Update` | 更新公式时的提交按钮文本。 |
212
- | `cancelText` | `Cancel` | 取消按钮文本。 |
213
- | `closeOnBackdrop` | `true` | 点击遮罩层时是否关闭弹窗。 |
214
-
215
- ### Editor 配置
216
-
217
- | 配置项 | 默认值 | 说明 |
218
- | --- | --- | --- |
219
- | `height` | `100%` | 内嵌编辑器高度。 |
220
- | `autofocus` | `true` | 内嵌编辑器是否自动聚焦。 |
221
- | `assets` | `{}` | 可选的 Kity runtime 资源覆盖配置。 |
222
- | `render.fontsize` | `40` | 公式渲染字号。 |
223
-
224
- ## 导出 API
225
-
226
- | 导出 | 说明 |
227
- | --- | --- |
228
- | `FormulaX` | CKEditor 5 插件类。 |
229
- | `default` | 与 `FormulaX` 相同。 |
230
- | `FormulaXCommand` | 插件内部使用的命令实现。 |
231
- | `resolveOptions` | 将用户配置与默认配置合并为完整配置。 |
232
- | `openFormulaXModal` | 直接打开 FormulaX 弹窗。 |
233
- | `DEFAULT_MODEL_NAME` | 默认的 CKEditor 5 model 名称。 |
234
- | `DEFAULT_BUTTON_NAME` | 默认的 CKEditor 5 命令名和工具栏按钮名。 |
235
- | `DEFAULT_FORMULA_ATTRIBUTE` | 默认的 LaTeX 持久化属性名。 |
236
- | `DEFAULT_FORMULA_CLASS` | 默认的公式节点 CSS class。 |
237
- | `FORMULA_FLAG_ATTRIBUTE` | 用于在编辑器数据中识别 FormulaX 节点的属性。 |
238
-
239
- ## 开发
240
-
241
- 在仓库根目录执行:
242
-
243
- ```bash
244
- pnpm install
245
- pnpm dev:ckeditor5
246
- ```
247
-
248
- 仅构建该包:
249
-
250
- ```bash
251
- pnpm --filter @formulaxjs/ckeditor5 build
252
- ```
253
-
254
- 运行该包类型检查:
255
-
256
- ```bash
257
- pnpm --filter @formulaxjs/ckeditor5 typecheck
258
- ```
259
-
260
- ## Demo
261
-
262
- 本地 demo:
263
-
264
- ```bash
265
- pnpm dev:ckeditor5
266
- ```
267
-
268
- GitHub Pages demo:
269
-
270
- [https://vndmea.github.io/formulaX/ckeditor5/](https://vndmea.github.io/formulaX/ckeditor5/)
1
+ # @formulaxjs/ckeditor5
2
+
3
+ [English](./README.md) | 简体中文
4
+
5
+ FormulaX 的 CKEditor 5 集成适配器。
6
+
7
+ `@formulaxjs/ckeditor5` 提供了一个 CKEditor 5 插件,会以弹窗形式打开 FormulaX 编辑器,用于在 CKEditor 5 内容中插入和更新行内公式。
8
+
9
+ > 状态:实验阶段。在首个稳定版本发布前,公共 API 仍可能调整。
10
+
11
+ ## 功能特性
12
+
13
+ - 通过 `FormulaX` 导出 CKEditor 5 插件
14
+ - 基于 CKEditor 5 `componentFactory` 注册工具栏按钮
15
+ - 通过 `editor.execute('formulaX')` 在代码中主动打开
16
+ - 将公式作为行内 widget object 插入和更新
17
+ - 支持双击编辑已有公式
18
+ - 在 CKEditor 5 model 中仅持久化 LaTeX 源内容
19
+ - 在 editing view 中运行时渲染 SVG
20
+ - 默认通过 `@formulaxjs/renderer-kity` 完成只读渲染
21
+ - 支持在首次打开弹窗前预加载 runtime
22
+ - 支持在编辑器数据中对公式 markup 进行 upcast 和 downcast
23
+ - 直接导出弹窗工具函数 `openFormulaXModal`
24
+
25
+ ## 兼容性
26
+
27
+ 该包将 CKEditor 5 声明为可选 peer dependency:
28
+
29
+ ```json
30
+ {
31
+ "ckeditor5": ">=42 <49"
32
+ }
33
+ ```
34
+
35
+ 当前工作空间中的 demo 使用的是 CKEditor 5 `46.1.1`。
36
+
37
+ ## 安装
38
+
39
+ 包发布后可使用:
40
+
41
+ ```bash
42
+ npm install @formulaxjs/ckeditor5 ckeditor5
43
+ ```
44
+
45
+ 在 FormulaX 工作空间内,直接使用 workspace 包:
46
+
47
+ ```bash
48
+ pnpm install
49
+ pnpm dev:ckeditor5
50
+ ```
51
+
52
+ ## 基础使用
53
+
54
+ 将 `FormulaX` 加入 CKEditor 5 的插件列表,并配置对应的工具栏项:
55
+
56
+ ```ts
57
+ import {
58
+ ClassicEditor,
59
+ Essentials,
60
+ Paragraph,
61
+ } from 'ckeditor5';
62
+ import 'ckeditor5/ckeditor5.css';
63
+
64
+ import { FormulaX } from '@formulaxjs/ckeditor5';
65
+ import { createKityFormulaRenderer } from '@formulaxjs/renderer-kity';
66
+
67
+ await ClassicEditor.create(document.querySelector('#editor')!, {
68
+ licenseKey: 'GPL',
69
+ plugins: [
70
+ Essentials,
71
+ Paragraph,
72
+ FormulaX,
73
+ ],
74
+ toolbar: ['formulaX'],
75
+ formulaX: {
76
+ toolbarText: 'FormulaX',
77
+ tooltip: '插入或编辑公式',
78
+ modal: {
79
+ title: 'FormulaX 公式编辑器',
80
+ },
81
+ renderer: createKityFormulaRenderer({
82
+ fontSize: 40,
83
+ }),
84
+ editor: {
85
+ render: {
86
+ fontsize: 40,
87
+ },
88
+ },
89
+ },
90
+ } as any);
91
+ ```
92
+
93
+ 之后用户可以点击 `FormulaX` 工具栏按钮插入公式。已有公式可以通过双击更新,也可以先选中后再次执行同一个命令进行更新。
94
+
95
+ ## 自定义 model 名称
96
+
97
+ 默认的 CKEditor 5 model 名称是 `formulaX`。
98
+
99
+ 如果宿主编辑器里已经存在同名 model,可以在 `formulaX` 配置里传入自定义 `name`:
100
+
101
+ ```ts
102
+ await ClassicEditor.create(document.querySelector('#editor')!, {
103
+ plugins: [Essentials, Paragraph, FormulaX],
104
+ toolbar: ['formulaX'],
105
+ formulaX: {
106
+ name: 'inlineMath',
107
+ },
108
+ } as any);
109
+ ```
110
+
111
+ 如果 CKEditor 5 检测到配置的 model 名称已经被注册,插件会输出一条 `console.error`,并跳过本次注册,避免悄悄创建冲突的 schema 定义。
112
+
113
+ ## 代码中主动打开
114
+
115
+ 插件注册的命令名与 `buttonName` 保持一致。默认配置下可直接调用:
116
+
117
+ ```ts
118
+ editor.execute('formulaX');
119
+ ```
120
+
121
+ ## 自定义按钮名称
122
+
123
+ 默认的工具栏按钮名和命令名都是 `formulaX`。
124
+
125
+ 如果自定义它,需要确保 CKEditor 5 的 `toolbar` 配置与自定义名称保持一致:
126
+
127
+ ```ts
128
+ await ClassicEditor.create(document.querySelector('#editor')!, {
129
+ plugins: [Essentials, Paragraph, FormulaX],
130
+ toolbar: ['myFormulaX'],
131
+ formulaX: {
132
+ buttonName: 'myFormulaX',
133
+ toolbarText: 'FormulaX',
134
+ },
135
+ } as any);
136
+ ```
137
+
138
+ 代码中主动打开时,也要使用同一个自定义命令名:
139
+
140
+ ```ts
141
+ editor.execute('myFormulaX');
142
+ ```
143
+
144
+ ## 持久化数据与 markup
145
+
146
+ CKEditor 5 的 model 层只保存公式的 LaTeX 源内容:
147
+
148
+ ```ts
149
+ <formulaX latex="\\sqrt{x}" />
150
+ ```
151
+
152
+ 当编辑器数据 downcast 为 HTML 时,生成的公式节点默认会通过 `data-formulax="true"` 标记,并将 LaTeX 源内容保存在 `data-formulax-latex` 中:
153
+
154
+ ```html
155
+ <span
156
+ class="formulax-math"
157
+ data-formulax="true"
158
+ data-formulax-latex="\\sqrt{x}"
159
+ data-latex="\\sqrt{x}"
160
+ contenteditable="false"
161
+ role="button"
162
+ style="cursor: pointer"
163
+ tabindex="0"
164
+ ></span>
165
+ ```
166
+
167
+ 编辑态中的 SVG 由持久化的 LaTeX 在运行时生成。用于渲染公式的内部 HTML 结构属于实现细节,后续可能演进。业务侧应优先依赖插件能力和导出的配置项,而不是写死完整 markup 结构。
168
+
169
+ ## 自定义 renderer
170
+
171
+ 该适配器支持 `renderer` 配置项。默认值是来自 `@formulaxjs/renderer-kity` 的 `createKityFormulaRenderer()`。
172
+
173
+ ## 配置项
174
+
175
+ ```ts
176
+ interface FormulaXCKEditor5Options {
177
+ name?: string;
178
+ buttonName?: string;
179
+ toolbarText?: string;
180
+ tooltip?: string;
181
+ cursorStyle?: string;
182
+ formulaClassName?: string;
183
+ formulaAttributeName?: string;
184
+ renderer?: FormulaRenderer;
185
+ preload?: FormulaXEditorPreloadMode;
186
+ modal?: {
187
+ title?: string;
188
+ insertText?: string;
189
+ updateText?: string;
190
+ cancelText?: string;
191
+ closeOnBackdrop?: boolean;
192
+ };
193
+ editor?: {
194
+ height?: number | string;
195
+ autofocus?: boolean;
196
+ assets?: Partial<KityEditorAssets>;
197
+ render?: {
198
+ fontsize?: number;
199
+ };
200
+ };
201
+ }
202
+ ```
203
+
204
+ | 配置项 | 默认值 | 说明 |
205
+ | --- | --- | --- |
206
+ | `name` | `formulaX` | 用于持久化公式节点的 CKEditor 5 model/schema 元素名。 |
207
+ | `buttonName` | `formulaX` | CKEditor 5 的工具栏按钮名,同时也是命令名。 |
208
+ | `toolbarText` | `FormulaX` | 工具栏按钮显示文本。 |
209
+ | `tooltip` | `Insert or edit formula` | 工具栏按钮 tooltip。 |
210
+ | `cursorStyle` | `pointer` | 应用于生成公式节点的鼠标光标样式。 |
211
+ | `formulaClassName` | `formulax-math` | 生成的公式节点 CSS class。 |
212
+ | `formulaAttributeName` | `data-formulax-latex` | 用于保存 LaTeX 源内容的属性。 |
213
+ | `renderer` | `createKityFormulaRenderer()` | editing view 中运行时 SVG 输出使用的 renderer。 |
214
+ | `preload` | `idle` | 控制在浏览器空闲时、宿主 hover/focus 时,或完全不预加载 FormulaX runtime。 |
215
+ | `modal` | 见下方 | 弹窗文案和关闭行为。 |
216
+ | `editor` | 见下方 | 内嵌 FormulaX 编辑器配置。 |
217
+
218
+ ### Modal 配置
219
+
220
+ | 配置项 | 默认值 | 说明 |
221
+ | --- | --- | --- |
222
+ | `title` | `FormulaX Editor` | 弹窗标题。 |
223
+ | `insertText` | `Insert` | 插入公式时的提交按钮文本。 |
224
+ | `updateText` | `Update` | 更新公式时的提交按钮文本。 |
225
+ | `cancelText` | `Cancel` | 取消按钮文本。 |
226
+ | `closeOnBackdrop` | `true` | 点击遮罩层时是否关闭弹窗。 |
227
+
228
+ ### Editor 配置
229
+
230
+ | 配置项 | 默认值 | 说明 |
231
+ | --- | --- | --- |
232
+ | `height` | `100%` | 内嵌编辑器高度。 |
233
+ | `autofocus` | `true` | 内嵌编辑器是否自动聚焦。 |
234
+ | `assets` | `{}` | 可选的 Kity runtime 资源覆盖配置。 |
235
+ | `render.fontsize` | `40` | 公式渲染字号。 |
236
+
237
+ ## 导出 API
238
+
239
+ | 导出 | 说明 |
240
+ | --- | --- |
241
+ | `FormulaX` | CKEditor 5 插件类。 |
242
+ | `default` | 与 `FormulaX` 相同。 |
243
+ | `FormulaXCommand` | 插件内部使用的命令实现。 |
244
+ | `resolveOptions` | 将用户配置与默认配置合并为完整配置。 |
245
+ | `openFormulaXModal` | 直接打开 FormulaX 弹窗。 |
246
+ | `DEFAULT_MODEL_NAME` | 默认的 CKEditor 5 model 名称。 |
247
+ | `DEFAULT_BUTTON_NAME` | 默认的 CKEditor 5 命令名和工具栏按钮名。 |
248
+ | `DEFAULT_FORMULA_ATTRIBUTE` | 默认的 LaTeX 持久化属性名。 |
249
+ | `DEFAULT_FORMULA_CLASS` | 默认的公式节点 CSS class。 |
250
+ | `FORMULA_FLAG_ATTRIBUTE` | 用于在编辑器数据中识别 FormulaX 节点的属性。 |
251
+
252
+ ## 开发
253
+
254
+ 在仓库根目录执行:
255
+
256
+ ```bash
257
+ pnpm install
258
+ pnpm dev:ckeditor5
259
+ ```
260
+
261
+ 仅构建该包:
262
+
263
+ ```bash
264
+ pnpm --filter @formulaxjs/ckeditor5 build
265
+ ```
266
+
267
+ 运行该包类型检查:
268
+
269
+ ```bash
270
+ pnpm --filter @formulaxjs/ckeditor5 typecheck
271
+ ```
272
+
273
+ ## Demo
274
+
275
+ 本地 demo:
276
+
277
+ ```bash
278
+ pnpm dev:ckeditor5
279
+ ```
280
+
281
+ GitHub Pages demo:
282
+
283
+ [https://vndmea.github.io/formulaX/ckeditor5/](https://vndmea.github.io/formulaX/ckeditor5/)