forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridSuggestionMenuController.tsx
More file actions
181 lines (168 loc) · 5.58 KB
/
GridSuggestionMenuController.tsx
File metadata and controls
181 lines (168 loc) · 5.58 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
import { BlockSchema, InlineContentSchema, StyleSchema } from "@blocknote/core";
import { SuggestionMenu } from "@blocknote/core/extensions";
import { autoPlacement, offset, shift, size } from "@floating-ui/react";
import { FC, useEffect, useMemo } from "react";
import { useBlockNoteEditor } from "../../../hooks/useBlockNoteEditor.js";
import {
useExtension,
useExtensionState,
} from "../../../hooks/useExtension.js";
import { FloatingUIOptions } from "../../Popovers/FloatingUIOptions.js";
import { GenericPopover } from "../../Popovers/GenericPopover.js";
import { getDefaultReactEmojiPickerItems } from "./getDefaultReactEmojiPickerItems.js";
import { GridSuggestionMenu } from "./GridSuggestionMenu.js";
import { GridSuggestionMenuWrapper } from "./GridSuggestionMenuWrapper.js";
import {
DefaultReactGridSuggestionItem,
GridSuggestionMenuProps,
} from "./types.js";
type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
type ItemType<GetItemsType extends (query: string) => Promise<any[]>> =
ArrayElement<Awaited<ReturnType<GetItemsType>>>;
export function GridSuggestionMenuController<
// This is a bit hacky, but only way I found to make types work so the optionality
// of suggestionMenuComponent depends on the return type of getItems
GetItemsType extends (query: string) => Promise<any[]> = (
query: string,
) => Promise<DefaultReactGridSuggestionItem[]>,
>(
props: {
triggerCharacter: string;
getItems?: GetItemsType;
columns: number;
minQueryLength?: number;
floatingUIOptions?: FloatingUIOptions;
} & (ItemType<GetItemsType> extends DefaultReactGridSuggestionItem
? {
// can be undefined
gridSuggestionMenuComponent?: FC<
GridSuggestionMenuProps<ItemType<GetItemsType>>
>;
onItemClick?: (item: ItemType<GetItemsType>) => void;
}
: {
// getItems doesn't return DefaultSuggestionItem, so suggestionMenuComponent is required
gridSuggestionMenuComponent: FC<
GridSuggestionMenuProps<ItemType<GetItemsType>>
>;
onItemClick: (item: ItemType<GetItemsType>) => void;
}),
) {
const editor = useBlockNoteEditor<
BlockSchema,
InlineContentSchema,
StyleSchema
>();
const {
triggerCharacter,
gridSuggestionMenuComponent,
columns,
minQueryLength,
onItemClick,
getItems,
} = props;
const onItemClickOrDefault = useMemo(() => {
return (
onItemClick ||
((item: ItemType<GetItemsType>) => {
item.onItemClick(editor);
})
);
}, [editor, onItemClick]);
const getItemsOrDefault = useMemo(() => {
return (
getItems ||
((async (query: string) =>
await getDefaultReactEmojiPickerItems(
editor,
query,
)) as any as typeof getItems)
);
}, [editor, getItems])!;
const suggestionMenu = useExtension(SuggestionMenu);
useEffect(() => {
suggestionMenu.addTriggerCharacter(triggerCharacter);
}, [suggestionMenu, triggerCharacter]);
const state = useExtensionState(SuggestionMenu);
const reference = useExtensionState(SuggestionMenu, {
selector: (state) => ({
// Use first child as the editor DOM element may itself be scrollable.
// For FloatingUI to auto-update the position during scrolling, the
// `contextElement` must be a descendant of the scroll container.
element: editor.domElement?.firstChild || undefined,
getBoundingClientRect: () => state?.referencePos || new DOMRect(),
}),
});
const floatingUIOptions = useMemo<FloatingUIOptions>(
() => ({
...props.floatingUIOptions,
useFloatingOptions: {
open: state?.show && state?.triggerCharacter === triggerCharacter,
onOpenChange: (open) => {
if (!open) {
suggestionMenu.closeMenu();
}
},
placement: "bottom-start",
middleware: [
offset(10),
// Flips the menu placement to maximize the space available, and prevents
// the menu from being cut off by the confines of the screen.
autoPlacement({
allowedPlacements: ["bottom-start", "top-start"],
padding: 10,
}),
shift(),
size({
apply({ elements, availableHeight }) {
elements.floating.style.maxHeight = `${Math.max(0, availableHeight)}px`;
},
padding: 10,
}),
],
...props.floatingUIOptions?.useFloatingOptions,
},
elementProps: {
// Prevents editor blurring when clicking the scroll bar.
onMouseDownCapture: (event) => event.preventDefault(),
style: {
zIndex: 70,
},
...props.floatingUIOptions?.elementProps,
},
}),
[
props.floatingUIOptions,
state?.show,
state?.triggerCharacter,
suggestionMenu,
triggerCharacter,
],
);
if (
!state ||
(!state.ignoreQueryLength &&
minQueryLength &&
(state.query.startsWith(" ") || state.query.length < minQueryLength))
) {
return null;
}
return (
<GenericPopover reference={reference} {...floatingUIOptions}>
{triggerCharacter && (
<GridSuggestionMenuWrapper
query={state.query}
closeMenu={suggestionMenu.closeMenu}
clearQuery={suggestionMenu.clearQuery}
getItems={getItemsOrDefault}
columns={columns}
gridSuggestionMenuComponent={
gridSuggestionMenuComponent ||
GridSuggestionMenu<ItemType<GetItemsType>>
}
onItemClick={onItemClickOrDefault}
/>
)}
</GenericPopover>
);
}