@formulaxjs/tiptap 0.1.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/LICENSE +35 -0
- package/README.md +193 -0
- package/README.zh-CN.md +193 -0
- package/dist/index.cjs +440 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.global.js +31622 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +421 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 FormulaX Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Legacy KityFormula Code
|
|
26
|
+
|
|
27
|
+
Portions of FormulaX, specifically within the `@formulaxjs/kity-runtime` package,
|
|
28
|
+
contain code adapted from [KityFormula](https://github.com/BaiduFE/kityformula)
|
|
29
|
+
by Baidu FEX Team, originally released under the MIT License.
|
|
30
|
+
|
|
31
|
+
KityFormula-related code in FormulaX is retained under the same MIT License
|
|
32
|
+
and is subject to the copyright notice above.
|
|
33
|
+
|
|
34
|
+
For details on the original KityFormula project, see:
|
|
35
|
+
https://github.com/BaiduFE/kityformula
|
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# @formulaxjs/tiptap
|
|
2
|
+
|
|
3
|
+
English | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Tiptap integration adapter for FormulaX.
|
|
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.
|
|
8
|
+
|
|
9
|
+
> Status: experimental. Public APIs may change before the first stable release.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Tiptap node extension export through `FormulaXNode`
|
|
14
|
+
- Extension factory export through `createFormulaXNode`
|
|
15
|
+
- `openFormulaX` command for toolbar buttons or programmatic opening
|
|
16
|
+
- Double-click editing for existing formulas
|
|
17
|
+
- Persist only LaTeX in node attrs
|
|
18
|
+
- Runtime SVG rendering in the node view
|
|
19
|
+
- Modal helper export through `openFormulaXTiptapModal`
|
|
20
|
+
- Compatible peer dependency range for Tiptap 2 and 3
|
|
21
|
+
|
|
22
|
+
## Compatibility
|
|
23
|
+
|
|
24
|
+
The package declares `@tiptap/core` as an optional peer dependency:
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"@tiptap/core": ">=2 <4"
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The workspace demo can switch between Tiptap 2 and 3 for compatibility verification.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
When the package is published:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pnpm add @formulaxjs/tiptap
|
|
40
|
+
pnpm add @tiptap/core
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Inside the FormulaX workspace, use the workspace package directly:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pnpm install
|
|
47
|
+
pnpm dev:tiptap
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Basic usage
|
|
51
|
+
|
|
52
|
+
Create the FormulaX node extension and add it to the Tiptap extension list:
|
|
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>Click the toolbar button to insert a formula.</p>',
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
To open the FormulaX modal programmatically:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
editor.commands.openFormulaX();
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Persisted data
|
|
76
|
+
|
|
77
|
+
The Tiptap node stores only the LaTeX source:
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"type": "formulaX",
|
|
82
|
+
"attrs": {
|
|
83
|
+
"latex": "\\sqrt{x}"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
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.
|
|
89
|
+
|
|
90
|
+
## Options
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
interface FormulaXTiptapOptions {
|
|
94
|
+
formulaClassName?: string;
|
|
95
|
+
formulaAttributeName?: string;
|
|
96
|
+
cursorStyle?: string;
|
|
97
|
+
initialLatex?: string;
|
|
98
|
+
modal?: {
|
|
99
|
+
title?: string;
|
|
100
|
+
insertText?: string;
|
|
101
|
+
updateText?: string;
|
|
102
|
+
cancelText?: string;
|
|
103
|
+
closeOnBackdrop?: boolean;
|
|
104
|
+
};
|
|
105
|
+
editor?: {
|
|
106
|
+
height?: number | string;
|
|
107
|
+
autofocus?: boolean;
|
|
108
|
+
assets?: Partial<KityEditorAssets>;
|
|
109
|
+
render?: {
|
|
110
|
+
fontsize?: number;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
| Option | Default | Description |
|
|
117
|
+
| --- | --- | --- |
|
|
118
|
+
| `formulaClassName` | `formulax-math` | CSS class used by rendered formula nodes. |
|
|
119
|
+
| `formulaAttributeName` | `data-formulax-latex` | Attribute used in rendered DOM for the source LaTeX. |
|
|
120
|
+
| `cursorStyle` | `pointer` | Cursor style applied to rendered formula nodes. |
|
|
121
|
+
| `initialLatex` | empty string | Initial LaTeX when inserting a new formula. |
|
|
122
|
+
| `modal` | see below | Modal labels and closing behavior. |
|
|
123
|
+
| `editor` | see below | Embedded FormulaX editor options. |
|
|
124
|
+
|
|
125
|
+
### Modal options
|
|
126
|
+
|
|
127
|
+
| Option | Default | Description |
|
|
128
|
+
| --- | --- | --- |
|
|
129
|
+
| `title` | `FormulaX Editor` | Modal title. |
|
|
130
|
+
| `insertText` | `Insert` | Submit button text when inserting. |
|
|
131
|
+
| `updateText` | `Update` | Submit button text when updating. |
|
|
132
|
+
| `cancelText` | `Cancel` | Cancel button text. |
|
|
133
|
+
| `closeOnBackdrop` | `true` | Whether clicking the backdrop closes the modal. |
|
|
134
|
+
|
|
135
|
+
### Editor options
|
|
136
|
+
|
|
137
|
+
| Option | Default | Description |
|
|
138
|
+
| --- | --- | --- |
|
|
139
|
+
| `height` | `100%` | Embedded editor height. |
|
|
140
|
+
| `autofocus` | `true` | Whether the embedded editor should autofocus. |
|
|
141
|
+
| `assets` | `{}` | Optional Kity runtime asset overrides. |
|
|
142
|
+
| `render.fontsize` | `40` | Formula render font size. |
|
|
143
|
+
|
|
144
|
+
## Exported API
|
|
145
|
+
|
|
146
|
+
| Export | Description |
|
|
147
|
+
| --- | --- |
|
|
148
|
+
| `FormulaXNode` | Default FormulaX Tiptap node extension. |
|
|
149
|
+
| `createFormulaXNode` | Creates a FormulaX node extension, optionally with custom options. |
|
|
150
|
+
| `resolveOptions` | Resolves user options into required defaults. |
|
|
151
|
+
| `openFormulaXTiptapModal` | Opens the FormulaX modal directly. |
|
|
152
|
+
| `FORMULAX_NODE_NAME` | Default Tiptap node name. |
|
|
153
|
+
| `createFormulaXPayload` | Parses LaTeX into a FormulaX document. |
|
|
154
|
+
| `serializeFormulaXPayload` | Serializes a FormulaX document back to LaTeX. |
|
|
155
|
+
|
|
156
|
+
## Development
|
|
157
|
+
|
|
158
|
+
From the repository root:
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
pnpm install
|
|
162
|
+
pnpm dev:tiptap
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Build only this package:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
pnpm --filter @formulaxjs/tiptap build
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Run package tests:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
pnpm --filter @formulaxjs/tiptap test
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Run package type checking:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
pnpm --filter @formulaxjs/tiptap typecheck
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Demo
|
|
184
|
+
|
|
185
|
+
Local demo:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
pnpm dev:tiptap
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
GitHub Pages demo:
|
|
192
|
+
|
|
193
|
+
[https://vndmea.github.io/formulaX/tiptap/](https://vndmea.github.io/formulaX/tiptap/)
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
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 节点中只保存 LaTeX 源内容:
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"type": "formulaX",
|
|
82
|
+
"attrs": {
|
|
83
|
+
"latex": "\\sqrt{x}"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
节点视图会根据保存的 LaTeX 在运行时渲染 SVG。生成的 DOM 会带有 `data-formulax="true"` 和 `data-formulax-latex`,但这些渲染后的 DOM 不是持久化数据的真实来源。
|
|
89
|
+
|
|
90
|
+
## 配置项
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
interface FormulaXTiptapOptions {
|
|
94
|
+
formulaClassName?: string;
|
|
95
|
+
formulaAttributeName?: string;
|
|
96
|
+
cursorStyle?: string;
|
|
97
|
+
initialLatex?: string;
|
|
98
|
+
modal?: {
|
|
99
|
+
title?: string;
|
|
100
|
+
insertText?: string;
|
|
101
|
+
updateText?: string;
|
|
102
|
+
cancelText?: string;
|
|
103
|
+
closeOnBackdrop?: boolean;
|
|
104
|
+
};
|
|
105
|
+
editor?: {
|
|
106
|
+
height?: number | string;
|
|
107
|
+
autofocus?: boolean;
|
|
108
|
+
assets?: Partial<KityEditorAssets>;
|
|
109
|
+
render?: {
|
|
110
|
+
fontsize?: number;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
| 配置项 | 默认值 | 说明 |
|
|
117
|
+
| --- | --- | --- |
|
|
118
|
+
| `formulaClassName` | `formulax-math` | 渲染后公式节点使用的 CSS class。 |
|
|
119
|
+
| `formulaAttributeName` | `data-formulax-latex` | 渲染后 DOM 中保存 LaTeX 源内容的属性名。 |
|
|
120
|
+
| `cursorStyle` | `pointer` | 渲染后公式节点的鼠标光标样式。 |
|
|
121
|
+
| `initialLatex` | 空字符串 | 插入新公式时的初始 LaTeX。 |
|
|
122
|
+
| `modal` | 见下方 | 弹窗文案和关闭行为。 |
|
|
123
|
+
| `editor` | 见下方 | 内嵌 FormulaX 编辑器配置。 |
|
|
124
|
+
|
|
125
|
+
### Modal 配置
|
|
126
|
+
|
|
127
|
+
| 配置项 | 默认值 | 说明 |
|
|
128
|
+
| --- | --- | --- |
|
|
129
|
+
| `title` | `FormulaX Editor` | 弹窗标题。 |
|
|
130
|
+
| `insertText` | `Insert` | 插入公式时的提交按钮文本。 |
|
|
131
|
+
| `updateText` | `Update` | 更新公式时的提交按钮文本。 |
|
|
132
|
+
| `cancelText` | `Cancel` | 取消按钮文本。 |
|
|
133
|
+
| `closeOnBackdrop` | `true` | 点击遮罩层时是否关闭弹窗。 |
|
|
134
|
+
|
|
135
|
+
### Editor 配置
|
|
136
|
+
|
|
137
|
+
| 配置项 | 默认值 | 说明 |
|
|
138
|
+
| --- | --- | --- |
|
|
139
|
+
| `height` | `100%` | 内嵌编辑器高度。 |
|
|
140
|
+
| `autofocus` | `true` | 内嵌编辑器是否自动聚焦。 |
|
|
141
|
+
| `assets` | `{}` | 可选的 Kity runtime 资源覆盖配置。 |
|
|
142
|
+
| `render.fontsize` | `40` | 公式渲染字号。 |
|
|
143
|
+
|
|
144
|
+
## 导出 API
|
|
145
|
+
|
|
146
|
+
| 导出 | 说明 |
|
|
147
|
+
| --- | --- |
|
|
148
|
+
| `FormulaXNode` | 默认的 FormulaX Tiptap 节点扩展。 |
|
|
149
|
+
| `createFormulaXNode` | 创建 FormulaX 节点扩展,可传入自定义配置。 |
|
|
150
|
+
| `resolveOptions` | 将用户配置与默认配置合并为完整配置。 |
|
|
151
|
+
| `openFormulaXTiptapModal` | 直接打开 FormulaX 弹窗。 |
|
|
152
|
+
| `FORMULAX_NODE_NAME` | 默认的 Tiptap 节点名。 |
|
|
153
|
+
| `createFormulaXPayload` | 将 LaTeX 解析为 FormulaX 文档。 |
|
|
154
|
+
| `serializeFormulaXPayload` | 将 FormulaX 文档序列化回 LaTeX。 |
|
|
155
|
+
|
|
156
|
+
## 开发
|
|
157
|
+
|
|
158
|
+
在仓库根目录执行:
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
pnpm install
|
|
162
|
+
pnpm dev:tiptap
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
仅构建该包:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
pnpm --filter @formulaxjs/tiptap build
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
运行该包测试:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
pnpm --filter @formulaxjs/tiptap test
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
运行该包类型检查:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
pnpm --filter @formulaxjs/tiptap typecheck
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Demo
|
|
184
|
+
|
|
185
|
+
本地 demo:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
pnpm dev:tiptap
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
GitHub Pages demo:
|
|
192
|
+
|
|
193
|
+
[https://vndmea.github.io/formulaX/tiptap/](https://vndmea.github.io/formulaX/tiptap/)
|