Skip to content

Commit 82527fd

Browse files
feat: added type guards, types, and editor prop to custom inline content rendering (#1736)
1 parent 541c747 commit 82527fd

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

packages/core/src/blocks/defaultBlockTypeGuards.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { CellSelection } from "prosemirror-tables";
22
import type { BlockNoteEditor } from "../editor/BlockNoteEditor.js";
33
import {
4+
BlockConfig,
45
BlockFromConfig,
56
BlockSchema,
67
FileBlockConfig,
8+
InlineContentConfig,
79
InlineContentSchema,
810
StyleSchema,
911
} from "../schema/index.js";
@@ -35,6 +37,20 @@ export function checkDefaultBlockTypeInSchema<
3537
);
3638
}
3739

40+
export function checkBlockTypeInSchema<
41+
BlockType extends string,
42+
Config extends BlockConfig,
43+
>(
44+
blockType: BlockType,
45+
blockConfig: Config,
46+
editor: BlockNoteEditor<any, any, any>,
47+
): editor is BlockNoteEditor<{ [T in BlockType]: Config }, any, any> {
48+
return (
49+
blockType in editor.schema.blockSchema &&
50+
editor.schema.blockSchema[blockType] === blockConfig
51+
);
52+
}
53+
3854
export function checkDefaultInlineContentTypeInSchema<
3955
InlineContentType extends keyof DefaultInlineContentSchema,
4056
B extends BlockSchema,
@@ -54,6 +70,20 @@ export function checkDefaultInlineContentTypeInSchema<
5470
);
5571
}
5672

73+
export function checkInlineContentTypeInSchema<
74+
InlineContentType extends string,
75+
Config extends InlineContentConfig,
76+
>(
77+
inlineContentType: InlineContentType,
78+
inlineContentConfig: Config,
79+
editor: BlockNoteEditor<any, any, any>,
80+
): editor is BlockNoteEditor<any, { [T in InlineContentType]: Config }, any> {
81+
return (
82+
inlineContentType in editor.schema.inlineContentSchema &&
83+
editor.schema.inlineContentSchema[inlineContentType] === inlineContentConfig
84+
);
85+
}
86+
5787
export function checkBlockIsDefaultType<
5888
BlockType extends keyof DefaultBlockSchema,
5989
I extends InlineContentSchema,

packages/core/src/schema/inlineContent/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ export type InlineContentImplementation<T extends InlineContentConfig> =
2323
node: Node;
2424
};
2525

26+
export type InlineContentSchemaWithInlineContent<
27+
IType extends string,
28+
C extends InlineContentConfig,
29+
> = {
30+
[k in IType]: C;
31+
};
32+
2633
// Container for both the config and implementation of InlineContent,
2734
// and the type of `implementation` is based on that of the config
2835
export type InlineContentSpec<T extends InlineContentConfig> = {

packages/react/src/schema/ReactInlineContentSpec.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
propsToAttributes,
1616
StyleSchema,
1717
BlockNoteEditor,
18+
InlineContentSchemaWithInlineContent,
1819
} from "@blocknote/core";
1920
import {
2021
NodeViewProps,
@@ -27,19 +28,29 @@ import { FC, JSX } from "react";
2728
import { renderToDOMSpec } from "./@util/ReactRenderUtil.js";
2829
// this file is mostly analogoues to `customBlocks.ts`, but for React blocks
2930

31+
export type ReactCustomInlineContentRenderProps<
32+
T extends CustomInlineContentConfig,
33+
S extends StyleSchema,
34+
> = {
35+
inlineContent: InlineContentFromConfig<T, S>;
36+
updateInlineContent: (
37+
update: PartialCustomInlineContentFromConfig<T, S>,
38+
) => void;
39+
editor: BlockNoteEditor<
40+
any,
41+
InlineContentSchemaWithInlineContent<T["type"], T>,
42+
S
43+
>;
44+
contentRef: (node: HTMLElement | null) => void;
45+
};
46+
3047
// extend BlockConfig but use a React render function
3148
export type ReactInlineContentImplementation<
3249
T extends CustomInlineContentConfig,
3350
// I extends InlineContentSchema,
3451
S extends StyleSchema,
3552
> = {
36-
render: FC<{
37-
inlineContent: InlineContentFromConfig<T, S>;
38-
updateInlineContent: (
39-
update: PartialCustomInlineContentFromConfig<T, S>,
40-
) => void;
41-
contentRef: (node: HTMLElement | null) => void;
42-
}>;
53+
render: FC<ReactCustomInlineContentRenderProps<T, S>>;
4354
// TODO?
4455
// toExternalHTML?: FC<{
4556
// block: BlockFromConfig<T, I, S>;
@@ -134,6 +145,7 @@ export function createReactInlineContentSpec<
134145
updateInlineContent={() => {
135146
// No-op
136147
}}
148+
editor={editor}
137149
contentRef={refCB}
138150
/>
139151
),
@@ -169,6 +181,7 @@ export function createReactInlineContentSpec<
169181
>
170182
<Content
171183
contentRef={ref}
184+
editor={editor}
172185
inlineContent={
173186
nodeToCustomInlineContent(
174187
props.node,

0 commit comments

Comments
 (0)