forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
93 lines (85 loc) · 2.21 KB
/
App.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
// import logo from './logo.svg'
import { z } from 'zod';
import { DefaultBlockSchema, defaultBlockSchema } from '@blocknote/core';
import { BlockNoteView, useBlockNote, createReactBlockSpec, ReactSlashMenuItem, defaultReactSlashMenuItems } from "@blocknote/react";
import "@blocknote/core/style.css";
import styles from "./App.module.css";
export const AccordionBlock = createReactBlockSpec({
type: 'accordion',
propSchema: z.object({
label: z.string(),
autoLayout: z.object({
enabled: z.boolean(),
}).optional(),
}),
render: ({ editor, block }) => {
return (
<>
<h2 className='mb-2'>{block.props.label}</h2>
{
block.props.autoLayout?.enabled ?
(
<div className='flex flex-col'>
Enabled
</div>
) :
<></>
}
</>
);
},
containsInlineContent: false,
});
// Creates a slash menu item for inserting an image block.
export const insertAccordion = new ReactSlashMenuItem<
DefaultBlockSchema & { accordion: typeof AccordionBlock }
>(
'Insert Accordion',
(editor) => {
editor.insertBlocks(
[
// Default values are set here
{
type: 'accordion',
props: {
label: 'Default',
autoLayout: {
enabled: true
}
},
},
],
editor.getTextCursorPosition().block,
'before'
);
},
['accordion'],
'Containers',
<>+</>,
'Used to group content in an accordion.'
);
type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any };
function App() {
const editor = useBlockNote({
onEditorContentChange: (editor) => {
console.log(editor.topLevelBlocks);
},
blockSchema: {
...defaultBlockSchema,
accordion: AccordionBlock,
},
slashCommands: [
...defaultReactSlashMenuItems,
insertAccordion
],
editorDOMAttributes: {
class: styles.editor,
"data-test": "editor",
},
theme: "light",
});
// Give tests a way to get prosemirror instance
(window as WindowWithProseMirror).ProseMirror = editor?._tiptapEditor;
return <BlockNoteView editor={editor} />;
}
export default App;