@formulaxjs/tiptap 0.2.2 → 0.3.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 CHANGED
@@ -4,7 +4,7 @@ English | [简体中文](./README.zh-CN.md)
4
4
 
5
5
  Tiptap integration adapter for FormulaX.
6
6
 
7
- `@formulaxjs/tiptap` provides a FormulaX inline node extension for Tiptap and a modal-based formula editing flow. The extension persists only LaTeX in the document model and renders formula output at runtime.
7
+ `@formulaxjs/tiptap` provides a FormulaX inline node extension for Tiptap and a modal-based formula editing flow. The extension persists only LaTeX in the document model and renders formula output at runtime through the shared FormulaX renderer interface.
8
8
 
9
9
  > Status: experimental. Public APIs may change before the first stable release.
10
10
 
@@ -16,6 +16,8 @@ Tiptap integration adapter for FormulaX.
16
16
  - Double-click editing for existing formulas
17
17
  - Persist only LaTeX in node attrs
18
18
  - Runtime SVG rendering in the node view
19
+ - Default read-only rendering through `@formulaxjs/renderer-kity`
20
+ - Optional runtime preload before the first modal open
19
21
  - Modal helper export through `openFormulaXTiptapModal`
20
22
  - Compatible peer dependency range for Tiptap 2 and 3
21
23
 
@@ -107,6 +109,21 @@ The Tiptap node stores only the LaTeX source:
107
109
 
108
110
  The node view renders formula SVG at runtime from the stored LaTeX. Generated DOM markup includes `data-formulax="true"` and `data-formulax-latex`, but that rendered DOM is not the persisted source of truth.
109
111
 
112
+ ## Custom renderer
113
+
114
+ The adapter accepts a `renderer` option. By default it uses `createKityFormulaRenderer()` from `@formulaxjs/renderer-kity`.
115
+
116
+ ```ts
117
+ import { createFormulaXNode } from '@formulaxjs/tiptap';
118
+ import { createKityFormulaRenderer } from '@formulaxjs/renderer-kity';
119
+
120
+ const formulaNode = createFormulaXNode(undefined, {
121
+ renderer: createKityFormulaRenderer({
122
+ fontSize: 44,
123
+ }),
124
+ });
125
+ ```
126
+
110
127
  ## Options
111
128
 
112
129
  ```ts
@@ -116,6 +133,8 @@ interface FormulaXTiptapOptions {
116
133
  formulaAttributeName?: string;
117
134
  cursorStyle?: string;
118
135
  initialLatex?: string;
136
+ renderer?: FormulaRenderer;
137
+ preload?: FormulaXEditorPreloadMode;
119
138
  modal?: {
120
139
  title?: string;
121
140
  insertText?: string;
@@ -141,6 +160,8 @@ interface FormulaXTiptapOptions {
141
160
  | `formulaAttributeName` | `data-formulax-latex` | Attribute used in rendered DOM for the source LaTeX. |
142
161
  | `cursorStyle` | `pointer` | Cursor style applied to rendered formula nodes. |
143
162
  | `initialLatex` | empty string | Initial LaTeX when inserting a new formula. |
163
+ | `renderer` | `createKityFormulaRenderer()` | Renderer used for read-only formula output in the node view. |
164
+ | `preload` | `idle` | Preloads the FormulaX runtime on browser idle, on host hover/focus, or never. |
144
165
  | `modal` | see below | Modal labels and closing behavior. |
145
166
  | `editor` | see below | Embedded FormulaX editor options. |
146
167
 
package/README.zh-CN.md CHANGED
@@ -1,215 +1,236 @@
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` 命令,便于接工具栏按钮或代码中主动打开
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
16
  - 支持双击编辑已有公式
17
17
  - 节点 attrs 中仅持久化 LaTeX
18
18
  - 在 node view 中运行时渲染 SVG
19
+ - 默认通过 `@formulaxjs/renderer-kity` 完成只读渲染
20
+ - 支持在首次打开弹窗前预加载 runtime
19
21
  - 直接导出弹窗工具函数 `openFormulaXTiptapModal`
20
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
+ pnpm add @formulaxjs/tiptap
42
+ pnpm add @tiptap/core
43
+ ```
44
+
45
+ 在 FormulaX 工作空间内,直接使用 workspace 包:
46
+
47
+ ```bash
48
+ pnpm install
49
+ pnpm dev:tiptap
50
+ ```
51
+
52
+ ## 基础使用
53
+
54
+ 创建 FormulaX 节点扩展,并加入 Tiptap 的扩展列表:
55
+
56
+ ```ts
57
+ import { Editor } from '@tiptap/core';
58
+ import StarterKit from '@tiptap/starter-kit';
59
+ import { createFormulaXNode } from '@formulaxjs/tiptap';
60
+
61
+ const editor = new Editor({
62
+ element: document.querySelector('#editor')!,
63
+ extensions: [
64
+ StarterKit,
65
+ createFormulaXNode(),
66
+ ],
67
+ content: '<p>点击工具栏按钮插入公式。</p>',
68
+ });
69
+ ```
70
+
71
+ 如果要在代码中主动打开 FormulaX 弹窗:
72
+
73
+ ```ts
74
+ editor.commands.openFormulaX();
75
+ ```
76
+
77
+ ## 自定义节点名称
78
+
79
+ 默认的 Tiptap 节点名是 `formulaX`。
80
+
81
+ 如果宿主编辑器里已经存在同名节点,可以在创建扩展时传入自定义 `name`:
82
+
83
+ ```ts
84
+ const editor = new Editor({
85
+ element: document.querySelector('#editor')!,
86
+ extensions: [
87
+ StarterKit,
88
+ createFormulaXNode(undefined, {
89
+ name: 'inlineMath',
90
+ }),
91
+ ],
92
+ });
93
+ ```
94
+
95
+ 如果 Tiptap 检测到配置的节点名已经被注册,扩展会输出一条 `console.warn`,提醒你尽快改成唯一名称,避免 schema 冲突。
96
+
97
+ ## 持久化数据
98
+
99
+ Tiptap 节点中只保存 LaTeX 源内容:
100
+
101
+ ```json
102
+ {
103
+ "type": "formulaX",
104
+ "attrs": {
105
+ "latex": "\\sqrt{x}"
106
+ }
107
+ }
108
+ ```
109
+
110
+ 节点视图会根据保存的 LaTeX 在运行时渲染 SVG。生成的 DOM 会带有 `data-formulax="true"` 和 `data-formulax-latex`,但这些渲染后的 DOM 不是持久化数据的真实来源。
21
111
 
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
- ## 基础使用
112
+ ## 自定义 renderer
51
113
 
52
- 创建 FormulaX 节点扩展,并加入 Tiptap 的扩展列表:
114
+ 该适配器支持 `renderer` 配置项。默认值是来自 `@formulaxjs/renderer-kity` 的 `createKityFormulaRenderer()`。
53
115
 
54
116
  ```ts
55
- import { Editor } from '@tiptap/core';
56
- import StarterKit from '@tiptap/starter-kit';
57
117
  import { createFormulaXNode } from '@formulaxjs/tiptap';
118
+ import { createKityFormulaRenderer } from '@formulaxjs/renderer-kity';
58
119
 
59
- const editor = new Editor({
60
- element: document.querySelector('#editor')!,
61
- extensions: [
62
- StarterKit,
63
- createFormulaXNode(),
64
- ],
65
- content: '<p>点击工具栏按钮插入公式。</p>',
120
+ const formulaNode = createFormulaXNode(undefined, {
121
+ renderer: createKityFormulaRenderer({
122
+ fontSize: 44,
123
+ }),
66
124
  });
67
125
  ```
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
126
+
127
+ ## 配置项
128
+
129
+ ```ts
113
130
  interface FormulaXTiptapOptions {
114
131
  name?: string;
115
132
  formulaClassName?: string;
116
133
  formulaAttributeName?: string;
117
134
  cursorStyle?: string;
118
135
  initialLatex?: string;
136
+ renderer?: FormulaRenderer;
137
+ preload?: FormulaXEditorPreloadMode;
119
138
  modal?: {
120
139
  title?: string;
121
140
  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 源内容的属性名。 |
141
+ updateText?: string;
142
+ cancelText?: string;
143
+ closeOnBackdrop?: boolean;
144
+ };
145
+ editor?: {
146
+ height?: number | string;
147
+ autofocus?: boolean;
148
+ assets?: Partial<KityEditorAssets>;
149
+ render?: {
150
+ fontsize?: number;
151
+ };
152
+ };
153
+ }
154
+ ```
155
+
156
+ | 配置项 | 默认值 | 说明 |
157
+ | --- | --- | --- |
158
+ | `name` | `formulaX` | 用于文档 schema 的 Tiptap 节点名。 |
159
+ | `formulaClassName` | `formulax-math` | 渲染后公式节点使用的 CSS class。 |
160
+ | `formulaAttributeName` | `data-formulax-latex` | 渲染后 DOM 中保存 LaTeX 源内容的属性名。 |
142
161
  | `cursorStyle` | `pointer` | 渲染后公式节点的鼠标光标样式。 |
143
162
  | `initialLatex` | 空字符串 | 插入新公式时的初始 LaTeX。 |
163
+ | `renderer` | `createKityFormulaRenderer()` | node view 中用于只读公式输出的 renderer。 |
164
+ | `preload` | `idle` | 控制在浏览器空闲时、宿主 hover/focus 时,或完全不预加载 FormulaX runtime。 |
144
165
  | `modal` | 见下方 | 弹窗文案和关闭行为。 |
145
166
  | `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/)
167
+
168
+ ### Modal 配置
169
+
170
+ | 配置项 | 默认值 | 说明 |
171
+ | --- | --- | --- |
172
+ | `title` | `FormulaX Editor` | 弹窗标题。 |
173
+ | `insertText` | `Insert` | 插入公式时的提交按钮文本。 |
174
+ | `updateText` | `Update` | 更新公式时的提交按钮文本。 |
175
+ | `cancelText` | `Cancel` | 取消按钮文本。 |
176
+ | `closeOnBackdrop` | `true` | 点击遮罩层时是否关闭弹窗。 |
177
+
178
+ ### Editor 配置
179
+
180
+ | 配置项 | 默认值 | 说明 |
181
+ | --- | --- | --- |
182
+ | `height` | `100%` | 内嵌编辑器高度。 |
183
+ | `autofocus` | `true` | 内嵌编辑器是否自动聚焦。 |
184
+ | `assets` | `{}` | 可选的 Kity runtime 资源覆盖配置。 |
185
+ | `render.fontsize` | `40` | 公式渲染字号。 |
186
+
187
+ ## 导出 API
188
+
189
+ | 导出 | 说明 |
190
+ | --- | --- |
191
+ | `FormulaXNode` | 默认的 FormulaX Tiptap 节点扩展。 |
192
+ | `createFormulaXNode` | 创建 FormulaX 节点扩展,可传入自定义配置。 |
193
+ | `resolveOptions` | 将用户配置与默认配置合并为完整配置。 |
194
+ | `openFormulaXTiptapModal` | 直接打开 FormulaX 弹窗。 |
195
+ | `FORMULAX_NODE_NAME` | 默认的 Tiptap 节点名。 |
196
+ | `createFormulaXPayload` | 将 LaTeX 解析为 FormulaX 文档。 |
197
+ | `serializeFormulaXPayload` | 将 FormulaX 文档序列化回 LaTeX。 |
198
+
199
+ ## 开发
200
+
201
+ 在仓库根目录执行:
202
+
203
+ ```bash
204
+ pnpm install
205
+ pnpm dev:tiptap
206
+ ```
207
+
208
+ 仅构建该包:
209
+
210
+ ```bash
211
+ pnpm --filter @formulaxjs/tiptap build
212
+ ```
213
+
214
+ 运行该包测试:
215
+
216
+ ```bash
217
+ pnpm --filter @formulaxjs/tiptap test
218
+ ```
219
+
220
+ 运行该包类型检查:
221
+
222
+ ```bash
223
+ pnpm --filter @formulaxjs/tiptap typecheck
224
+ ```
225
+
226
+ ## Demo
227
+
228
+ 本地 demo:
229
+
230
+ ```bash
231
+ pnpm dev:tiptap
232
+ ```
233
+
234
+ GitHub Pages demo:
235
+
236
+ [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');