|
| 1 | +import React, { useState, useRef, useEffect } from "react"; |
| 2 | +import { BlockNoteSchema, defaultInlineContentSpecs, |
| 3 | + InlineContentFromConfig, |
| 4 | + PartialCustomInlineContentFromConfig, |
| 5 | + CustomInlineContentConfig, } from "@blocknote/core"; |
| 6 | +import { useCreateBlockNote, |
| 7 | + createReactInlineContentSpec, |
| 8 | + useComponentsContext } from "@blocknote/react"; |
| 9 | +import { BlockNoteView } from "@blocknote/ariakit"; |
| 10 | +import katex from "katex"; |
| 11 | +import { TbMathFunction } from "react-icons/tb"; |
| 12 | + |
| 13 | +const MathBlockSchema = { |
| 14 | + type: "math", |
| 15 | + propSchema: { |
| 16 | + rawContent: { |
| 17 | + default: "a_b = c", |
| 18 | + }, |
| 19 | + }, |
| 20 | + content: "none", |
| 21 | +} as const satisfies CustomInlineContentConfig; |
| 22 | + |
| 23 | +const MathBlockComponent: React.FC<{ |
| 24 | + inlineContent: InlineContentFromConfig<typeof MathBlockSchema, any>; |
| 25 | + updateInlineContent: ( |
| 26 | + update: PartialCustomInlineContentFromConfig<typeof MathBlockSchema, any> |
| 27 | + ) => void; |
| 28 | +}> = (props) => { |
| 29 | + const Components = useComponentsContext()!; |
| 30 | + const [isOpen, setIsOpen] = useState(false); |
| 31 | + const [inputValue, setInputValue] = useState( |
| 32 | + props.inlineContent.props.rawContent |
| 33 | + ); |
| 34 | + const renderedRef = useRef<HTMLSpanElement>(null); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + katex.render(props.inlineContent.props.rawContent, renderedRef.current!, { |
| 38 | + output: "mathml", |
| 39 | + throwOnError: false, |
| 40 | + }); |
| 41 | + }, [props]); |
| 42 | + |
| 43 | + const handleClick = () => { |
| 44 | + setIsOpen(!isOpen); |
| 45 | + }; |
| 46 | + |
| 47 | + const handleSubmit = () => { |
| 48 | + props.updateInlineContent({ |
| 49 | + type: MathBlockSchema.type, |
| 50 | + props: { rawContent: inputValue }, |
| 51 | + }); |
| 52 | + setIsOpen(false); |
| 53 | + }; |
| 54 | + |
| 55 | + const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 56 | + if (e.key === "Enter") { |
| 57 | + handleSubmit(); |
| 58 | + } else if (e.key === "Escape") { |
| 59 | + setIsOpen(false); |
| 60 | + } else if (e.key === "click") { |
| 61 | + setIsOpen(false); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + const handleRawContentChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 66 | + setInputValue(e.target.value); |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <Components.Generic.Popover.Root opened={isOpen} position="bottom"> |
| 71 | + <Components.Generic.Popover.Trigger> |
| 72 | + <span ref={renderedRef} onClick={handleClick} /> |
| 73 | + </Components.Generic.Popover.Trigger> |
| 74 | + <Components.Generic.Popover.Content variant="form-popover"> |
| 75 | + <Components.Generic.Form.Root> |
| 76 | + <Components.Generic.Form.TextInput |
| 77 | + className={"bn-text-input"} |
| 78 | + name="rawContent" |
| 79 | + icon={<TbMathFunction />} |
| 80 | + autoFocus={true} |
| 81 | + placeholder="Enter your TeX code here" |
| 82 | + value={inputValue} |
| 83 | + onKeyDown={handleKeyDown} |
| 84 | + onChange={handleRawContentChange} |
| 85 | + onSubmit={handleSubmit} |
| 86 | + /> |
| 87 | + </Components.Generic.Form.Root> |
| 88 | + </Components.Generic.Popover.Content> |
| 89 | + </Components.Generic.Popover.Root> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +const MathBlock = createReactInlineContentSpec(MathBlockSchema, { |
| 94 | + render: MathBlockComponent, |
| 95 | +}); |
| 96 | + |
| 97 | +const schema = BlockNoteSchema.create({ |
| 98 | + inlineContentSpecs: { |
| 99 | + ...defaultInlineContentSpecs, |
| 100 | + math: MathBlock, |
| 101 | + }, |
| 102 | +}); |
| 103 | + |
| 104 | +export default function App() { |
| 105 | + const editor = useCreateBlockNote({ |
| 106 | + schema, |
| 107 | + initialContent: [ |
| 108 | + { |
| 109 | + type: "paragraph", |
| 110 | + content: "Welcome to this demo!", |
| 111 | + }, |
| 112 | + { |
| 113 | + type: "paragraph", |
| 114 | + content: [ |
| 115 | + "You can add a math block by typing /math and pressing enter.", |
| 116 | + { |
| 117 | + type: "math", |
| 118 | + props: { |
| 119 | + rawContent: "a_b = c", |
| 120 | + }, |
| 121 | + }, |
| 122 | + ], |
| 123 | + }, |
| 124 | + ], |
| 125 | + }); |
| 126 | + |
| 127 | + return <BlockNoteView editor={editor} />; |
| 128 | +} |
0 commit comments