forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactInlineContentSpec.tsx
216 lines (201 loc) · 6.64 KB
/
ReactInlineContentSpec.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import {
addInlineContentAttributes,
addInlineContentKeyboardShortcuts,
camelToDataKebab,
createInternalInlineContentSpec,
createStronglyTypedTiptapNode,
CustomInlineContentConfig,
getInlineContentParseRules,
InlineContentFromConfig,
inlineContentToNodes,
nodeToCustomInlineContent,
PartialCustomInlineContentFromConfig,
Props,
PropSchema,
propsToAttributes,
StyleSchema,
} from "@blocknote/core";
import {
NodeViewProps,
NodeViewWrapper,
ReactNodeViewRenderer,
useReactNodeView,
} from "@tiptap/react";
// import { useReactNodeView } from "@tiptap/react/dist/packages/react/src/useReactNodeView";
import { FC } from "react";
import { renderToDOMSpec } from "./@util/ReactRenderUtil.js";
// this file is mostly analogoues to `customBlocks.ts`, but for React blocks
// extend BlockConfig but use a React render function
export type ReactInlineContentImplementation<
T extends CustomInlineContentConfig,
// I extends InlineContentSchema,
S extends StyleSchema
> = {
render: FC<{
inlineContent: InlineContentFromConfig<T, S>;
updateInlineContent: (
update: PartialCustomInlineContentFromConfig<T, S>
) => void;
contentRef: (node: HTMLElement | null) => void;
node: NodeViewProps["node"];
getPos: NodeViewProps["getPos"];
selected: NodeViewProps["selected"];
}>;
// TODO?
// toExternalHTML?: FC<{
// block: BlockFromConfig<T, I, S>;
// editor: BlockNoteEditor<BlockSchemaWithBlock<T["type"], T>, I, S>;
// }>;
};
// Function that adds a wrapper with necessary classes and attributes to the
// component returned from a custom inline content's 'render' function, to
// ensure no data is lost on internal copy & paste.
export function InlineContentWrapper<
IType extends string,
PSchema extends PropSchema
>(props: {
children: JSX.Element;
inlineContentType: IType;
inlineContentProps: Props<PSchema>;
propSchema: PSchema;
}) {
return (
// Creates inline content section element
<NodeViewWrapper
as={"span"}
// Sets inline content section class
className={"bn-inline-content-section"}
// Sets content type attribute
data-inline-content-type={props.inlineContentType}
// Adds props as HTML attributes in kebab-case with "data-" prefix. Skips
// props set to their default values.
{...Object.fromEntries(
Object.entries(props.inlineContentProps)
.filter(([prop, value]) => {
const spec = props.propSchema[prop];
return value !== spec.default;
})
.map(([prop, value]) => {
return [camelToDataKebab(prop), value];
})
)}>
{props.children}
</NodeViewWrapper>
);
}
// A function to create custom block for API consumers
// we want to hide the tiptap node from API consumers and provide a simpler API surface instead
export function createReactInlineContentSpec<
T extends CustomInlineContentConfig,
// I extends InlineContentSchema,
S extends StyleSchema
>(
inlineContentConfig: T,
inlineContentImplementation: ReactInlineContentImplementation<T, S>
) {
const node = createStronglyTypedTiptapNode({
name: inlineContentConfig.type as T["type"],
inline: true,
group: "inline",
selectable: inlineContentConfig.content === "styled",
atom: inlineContentConfig.content === "none",
content: (inlineContentConfig.content === "styled"
? "inline*"
: "") as T["content"] extends "styled" ? "inline*" : "",
addAttributes() {
return propsToAttributes(inlineContentConfig.propSchema);
},
addKeyboardShortcuts() {
return addInlineContentKeyboardShortcuts(inlineContentConfig);
},
parseHTML() {
return getInlineContentParseRules(inlineContentConfig);
},
renderHTML({ node }) {
const editor = this.options.editor;
const ic = nodeToCustomInlineContent(
node,
editor.schema.inlineContentSchema,
editor.schema.styleSchema
) as any as InlineContentFromConfig<T, S>; // TODO: fix cast
const Content = inlineContentImplementation.render;
const output = renderToDOMSpec(
(refCB) => (
<Content
inlineContent={ic}
updateInlineContent={() => {
// No-op
}}
contentRef={refCB}
// @ts-expect-error
getPos={() => {}}
selected={false}
/>
),
editor
);
return addInlineContentAttributes(
output,
inlineContentConfig.type,
node.attrs as Props<T["propSchema"]>,
inlineContentConfig.propSchema
);
},
// TODO: needed?
addNodeView() {
const editor = this.options.editor;
return (props) =>
ReactNodeViewRenderer(
(props: NodeViewProps) => {
const ref = useReactNodeView().nodeViewContentRef;
if (!ref) {
throw new Error("nodeViewContentRef is not set");
}
const Content = inlineContentImplementation.render;
return (
<InlineContentWrapper
inlineContentProps={props.node.attrs as Props<T["propSchema"]>}
inlineContentType={inlineContentConfig.type}
propSchema={inlineContentConfig.propSchema}>
<Content
contentRef={ref}
inlineContent={
nodeToCustomInlineContent(
props.node,
editor.schema.inlineContentSchema,
editor.schema.styleSchema
) as any as InlineContentFromConfig<T, S> // TODO: fix cast
}
updateInlineContent={(update) => {
const content = inlineContentToNodes(
[update],
editor._tiptapEditor.schema,
editor.schema.styleSchema
);
editor.dispatch(
editor.prosemirrorView.state.tr.replaceWith(
props.getPos(),
props.getPos() + props.node.nodeSize,
content
)
);
}}
getPos={props.getPos}
selected={props.selected}
node={props.node}
/>
</InlineContentWrapper>
);
},
{
className: "bn-ic-react-node-view-renderer",
as: "span",
// contentDOMElementTag: "span", (requires tt upgrade)
}
)(props);
},
});
return createInternalInlineContentSpec(inlineContentConfig, {
node: node,
} as any);
}