-
-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathEquation.tsx
250 lines (227 loc) · 6.76 KB
/
Equation.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { InlineContentFromConfig } from "@blocknote/core";
import {
createReactInlineContentSpec,
useBlockNoteEditor,
useComponentsContext,
} from "@blocknote/react";
import { Node as TipTapNode } from "@tiptap/pm/model";
import { NodeViewWrapper } from "@tiptap/react";
import katex from "katex";
import "katex/dist/katex.min.css";
import {
ChangeEvent,
MouseEvent as ReactMouseEvent,
TextareaHTMLAttributes,
forwardRef,
useCallback,
useEffect,
useMemo,
useRef,
} from "react";
import { AiOutlineEnter } from "react-icons/ai";
import "./styles.css";
const TextareaView = forwardRef<
HTMLTextAreaElement,
{
autofocus?: boolean;
} & TextareaHTMLAttributes<HTMLTextAreaElement>
>((props, ref) => {
const { autofocus, ...rest } = props;
useEffect(() => {
if (autofocus && ref && typeof ref !== "function" && ref.current) {
ref.current.setSelectionRange(0, ref.current.value.length);
ref.current.focus();
}
}, [autofocus, ref]);
return (
<textarea
ref={ref}
className={"equation-textarea"}
value={props.value}
onChange={props.onChange}
{...rest}
/>
);
});
export const InlineEquationView = (props: {
inlineContent: InlineContentFromConfig<typeof InlineEquation.config, any>;
node: TipTapNode;
isSelected: boolean;
}) => {
const content = props.inlineContent.props.content;
const nodeSize = props.node.nodeSize;
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const contentRef = useRef<HTMLElement | null>(null);
const containerRef = useRef<HTMLElement | null>(null);
const Components = useComponentsContext()!;
const editor = useBlockNoteEditor();
const html = useMemo(
() =>
katex.renderToString(content, {
throwOnError: false,
}),
[content]
);
const getTextareaEdge = () => {
const $textarea = textareaRef.current;
if (!$textarea) {
return {};
}
return {
isLeftEdge:
$textarea.selectionStart === 0 && $textarea.selectionEnd === 0,
isRightEdge:
$textarea.selectionStart === $textarea.value.length &&
$textarea.selectionEnd === $textarea.value.length,
};
};
const getPos = useCallback((): number => {
let position = 0;
editor._tiptapEditor.state.doc.descendants(
(node: TipTapNode, pos: number) => {
if (node === props.node) {
position = pos;
return false;
}
}
);
return position;
}, [editor, props.node]);
const handleEnter = useCallback(
(event: ReactMouseEvent | React.KeyboardEvent) => {
event.preventDefault();
const pos = getPos();
if (!content) {
// TODO: implement BlockNote API to easily delete inline content
const node = props.node;
const view = editor._tiptapEditor.view;
const tr = view.state.tr.delete(pos, pos + node.nodeSize);
view.dispatch(tr);
editor._tiptapEditor.commands.setTextSelection(pos);
} else {
// TODO: implement BlockNote API to easily update cursor position
editor._tiptapEditor.commands.setTextSelection(pos + nodeSize);
}
editor.focus();
},
[content, editor, getPos, nodeSize, props.node]
);
const handleMenuNavigationKeys = useCallback(
(event: React.KeyboardEvent) => {
const textareaEdge = getTextareaEdge();
const pos = getPos();
if (event.key === "ArrowLeft") {
if (textareaEdge.isLeftEdge) {
// TODO: implement BlockNote API to set cursor position
event.preventDefault();
editor.focus();
editor._tiptapEditor.commands.setTextSelection(pos);
}
return true;
}
if (event.key === "ArrowRight") {
if (textareaEdge.isRightEdge) {
// TODO: implement BlockNote API to set cursor position
event.preventDefault();
editor.focus();
editor._tiptapEditor.commands.setTextSelection(pos + nodeSize);
}
return true;
}
if (event.key === "Enter" && props.isSelected) {
handleEnter(event);
return true;
}
return false;
},
[editor, getPos, handleEnter, nodeSize, props.isSelected]
);
// TODO: implement BlockNote API to easily update inline content
const handleChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
const val = e.target.value;
const pos = getPos();
const node = props.node;
const view = editor._tiptapEditor.view;
const tr = view.state.tr.replaceWith(
pos,
pos + node.nodeSize,
view.state.schema.nodes.inlineEquation.create(
{
...node.attrs,
content: val || "",
},
null
)
);
view.dispatch(tr);
};
return (
<NodeViewWrapper as={"span"} ref={containerRef}>
<Components.Generic.Popover.Root opened={props.isSelected}>
<Components.Generic.Popover.Trigger>
<span
className={"equation " + (props.isSelected ? "focus" : "")}
ref={contentRef}>
{!content ? (
<span className={"equation-empty"}>New Equation</span>
) : (
<span
className={"equation-content"}
dangerouslySetInnerHTML={{ __html: html }}></span>
)}
</span>
</Components.Generic.Popover.Trigger>
<Components.Generic.Popover.Content
className={"bn-popover-content bn-form-popover"}
variant={"form-popover"}>
<label className={"equation-label"}>
<TextareaView
placeholder={"c^2 = a^2 + b^2"}
ref={textareaRef}
autofocus
value={content}
onChange={handleChange}
onKeyDown={handleMenuNavigationKeys}
/>
<span onClick={handleEnter} className={"equation-enter"}>
<AiOutlineEnter />
</span>
</label>
</Components.Generic.Popover.Content>
</Components.Generic.Popover.Root>
</NodeViewWrapper>
);
};
export const InlineEquation = createReactInlineContentSpec(
{
type: "inlineEquation",
propSchema: {
content: {
default: "",
},
},
content: "none",
// copy content
renderHTML: (props) => {
const { HTMLAttributes, node } = props;
const dom = document.createElement("span");
dom.setAttribute("data-inline-content-type", "inlineEquation");
Object.keys(HTMLAttributes).forEach((key) => {
dom.setAttribute(key, HTMLAttributes[key]);
});
dom.innerText = node.attrs.content;
return { dom };
},
},
{
render: (props) => {
return (
<InlineEquationView
node={props.node}
inlineContent={props.inlineContent}
isSelected={props.isSelected}
/>
);
},
}
);