forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNoteView.tsx
More file actions
89 lines (79 loc) · 2.82 KB
/
BlockNoteView.tsx
File metadata and controls
89 lines (79 loc) · 2.82 KB
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
import { BlockNoteEditor, BlockSchema, mergeCSSClasses } from "@blocknote/core";
import { createStyles, MantineProvider } from "@mantine/core";
import { EditorContent } from "@tiptap/react";
import { HTMLAttributes, ReactNode, useMemo } from "react";
import usePrefersColorScheme from "use-prefers-color-scheme";
import { blockNoteToMantineTheme, Theme } from "./BlockNoteTheme";
import { FormattingToolbarPositioner } from "./FormattingToolbar/components/FormattingToolbarPositioner";
import { HyperlinkToolbarPositioner } from "./HyperlinkToolbar/components/HyperlinkToolbarPositioner";
import { SideMenuPositioner } from "./SideMenu/components/SideMenuPositioner";
import { SlashMenuPositioner } from "./SlashMenu/components/SlashMenuPositioner";
import { darkDefaultTheme, lightDefaultTheme } from "./defaultThemes";
import { ImageToolbarPositioner } from "./ImageToolbar/components/ImageToolbarPositioner";
// Renders the editor as well as all menus & toolbars using default styles.
function BaseBlockNoteView<BSchema extends BlockSchema>(
props: {
editor: BlockNoteEditor<BSchema>;
children?: ReactNode;
} & HTMLAttributes<HTMLDivElement>
) {
const { classes } = createStyles({ root: {} })(undefined, {
name: "Editor",
});
const { editor, children, className, ...rest } = props;
return (
<EditorContent
editor={props.editor?._tiptapEditor}
className={mergeCSSClasses(classes.root, props.className || "")}
{...rest}>
{props.children || (
<>
<FormattingToolbarPositioner editor={props.editor} />
<HyperlinkToolbarPositioner editor={props.editor} />
<SlashMenuPositioner editor={props.editor} />
<SideMenuPositioner editor={props.editor} />
<ImageToolbarPositioner editor={props.editor} />
</>
)}
</EditorContent>
);
}
export function BlockNoteView<BSchema extends BlockSchema>(
props: {
editor: BlockNoteEditor<BSchema>;
theme?:
| "light"
| "dark"
| Theme
| {
light: Theme;
dark: Theme;
};
children?: ReactNode;
} & HTMLAttributes<HTMLDivElement>
) {
const {
theme = { light: lightDefaultTheme, dark: darkDefaultTheme },
...rest
} = props;
const preferredTheme = usePrefersColorScheme();
const mantineTheme = useMemo(() => {
if (theme === "light") {
return blockNoteToMantineTheme(lightDefaultTheme);
}
if (theme === "dark") {
return blockNoteToMantineTheme(darkDefaultTheme);
}
if ("light" in theme && "dark" in theme) {
return blockNoteToMantineTheme(
theme[preferredTheme === "dark" ? "dark" : "light"]
);
}
return blockNoteToMantineTheme(theme);
}, [preferredTheme, theme]);
return (
<MantineProvider theme={mantineTheme}>
<BaseBlockNoteView {...rest} />
</MantineProvider>
);
}