@formulaxjs/tiptap 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,215 +1,235 @@
1
- # @formulaxjs/tiptap
2
-
3
- [English](./README.md) | 简体中文
4
-
5
- FormulaX 的 Tiptap 集成适配器。
6
-
7
- `@formulaxjs/tiptap` 提供了一个 FormulaX 行内节点扩展和基于弹窗的公式编辑流程。该扩展在文档模型中只持久化 LaTeX,并在运行时渲染公式展示结果。
8
-
9
- > 状态:实验阶段。在首个稳定版本发布前,公共 API 仍可能调整。
10
-
11
- ## 功能特性
12
-
13
- - 通过 `FormulaXNode` 导出 Tiptap 节点扩展
14
- - 通过 `createFormulaXNode` 导出扩展工厂函数
15
- - 提供 `openFormulaX` 命令,便于接工具栏按钮或代码中主动打开
16
- - 支持双击编辑已有公式
17
- - 节点 attrs 中仅持久化 LaTeX
18
- - 在 node view 中运行时渲染 SVG
19
- - 直接导出弹窗工具函数 `openFormulaXTiptapModal`
20
- - 兼容 Tiptap 2 和 3 的 peer dependency 范围
21
-
22
- ## 兼容性
23
-
24
- 该包将 `@tiptap/core` 声明为可选 peer dependency:
25
-
26
- ```json
27
- {
28
- "@tiptap/core": ">=2 <4"
29
- }
30
- ```
31
-
32
- 工作空间中的 demo 可以在 Tiptap 2 和 3 之间切换,用于兼容性验证。
33
-
34
- ## 安装
35
-
36
- 包发布后可使用:
37
-
38
- ```bash
39
- pnpm add @formulaxjs/tiptap
40
- pnpm add @tiptap/core
41
- ```
42
-
43
- 在 FormulaX 工作空间内,直接使用 workspace 包:
44
-
45
- ```bash
46
- pnpm install
47
- pnpm dev:tiptap
48
- ```
49
-
50
- ## 基础使用
51
-
52
- 创建 FormulaX 节点扩展,并加入 Tiptap 的扩展列表:
53
-
54
- ```ts
55
- import { Editor } from '@tiptap/core';
56
- import StarterKit from '@tiptap/starter-kit';
57
- import { createFormulaXNode } from '@formulaxjs/tiptap';
58
-
59
- const editor = new Editor({
60
- element: document.querySelector('#editor')!,
61
- extensions: [
62
- StarterKit,
63
- createFormulaXNode(),
64
- ],
65
- content: '<p>点击工具栏按钮插入公式。</p>',
66
- });
67
- ```
68
-
69
- 如果要在代码中主动打开 FormulaX 弹窗:
70
-
71
- ```ts
72
- editor.commands.openFormulaX();
73
- ```
74
-
75
- ## 自定义节点名称
76
-
77
- 默认的 Tiptap 节点名是 `formulaX`。
78
-
79
- 如果宿主编辑器里已经存在同名节点,可以在创建扩展时传入自定义 `name`:
80
-
81
- ```ts
82
- const editor = new Editor({
83
- element: document.querySelector('#editor')!,
84
- extensions: [
85
- StarterKit,
86
- createFormulaXNode(undefined, {
87
- name: 'inlineMath',
88
- }),
89
- ],
90
- });
91
- ```
92
-
93
- 如果 Tiptap 检测到配置的节点名已经被注册,扩展会输出一条 `console.warn`,提醒你尽快改成唯一名称,避免 schema 冲突。
94
-
95
- ## 持久化数据
96
-
97
- Tiptap 节点中只保存 LaTeX 源内容:
98
-
99
- ```json
100
- {
101
- "type": "formulaX",
102
- "attrs": {
103
- "latex": "\\sqrt{x}"
104
- }
105
- }
106
- ```
107
-
108
- 节点视图会根据保存的 LaTeX 在运行时渲染 SVG。生成的 DOM 会带有 `data-formulax="true"` 和 `data-formulax-latex`,但这些渲染后的 DOM 不是持久化数据的真实来源。
109
-
110
- ## 配置项
111
-
112
- ```ts
113
- interface FormulaXTiptapOptions {
114
- name?: string;
115
- formulaClassName?: string;
116
- formulaAttributeName?: string;
117
- cursorStyle?: string;
118
- initialLatex?: string;
119
- modal?: {
120
- title?: string;
121
- insertText?: string;
122
- updateText?: string;
123
- cancelText?: string;
124
- closeOnBackdrop?: boolean;
125
- };
126
- editor?: {
127
- height?: number | string;
128
- autofocus?: boolean;
129
- assets?: Partial<KityEditorAssets>;
130
- render?: {
131
- fontsize?: number;
132
- };
133
- };
134
- }
135
- ```
136
-
137
- | 配置项 | 默认值 | 说明 |
138
- | --- | --- | --- |
139
- | `name` | `formulaX` | 用于文档 schema 的 Tiptap 节点名。 |
140
- | `formulaClassName` | `formulax-math` | 渲染后公式节点使用的 CSS class。 |
141
- | `formulaAttributeName` | `data-formulax-latex` | 渲染后 DOM 中保存 LaTeX 源内容的属性名。 |
142
- | `cursorStyle` | `pointer` | 渲染后公式节点的鼠标光标样式。 |
143
- | `initialLatex` | 空字符串 | 插入新公式时的初始 LaTeX。 |
144
- | `modal` | 见下方 | 弹窗文案和关闭行为。 |
145
- | `editor` | 见下方 | 内嵌 FormulaX 编辑器配置。 |
146
-
147
- ### Modal 配置
148
-
149
- | 配置项 | 默认值 | 说明 |
150
- | --- | --- | --- |
151
- | `title` | `FormulaX Editor` | 弹窗标题。 |
152
- | `insertText` | `Insert` | 插入公式时的提交按钮文本。 |
153
- | `updateText` | `Update` | 更新公式时的提交按钮文本。 |
154
- | `cancelText` | `Cancel` | 取消按钮文本。 |
155
- | `closeOnBackdrop` | `true` | 点击遮罩层时是否关闭弹窗。 |
156
-
157
- ### Editor 配置
158
-
159
- | 配置项 | 默认值 | 说明 |
160
- | --- | --- | --- |
161
- | `height` | `100%` | 内嵌编辑器高度。 |
162
- | `autofocus` | `true` | 内嵌编辑器是否自动聚焦。 |
163
- | `assets` | `{}` | 可选的 Kity runtime 资源覆盖配置。 |
164
- | `render.fontsize` | `40` | 公式渲染字号。 |
165
-
166
- ## 导出 API
167
-
168
- | 导出 | 说明 |
169
- | --- | --- |
170
- | `FormulaXNode` | 默认的 FormulaX Tiptap 节点扩展。 |
171
- | `createFormulaXNode` | 创建 FormulaX 节点扩展,可传入自定义配置。 |
172
- | `resolveOptions` | 将用户配置与默认配置合并为完整配置。 |
173
- | `openFormulaXTiptapModal` | 直接打开 FormulaX 弹窗。 |
174
- | `FORMULAX_NODE_NAME` | 默认的 Tiptap 节点名。 |
175
- | `createFormulaXPayload` | LaTeX 解析为 FormulaX 文档。 |
176
- | `serializeFormulaXPayload` | 将 FormulaX 文档序列化回 LaTeX。 |
177
-
178
- ## 开发
179
-
180
- 在仓库根目录执行:
181
-
182
- ```bash
183
- pnpm install
184
- pnpm dev:tiptap
185
- ```
186
-
187
- 仅构建该包:
188
-
189
- ```bash
190
- pnpm --filter @formulaxjs/tiptap build
191
- ```
192
-
193
- 运行该包测试:
194
-
195
- ```bash
196
- pnpm --filter @formulaxjs/tiptap test
197
- ```
198
-
199
- 运行该包类型检查:
200
-
201
- ```bash
202
- pnpm --filter @formulaxjs/tiptap typecheck
203
- ```
204
-
205
- ## Demo
206
-
207
- 本地 demo:
208
-
209
- ```bash
210
- pnpm dev:tiptap
211
- ```
212
-
213
- GitHub Pages demo:
214
-
215
- [https://vndmea.github.io/formulaX/tiptap/](https://vndmea.github.io/formulaX/tiptap/)
1
+ # @formulaxjs/tiptap
2
+
3
+ [English](./README.md) | 简体中文
4
+
5
+ FormulaX 的 Tiptap 集成适配器。
6
+
7
+ `@formulaxjs/tiptap` 提供了一个 FormulaX 行内节点扩展和基于弹窗的公式编辑流程。该扩展在文档模型中只持久化 LaTeX,并通过共享的 FormulaX renderer 接口在运行时渲染公式展示结果。
8
+
9
+ > 状态:实验阶段。在首个稳定版本发布前,公共 API 仍可能调整。
10
+
11
+ ## 功能特性
12
+
13
+ - 通过 `FormulaXNode` 导出 Tiptap 节点扩展
14
+ - 通过 `createFormulaXNode` 导出扩展工厂函数
15
+ - 提供 `openFormulaX` 命令,便于接工具栏按钮或代码中主动打开
16
+ - 支持双击编辑已有公式
17
+ - 节点 attrs 中仅持久化 LaTeX
18
+ - 在 node view 中运行时渲染 SVG
19
+ - 默认通过 `@formulaxjs/renderer-kity` 完成只读渲染
20
+ - 支持在首次打开弹窗前预加载 runtime
21
+ - 直接导出弹窗工具函数 `openFormulaXTiptapModal`
22
+ - 兼容 Tiptap 2 和 3 的 peer dependency 范围
23
+
24
+ ## 兼容性
25
+
26
+ 该包将 `@tiptap/core` 声明为可选 peer dependency:
27
+
28
+ ```json
29
+ {
30
+ "@tiptap/core": ">=2 <4"
31
+ }
32
+ ```
33
+
34
+ 工作空间中的 demo 可以在 Tiptap 2 和 3 之间切换,用于兼容性验证。
35
+
36
+ ## 安装
37
+
38
+ 包发布后可使用:
39
+
40
+ ```bash
41
+ npm install @formulaxjs/tiptap @tiptap/core
42
+ ```
43
+
44
+ 在 FormulaX 工作空间内,直接使用 workspace 包:
45
+
46
+ ```bash
47
+ pnpm install
48
+ pnpm dev:tiptap
49
+ ```
50
+
51
+ ## 基础使用
52
+
53
+ 创建 FormulaX 节点扩展,并加入 Tiptap 的扩展列表:
54
+
55
+ ```ts
56
+ import { Editor } from '@tiptap/core';
57
+ import StarterKit from '@tiptap/starter-kit';
58
+ import { createFormulaXNode } from '@formulaxjs/tiptap';
59
+
60
+ const editor = new Editor({
61
+ element: document.querySelector('#editor')!,
62
+ extensions: [
63
+ StarterKit,
64
+ createFormulaXNode(),
65
+ ],
66
+ content: '<p>点击工具栏按钮插入公式。</p>',
67
+ });
68
+ ```
69
+
70
+ 如果要在代码中主动打开 FormulaX 弹窗:
71
+
72
+ ```ts
73
+ editor.commands.openFormulaX();
74
+ ```
75
+
76
+ ## 自定义节点名称
77
+
78
+ 默认的 Tiptap 节点名是 `formulaX`。
79
+
80
+ 如果宿主编辑器里已经存在同名节点,可以在创建扩展时传入自定义 `name`:
81
+
82
+ ```ts
83
+ const editor = new Editor({
84
+ element: document.querySelector('#editor')!,
85
+ extensions: [
86
+ StarterKit,
87
+ createFormulaXNode(undefined, {
88
+ name: 'inlineMath',
89
+ }),
90
+ ],
91
+ });
92
+ ```
93
+
94
+ 如果 Tiptap 检测到配置的节点名已经被注册,扩展会输出一条 `console.warn`,提醒你尽快改成唯一名称,避免 schema 冲突。
95
+
96
+ ## 持久化数据
97
+
98
+ Tiptap 节点中只保存 LaTeX 源内容:
99
+
100
+ ```json
101
+ {
102
+ "type": "formulaX",
103
+ "attrs": {
104
+ "latex": "\\sqrt{x}"
105
+ }
106
+ }
107
+ ```
108
+
109
+ 节点视图会根据保存的 LaTeX 在运行时渲染 SVG。生成的 DOM 会带有 `data-formulax="true"` 和 `data-formulax-latex`,但这些渲染后的 DOM 不是持久化数据的真实来源。
110
+
111
+ ## 自定义 renderer
112
+
113
+ 该适配器支持 `renderer` 配置项。默认值是来自 `@formulaxjs/renderer-kity` 的 `createKityFormulaRenderer()`。
114
+
115
+ ```ts
116
+ import { createFormulaXNode } from '@formulaxjs/tiptap';
117
+ import { createKityFormulaRenderer } from '@formulaxjs/renderer-kity';
118
+
119
+ const formulaNode = createFormulaXNode(undefined, {
120
+ renderer: createKityFormulaRenderer({
121
+ fontSize: 44,
122
+ }),
123
+ });
124
+ ```
125
+
126
+ ## 配置项
127
+
128
+ ```ts
129
+ interface FormulaXTiptapOptions {
130
+ name?: string;
131
+ formulaClassName?: string;
132
+ formulaAttributeName?: string;
133
+ cursorStyle?: string;
134
+ initialLatex?: string;
135
+ renderer?: FormulaRenderer;
136
+ preload?: FormulaXEditorPreloadMode;
137
+ modal?: {
138
+ title?: string;
139
+ insertText?: string;
140
+ updateText?: string;
141
+ cancelText?: string;
142
+ closeOnBackdrop?: boolean;
143
+ };
144
+ editor?: {
145
+ height?: number | string;
146
+ autofocus?: boolean;
147
+ assets?: Partial<KityEditorAssets>;
148
+ render?: {
149
+ fontsize?: number;
150
+ };
151
+ };
152
+ }
153
+ ```
154
+
155
+ | 配置项 | 默认值 | 说明 |
156
+ | --- | --- | --- |
157
+ | `name` | `formulaX` | 用于文档 schema 的 Tiptap 节点名。 |
158
+ | `formulaClassName` | `formulax-math` | 渲染后公式节点使用的 CSS class。 |
159
+ | `formulaAttributeName` | `data-formulax-latex` | 渲染后 DOM 中保存 LaTeX 源内容的属性名。 |
160
+ | `cursorStyle` | `pointer` | 渲染后公式节点的鼠标光标样式。 |
161
+ | `initialLatex` | 空字符串 | 插入新公式时的初始 LaTeX。 |
162
+ | `renderer` | `createKityFormulaRenderer()` | node view 中用于只读公式输出的 renderer。 |
163
+ | `preload` | `idle` | 控制在浏览器空闲时、宿主 hover/focus 时,或完全不预加载 FormulaX runtime |
164
+ | `modal` | 见下方 | 弹窗文案和关闭行为。 |
165
+ | `editor` | 见下方 | 内嵌 FormulaX 编辑器配置。 |
166
+
167
+ ### Modal 配置
168
+
169
+ | 配置项 | 默认值 | 说明 |
170
+ | --- | --- | --- |
171
+ | `title` | `FormulaX Editor` | 弹窗标题。 |
172
+ | `insertText` | `Insert` | 插入公式时的提交按钮文本。 |
173
+ | `updateText` | `Update` | 更新公式时的提交按钮文本。 |
174
+ | `cancelText` | `Cancel` | 取消按钮文本。 |
175
+ | `closeOnBackdrop` | `true` | 点击遮罩层时是否关闭弹窗。 |
176
+
177
+ ### Editor 配置
178
+
179
+ | 配置项 | 默认值 | 说明 |
180
+ | --- | --- | --- |
181
+ | `height` | `100%` | 内嵌编辑器高度。 |
182
+ | `autofocus` | `true` | 内嵌编辑器是否自动聚焦。 |
183
+ | `assets` | `{}` | 可选的 Kity runtime 资源覆盖配置。 |
184
+ | `render.fontsize` | `40` | 公式渲染字号。 |
185
+
186
+ ## 导出 API
187
+
188
+ | 导出 | 说明 |
189
+ | --- | --- |
190
+ | `FormulaXNode` | 默认的 FormulaX Tiptap 节点扩展。 |
191
+ | `createFormulaXNode` | 创建 FormulaX 节点扩展,可传入自定义配置。 |
192
+ | `resolveOptions` | 将用户配置与默认配置合并为完整配置。 |
193
+ | `openFormulaXTiptapModal` | 直接打开 FormulaX 弹窗。 |
194
+ | `FORMULAX_NODE_NAME` | 默认的 Tiptap 节点名。 |
195
+ | `createFormulaXPayload` | 将 LaTeX 解析为 FormulaX 文档。 |
196
+ | `serializeFormulaXPayload` | 将 FormulaX 文档序列化回 LaTeX。 |
197
+
198
+ ## 开发
199
+
200
+ 在仓库根目录执行:
201
+
202
+ ```bash
203
+ pnpm install
204
+ pnpm dev:tiptap
205
+ ```
206
+
207
+ 仅构建该包:
208
+
209
+ ```bash
210
+ pnpm --filter @formulaxjs/tiptap build
211
+ ```
212
+
213
+ 运行该包测试:
214
+
215
+ ```bash
216
+ pnpm --filter @formulaxjs/tiptap test
217
+ ```
218
+
219
+ 运行该包类型检查:
220
+
221
+ ```bash
222
+ pnpm --filter @formulaxjs/tiptap typecheck
223
+ ```
224
+
225
+ ## Demo
226
+
227
+ 本地 demo:
228
+
229
+ ```bash
230
+ pnpm dev:tiptap
231
+ ```
232
+
233
+ GitHub Pages demo:
234
+
235
+ [https://vndmea.github.io/formulaX/tiptap/](https://vndmea.github.io/formulaX/tiptap/)
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,3 @@
1
+ @import url('./base.css');
2
+ @import url('./ui.css');
3
+ @import url('./scrollbar.css');