@tiptap/extension-mathematics 3.0.0-beta.15 → 3.0.0-beta.17
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/dist/index.cjs +266 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +118 -17
- package/dist/index.d.ts +118 -17
- package/dist/index.js +262 -146
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/extensions/BlockMath.ts +210 -0
- package/src/extensions/InlineMath.ts +210 -0
- package/src/extensions/index.ts +2 -0
- package/src/index.ts +3 -3
- package/src/mathematics.ts +9 -16
- package/src/types.ts +4 -4
- package/src/MathematicsPlugin.ts +0 -205
package/dist/index.cjs
CHANGED
|
@@ -30,179 +30,296 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
BlockMath: () => BlockMath,
|
|
34
|
+
InlineMath: () => InlineMath,
|
|
35
|
+
Math: () => Math2,
|
|
33
36
|
Mathematics: () => Mathematics,
|
|
34
|
-
|
|
35
|
-
default: () => index_default,
|
|
36
|
-
defaultShouldRender: () => defaultShouldRender
|
|
37
|
+
default: () => index_default
|
|
37
38
|
});
|
|
38
39
|
module.exports = __toCommonJS(index_exports);
|
|
39
40
|
|
|
40
41
|
// src/mathematics.ts
|
|
41
|
-
var
|
|
42
|
+
var import_core3 = require("@tiptap/core");
|
|
42
43
|
|
|
43
|
-
// src/
|
|
44
|
+
// src/extensions/BlockMath.ts
|
|
44
45
|
var import_core = require("@tiptap/core");
|
|
45
|
-
var import_state = require("@tiptap/pm/state");
|
|
46
|
-
var import_view = require("@tiptap/pm/view");
|
|
47
46
|
var import_katex = __toESM(require("katex"), 1);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const { regex, katexOptions = {}, editor, shouldRender } = options;
|
|
79
|
-
return new import_state.Plugin({
|
|
80
|
-
key: new import_state.PluginKey("mathematics"),
|
|
81
|
-
state: {
|
|
82
|
-
init() {
|
|
83
|
-
return { decorations: void 0, isEditable: void 0 };
|
|
47
|
+
var BlockMath = import_core.Node.create({
|
|
48
|
+
name: "blockMath",
|
|
49
|
+
group: "block",
|
|
50
|
+
atom: true,
|
|
51
|
+
addOptions() {
|
|
52
|
+
return {
|
|
53
|
+
onClick: void 0
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
addAttributes() {
|
|
57
|
+
return {
|
|
58
|
+
latex: {
|
|
59
|
+
default: "",
|
|
60
|
+
parseHTML: (element) => element.getAttribute("data-latex"),
|
|
61
|
+
renderHTML: (attributes) => {
|
|
62
|
+
return {
|
|
63
|
+
"data-latex": attributes.latex
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
addCommands() {
|
|
70
|
+
return {
|
|
71
|
+
setBlockMath: (options) => ({ commands, editor }) => {
|
|
72
|
+
const { latex, pos } = options;
|
|
73
|
+
return commands.insertContentAt(pos != null ? pos : editor.state.selection.from, {
|
|
74
|
+
type: this.name,
|
|
75
|
+
attrs: { latex }
|
|
76
|
+
});
|
|
84
77
|
},
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// Are the decorations already present?
|
|
109
|
-
nextDecorationSet.find(
|
|
110
|
-
from,
|
|
111
|
-
to,
|
|
112
|
-
(deco) => isEditing === deco.isEditing && content === deco.content && isEditable === deco.isEditable && katexOptions === deco.katexOptions
|
|
113
|
-
).length
|
|
114
|
-
) {
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
decorationsToAdd.push(
|
|
118
|
-
import_view.Decoration.inline(
|
|
119
|
-
from,
|
|
120
|
-
to,
|
|
121
|
-
{
|
|
122
|
-
class: isEditing && isEditable ? "Tiptap-mathematics-editor" : "Tiptap-mathematics-editor Tiptap-mathematics-editor--hidden",
|
|
123
|
-
style: !isEditing || !isEditable ? "display: inline-block; height: 0; opacity: 0; overflow: hidden; position: absolute; width: 0;" : void 0
|
|
124
|
-
},
|
|
125
|
-
{
|
|
126
|
-
content,
|
|
127
|
-
isEditable,
|
|
128
|
-
isEditing,
|
|
129
|
-
katexOptions
|
|
130
|
-
}
|
|
131
|
-
)
|
|
132
|
-
);
|
|
133
|
-
if (!isEditable || !isEditing) {
|
|
134
|
-
decorationsToAdd.push(
|
|
135
|
-
import_view.Decoration.widget(
|
|
136
|
-
from,
|
|
137
|
-
() => {
|
|
138
|
-
const element = document.createElement("span");
|
|
139
|
-
element.classList.add("Tiptap-mathematics-render");
|
|
140
|
-
if (isEditable) {
|
|
141
|
-
element.classList.add("Tiptap-mathematics-render--editable");
|
|
142
|
-
}
|
|
143
|
-
try {
|
|
144
|
-
import_katex.default.render(content, element, katexOptions);
|
|
145
|
-
} catch {
|
|
146
|
-
element.innerHTML = content;
|
|
147
|
-
}
|
|
148
|
-
return element;
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
content,
|
|
152
|
-
isEditable,
|
|
153
|
-
isEditing,
|
|
154
|
-
katexOptions
|
|
155
|
-
}
|
|
156
|
-
)
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
78
|
+
unsetBlockMath: (options) => ({ editor, tr }) => {
|
|
79
|
+
var _a;
|
|
80
|
+
const pos = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.$from.pos;
|
|
81
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
82
|
+
if (!node || node.type.name !== this.name) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
tr.delete(pos, pos + node.nodeSize);
|
|
86
|
+
return true;
|
|
87
|
+
},
|
|
88
|
+
updateBlockMath: (options) => ({ editor, tr }) => {
|
|
89
|
+
const latex = options == null ? void 0 : options.latex;
|
|
90
|
+
let pos = options == null ? void 0 : options.pos;
|
|
91
|
+
if (pos === void 0) {
|
|
92
|
+
pos = editor.state.selection.$from.pos;
|
|
93
|
+
}
|
|
94
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
95
|
+
if (!node || node.type.name !== this.name) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
tr.setNodeMarkup(pos, this.type, {
|
|
99
|
+
...node.attrs,
|
|
100
|
+
latex: latex || node.attrs.latex
|
|
162
101
|
});
|
|
163
|
-
|
|
164
|
-
return {
|
|
165
|
-
decorations: nextDecorationSet.remove(decorationsToRemove).add(tr.doc, decorationsToAdd),
|
|
166
|
-
isEditable
|
|
167
|
-
};
|
|
102
|
+
return true;
|
|
168
103
|
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
parseHTML() {
|
|
107
|
+
return [
|
|
108
|
+
{
|
|
109
|
+
tag: 'div[data-type="block-math"]'
|
|
174
110
|
}
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
}
|
|
111
|
+
];
|
|
112
|
+
},
|
|
113
|
+
renderHTML({ HTMLAttributes }) {
|
|
114
|
+
return ["div", (0, import_core.mergeAttributes)(HTMLAttributes, { "data-type": "block-math" })];
|
|
115
|
+
},
|
|
116
|
+
addInputRules() {
|
|
117
|
+
return [
|
|
118
|
+
new import_core.InputRule({
|
|
119
|
+
find: /^\$\$\$([^$]+)\$\$\$$/,
|
|
120
|
+
handler: ({ state, range, match }) => {
|
|
121
|
+
const [, latex] = match;
|
|
122
|
+
const { tr } = state;
|
|
123
|
+
const start = range.from;
|
|
124
|
+
const end = range.to;
|
|
125
|
+
tr.replaceWith(start, end, this.type.create({ latex }));
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
];
|
|
129
|
+
},
|
|
130
|
+
addNodeView() {
|
|
131
|
+
return ({ node, getPos }) => {
|
|
132
|
+
const wrapper = document.createElement("div");
|
|
133
|
+
const innerWrapper = document.createElement("div");
|
|
134
|
+
wrapper.className = "Tiptap-mathematics-render Tiptap-mathematics-render--editable";
|
|
135
|
+
innerWrapper.className = "block-math-inner";
|
|
136
|
+
wrapper.dataset.type = "block-math";
|
|
137
|
+
wrapper.setAttribute("data-latex", node.attrs.latex);
|
|
138
|
+
wrapper.appendChild(innerWrapper);
|
|
139
|
+
function renderMath() {
|
|
140
|
+
try {
|
|
141
|
+
import_katex.default.render(node.attrs.latex, innerWrapper);
|
|
142
|
+
wrapper.classList.remove("block-math-error");
|
|
143
|
+
} catch {
|
|
144
|
+
wrapper.textContent = node.attrs.latex;
|
|
145
|
+
wrapper.classList.add("block-math-error");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const handleClick = (event) => {
|
|
149
|
+
event.preventDefault();
|
|
150
|
+
event.stopPropagation();
|
|
151
|
+
const pos = getPos();
|
|
152
|
+
if (pos == null) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (this.options.onClick) {
|
|
156
|
+
this.options.onClick(node, pos);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
if (this.options.onClick) {
|
|
160
|
+
wrapper.addEventListener("click", handleClick);
|
|
161
|
+
}
|
|
162
|
+
renderMath();
|
|
163
|
+
return {
|
|
164
|
+
dom: wrapper,
|
|
165
|
+
destroy() {
|
|
166
|
+
wrapper.removeEventListener("click", handleClick);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// src/extensions/InlineMath.ts
|
|
174
|
+
var import_core2 = require("@tiptap/core");
|
|
175
|
+
var import_katex2 = __toESM(require("katex"), 1);
|
|
176
|
+
var InlineMath = import_core2.Node.create({
|
|
177
|
+
name: "inlineMath",
|
|
178
|
+
group: "inline",
|
|
179
|
+
inline: true,
|
|
180
|
+
atom: true,
|
|
181
|
+
addOptions() {
|
|
182
|
+
return {
|
|
183
|
+
onClick: void 0
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
addAttributes() {
|
|
187
|
+
return {
|
|
188
|
+
latex: {
|
|
189
|
+
default: "",
|
|
190
|
+
parseHTML: (element) => element.getAttribute("data-latex"),
|
|
191
|
+
renderHTML: (attributes) => {
|
|
192
|
+
return {
|
|
193
|
+
"data-latex": attributes.latex
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
addCommands() {
|
|
200
|
+
return {
|
|
201
|
+
setInlineMath: (options) => ({ editor, tr }) => {
|
|
202
|
+
var _a;
|
|
203
|
+
const latex = options == null ? void 0 : options.latex;
|
|
204
|
+
const pos = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.$from.pos;
|
|
205
|
+
if (!latex) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
tr.replaceWith(pos, pos, this.type.create({ latex }));
|
|
209
|
+
return true;
|
|
210
|
+
},
|
|
211
|
+
unsetInlineMath: (options) => ({ editor, tr }) => {
|
|
212
|
+
var _a;
|
|
213
|
+
const pos = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.$from.pos;
|
|
214
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
215
|
+
if (!node || node.type.name !== this.name) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
tr.delete(pos, pos + node.nodeSize);
|
|
219
|
+
return true;
|
|
220
|
+
},
|
|
221
|
+
updateInlineMath: (options) => ({ editor, tr }) => {
|
|
222
|
+
const latex = options == null ? void 0 : options.latex;
|
|
223
|
+
let pos = options == null ? void 0 : options.pos;
|
|
224
|
+
if (pos === void 0) {
|
|
225
|
+
pos = editor.state.selection.$from.pos;
|
|
226
|
+
}
|
|
227
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
228
|
+
if (!node || node.type.name !== this.name) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
tr.setNodeMarkup(pos, this.type, { ...node.attrs, latex });
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
parseHTML() {
|
|
237
|
+
return [
|
|
238
|
+
{
|
|
239
|
+
tag: 'span[data-type="inline-math"]'
|
|
240
|
+
}
|
|
241
|
+
];
|
|
242
|
+
},
|
|
243
|
+
renderHTML({ HTMLAttributes }) {
|
|
244
|
+
return ["span", (0, import_core2.mergeAttributes)(HTMLAttributes, { "data-type": "inline-math" })];
|
|
245
|
+
},
|
|
246
|
+
addInputRules() {
|
|
247
|
+
return [
|
|
248
|
+
new import_core2.InputRule({
|
|
249
|
+
find: /(?<!\$)\$\$([^$\n]+)\$\$(?!\$)$/,
|
|
250
|
+
handler: ({ state, range, match }) => {
|
|
251
|
+
const [, latex] = match;
|
|
252
|
+
const { tr } = state;
|
|
253
|
+
const start = range.from;
|
|
254
|
+
const end = range.to;
|
|
255
|
+
tr.replaceWith(start, end, this.type.create({ latex }));
|
|
256
|
+
}
|
|
257
|
+
})
|
|
258
|
+
];
|
|
259
|
+
},
|
|
260
|
+
addNodeView() {
|
|
261
|
+
return ({ node, getPos }) => {
|
|
262
|
+
const wrapper = document.createElement("span");
|
|
263
|
+
wrapper.className = "Tiptap-mathematics-render Tiptap-mathematics-render--editable";
|
|
264
|
+
wrapper.dataset.type = "inline-math";
|
|
265
|
+
wrapper.setAttribute("data-latex", node.attrs.latex);
|
|
266
|
+
function renderMath() {
|
|
267
|
+
try {
|
|
268
|
+
import_katex2.default.render(node.attrs.latex, wrapper);
|
|
269
|
+
wrapper.classList.remove("inline-math-error");
|
|
270
|
+
} catch {
|
|
271
|
+
wrapper.textContent = node.attrs.latex;
|
|
272
|
+
wrapper.classList.add("inline-math-error");
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const handleClick = (event) => {
|
|
276
|
+
event.preventDefault();
|
|
277
|
+
event.stopPropagation();
|
|
278
|
+
const pos = getPos();
|
|
279
|
+
if (pos == null) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (this.options.onClick) {
|
|
283
|
+
this.options.onClick(node, pos);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
if (this.options.onClick) {
|
|
287
|
+
wrapper.addEventListener("click", handleClick);
|
|
288
|
+
}
|
|
289
|
+
renderMath();
|
|
290
|
+
return {
|
|
291
|
+
dom: wrapper,
|
|
292
|
+
destroy() {
|
|
293
|
+
wrapper.removeEventListener("click", handleClick);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
});
|
|
178
299
|
|
|
179
300
|
// src/mathematics.ts
|
|
180
|
-
var
|
|
181
|
-
const $pos = state.doc.resolve(pos);
|
|
182
|
-
const isInCodeBlock = $pos.parent.type.name === "codeBlock";
|
|
183
|
-
return !isInCodeBlock;
|
|
184
|
-
};
|
|
185
|
-
var Mathematics = import_core2.Extension.create({
|
|
301
|
+
var Math2 = import_core3.Extension.create({
|
|
186
302
|
name: "Mathematics",
|
|
187
303
|
addOptions() {
|
|
188
304
|
return {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
katexOptions: void 0
|
|
192
|
-
shouldRender: defaultShouldRender
|
|
305
|
+
inlineOptions: void 0,
|
|
306
|
+
blockOptions: void 0,
|
|
307
|
+
katexOptions: void 0
|
|
193
308
|
};
|
|
194
309
|
},
|
|
195
|
-
|
|
196
|
-
return [
|
|
310
|
+
addExtensions() {
|
|
311
|
+
return [BlockMath.configure(this.options.blockOptions), InlineMath.configure(this.options.inlineOptions)];
|
|
197
312
|
}
|
|
198
313
|
});
|
|
314
|
+
var Mathematics = Math2;
|
|
199
315
|
|
|
200
316
|
// src/index.ts
|
|
201
|
-
var index_default =
|
|
317
|
+
var index_default = Math2;
|
|
202
318
|
// Annotate the CommonJS export names for ESM import in node:
|
|
203
319
|
0 && (module.exports = {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
320
|
+
BlockMath,
|
|
321
|
+
InlineMath,
|
|
322
|
+
Math,
|
|
323
|
+
Mathematics
|
|
207
324
|
});
|
|
208
325
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/mathematics.ts","../src/MathematicsPlugin.ts"],"sourcesContent":["import { Mathematics } from './mathematics.js'\n\nexport * from './mathematics.js'\nexport * from './MathematicsPlugin.js'\nexport * from './types.js'\n\nexport default Mathematics\n","import { Extension } from '@tiptap/core'\nimport type { EditorState } from '@tiptap/pm/state'\n\nimport { MathematicsPlugin } from './MathematicsPlugin.js'\nimport type { MathematicsOptions } from './types.js'\n\nexport const defaultShouldRender = (state: EditorState, pos: number) => {\n const $pos = state.doc.resolve(pos)\n const isInCodeBlock = $pos.parent.type.name === 'codeBlock'\n\n return !isInCodeBlock\n}\n\nexport const Mathematics = Extension.create<MathematicsOptions>({\n name: 'Mathematics',\n\n addOptions() {\n return {\n // eslint-disable-next-line no-useless-escape\n regex: /\\$([^\\$]*)\\$/gi,\n katexOptions: undefined,\n shouldRender: defaultShouldRender,\n }\n },\n\n addProseMirrorPlugins() {\n return [MathematicsPlugin({ ...this.options, editor: this.editor })]\n },\n})\n\nexport default Mathematics\n","import { getChangedRanges } from '@tiptap/core'\nimport type { EditorState, Transaction } from '@tiptap/pm/state'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\nimport katex from 'katex'\n\nimport type { MathematicsOptionsWithEditor } from './types.js'\n\ntype DecoSpec = {\n isEditable: boolean\n isEditing: boolean\n katexOptions: MathematicsOptionsWithEditor['katexOptions']\n content: string\n}\n\ntype Deco = Omit<Decoration, 'spec'> & { spec: DecoSpec }\n\ntype PluginState =\n | { decorations: DecorationSet; isEditable: boolean }\n | { decorations: undefined; isEditable: undefined }\n\n/**\n * Get the range of positions that have been affected by a transaction\n */\nfunction getAffectedRange(\n newState: EditorState,\n previousPluginState: PluginState,\n isEditable: boolean,\n tr: Transaction,\n state: EditorState,\n) {\n const docSize = newState.doc.nodeSize - 2\n let minFrom = 0\n let maxTo = docSize\n\n if (previousPluginState.isEditable !== isEditable) {\n // When the editable state changes, run on all nodes just to be safe\n minFrom = 0\n maxTo = docSize\n } else if (tr.docChanged) {\n // When the document changes, only run on the nodes that have changed\n minFrom = docSize\n maxTo = 0\n\n getChangedRanges(tr).forEach(range => {\n // Purposefully over scan the range to ensure we catch all decorations\n minFrom = Math.min(minFrom, range.newRange.from - 1, range.oldRange.from - 1)\n maxTo = Math.max(maxTo, range.newRange.to + 1, range.oldRange.to + 1)\n })\n } else if (tr.selectionSet) {\n const { $from, $to } = state.selection\n const { $from: $newFrom, $to: $newTo } = newState.selection\n\n // When the selection changes, run on all the nodes between the old and new selection\n minFrom = Math.min(\n // Purposefully over scan the range to ensure we catch all decorations\n $from.depth === 0 ? 0 : $from.before(),\n $newFrom.depth === 0 ? 0 : $newFrom.before(),\n )\n maxTo = Math.max($to.depth === 0 ? maxTo : $to.after(), $newTo.depth === 0 ? maxTo : $newTo.after())\n }\n\n return {\n minFrom: Math.max(minFrom, 0),\n maxTo: Math.min(maxTo, docSize),\n }\n}\n\nexport const MathematicsPlugin = (options: MathematicsOptionsWithEditor) => {\n const { regex, katexOptions = {}, editor, shouldRender } = options\n\n return new Plugin<PluginState>({\n key: new PluginKey('mathematics'),\n\n state: {\n init() {\n return { decorations: undefined, isEditable: undefined }\n },\n apply(tr, previousPluginState, state, newState) {\n if (!tr.docChanged && !tr.selectionSet && previousPluginState.decorations) {\n // Just reuse the existing decorations, since nothing should have changed\n return previousPluginState\n }\n\n const nextDecorationSet = (previousPluginState.decorations || DecorationSet.empty).map(tr.mapping, tr.doc)\n const { selection } = newState\n const isEditable = editor.isEditable\n const decorationsToAdd = [] as Deco[]\n const { minFrom, maxTo } = getAffectedRange(newState, previousPluginState, isEditable, tr, state)\n\n newState.doc.nodesBetween(minFrom, maxTo, (node, pos) => {\n const enabled = shouldRender(newState, pos, node)\n\n if (node.isText && node.text && enabled) {\n let match: RegExpExecArray | null\n\n // eslint-disable-next-line no-cond-assign\n while ((match = regex.exec(node.text))) {\n const from = pos + match.index\n const to = from + match[0].length\n const content = match.slice(1).find(Boolean)\n\n if (content) {\n const selectionSize = selection.from - selection.to\n const anchorIsInside = selection.anchor >= from && selection.anchor <= to\n const rangeIsInside = selection.from >= from && selection.to <= to\n const isEditing = (selectionSize === 0 && anchorIsInside) || rangeIsInside\n\n if (\n // Are the decorations already present?\n nextDecorationSet.find(\n from,\n to,\n (deco: DecoSpec) =>\n isEditing === deco.isEditing &&\n content === deco.content &&\n isEditable === deco.isEditable &&\n katexOptions === deco.katexOptions,\n ).length\n ) {\n // Decoration exists in set, no need to add it again\n continue\n }\n // Use an inline decoration to either hide original (preview is showing) or show it (editing \"mode\")\n decorationsToAdd.push(\n Decoration.inline(\n from,\n to,\n {\n class:\n isEditing && isEditable\n ? 'Tiptap-mathematics-editor'\n : 'Tiptap-mathematics-editor Tiptap-mathematics-editor--hidden',\n style:\n !isEditing || !isEditable\n ? 'display: inline-block; height: 0; opacity: 0; overflow: hidden; position: absolute; width: 0;'\n : undefined,\n },\n {\n content,\n isEditable,\n isEditing,\n katexOptions,\n } satisfies DecoSpec,\n ),\n )\n\n if (!isEditable || !isEditing) {\n // Create decoration widget and add KaTeX preview if selection is not within the math-editor\n decorationsToAdd.push(\n Decoration.widget(\n from,\n () => {\n const element = document.createElement('span')\n\n // TODO: changeable class names\n element.classList.add('Tiptap-mathematics-render')\n\n if (isEditable) {\n element.classList.add('Tiptap-mathematics-render--editable')\n }\n\n try {\n katex.render(content!, element, katexOptions)\n } catch {\n element.innerHTML = content!\n }\n\n return element\n },\n {\n content,\n isEditable,\n isEditing,\n katexOptions,\n } satisfies DecoSpec,\n ),\n )\n }\n }\n }\n }\n })\n\n // Remove any decorations that exist at the same position, they will be replaced by the new decorations\n const decorationsToRemove = decorationsToAdd.flatMap(deco => nextDecorationSet.find(deco.from, deco.to))\n\n return {\n decorations: nextDecorationSet\n // Remove existing decorations that are going to be replaced\n .remove(decorationsToRemove)\n // Add any new decorations\n .add(tr.doc, decorationsToAdd),\n isEditable,\n }\n },\n },\n\n props: {\n decorations(state) {\n return this.getState(state)?.decorations ?? DecorationSet.empty\n },\n },\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,eAA0B;;;ACA1B,kBAAiC;AAEjC,mBAAkC;AAClC,kBAA0C;AAC1C,mBAAkB;AAoBlB,SAAS,iBACP,UACA,qBACA,YACA,IACA,OACA;AACA,QAAM,UAAU,SAAS,IAAI,WAAW;AACxC,MAAI,UAAU;AACd,MAAI,QAAQ;AAEZ,MAAI,oBAAoB,eAAe,YAAY;AAEjD,cAAU;AACV,YAAQ;AAAA,EACV,WAAW,GAAG,YAAY;AAExB,cAAU;AACV,YAAQ;AAER,sCAAiB,EAAE,EAAE,QAAQ,WAAS;AAEpC,gBAAU,KAAK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ex"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/mathematics.ts","../src/extensions/BlockMath.ts","../src/extensions/InlineMath.ts"],"sourcesContent":["import { Math } from './mathematics.js'\n\nexport * from './extensions/index.js'\nexport * from './mathematics.js'\nexport * from './types.js'\n\nexport default Math\n","import { Extension } from '@tiptap/core'\n\nimport { BlockMath, InlineMath } from './extensions/index.js'\nimport type { MathematicsOptions } from './types.js'\n\nexport const Math = Extension.create<MathematicsOptions>({\n name: 'Mathematics',\n\n addOptions() {\n return {\n inlineOptions: undefined,\n blockOptions: undefined,\n katexOptions: undefined,\n }\n },\n\n addExtensions() {\n return [BlockMath.configure(this.options.blockOptions), InlineMath.configure(this.options.inlineOptions)]\n },\n})\n\nexport const Mathematics = Math\n\nexport default Math\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex from 'katex'\n\nexport type BlockMathOptions = {\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n blockMath: {\n /**\n * Set block math node with LaTeX string.\n * @param options - Options for setting block math.\n * @returns ReturnType\n */\n setBlockMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Unset block math node.\n * @returns ReturnType\n */\n unsetBlockMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update block math node with optional LaTeX string.\n * @param options - Options for updating block math.\n * @returns ReturnType\n */\n updateBlockMath: (options?: { latex?: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * BlockMath is a Tiptap extension for rendering block mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions block within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { BlockMath } from 'your-extension-path'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * BlockMath.configure({\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const BlockMath = Node.create({\n name: 'blockMath',\n\n group: 'block',\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n setBlockMath:\n options =>\n ({ commands, editor }) => {\n const { latex, pos } = options\n return commands.insertContentAt(pos ?? editor.state.selection.from, {\n type: this.name,\n attrs: { latex },\n })\n },\n\n unsetBlockMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateBlockMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, {\n ...node.attrs,\n latex: latex || node.attrs.latex,\n })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'div[data-type=\"block-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['div', mergeAttributes(HTMLAttributes, { 'data-type': 'block-math' })]\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /^\\$\\$\\$([^$]+)\\$\\$\\$$/,\n handler: ({ state, range, match }) => {\n const [, latex] = match\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n return ({ node, getPos }) => {\n const wrapper = document.createElement('div')\n const innerWrapper = document.createElement('div')\n wrapper.className = 'Tiptap-mathematics-render Tiptap-mathematics-render--editable'\n innerWrapper.className = 'block-math-inner'\n wrapper.dataset.type = 'block-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n wrapper.appendChild(innerWrapper)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, innerWrapper)\n wrapper.classList.remove('block-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('block-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex from 'katex'\n\nexport type InlineMathOptions = {\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n inlineMath: {\n /**\n * Set inline math node with LaTeX string.\n * @param options - Options for setting inline math.\n * @returns ReturnType\n */\n setInlineMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Unset inline math node.\n * @returns ReturnType\n */\n unsetInlineMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update inline math node with optional LaTeX string.\n * @param options - Options for updating inline math.\n * @returns ReturnType\n */\n updateInlineMath: (options?: { latex?: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * InlineMath is a Tiptap extension for rendering inline mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions inline within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { InlineMath } from 'your-extension-path'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * InlineMath.configure({\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const InlineMath = Node.create<InlineMathOptions>({\n name: 'inlineMath',\n\n group: 'inline',\n\n inline: true,\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n setInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n const pos = options?.pos ?? editor.state.selection.$from.pos\n\n if (!latex) {\n return false\n }\n\n tr.replaceWith(pos, pos, this.type.create({ latex }))\n return true\n },\n\n unsetInlineMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, { ...node.attrs, latex })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'span[data-type=\"inline-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['span', mergeAttributes(HTMLAttributes, { 'data-type': 'inline-math' })]\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /(?<!\\$)\\$\\$([^$\\n]+)\\$\\$(?!\\$)$/,\n handler: ({ state, range, match }) => {\n const [, latex] = match\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n return ({ node, getPos }) => {\n const wrapper = document.createElement('span')\n wrapper.className = 'Tiptap-mathematics-render Tiptap-mathematics-render--editable'\n wrapper.dataset.type = 'inline-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, wrapper)\n wrapper.classList.remove('inline-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('inline-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA,cAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAC,eAA0B;;;ACA1B,kBAAiD;AAEjD,mBAAkB;AAoDX,IAAM,YAAY,iBAAK,OAAO;AAAA,EACnC,MAAM;AAAA,EAEN,OAAO;AAAA,EAEP,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,SAAS;AAAA,IACX;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO;AAAA,MACL,OAAO;AAAA,QACL,SAAS;AAAA,QACT,WAAW,aAAW,QAAQ,aAAa,YAAY;AAAA,QACvD,YAAY,gBAAc;AACxB,iBAAO;AAAA,YACL,cAAc,WAAW;AAAA,UAC3B;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,cAAc;AACZ,WAAO;AAAA,MACL,cACE,aACA,CAAC,EAAE,UAAU,OAAO,MAAM;AACxB,cAAM,EAAE,OAAO,IAAI,IAAI;AACvB,eAAO,SAAS,gBAAgB,oBAAO,OAAO,MAAM,UAAU,MAAM;AAAA,UAClE,MAAM,KAAK;AAAA,UAC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B,cAAM,gBAAgB;AACtB,cAAM,MAAM,OAAO;AAEnB,YAAI,OAAO,MAAM;AACf;AAAA,QACF;AAEA,YAAI,KAAK,QAAQ,SAAS;AACxB,eAAK,QAAQ,QAAQ,MAAM,GAAG;AAAA,QAChC;AAAA,MACF;AAEA,UAAI,KAAK,QAAQ,SAAS;AACxB,gBAAQ,iBAAiB,SAAS,WAAW;AAAA,MAC/C;AAEA,iBAAW;AAEX,aAAO;AAAA,QACL,KAAK;AAAA,QACL,UAAU;AACR,kBAAQ,oBAAoB,SAAS,WAAW;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;;;AF5MM,IAAMC,QAAO,uBAAU,OAA2B;AAAA,EACvD,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,eAAe;AAAA,MACf,cAAc;AAAA,MACd,cAAc;AAAA,IAChB;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO,CAAC,UAAU,UAAU,KAAK,QAAQ,YAAY,GAAG,WAAW,UAAU,KAAK,QAAQ,aAAa,CAAC;AAAA,EAC1G;AACF,CAAC;AAEM,IAAM,cAAcA;;;ADf3B,IAAO,gBAAQC;","names":["Math","import_core","katex","import_core","import_katex","katex","Math","Math"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,28 +1,129 @@
|
|
|
1
|
-
import { Editor, Extension } from '@tiptap/core';
|
|
2
|
-
import { EditorState, Plugin } from '@tiptap/pm/state';
|
|
3
|
-
import { Node } from '@tiptap/pm/model';
|
|
1
|
+
import { Node as Node$1, Editor, Extension } from '@tiptap/core';
|
|
4
2
|
import { KatexOptions } from 'katex';
|
|
5
|
-
import {
|
|
3
|
+
import { Node } from '@tiptap/pm/model';
|
|
4
|
+
|
|
5
|
+
type BlockMathOptions = {
|
|
6
|
+
onClick?: (node: Node, pos: number) => void;
|
|
7
|
+
};
|
|
8
|
+
declare module '@tiptap/core' {
|
|
9
|
+
interface Commands<ReturnType> {
|
|
10
|
+
blockMath: {
|
|
11
|
+
/**
|
|
12
|
+
* Set block math node with LaTeX string.
|
|
13
|
+
* @param options - Options for setting block math.
|
|
14
|
+
* @returns ReturnType
|
|
15
|
+
*/
|
|
16
|
+
setBlockMath: (options: {
|
|
17
|
+
latex: string;
|
|
18
|
+
pos?: number;
|
|
19
|
+
}) => ReturnType;
|
|
20
|
+
/**
|
|
21
|
+
* Unset block math node.
|
|
22
|
+
* @returns ReturnType
|
|
23
|
+
*/
|
|
24
|
+
unsetBlockMath: (options?: {
|
|
25
|
+
pos?: number;
|
|
26
|
+
}) => ReturnType;
|
|
27
|
+
/**
|
|
28
|
+
* Update block math node with optional LaTeX string.
|
|
29
|
+
* @param options - Options for updating block math.
|
|
30
|
+
* @returns ReturnType
|
|
31
|
+
*/
|
|
32
|
+
updateBlockMath: (options?: {
|
|
33
|
+
latex?: string;
|
|
34
|
+
pos?: number;
|
|
35
|
+
}) => ReturnType;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* BlockMath is a Tiptap extension for rendering block mathematical expressions using KaTeX.
|
|
41
|
+
* It allows users to insert LaTeX formatted math expressions block within text.
|
|
42
|
+
* It supports rendering, input rules for LaTeX syntax, and click handling for interaction.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```javascript
|
|
46
|
+
* import { BlockMath } from 'your-extension-path'
|
|
47
|
+
* import { Editor } from '@tiptap/core'
|
|
48
|
+
*
|
|
49
|
+
* const editor = new Editor({
|
|
50
|
+
* extensions: [
|
|
51
|
+
* BlockMath.configure({
|
|
52
|
+
* onClick: (node, pos) => {
|
|
53
|
+
* console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)
|
|
54
|
+
* },
|
|
55
|
+
* }),
|
|
56
|
+
* ],
|
|
57
|
+
* })
|
|
58
|
+
*/
|
|
59
|
+
declare const BlockMath: Node$1<any, any>;
|
|
60
|
+
|
|
61
|
+
type InlineMathOptions = {
|
|
62
|
+
onClick?: (node: Node, pos: number) => void;
|
|
63
|
+
};
|
|
64
|
+
declare module '@tiptap/core' {
|
|
65
|
+
interface Commands<ReturnType> {
|
|
66
|
+
inlineMath: {
|
|
67
|
+
/**
|
|
68
|
+
* Set inline math node with LaTeX string.
|
|
69
|
+
* @param options - Options for setting inline math.
|
|
70
|
+
* @returns ReturnType
|
|
71
|
+
*/
|
|
72
|
+
setInlineMath: (options: {
|
|
73
|
+
latex: string;
|
|
74
|
+
pos?: number;
|
|
75
|
+
}) => ReturnType;
|
|
76
|
+
/**
|
|
77
|
+
* Unset inline math node.
|
|
78
|
+
* @returns ReturnType
|
|
79
|
+
*/
|
|
80
|
+
unsetInlineMath: (options?: {
|
|
81
|
+
pos?: number;
|
|
82
|
+
}) => ReturnType;
|
|
83
|
+
/**
|
|
84
|
+
* Update inline math node with optional LaTeX string.
|
|
85
|
+
* @param options - Options for updating inline math.
|
|
86
|
+
* @returns ReturnType
|
|
87
|
+
*/
|
|
88
|
+
updateInlineMath: (options?: {
|
|
89
|
+
latex?: string;
|
|
90
|
+
pos?: number;
|
|
91
|
+
}) => ReturnType;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* InlineMath is a Tiptap extension for rendering inline mathematical expressions using KaTeX.
|
|
97
|
+
* It allows users to insert LaTeX formatted math expressions inline within text.
|
|
98
|
+
* It supports rendering, input rules for LaTeX syntax, and click handling for interaction.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```javascript
|
|
102
|
+
* import { InlineMath } from 'your-extension-path'
|
|
103
|
+
* import { Editor } from '@tiptap/core'
|
|
104
|
+
*
|
|
105
|
+
* const editor = new Editor({
|
|
106
|
+
* extensions: [
|
|
107
|
+
* InlineMath.configure({
|
|
108
|
+
* onClick: (node, pos) => {
|
|
109
|
+
* console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)
|
|
110
|
+
* },
|
|
111
|
+
* }),
|
|
112
|
+
* ],
|
|
113
|
+
* })
|
|
114
|
+
*/
|
|
115
|
+
declare const InlineMath: Node$1<InlineMathOptions, any>;
|
|
6
116
|
|
|
7
117
|
type MathematicsOptions = {
|
|
8
|
-
|
|
118
|
+
inlineOptions?: InlineMathOptions;
|
|
119
|
+
blockOptions?: BlockMathOptions;
|
|
9
120
|
katexOptions?: KatexOptions;
|
|
10
|
-
shouldRender: (state: EditorState, pos: number, node: Node) => boolean;
|
|
11
121
|
};
|
|
12
122
|
type MathematicsOptionsWithEditor = MathematicsOptions & {
|
|
13
123
|
editor: Editor;
|
|
14
124
|
};
|
|
15
125
|
|
|
16
|
-
declare const
|
|
126
|
+
declare const Math: Extension<MathematicsOptions, any>;
|
|
17
127
|
declare const Mathematics: Extension<MathematicsOptions, any>;
|
|
18
128
|
|
|
19
|
-
type
|
|
20
|
-
decorations: DecorationSet;
|
|
21
|
-
isEditable: boolean;
|
|
22
|
-
} | {
|
|
23
|
-
decorations: undefined;
|
|
24
|
-
isEditable: undefined;
|
|
25
|
-
};
|
|
26
|
-
declare const MathematicsPlugin: (options: MathematicsOptionsWithEditor) => Plugin<PluginState>;
|
|
27
|
-
|
|
28
|
-
export { Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor, MathematicsPlugin, Mathematics as default, defaultShouldRender };
|
|
129
|
+
export { BlockMath, type BlockMathOptions, InlineMath, type InlineMathOptions, Math, Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor, Math as default };
|