Skip to content

Commit 6bb6271

Browse files
Added code toggle formatting toolbar example (#531)
1 parent 352c8e7 commit 6bb6271

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

packages/website/docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ const EXAMPLES_SIDEBAR = [
137137
text: "Changing Font",
138138
link: "/examples/changing-font",
139139
},
140+
{
141+
text: "Formatting Toolbar Buttons",
142+
link: "/examples/formatting-toolbar-buttons",
143+
},
140144
],
141145
},
142146
{
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
title: Formatting Toolbar Buttons
3+
description: In this example, we change add a code styles button to the formatting toolbar.
4+
imageTitle: Formatting Toolbar Buttons
5+
path: /examples/formatting-toolbar-buttons
6+
---
7+
8+
<script setup>
9+
import { useData } from 'vitepress';
10+
import { getTheme, getStyles } from "../demoUtils";
11+
12+
const { isDark } = useData();
13+
</script>
14+
15+
# Formatting Toolbar Buttons
16+
17+
In this example, we add a code style toggle button to the Formatting Toolbar.
18+
19+
**Relevant Docs:**
20+
21+
- [Custom Formatting Toolbar](/docs/formatting-toolbar#custom-formatting-toolbar)
22+
23+
::: sandbox {template=react-ts}
24+
25+
```typescript-vue /App.tsx
26+
import { BlockNoteEditor } from "@blocknote/core";
27+
import {
28+
BlockNoteView,
29+
BlockTypeDropdown,
30+
ColorStyleButton,
31+
CreateLinkButton,
32+
FormattingToolbarPositioner,
33+
FormattingToolbarProps,
34+
HyperlinkToolbarPositioner,
35+
ImageCaptionButton,
36+
ImageToolbarPositioner,
37+
NestBlockButton,
38+
ReplaceImageButton,
39+
SideMenuPositioner,
40+
SlashMenuPositioner,
41+
TableHandlesPositioner,
42+
TextAlignButton,
43+
ToggledStyleButton,
44+
Toolbar,
45+
UnnestBlockButton,
46+
useBlockNote,
47+
} from "@blocknote/react";
48+
import "@blocknote/react/style.css";
49+
50+
// From https://github.com/TypeCellOS/BlockNote/blob/main/packages/react/src/components/FormattingToolbar/DefaultFormattingToolbar.tsx
51+
function CustomFormattingToolbar(props: FormattingToolbarProps) {
52+
return (
53+
<Toolbar>
54+
<BlockTypeDropdown {...props} />
55+
56+
<ImageCaptionButton editor={props.editor} />
57+
<ReplaceImageButton editor={props.editor} />
58+
59+
<ToggledStyleButton editor={props.editor} toggledStyle={"bold"} />
60+
<ToggledStyleButton editor={props.editor} toggledStyle={"italic"} />
61+
<ToggledStyleButton editor={props.editor} toggledStyle={"underline"} />
62+
<ToggledStyleButton editor={props.editor} toggledStyle={"strike"} />
63+
{/* Added code toggle button */}
64+
<ToggledStyleButton editor={props.editor} toggledStyle={"code"} />
65+
66+
<TextAlignButton editor={props.editor as any} textAlignment={"left"} />
67+
<TextAlignButton editor={props.editor as any} textAlignment={"center"} />
68+
<TextAlignButton editor={props.editor as any} textAlignment={"right"} />
69+
70+
<ColorStyleButton editor={props.editor} />
71+
72+
<NestBlockButton editor={props.editor} />
73+
<UnnestBlockButton editor={props.editor} />
74+
75+
<CreateLinkButton editor={props.editor} />
76+
</Toolbar>
77+
);
78+
}
79+
80+
export default function App() {
81+
// Creates a new editor instance.
82+
const editor: BlockNoteEditor = useBlockNote();
83+
84+
// Renders the editor instance using a React component.
85+
return (
86+
<BlockNoteView editor={editor} theme={"{{ getTheme(isDark) }}"}>
87+
<FormattingToolbarPositioner
88+
editor={editor}
89+
formattingToolbar={CustomFormattingToolbar}
90+
/>
91+
<HyperlinkToolbarPositioner editor={editor} />
92+
<SlashMenuPositioner editor={editor} />
93+
<SideMenuPositioner editor={editor} />
94+
<ImageToolbarPositioner editor={editor} />
95+
<TableHandlesPositioner editor={editor} />
96+
</BlockNoteView>
97+
);
98+
}
99+
100+
```
101+
102+
```css-vue /styles.css [hidden]
103+
{{ getStyles(isDark) }}
104+
```

0 commit comments

Comments
 (0)