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
190 lines (171 loc) · 4.76 KB
/
Copy pathBlockNoteView.tsx
File metadata and controls
190 lines (171 loc) · 4.76 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import {
BlockNoteEditor,
BlockSchema,
InlineContentSchema,
StyleSchema,
mergeCSSClasses,
} from "@blocknote/core";
import React, {
HTMLAttributes,
ReactNode,
Ref,
useCallback,
useEffect,
useMemo,
useState,
} from "react";
import { useEditorChange } from "../hooks/useEditorChange.js";
import { useEditorSelectionChange } from "../hooks/useEditorSelectionChange.js";
import { usePrefersColorScheme } from "../hooks/usePrefersColorScheme.js";
import { BlockNoteContext, useBlockNoteContext } from "./BlockNoteContext.js";
import {
BlockNoteDefaultUI,
BlockNoteDefaultUIProps,
} from "./BlockNoteDefaultUI.js";
import { EditorContent } from "./EditorContent.js";
import { ElementRenderer } from "./ElementRenderer.js";
import "./styles.css";
const emptyFn = () => {
// noop
};
export type BlockNoteViewProps<
BSchema extends BlockSchema,
ISchema extends InlineContentSchema,
SSchema extends StyleSchema
> = {
editor: BlockNoteEditor<BSchema, ISchema, SSchema>;
theme?: "light" | "dark";
/**
* Locks the editor from being editable by the user if set to `false`.
*/
editable?: boolean;
/**
* A callback function that runs whenever the text cursor position or selection changes.
*/
onSelectionChange?: () => void;
/**
* A callback function that runs whenever the editor's contents change.
*/
onChange?: () => void;
children?: ReactNode;
ref?: Ref<HTMLDivElement> | undefined; // only here to get types working with the generics. Regular form doesn't work
} & Omit<
HTMLAttributes<HTMLDivElement>,
"onChange" | "onSelectionChange" | "children"
> &
BlockNoteDefaultUIProps;
function BlockNoteViewComponent<
BSchema extends BlockSchema,
ISchema extends InlineContentSchema,
SSchema extends StyleSchema
>(
props: BlockNoteViewProps<BSchema, ISchema, SSchema>,
ref: React.Ref<HTMLDivElement>
) {
const {
editor,
className,
theme,
children,
editable,
onSelectionChange,
onChange,
formattingToolbar,
linkToolbar,
slashMenu,
emojiPicker,
sideMenu,
filePanel,
tableHandles,
...rest
} = props;
// Used so other components (suggestion menu) can set
// aria related props to the contenteditable div
const [contentEditableProps, setContentEditableProps] =
useState<Record<string, any>>();
const existingContext = useBlockNoteContext();
const systemColorScheme = usePrefersColorScheme();
const defaultColorScheme =
existingContext?.colorSchemePreference || systemColorScheme;
const editorColorScheme =
theme || (defaultColorScheme === "dark" ? "dark" : "light");
useEditorChange(onChange || emptyFn, editor);
useEditorSelectionChange(onSelectionChange || emptyFn, editor);
useEffect(() => {
editor.isEditable = editable !== false;
}, [editable, editor]);
const renderChildren = useMemo(() => {
return (
<>
{children}
<BlockNoteDefaultUI
formattingToolbar={formattingToolbar}
linkToolbar={linkToolbar}
slashMenu={slashMenu}
emojiPicker={emojiPicker}
sideMenu={sideMenu}
filePanel={filePanel}
tableHandles={tableHandles}
/>
</>
);
}, [
children,
formattingToolbar,
linkToolbar,
slashMenu,
emojiPicker,
sideMenu,
filePanel,
tableHandles,
]);
const context = useMemo(() => {
return {
...existingContext,
editor,
setContentEditableProps,
};
}, [existingContext, editor]);
const setElementRenderer = useCallback(
(ref: (typeof editor)["elementRenderer"]) => {
editor.elementRenderer = ref;
},
[editor]
);
return (
<BlockNoteContext.Provider value={context as any}>
<ElementRenderer ref={setElementRenderer} />
{!editor.headless && (
<EditorContent editor={editor}>
<div
className={mergeCSSClasses(
"bn-container",
editorColorScheme || "",
className || ""
)}
data-color-scheme={editorColorScheme}
{...rest}
ref={ref}>
<div
aria-autocomplete="list"
aria-haspopup="listbox"
ref={editor.mount}
{...contentEditableProps}
/>
{renderChildren}
</div>
</EditorContent>
)}
</BlockNoteContext.Provider>
);
}
// https://fettblog.eu/typescript-react-generic-forward-refs/
export const BlockNoteViewRaw = React.forwardRef(BlockNoteViewComponent) as <
BSchema extends BlockSchema,
ISchema extends InlineContentSchema,
SSchema extends StyleSchema
>(
props: BlockNoteViewProps<BSchema, ISchema, SSchema> & {
ref?: React.ForwardedRef<HTMLDivElement>;
}
) => ReturnType<typeof BlockNoteViewComponent<BSchema, ISchema, SSchema>>;