@lofcz/platejs-math 53.0.0 → 53.4.10
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.
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ElementApi, KEYS, bindFirst, createSlatePlugin } from "platejs";
|
|
2
|
+
import "katex/dist/katex.min.css";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/transforms/insertEquation.ts
|
|
5
|
+
const insertEquation = (editor, options) => {
|
|
6
|
+
editor.tf.insertNodes({
|
|
7
|
+
children: [{ text: "" }],
|
|
8
|
+
texExpression: "",
|
|
9
|
+
type: editor.getType(KEYS.equation)
|
|
10
|
+
}, options);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/lib/transforms/insertInlineEquation.ts
|
|
15
|
+
const insertInlineEquation = (editor, texExpression, options) => {
|
|
16
|
+
editor.tf.insertNodes({
|
|
17
|
+
children: [{ text: "" }],
|
|
18
|
+
texExpression: texExpression ?? editor.api.string(editor.selection),
|
|
19
|
+
type: editor.getType(KEYS.inlineEquation)
|
|
20
|
+
}, options);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/lib/getEquationExpression.internal.ts
|
|
25
|
+
const getEquationExpression = (element) => typeof element.texExpression === "string" || typeof element.texExpression === "number" || typeof element.texExpression === "boolean" ? String(element.texExpression) : "";
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/lib/withEquation.internal.ts
|
|
29
|
+
const withEquation = ({ editor, tf: { normalizeNode }, type }) => ({ transforms: { normalizeNode(entry) {
|
|
30
|
+
const [node, path] = entry;
|
|
31
|
+
if (ElementApi.isElement(node) && node.type === type && typeof node.texExpression !== "string") {
|
|
32
|
+
editor.tf.setNodes({ texExpression: getEquationExpression({ texExpression: node.texExpression }) }, { at: path });
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
return normalizeNode(entry);
|
|
36
|
+
} } });
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/lib/BaseEquationPlugin.ts
|
|
40
|
+
const BaseEquationPlugin = createSlatePlugin({
|
|
41
|
+
key: KEYS.equation,
|
|
42
|
+
node: {
|
|
43
|
+
isElement: true,
|
|
44
|
+
isVoid: true
|
|
45
|
+
},
|
|
46
|
+
parsers: { html: { deserializer: { toNodeProps: ({ element }) => ({ texExpression: element.getAttribute("data-slate-tex-expression") ?? "" }) } } }
|
|
47
|
+
}).overrideEditor(withEquation).extendEditorTransforms(({ editor }) => ({ insert: { equation: bindFirst(insertEquation, editor) } }));
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/lib/BaseInlineEquationPlugin.ts
|
|
51
|
+
const BaseInlineEquationPlugin = createSlatePlugin({
|
|
52
|
+
key: KEYS.inlineEquation,
|
|
53
|
+
node: {
|
|
54
|
+
isElement: true,
|
|
55
|
+
isInline: true,
|
|
56
|
+
isVoid: true
|
|
57
|
+
},
|
|
58
|
+
parsers: { html: { deserializer: { toNodeProps: ({ element }) => ({ texExpression: element.getAttribute("data-slate-tex-expression") ?? "" }) } } }
|
|
59
|
+
}).overrideEditor(withEquation).extendEditorTransforms(({ editor }) => ({ insert: { inlineEquation: bindFirst(insertInlineEquation, editor) } }));
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { insertEquation as a, insertInlineEquation as i, BaseEquationPlugin as n, getEquationExpression as r, BaseInlineEquationPlugin as t };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as insertEquation, i as insertInlineEquation, n as BaseEquationPlugin, r as getEquationExpression, t as BaseInlineEquationPlugin } from "./BaseInlineEquationPlugin-BtcwMqRJ.js";
|
|
2
2
|
import { KEYS, createRuleFactory, matchDelimitedInline } from "platejs";
|
|
3
3
|
import katex from "katex";
|
|
4
4
|
|
|
@@ -59,7 +59,7 @@ const MathRules = { markdown: createRuleFactory((options) => options.variant ===
|
|
|
59
59
|
|
|
60
60
|
//#endregion
|
|
61
61
|
//#region src/lib/utils/getEquationHtml.ts
|
|
62
|
-
const getEquationHtml = ({ element, options }) => katex.renderToString(element
|
|
62
|
+
const getEquationHtml = ({ element, options }) => katex.renderToString(getEquationExpression(element), options);
|
|
63
63
|
|
|
64
64
|
//#endregion
|
|
65
65
|
export { BaseEquationPlugin, BaseInlineEquationPlugin, MathRules, getEquationHtml, insertEquation, insertInlineEquation };
|
package/dist/react/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as BaseEquationPlugin, t as BaseInlineEquationPlugin } from "../BaseInlineEquationPlugin-
|
|
1
|
+
import { n as BaseEquationPlugin, r as getEquationExpression, t as BaseInlineEquationPlugin } from "../BaseInlineEquationPlugin-BtcwMqRJ.js";
|
|
2
2
|
import { isHotkey } from "platejs";
|
|
3
3
|
import katex from "katex";
|
|
4
4
|
import { toPlatePlugin, useEditorRef, useElement } from "platejs/react";
|
|
@@ -16,7 +16,7 @@ const InlineEquationPlugin = toPlatePlugin(BaseInlineEquationPlugin);
|
|
|
16
16
|
const useEquationElement = ({ element, katexRef, options }) => {
|
|
17
17
|
React.useEffect(() => {
|
|
18
18
|
if (!katexRef.current) return;
|
|
19
|
-
katex.render(element
|
|
19
|
+
katex.render(getEquationExpression(element), katexRef.current, options);
|
|
20
20
|
}, [element.texExpression]);
|
|
21
21
|
};
|
|
22
22
|
|
|
@@ -26,14 +26,14 @@ const useEquationInput = ({ isInline, open, onClose }) => {
|
|
|
26
26
|
const editor = useEditorRef();
|
|
27
27
|
const element = useElement();
|
|
28
28
|
const inputRef = useRef(null);
|
|
29
|
-
const [expressionInput, setExpressionInput] = React.useState(element
|
|
30
|
-
const initialExpressionRef = useRef(element
|
|
29
|
+
const [expressionInput, setExpressionInput] = React.useState(getEquationExpression(element));
|
|
30
|
+
const initialExpressionRef = useRef(getEquationExpression(element));
|
|
31
31
|
useEffect(() => {
|
|
32
32
|
if (open) setTimeout(() => {
|
|
33
33
|
if (inputRef.current) {
|
|
34
34
|
inputRef.current.focus();
|
|
35
35
|
inputRef.current.select();
|
|
36
|
-
if (isInline) initialExpressionRef.current = element
|
|
36
|
+
if (isInline) initialExpressionRef.current = getEquationExpression(element);
|
|
37
37
|
}
|
|
38
38
|
}, 0);
|
|
39
39
|
}, [open]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lofcz/platejs-math",
|
|
3
|
-
"version": "53.
|
|
3
|
+
"version": "53.4.10",
|
|
4
4
|
"description": "Math plugin for Plate",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"plate",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/katex": "0.16.7",
|
|
37
37
|
"@plate/scripts": "1.0.0",
|
|
38
|
-
"platejs": "npm:@lofcz/platejs@53.
|
|
38
|
+
"platejs": "npm:@lofcz/platejs@53.4.9"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"platejs": ">=53.0.0",
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { KEYS, bindFirst, createSlatePlugin } from "platejs";
|
|
2
|
-
import "katex/dist/katex.min.css";
|
|
3
|
-
|
|
4
|
-
//#region src/lib/transforms/insertEquation.ts
|
|
5
|
-
const insertEquation = (editor, options) => {
|
|
6
|
-
editor.tf.insertNodes({
|
|
7
|
-
children: [{ text: "" }],
|
|
8
|
-
texExpression: "",
|
|
9
|
-
type: editor.getType(KEYS.equation)
|
|
10
|
-
}, options);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
//#endregion
|
|
14
|
-
//#region src/lib/transforms/insertInlineEquation.ts
|
|
15
|
-
const insertInlineEquation = (editor, texExpression, options) => {
|
|
16
|
-
editor.tf.insertNodes({
|
|
17
|
-
children: [{ text: "" }],
|
|
18
|
-
texExpression: texExpression ?? editor.api.string(editor.selection),
|
|
19
|
-
type: editor.getType(KEYS.inlineEquation)
|
|
20
|
-
}, options);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
|
-
//#region src/lib/BaseEquationPlugin.ts
|
|
25
|
-
const BaseEquationPlugin = createSlatePlugin({
|
|
26
|
-
key: KEYS.equation,
|
|
27
|
-
node: {
|
|
28
|
-
isElement: true,
|
|
29
|
-
isVoid: true
|
|
30
|
-
}
|
|
31
|
-
}).extendEditorTransforms(({ editor }) => ({ insert: { equation: bindFirst(insertEquation, editor) } }));
|
|
32
|
-
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/lib/BaseInlineEquationPlugin.ts
|
|
35
|
-
const BaseInlineEquationPlugin = createSlatePlugin({
|
|
36
|
-
key: KEYS.inlineEquation,
|
|
37
|
-
node: {
|
|
38
|
-
isElement: true,
|
|
39
|
-
isInline: true,
|
|
40
|
-
isVoid: true
|
|
41
|
-
}
|
|
42
|
-
}).extendEditorTransforms(({ editor }) => ({ insert: { inlineEquation: bindFirst(insertInlineEquation, editor) } }));
|
|
43
|
-
|
|
44
|
-
//#endregion
|
|
45
|
-
export { insertEquation as i, BaseEquationPlugin as n, insertInlineEquation as r, BaseInlineEquationPlugin as t };
|