prosemirror-math 0.1.0 → 0.2.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 +1 -23
- package/dist/prosemirror-math.d.ts +48 -0
- package/dist/prosemirror-math.js +81 -15
- package/package.json +4 -3
- package/src/create-element.spec.ts +1 -1
- package/src/cursor-inside-plugin.spec.ts +4 -4
- package/src/index.ts +7 -7
- package/src/math-block-enter-rule.spec.ts +2 -2
- package/src/math-block-enter-rule.ts +6 -0
- package/src/math-block-spec.spec.ts +2 -2
- package/src/math-block-spec.ts +7 -2
- package/src/math-block-view.spec.ts +8 -8
- package/src/math-block-view.ts +18 -6
- package/src/math-inline-input-rule.spec.ts +2 -2
- package/src/math-inline-input-rule.ts +8 -0
- package/src/math-inline-spec.spec.ts +2 -2
- package/src/math-inline-spec.ts +7 -2
- package/src/math-inline-view.spec.ts +8 -8
- package/src/math-inline-view.ts +20 -7
- package/src/math-view-render.ts +31 -2
- package/src/testing.ts +10 -10
package/README.md
CHANGED
|
@@ -91,29 +91,7 @@ const plugin = createCursorInsidePlugin()
|
|
|
91
91
|
|
|
92
92
|
## API
|
|
93
93
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
- **`mathBlockSpec`** — NodeSpec for block math (`div.prosekit-math-block > pre > code`)
|
|
97
|
-
- **`mathInlineSpec`** — NodeSpec for inline math (`span.prosekit-math-inline > code`)
|
|
98
|
-
|
|
99
|
-
### Node views
|
|
100
|
-
|
|
101
|
-
- **`createMathBlockView(renderMath, node, decorations)`** — Creates a block math NodeView
|
|
102
|
-
- **`createMathInlineView(renderMath, node, decorations)`** — Creates an inline math NodeView
|
|
103
|
-
|
|
104
|
-
The `renderMath` callback receives `(text: string, element: HTMLElement)` and should render the math into the element.
|
|
105
|
-
|
|
106
|
-
### Input rules
|
|
107
|
-
|
|
108
|
-
- **`createMathInlineInputRule(nodeType)`** — Creates an InputRule for inline math
|
|
109
|
-
|
|
110
|
-
### Enter rules
|
|
111
|
-
|
|
112
|
-
- **`mathBlockEnterRule`** — EnterRule for converting `$$` into a math block
|
|
113
|
-
|
|
114
|
-
### Plugins
|
|
115
|
-
|
|
116
|
-
- **`createCursorInsidePlugin()`** — Plugin that decorates math nodes containing the cursor
|
|
94
|
+
[API Reference](https://npmx.dev/package-docs/prosemirror-math)
|
|
117
95
|
|
|
118
96
|
## License
|
|
119
97
|
|
|
@@ -17,9 +17,20 @@ import { Node, NodeSpec } from "prosemirror-model";
|
|
|
17
17
|
declare function createCursorInsidePlugin(): Plugin;
|
|
18
18
|
//#endregion
|
|
19
19
|
//#region src/math-block-enter-rule.d.ts
|
|
20
|
+
/**
|
|
21
|
+
* An {@link EnterRule} that converts a textblock node that only contains `$$` into a math
|
|
22
|
+
* block node when Enter is pressed.
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
20
26
|
declare const mathBlockEnterRule: EnterRule;
|
|
21
27
|
//#endregion
|
|
22
28
|
//#region src/math-block-spec.d.ts
|
|
29
|
+
/**
|
|
30
|
+
* A {@link NodeSpec} for a block-level math node.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
23
34
|
declare const mathBlockSpec: NodeSpec;
|
|
24
35
|
//#endregion
|
|
25
36
|
//#region src/math-block-view.d.ts
|
|
@@ -30,12 +41,37 @@ declare const mathBlockSpec: NodeSpec;
|
|
|
30
41
|
* @param element - A `<div>` element to render the math block.
|
|
31
42
|
*/
|
|
32
43
|
type RenderMathBlock = (text: string, element: HTMLElement) => void;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a {@link NodeView} for a block-level math node. The view will show a
|
|
46
|
+
* source editor or a rendered display area based on the text cursor position.
|
|
47
|
+
*
|
|
48
|
+
* @param renderMathBlock - A function that renders math text (e.g. TeX) into
|
|
49
|
+
* the display element. You can use libraries like
|
|
50
|
+
* [Temml](https://temml.org/) or [KaTeX](https://katex.org/).
|
|
51
|
+
* @param node - The ProseMirror node to render.
|
|
52
|
+
* @param decorations - The decorations applied to the node.
|
|
53
|
+
*
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
33
56
|
declare function createMathBlockView(renderMathBlock: RenderMathBlock, node: Node, decorations: readonly Decoration[]): NodeView;
|
|
34
57
|
//#endregion
|
|
35
58
|
//#region src/math-inline-input-rule.d.ts
|
|
59
|
+
/**
|
|
60
|
+
* Creates a ProseMirror {@link InputRule} that converts text wrapped in `$` or
|
|
61
|
+
* `$$` (e.g. `$x^2$`) into an inline math node.
|
|
62
|
+
*
|
|
63
|
+
* @param nodeType - The name of the inline math node type in your schema.
|
|
64
|
+
*
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
36
67
|
declare function createMathInlineInputRule(nodeType: string): InputRule;
|
|
37
68
|
//#endregion
|
|
38
69
|
//#region src/math-inline-spec.d.ts
|
|
70
|
+
/**
|
|
71
|
+
* A {@link NodeSpec} for an inline math node.
|
|
72
|
+
*
|
|
73
|
+
* @public
|
|
74
|
+
*/
|
|
39
75
|
declare const mathInlineSpec: NodeSpec;
|
|
40
76
|
//#endregion
|
|
41
77
|
//#region src/math-inline-view.d.ts
|
|
@@ -46,6 +82,18 @@ declare const mathInlineSpec: NodeSpec;
|
|
|
46
82
|
* @param element - A `<span>` element to render the math inline.
|
|
47
83
|
*/
|
|
48
84
|
type RenderMathInline = (text: string, element: HTMLElement) => void;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a {@link NodeView} for an inline math node. The view will show a
|
|
87
|
+
* source editor or a rendered display area based on the text cursor position.
|
|
88
|
+
*
|
|
89
|
+
* @param renderMathInline - A function that renders math text (e.g. TeX) into
|
|
90
|
+
* the display element. You can use libraries like [Temml](https://temml.org/)
|
|
91
|
+
* or [KaTeX](https://katex.org/).
|
|
92
|
+
* @param node - The ProseMirror node to render.
|
|
93
|
+
* @param decorations - The decorations applied to the node.
|
|
94
|
+
*
|
|
95
|
+
* @public
|
|
96
|
+
*/
|
|
49
97
|
declare function createMathInlineView(renderMathInline: RenderMathInline, node: Node, decorations: readonly Decoration[]): NodeView;
|
|
50
98
|
//#endregion
|
|
51
99
|
export { type RenderMathBlock, type RenderMathInline, createCursorInsidePlugin, createMathBlockView, createMathInlineInputRule, createMathInlineView, mathBlockEnterRule, mathBlockSpec, mathInlineSpec };
|
package/dist/prosemirror-math.js
CHANGED
|
@@ -49,6 +49,12 @@ function createCursorInsidePlugin() {
|
|
|
49
49
|
//#endregion
|
|
50
50
|
//#region src/math-block-enter-rule.ts
|
|
51
51
|
const MATH_BLOCK_ENTER_REGEXP = /^\$\$$/;
|
|
52
|
+
/**
|
|
53
|
+
* An {@link EnterRule} that converts a textblock node that only contains `$$` into a math
|
|
54
|
+
* block node when Enter is pressed.
|
|
55
|
+
*
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
52
58
|
const mathBlockEnterRule = /* @__PURE__ */ createTextBlockEnterRule({
|
|
53
59
|
regex: MATH_BLOCK_ENTER_REGEXP,
|
|
54
60
|
type: "mathBlock"
|
|
@@ -56,6 +62,11 @@ const mathBlockEnterRule = /* @__PURE__ */ createTextBlockEnterRule({
|
|
|
56
62
|
|
|
57
63
|
//#endregion
|
|
58
64
|
//#region src/math-block-spec.ts
|
|
65
|
+
/**
|
|
66
|
+
* A {@link NodeSpec} for a block-level math node.
|
|
67
|
+
*
|
|
68
|
+
* @public
|
|
69
|
+
*/
|
|
59
70
|
const mathBlockSpec = {
|
|
60
71
|
atom: false,
|
|
61
72
|
group: "block math",
|
|
@@ -64,12 +75,12 @@ const mathBlockSpec = {
|
|
|
64
75
|
toDOM() {
|
|
65
76
|
return [
|
|
66
77
|
"div",
|
|
67
|
-
{ class: "
|
|
78
|
+
{ class: "prosemirror-math-block" },
|
|
68
79
|
["pre", ["code", 0]]
|
|
69
80
|
];
|
|
70
81
|
},
|
|
71
82
|
parseDOM: [{
|
|
72
|
-
tag: "div.
|
|
83
|
+
tag: "div.prosemirror-math-block",
|
|
73
84
|
contentElement: "code"
|
|
74
85
|
}]
|
|
75
86
|
};
|
|
@@ -85,7 +96,7 @@ function createElement(tag, className, ...children) {
|
|
|
85
96
|
|
|
86
97
|
//#endregion
|
|
87
98
|
//#region src/math-view-render.ts
|
|
88
|
-
function createMathViewRender(renderMath, source, display) {
|
|
99
|
+
function createMathViewRender(renderMath, source, display, inline) {
|
|
89
100
|
let prevNode;
|
|
90
101
|
let prevText;
|
|
91
102
|
let prevSelected;
|
|
@@ -101,23 +112,52 @@ function createMathViewRender(renderMath, source, display) {
|
|
|
101
112
|
const selected = hasCursorInsideDecoration(decorations);
|
|
102
113
|
if (selected === prevSelected) return;
|
|
103
114
|
prevSelected = selected;
|
|
104
|
-
source.style.display = selected ? "" : "none";
|
|
105
115
|
display.style.display = selected ? "none" : "";
|
|
116
|
+
if (!inline) source.style.display = selected ? "" : "none";
|
|
117
|
+
else Object.assign(source.style, selected ? visibleInlineSourceStyle : hiddenInlineSourceStyle);
|
|
106
118
|
}
|
|
107
119
|
return function updateMathView(node, decorations) {
|
|
108
120
|
updateDisplay(node);
|
|
109
121
|
updateStyle(decorations);
|
|
110
122
|
};
|
|
111
123
|
}
|
|
124
|
+
const hiddenInlineSourceStyle = {
|
|
125
|
+
display: "inline-flex",
|
|
126
|
+
opacity: "0",
|
|
127
|
+
pointerEvents: "none",
|
|
128
|
+
maxWidth: "0",
|
|
129
|
+
maxHeight: "0",
|
|
130
|
+
overflow: "hidden"
|
|
131
|
+
};
|
|
132
|
+
const visibleInlineSourceStyle = {
|
|
133
|
+
display: "inline-flex",
|
|
134
|
+
opacity: "1",
|
|
135
|
+
pointerEvents: "",
|
|
136
|
+
maxWidth: "",
|
|
137
|
+
maxHeight: "",
|
|
138
|
+
overflow: ""
|
|
139
|
+
};
|
|
112
140
|
|
|
113
141
|
//#endregion
|
|
114
142
|
//#region src/math-block-view.ts
|
|
143
|
+
/**
|
|
144
|
+
* Creates a {@link NodeView} for a block-level math node. The view will show a
|
|
145
|
+
* source editor or a rendered display area based on the text cursor position.
|
|
146
|
+
*
|
|
147
|
+
* @param renderMathBlock - A function that renders math text (e.g. TeX) into
|
|
148
|
+
* the display element. You can use libraries like
|
|
149
|
+
* [Temml](https://temml.org/) or [KaTeX](https://katex.org/).
|
|
150
|
+
* @param node - The ProseMirror node to render.
|
|
151
|
+
* @param decorations - The decorations applied to the node.
|
|
152
|
+
*
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
115
155
|
function createMathBlockView(renderMathBlock, node, decorations) {
|
|
116
156
|
const code = createElement("code");
|
|
117
|
-
const source = createElement("pre", "
|
|
118
|
-
const display = createElement("div", "
|
|
119
|
-
const dom = createElement("div", "
|
|
120
|
-
const render = createMathViewRender(renderMathBlock, source, display);
|
|
157
|
+
const source = createElement("pre", "prosemirror-math-source", code);
|
|
158
|
+
const display = createElement("div", "prosemirror-math-display");
|
|
159
|
+
const dom = createElement("div", "prosemirror-math-block", source, display);
|
|
160
|
+
const render = createMathViewRender(renderMathBlock, source, display, false);
|
|
121
161
|
render(node, decorations);
|
|
122
162
|
return {
|
|
123
163
|
dom,
|
|
@@ -132,6 +172,14 @@ function createMathBlockView(renderMathBlock, node, decorations) {
|
|
|
132
172
|
//#endregion
|
|
133
173
|
//#region src/math-inline-input-rule.ts
|
|
134
174
|
const MATH_INPUT_REGEXP = (supportsRegexLookbehind() ? "(?<!\\$)" : "") + "(\\$\\$?)([^\\s$](?:[^$]*[^\\s$])?)\\1$";
|
|
175
|
+
/**
|
|
176
|
+
* Creates a ProseMirror {@link InputRule} that converts text wrapped in `$` or
|
|
177
|
+
* `$$` (e.g. `$x^2$`) into an inline math node.
|
|
178
|
+
*
|
|
179
|
+
* @param nodeType - The name of the inline math node type in your schema.
|
|
180
|
+
*
|
|
181
|
+
* @public
|
|
182
|
+
*/
|
|
135
183
|
function createMathInlineInputRule(nodeType) {
|
|
136
184
|
return new InputRule(new RegExp(MATH_INPUT_REGEXP), (state, match, start, end) => {
|
|
137
185
|
const { tr, schema } = state;
|
|
@@ -147,6 +195,11 @@ function createMathInlineInputRule(nodeType) {
|
|
|
147
195
|
|
|
148
196
|
//#endregion
|
|
149
197
|
//#region src/math-inline-spec.ts
|
|
198
|
+
/**
|
|
199
|
+
* A {@link NodeSpec} for an inline math node.
|
|
200
|
+
*
|
|
201
|
+
* @public
|
|
202
|
+
*/
|
|
150
203
|
const mathInlineSpec = {
|
|
151
204
|
atom: false,
|
|
152
205
|
inline: true,
|
|
@@ -157,27 +210,40 @@ const mathInlineSpec = {
|
|
|
157
210
|
toDOM() {
|
|
158
211
|
return [
|
|
159
212
|
"span",
|
|
160
|
-
{ class: "
|
|
213
|
+
{ class: "prosemirror-math-inline" },
|
|
161
214
|
["code", 0]
|
|
162
215
|
];
|
|
163
216
|
},
|
|
164
217
|
parseDOM: [{
|
|
165
|
-
tag: "span.
|
|
218
|
+
tag: "span.prosemirror-math-inline",
|
|
166
219
|
contentElement: "code"
|
|
167
220
|
}]
|
|
168
221
|
};
|
|
169
222
|
|
|
170
223
|
//#endregion
|
|
171
224
|
//#region src/math-inline-view.ts
|
|
225
|
+
/**
|
|
226
|
+
* Creates a {@link NodeView} for an inline math node. The view will show a
|
|
227
|
+
* source editor or a rendered display area based on the text cursor position.
|
|
228
|
+
*
|
|
229
|
+
* @param renderMathInline - A function that renders math text (e.g. TeX) into
|
|
230
|
+
* the display element. You can use libraries like [Temml](https://temml.org/)
|
|
231
|
+
* or [KaTeX](https://katex.org/).
|
|
232
|
+
* @param node - The ProseMirror node to render.
|
|
233
|
+
* @param decorations - The decorations applied to the node.
|
|
234
|
+
*
|
|
235
|
+
* @public
|
|
236
|
+
*/
|
|
172
237
|
function createMathInlineView(renderMathInline, node, decorations) {
|
|
173
|
-
const
|
|
174
|
-
const
|
|
175
|
-
const
|
|
176
|
-
const
|
|
238
|
+
const code = createElement("code");
|
|
239
|
+
const source = createElement("span", "prosemirror-math-source", code);
|
|
240
|
+
const display = createElement("span", "prosemirror-math-display");
|
|
241
|
+
const dom = createElement("span", "prosemirror-math-inline", source, display);
|
|
242
|
+
const render = createMathViewRender(renderMathInline, source, display, true);
|
|
177
243
|
render(node, decorations);
|
|
178
244
|
return {
|
|
179
245
|
dom,
|
|
180
|
-
contentDOM:
|
|
246
|
+
contentDOM: code,
|
|
181
247
|
update: (node, decorations) => {
|
|
182
248
|
render(node, decorations);
|
|
183
249
|
return true;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prosemirror-math",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Math extensions for ProseMirror",
|
|
7
7
|
"author": {
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@mathjax/src": "^4.1.0",
|
|
48
|
+
"@ocavue/tsconfig": "^0.6.3",
|
|
48
49
|
"diffable-html-snapshot": "^0.2.0",
|
|
49
50
|
"katex": "^0.16.28",
|
|
50
51
|
"temml": "^0.13.1",
|
|
@@ -52,8 +53,8 @@
|
|
|
52
53
|
"typescript": "~5.9.3",
|
|
53
54
|
"vitest": "^4.0.18",
|
|
54
55
|
"vitest-browser-commands": "^0.2.0",
|
|
55
|
-
"@prosekit/
|
|
56
|
-
"@prosekit/
|
|
56
|
+
"@prosekit/config-vitest": "0.0.0",
|
|
57
|
+
"@prosekit/core": "^0.10.0"
|
|
57
58
|
},
|
|
58
59
|
"publishConfig": {
|
|
59
60
|
"dev": {}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TextSelection } from 'prosemirror-state'
|
|
2
2
|
import { describe, expect, it } from 'vitest'
|
|
3
3
|
|
|
4
|
-
import { setupTest } from './testing'
|
|
4
|
+
import { setupTest } from './testing.ts'
|
|
5
5
|
|
|
6
6
|
describe('cursorInsidePlugin', () => {
|
|
7
7
|
it('applies decoration when cursor is inside mathBlock', () => {
|
|
@@ -15,7 +15,7 @@ describe('cursorInsidePlugin', () => {
|
|
|
15
15
|
)
|
|
16
16
|
editor.view.dispatch(tr)
|
|
17
17
|
|
|
18
|
-
const mathBlock = editor.view.dom.querySelector('.
|
|
18
|
+
const mathBlock = editor.view.dom.querySelector('.prosemirror-math-block')
|
|
19
19
|
expect(mathBlock?.classList.contains('prosemirror-math-head-inside')).toBe(true)
|
|
20
20
|
})
|
|
21
21
|
|
|
@@ -30,7 +30,7 @@ describe('cursorInsidePlugin', () => {
|
|
|
30
30
|
)
|
|
31
31
|
editor.view.dispatch(tr)
|
|
32
32
|
|
|
33
|
-
const mathInline = editor.view.dom.querySelector('.
|
|
33
|
+
const mathInline = editor.view.dom.querySelector('.prosemirror-math-inline')
|
|
34
34
|
expect(mathInline?.classList.contains('prosemirror-math-head-inside')).toBe(true)
|
|
35
35
|
})
|
|
36
36
|
|
|
@@ -45,7 +45,7 @@ describe('cursorInsidePlugin', () => {
|
|
|
45
45
|
)
|
|
46
46
|
editor.view.dispatch(tr)
|
|
47
47
|
|
|
48
|
-
const mathBlock = editor.view.dom.querySelector('.
|
|
48
|
+
const mathBlock = editor.view.dom.querySelector('.prosemirror-math-block')
|
|
49
49
|
expect(mathBlock?.classList.contains('prosemirror-math-head-inside')).toBe(false)
|
|
50
50
|
})
|
|
51
51
|
})
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { createCursorInsidePlugin } from './cursor-inside-plugin'
|
|
2
|
-
export { mathBlockEnterRule } from './math-block-enter-rule'
|
|
3
|
-
export { mathBlockSpec } from './math-block-spec'
|
|
4
|
-
export { createMathBlockView, type RenderMathBlock } from './math-block-view'
|
|
5
|
-
export { createMathInlineInputRule } from './math-inline-input-rule'
|
|
6
|
-
export { mathInlineSpec } from './math-inline-spec'
|
|
7
|
-
export { createMathInlineView, type RenderMathInline } from './math-inline-view'
|
|
1
|
+
export { createCursorInsidePlugin } from './cursor-inside-plugin.ts'
|
|
2
|
+
export { mathBlockEnterRule } from './math-block-enter-rule.ts'
|
|
3
|
+
export { mathBlockSpec } from './math-block-spec.ts'
|
|
4
|
+
export { createMathBlockView, type RenderMathBlock } from './math-block-view.ts'
|
|
5
|
+
export { createMathInlineInputRule } from './math-inline-input-rule.ts'
|
|
6
|
+
export { mathInlineSpec } from './math-inline-spec.ts'
|
|
7
|
+
export { createMathInlineView, type RenderMathInline } from './math-inline-view.ts'
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
2
|
import { userEvent } from 'vitest/browser'
|
|
3
3
|
|
|
4
|
-
import { MATH_BLOCK_ENTER_REGEXP } from './math-block-enter-rule'
|
|
5
|
-
import { setupTest } from './testing'
|
|
4
|
+
import { MATH_BLOCK_ENTER_REGEXP } from './math-block-enter-rule.ts'
|
|
5
|
+
import { setupTest } from './testing.ts'
|
|
6
6
|
|
|
7
7
|
describe('MATH_BLOCK_ENTER_REGEXP', () => {
|
|
8
8
|
const cases: Array<[input: string, matched: boolean]> = [
|
|
@@ -2,6 +2,12 @@ import { createTextBlockEnterRule, type EnterRule } from 'prosemirror-enter-rule
|
|
|
2
2
|
|
|
3
3
|
export const MATH_BLOCK_ENTER_REGEXP: RegExp = /^\$\$$/
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* An {@link EnterRule} that converts a textblock node that only contains `$$` into a math
|
|
7
|
+
* block node when Enter is pressed.
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
5
11
|
export const mathBlockEnterRule: EnterRule = /* @__PURE__ */ createTextBlockEnterRule({
|
|
6
12
|
regex: MATH_BLOCK_ENTER_REGEXP,
|
|
7
13
|
type: 'mathBlock',
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { formatHTML } from 'diffable-html-snapshot'
|
|
2
2
|
import { describe, expect, it } from 'vitest'
|
|
3
3
|
|
|
4
|
-
import { setupTest } from './testing'
|
|
4
|
+
import { setupTest } from './testing.ts'
|
|
5
5
|
|
|
6
6
|
describe('mathBlockSpec', () => {
|
|
7
7
|
it('can serialize to HTML', () => {
|
|
@@ -12,7 +12,7 @@ describe('mathBlockSpec', () => {
|
|
|
12
12
|
`
|
|
13
13
|
"
|
|
14
14
|
<div>
|
|
15
|
-
<div class="
|
|
15
|
+
<div class="prosemirror-math-block">
|
|
16
16
|
<pre>
|
|
17
17
|
<code>
|
|
18
18
|
E = mc^2
|
package/src/math-block-spec.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { NodeSpec } from 'prosemirror-model'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* A {@link NodeSpec} for a block-level math node.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
3
8
|
export const mathBlockSpec: NodeSpec = {
|
|
4
9
|
atom: false,
|
|
5
10
|
group: 'block math',
|
|
@@ -9,14 +14,14 @@ export const mathBlockSpec: NodeSpec = {
|
|
|
9
14
|
return [
|
|
10
15
|
'div',
|
|
11
16
|
{
|
|
12
|
-
class: '
|
|
17
|
+
class: 'prosemirror-math-block',
|
|
13
18
|
},
|
|
14
19
|
['pre', ['code', 0]],
|
|
15
20
|
]
|
|
16
21
|
},
|
|
17
22
|
parseDOM: [
|
|
18
23
|
{
|
|
19
|
-
tag: 'div.
|
|
24
|
+
tag: 'div.prosemirror-math-block',
|
|
20
25
|
|
|
21
26
|
// skip the `<pre>` wrapper so that the node `codeBlock` won't match the content.
|
|
22
27
|
// TODO: add test to verify it
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { formatHTML } from 'diffable-html-snapshot'
|
|
2
2
|
import { describe, expect, it } from 'vitest'
|
|
3
3
|
|
|
4
|
-
import { katexRenderer, mathjaxRenderer, renderers, setupTest, temmlRenderer } from './testing'
|
|
4
|
+
import { katexRenderer, mathjaxRenderer, renderers, setupTest, temmlRenderer } from './testing.ts'
|
|
5
5
|
|
|
6
6
|
describe.each(Object.keys(renderers))('createMathBlockView (%s)', (name) => {
|
|
7
7
|
const renderer = renderers[name as keyof typeof renderers]
|
|
@@ -11,17 +11,17 @@ describe.each(Object.keys(renderers))('createMathBlockView (%s)', (name) => {
|
|
|
11
11
|
editor.set(n.doc(n.mathBlock('x^2')))
|
|
12
12
|
|
|
13
13
|
const dom = editor.view.dom
|
|
14
|
-
const mathBlock = dom.querySelector('.
|
|
14
|
+
const mathBlock = dom.querySelector('.prosemirror-math-block')
|
|
15
15
|
expect(mathBlock).toBeTruthy()
|
|
16
|
-
expect(mathBlock?.querySelector('.
|
|
17
|
-
expect(mathBlock?.querySelector('.
|
|
16
|
+
expect(mathBlock?.querySelector('.prosemirror-math-source')).toBeTruthy()
|
|
17
|
+
expect(mathBlock?.querySelector('.prosemirror-math-display')).toBeTruthy()
|
|
18
18
|
})
|
|
19
19
|
|
|
20
20
|
it('updates display when content changes', () => {
|
|
21
21
|
const { editor, n } = setupTest(renderer)
|
|
22
22
|
editor.set(n.doc(n.mathBlock('x^2')))
|
|
23
23
|
|
|
24
|
-
const display = editor.view.dom.querySelector('.
|
|
24
|
+
const display = editor.view.dom.querySelector('.prosemirror-math-display')
|
|
25
25
|
const initialHTML = display?.innerHTML
|
|
26
26
|
|
|
27
27
|
// Dispatch a transaction to change content
|
|
@@ -39,7 +39,7 @@ describe('createMathBlockView (temml snapshot)', () => {
|
|
|
39
39
|
const { editor, n } = setupTest(temmlRenderer)
|
|
40
40
|
editor.set(n.doc(n.mathBlock('x^2')))
|
|
41
41
|
|
|
42
|
-
const display = editor.view.dom.querySelector('.
|
|
42
|
+
const display = editor.view.dom.querySelector('.prosemirror-math-display')
|
|
43
43
|
const html = formatHTML(display?.innerHTML || '')
|
|
44
44
|
expect(html).toMatchInlineSnapshot(`
|
|
45
45
|
"
|
|
@@ -72,7 +72,7 @@ describe('createMathBlockView (katex snapshot)', () => {
|
|
|
72
72
|
const { editor, n } = setupTest(katexRenderer)
|
|
73
73
|
editor.set(n.doc(n.mathBlock('x^2')))
|
|
74
74
|
|
|
75
|
-
const display = editor.view.dom.querySelector('.
|
|
75
|
+
const display = editor.view.dom.querySelector('.prosemirror-math-display')
|
|
76
76
|
const html = formatHTML(display?.innerHTML || '')
|
|
77
77
|
expect(html).toMatchInlineSnapshot(`
|
|
78
78
|
"
|
|
@@ -155,7 +155,7 @@ describe('createMathBlockView (mathjax snapshot)', () => {
|
|
|
155
155
|
const { editor, n } = setupTest(mathjaxRenderer)
|
|
156
156
|
editor.set(n.doc(n.mathBlock('x^2')))
|
|
157
157
|
|
|
158
|
-
const display = editor.view.dom.querySelector('.
|
|
158
|
+
const display = editor.view.dom.querySelector('.prosemirror-math-display')
|
|
159
159
|
const html = formatHTML(display?.innerHTML || '')
|
|
160
160
|
expect(html).toMatchInlineSnapshot(`
|
|
161
161
|
"
|
package/src/math-block-view.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Node as ProseMirrorNode } from 'prosemirror-model'
|
|
2
2
|
import type { Decoration, NodeView } from 'prosemirror-view'
|
|
3
3
|
|
|
4
|
-
import { createElement } from './create-element'
|
|
5
|
-
import { createMathViewRender } from './math-view-render'
|
|
4
|
+
import { createElement } from './create-element.ts'
|
|
5
|
+
import { createMathViewRender } from './math-view-render.ts'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* The function to render a math block.
|
|
@@ -12,13 +12,25 @@ import { createMathViewRender } from './math-view-render'
|
|
|
12
12
|
*/
|
|
13
13
|
export type RenderMathBlock = (text: string, element: HTMLElement) => void
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Creates a {@link NodeView} for a block-level math node. The view will show a
|
|
17
|
+
* source editor or a rendered display area based on the text cursor position.
|
|
18
|
+
*
|
|
19
|
+
* @param renderMathBlock - A function that renders math text (e.g. TeX) into
|
|
20
|
+
* the display element. You can use libraries like
|
|
21
|
+
* [Temml](https://temml.org/) or [KaTeX](https://katex.org/).
|
|
22
|
+
* @param node - The ProseMirror node to render.
|
|
23
|
+
* @param decorations - The decorations applied to the node.
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
15
27
|
export function createMathBlockView(renderMathBlock: RenderMathBlock, node: ProseMirrorNode, decorations: readonly Decoration[]): NodeView {
|
|
16
28
|
const code = createElement('code')
|
|
17
|
-
const source = createElement('pre', '
|
|
18
|
-
const display = createElement('div', '
|
|
19
|
-
const dom = createElement('div', '
|
|
29
|
+
const source = createElement('pre', 'prosemirror-math-source', code)
|
|
30
|
+
const display = createElement('div', 'prosemirror-math-display')
|
|
31
|
+
const dom = createElement('div', 'prosemirror-math-block', source, display)
|
|
20
32
|
|
|
21
|
-
const render = createMathViewRender(renderMathBlock, source, display)
|
|
33
|
+
const render = createMathViewRender(renderMathBlock, source, display, false)
|
|
22
34
|
|
|
23
35
|
render(node, decorations)
|
|
24
36
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
2
|
import { userEvent } from 'vitest/browser'
|
|
3
3
|
|
|
4
|
-
import { MATH_INPUT_REGEXP } from './math-inline-input-rule'
|
|
5
|
-
import { setupTest } from './testing'
|
|
4
|
+
import { MATH_INPUT_REGEXP } from './math-inline-input-rule.ts'
|
|
5
|
+
import { setupTest } from './testing.ts'
|
|
6
6
|
|
|
7
7
|
describe('MATH_INPUT_REGEXP', () => {
|
|
8
8
|
const regexp = new RegExp(MATH_INPUT_REGEXP)
|
|
@@ -19,6 +19,14 @@ export const MATH_INPUT_REGEXP: string = (
|
|
|
19
19
|
+ '$'
|
|
20
20
|
)
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Creates a ProseMirror {@link InputRule} that converts text wrapped in `$` or
|
|
24
|
+
* `$$` (e.g. `$x^2$`) into an inline math node.
|
|
25
|
+
*
|
|
26
|
+
* @param nodeType - The name of the inline math node type in your schema.
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
22
30
|
export function createMathInlineInputRule(
|
|
23
31
|
nodeType: string,
|
|
24
32
|
): InputRule {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { formatHTML } from 'diffable-html-snapshot'
|
|
2
2
|
import { describe, expect, it } from 'vitest'
|
|
3
3
|
|
|
4
|
-
import { setupTest } from './testing'
|
|
4
|
+
import { setupTest } from './testing.ts'
|
|
5
5
|
|
|
6
6
|
describe('mathInlineSpec', () => {
|
|
7
7
|
it('can serialize to HTML', () => {
|
|
@@ -13,7 +13,7 @@ describe('mathInlineSpec', () => {
|
|
|
13
13
|
"
|
|
14
14
|
<div>
|
|
15
15
|
<p>
|
|
16
|
-
<span class="
|
|
16
|
+
<span class="prosemirror-math-inline">
|
|
17
17
|
<code>
|
|
18
18
|
x^2
|
|
19
19
|
</code>
|
package/src/math-inline-spec.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { NodeSpec } from 'prosemirror-model'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* A {@link NodeSpec} for an inline math node.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
3
8
|
export const mathInlineSpec: NodeSpec = {
|
|
4
9
|
atom: false,
|
|
5
10
|
inline: true,
|
|
@@ -11,14 +16,14 @@ export const mathInlineSpec: NodeSpec = {
|
|
|
11
16
|
return [
|
|
12
17
|
'span',
|
|
13
18
|
{
|
|
14
|
-
class: '
|
|
19
|
+
class: 'prosemirror-math-inline',
|
|
15
20
|
},
|
|
16
21
|
['code', 0],
|
|
17
22
|
]
|
|
18
23
|
},
|
|
19
24
|
parseDOM: [
|
|
20
25
|
{
|
|
21
|
-
tag: 'span.
|
|
26
|
+
tag: 'span.prosemirror-math-inline',
|
|
22
27
|
contentElement: 'code',
|
|
23
28
|
},
|
|
24
29
|
],
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { formatHTML } from 'diffable-html-snapshot'
|
|
2
2
|
import { describe, expect, it } from 'vitest'
|
|
3
3
|
|
|
4
|
-
import { katexRenderer, mathjaxRenderer, renderers, setupTest, temmlRenderer } from './testing'
|
|
4
|
+
import { katexRenderer, mathjaxRenderer, renderers, setupTest, temmlRenderer } from './testing.ts'
|
|
5
5
|
|
|
6
6
|
describe.each(Object.keys(renderers))('createMathInlineView (%s)', (name) => {
|
|
7
7
|
const renderer = renderers[name as keyof typeof renderers]
|
|
@@ -11,17 +11,17 @@ describe.each(Object.keys(renderers))('createMathInlineView (%s)', (name) => {
|
|
|
11
11
|
editor.set(n.doc(n.paragraph(n.mathInline('x^2'))))
|
|
12
12
|
|
|
13
13
|
const dom = editor.view.dom
|
|
14
|
-
const mathInline = dom.querySelector('.
|
|
14
|
+
const mathInline = dom.querySelector('.prosemirror-math-inline')
|
|
15
15
|
expect(mathInline).toBeTruthy()
|
|
16
|
-
expect(mathInline?.querySelector('.
|
|
17
|
-
expect(mathInline?.querySelector('.
|
|
16
|
+
expect(mathInline?.querySelector('.prosemirror-math-source')).toBeTruthy()
|
|
17
|
+
expect(mathInline?.querySelector('.prosemirror-math-display')).toBeTruthy()
|
|
18
18
|
})
|
|
19
19
|
|
|
20
20
|
it('uses span elements for inline math', () => {
|
|
21
21
|
const { editor, n } = setupTest(renderer)
|
|
22
22
|
editor.set(n.doc(n.paragraph(n.mathInline('x'))))
|
|
23
23
|
|
|
24
|
-
const mathInline = editor.view.dom.querySelector('.
|
|
24
|
+
const mathInline = editor.view.dom.querySelector('.prosemirror-math-inline')
|
|
25
25
|
expect(mathInline?.tagName).toBe('SPAN')
|
|
26
26
|
})
|
|
27
27
|
})
|
|
@@ -31,7 +31,7 @@ describe('createMathInlineView (temml snapshot)', () => {
|
|
|
31
31
|
const { editor, n } = setupTest(temmlRenderer)
|
|
32
32
|
editor.set(n.doc(n.paragraph(n.mathInline('x^2'))))
|
|
33
33
|
|
|
34
|
-
const display = editor.view.dom.querySelector('.
|
|
34
|
+
const display = editor.view.dom.querySelector('.prosemirror-math-display')
|
|
35
35
|
const html = formatHTML(display?.innerHTML || '')
|
|
36
36
|
expect(html).toMatchInlineSnapshot(`
|
|
37
37
|
"
|
|
@@ -60,7 +60,7 @@ describe('createMathInlineView (katex snapshot)', () => {
|
|
|
60
60
|
const { editor, n } = setupTest(katexRenderer)
|
|
61
61
|
editor.set(n.doc(n.paragraph(n.mathInline('x^2'))))
|
|
62
62
|
|
|
63
|
-
const display = editor.view.dom.querySelector('.
|
|
63
|
+
const display = editor.view.dom.querySelector('.prosemirror-math-display')
|
|
64
64
|
const html = formatHTML(display?.innerHTML || '')
|
|
65
65
|
expect(html).toMatchInlineSnapshot(`
|
|
66
66
|
"
|
|
@@ -138,7 +138,7 @@ describe('createMathInlineView (mathjax snapshot)', () => {
|
|
|
138
138
|
const { editor, n } = setupTest(mathjaxRenderer)
|
|
139
139
|
editor.set(n.doc(n.paragraph(n.mathInline('x^2'))))
|
|
140
140
|
|
|
141
|
-
const display = editor.view.dom.querySelector('.
|
|
141
|
+
const display = editor.view.dom.querySelector('.prosemirror-math-display')
|
|
142
142
|
const html = formatHTML(display?.innerHTML || '')
|
|
143
143
|
expect(html).toMatchInlineSnapshot(`
|
|
144
144
|
"
|
package/src/math-inline-view.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Node as ProseMirrorNode } from 'prosemirror-model'
|
|
2
2
|
import type { Decoration, NodeView } from 'prosemirror-view'
|
|
3
3
|
|
|
4
|
-
import { createElement } from './create-element'
|
|
5
|
-
import { createMathViewRender } from './math-view-render'
|
|
4
|
+
import { createElement } from './create-element.ts'
|
|
5
|
+
import { createMathViewRender } from './math-view-render.ts'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* The function to render a math inline.
|
|
@@ -12,22 +12,35 @@ import { createMathViewRender } from './math-view-render'
|
|
|
12
12
|
*/
|
|
13
13
|
export type RenderMathInline = (text: string, element: HTMLElement) => void
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Creates a {@link NodeView} for an inline math node. The view will show a
|
|
17
|
+
* source editor or a rendered display area based on the text cursor position.
|
|
18
|
+
*
|
|
19
|
+
* @param renderMathInline - A function that renders math text (e.g. TeX) into
|
|
20
|
+
* the display element. You can use libraries like [Temml](https://temml.org/)
|
|
21
|
+
* or [KaTeX](https://katex.org/).
|
|
22
|
+
* @param node - The ProseMirror node to render.
|
|
23
|
+
* @param decorations - The decorations applied to the node.
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
15
27
|
export function createMathInlineView(
|
|
16
28
|
renderMathInline: RenderMathInline,
|
|
17
29
|
node: ProseMirrorNode,
|
|
18
30
|
decorations: readonly Decoration[],
|
|
19
31
|
): NodeView {
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
32
|
+
const code = createElement('code')
|
|
33
|
+
const source = createElement('span', 'prosemirror-math-source', code)
|
|
34
|
+
const display = createElement('span', 'prosemirror-math-display')
|
|
35
|
+
const dom = createElement('span', 'prosemirror-math-inline', source, display)
|
|
23
36
|
|
|
24
|
-
const render = createMathViewRender(renderMathInline, source, display)
|
|
37
|
+
const render = createMathViewRender(renderMathInline, source, display, true)
|
|
25
38
|
|
|
26
39
|
render(node, decorations)
|
|
27
40
|
|
|
28
41
|
return {
|
|
29
42
|
dom,
|
|
30
|
-
contentDOM:
|
|
43
|
+
contentDOM: code,
|
|
31
44
|
update: (node, decorations) => {
|
|
32
45
|
render(node, decorations)
|
|
33
46
|
return true
|
package/src/math-view-render.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Node } from 'prosemirror-model'
|
|
2
2
|
import type { Decoration } from 'prosemirror-view'
|
|
3
3
|
|
|
4
|
-
import { hasCursorInsideDecoration } from './cursor-inside-plugin'
|
|
4
|
+
import { hasCursorInsideDecoration } from './cursor-inside-plugin.ts'
|
|
5
5
|
|
|
6
6
|
type RenderMath = (text: string, element: HTMLElement) => void
|
|
7
7
|
|
|
@@ -9,6 +9,7 @@ export function createMathViewRender(
|
|
|
9
9
|
renderMath: RenderMath,
|
|
10
10
|
source: HTMLElement,
|
|
11
11
|
display: HTMLElement,
|
|
12
|
+
inline: boolean,
|
|
12
13
|
) {
|
|
13
14
|
let prevNode: Node | undefined
|
|
14
15
|
let prevText: string | undefined
|
|
@@ -32,8 +33,18 @@ export function createMathViewRender(
|
|
|
32
33
|
|
|
33
34
|
// When the math node is selected, show the source code.
|
|
34
35
|
// Otherwise, show the rendered result.
|
|
35
|
-
source.style.display = selected ? '' : 'none'
|
|
36
36
|
display.style.display = selected ? 'none' : ''
|
|
37
|
+
if (!inline) {
|
|
38
|
+
source.style.display = selected ? '' : 'none'
|
|
39
|
+
} else {
|
|
40
|
+
// For inline source code, we don't use `display: none` because we need
|
|
41
|
+
// the source text rendered in the DOM to ensure the text cursor can be
|
|
42
|
+
// placed correctly.
|
|
43
|
+
Object.assign(
|
|
44
|
+
source.style,
|
|
45
|
+
selected ? visibleInlineSourceStyle : hiddenInlineSourceStyle,
|
|
46
|
+
)
|
|
47
|
+
}
|
|
37
48
|
}
|
|
38
49
|
|
|
39
50
|
return function updateMathView(
|
|
@@ -44,3 +55,21 @@ export function createMathViewRender(
|
|
|
44
55
|
updateStyle(decorations)
|
|
45
56
|
}
|
|
46
57
|
}
|
|
58
|
+
|
|
59
|
+
const hiddenInlineSourceStyle: Partial<CSSStyleDeclaration> = {
|
|
60
|
+
display: 'inline-flex',
|
|
61
|
+
opacity: '0',
|
|
62
|
+
pointerEvents: 'none',
|
|
63
|
+
maxWidth: '0',
|
|
64
|
+
maxHeight: '0',
|
|
65
|
+
overflow: 'hidden',
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const visibleInlineSourceStyle: Partial<CSSStyleDeclaration> = {
|
|
69
|
+
display: 'inline-flex',
|
|
70
|
+
opacity: '1',
|
|
71
|
+
pointerEvents: '',
|
|
72
|
+
maxWidth: '',
|
|
73
|
+
maxHeight: '',
|
|
74
|
+
overflow: '',
|
|
75
|
+
}
|
package/src/testing.ts
CHANGED
|
@@ -19,16 +19,16 @@ import { createEnterRuleCommand, type EnterRule } from 'prosemirror-enter-rules'
|
|
|
19
19
|
import { inputRules } from 'prosemirror-inputrules'
|
|
20
20
|
import type { Attrs } from 'prosemirror-model'
|
|
21
21
|
|
|
22
|
-
import { createCursorInsidePlugin } from './cursor-inside-plugin'
|
|
23
|
-
import { renderKaTeXMathBlock, renderKaTeXMathInline } from './katex'
|
|
24
|
-
import { mathBlockEnterRule } from './math-block-enter-rule'
|
|
25
|
-
import { mathBlockSpec } from './math-block-spec'
|
|
26
|
-
import { createMathBlockView } from './math-block-view'
|
|
27
|
-
import { createMathInlineInputRule } from './math-inline-input-rule'
|
|
28
|
-
import { mathInlineSpec } from './math-inline-spec'
|
|
29
|
-
import { createMathInlineView } from './math-inline-view'
|
|
30
|
-
import { renderMathJaxMathBlock, renderMathJaxMathInline } from './mathjax'
|
|
31
|
-
import { renderTemmlMathBlock, renderTemmlMathInline } from './temml'
|
|
22
|
+
import { createCursorInsidePlugin } from './cursor-inside-plugin.ts'
|
|
23
|
+
import { renderKaTeXMathBlock, renderKaTeXMathInline } from './katex.ts'
|
|
24
|
+
import { mathBlockEnterRule } from './math-block-enter-rule.ts'
|
|
25
|
+
import { mathBlockSpec } from './math-block-spec.ts'
|
|
26
|
+
import { createMathBlockView } from './math-block-view.ts'
|
|
27
|
+
import { createMathInlineInputRule } from './math-inline-input-rule.ts'
|
|
28
|
+
import { mathInlineSpec } from './math-inline-spec.ts'
|
|
29
|
+
import { createMathInlineView } from './math-inline-view.ts'
|
|
30
|
+
import { renderMathJaxMathBlock, renderMathJaxMathInline } from './mathjax.ts'
|
|
31
|
+
import { renderTemmlMathBlock, renderTemmlMathInline } from './temml.ts'
|
|
32
32
|
|
|
33
33
|
type RenderMath = (text: string, element: HTMLElement) => void
|
|
34
34
|
|