Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/equation #953

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/03-ui-components/13-equation/.bnexample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"playground": true,
"docs": false,
"author": "matthewlipski",
"tags": ["Equation", "Inline Equation", "Custom Schemas", "Latex", "Slash Menu"],
"dependencies": {
"katex": "^0.16.11",
"@types/katex": "^0.16.7",
"react-icons": "^5.2.1"
}
}
91 changes: 91 additions & 0 deletions examples/03-ui-components/13-equation/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
BlockNoteSchema,
defaultInlineContentSpecs,
filterSuggestionItems,
} from "@blocknote/core";
import "@blocknote/core/fonts/inter.css";
import {
SuggestionMenuController,
getDefaultReactSlashMenuItems,
useCreateBlockNote,
} from "@blocknote/react";
import { BlockNoteView } from "@blocknote/mantine";
import { RiFormula } from "react-icons/ri";
import "@blocknote/mantine/style.css";

import { InlineEquation } from "./Equation";

// Our schema with block specs, which contain the configs and implementations for blocks
// that we want our editor to use.
const schema = BlockNoteSchema.create({
inlineContentSpecs: {
...defaultInlineContentSpecs,
inlineEquation: InlineEquation,
},
});

// Slash menu item to insert an Alert block
const insertLaTex = (editor: typeof schema.BlockNoteEditor) => ({
icon: <RiFormula size={18}/>,
title: "Inline Equation",
key: "inlineEquation",
subtext: "Insert mathematical symbols in text.",
aliases: ["equation", "latex", "katex"],
group: "Other",
onItemClick: () => {
const view = editor._tiptapEditor.view;
const pos = editor._tiptapEditor.state.selection.from;
const tr = view.state.tr.insert(
pos,
view.state.schema.nodes.inlineEquation.create(),
);
view.dispatch(tr);
},
});

export default function App() {
// Creates a new editor instance.
const editor = useCreateBlockNote({
schema,
initialContent: [
{
type: "paragraph",
content: [
"This is an example inline equation ",
{
type: "inlineEquation",
content: "c = \\pm\\sqrt{a^2 + b^2}",
},
],
},
{
type: "paragraph",
content: "Press the '/' key to open the Slash Menu and add another",
},
{
type: "paragraph",
},
{
type: "paragraph",
},
{
type: "paragraph",
},
],
});

// Renders the editor instance.
return (
<BlockNoteView editor={editor} slashMenu={false}>
<SuggestionMenuController
triggerCharacter={"/"}
getItems={async (query: any) =>
filterSuggestionItems(
[...getDefaultReactSlashMenuItems(editor), insertLaTex(editor)],
query
)
}
/>
</BlockNoteView>
);
}
Loading
Loading