Skip to content

Commit 17398cc

Browse files
committed
fix: update the types to have better inference based on options provided
1 parent 87e57e3 commit 17398cc

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

packages/core/src/schema/blocks/createSpec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,46 @@ export function createBlockSpec<
290290
TName extends string,
291291
TProps extends PropSchema,
292292
TContent extends "inline" | "none",
293-
TOptions extends Record<string, any> | undefined = undefined,
293+
BlockConf extends BlockConfig<TName, TProps, TContent>,
294+
TOptions extends Partial<Record<string, any>>,
295+
>(
296+
blockCreator: (options: Partial<TOptions>) => BlockConf,
297+
blockImplementationOrCreator:
298+
| BlockImplementation<
299+
BlockConf["type"],
300+
BlockConf["propSchema"],
301+
BlockConf["content"]
302+
>
303+
| (TOptions extends undefined
304+
? () => BlockImplementation<
305+
BlockConf["type"],
306+
BlockConf["propSchema"],
307+
BlockConf["content"]
308+
>
309+
: (
310+
options: TOptions,
311+
) => BlockImplementation<
312+
BlockConf["type"],
313+
BlockConf["propSchema"],
314+
BlockConf["content"]
315+
>),
316+
extensionsOrCreator?:
317+
| BlockNoteExtension<any>[]
318+
| (TOptions extends undefined
319+
? () => BlockNoteExtension<any>[]
320+
: (options: TOptions) => BlockNoteExtension<any>[]),
321+
): (
322+
options?: TOptions,
323+
) => BlockSpec<
324+
BlockConf["type"],
325+
BlockConf["propSchema"],
326+
BlockConf["content"]
327+
>;
328+
export function createBlockSpec<
329+
TName extends string,
330+
TProps extends PropSchema,
331+
TContent extends "inline" | "none",
332+
TOptions extends Partial<Record<string, any>> | undefined = undefined,
294333
>(
295334
blockConfigOrCreator:
296335
| BlockConfig<TName, TProps, TContent>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BlockNoteSchema, defaultStyleSpecs } from "@blocknote/core";
2-
// import { createParagraphBlockSpec } from "@blocknote/core";
2+
import { createParagraphBlockSpec } from "@blocknote/core";
33

44
// this is quite convoluted. we'll clean this up when we make
55
// it easier to extend / customize the default blocks
@@ -9,8 +9,8 @@ const { textColor, backgroundColor, ...styleSpecs } = defaultStyleSpecs;
99

1010
// the schema to use for comments
1111
export const schema = BlockNoteSchema.create({
12-
// blockSpecs: {
13-
// paragraph: createParagraphBlockSpec(),
14-
// },
12+
blockSpecs: {
13+
paragraph: createParagraphBlockSpec(),
14+
},
1515
styleSpecs,
1616
});

packages/react/src/schema/ReactBlockSpec.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,49 @@ export function BlockContentWrapper<
117117
);
118118
}
119119

120+
/**
121+
* Helper function to create a React block definition.
122+
* Can accept either functions that return the required objects, or the objects directly.
123+
*/
124+
export function createReactBlockSpec<
125+
TName extends string,
126+
TProps extends PropSchema,
127+
TContent extends "inline" | "none",
128+
BlockConf extends BlockConfig<TName, TProps, TContent>,
129+
TOptions extends Partial<Record<string, any>>,
130+
>(
131+
blockCreator: (options: Partial<TOptions>) => BlockConf,
132+
blockImplementationOrCreator:
133+
| ReactCustomBlockImplementation<
134+
BlockConf["type"],
135+
BlockConf["propSchema"],
136+
BlockConf["content"]
137+
>
138+
| (TOptions extends undefined
139+
? () => ReactCustomBlockImplementation<
140+
BlockConf["type"],
141+
BlockConf["propSchema"],
142+
BlockConf["content"]
143+
>
144+
: (
145+
options: TOptions,
146+
) => ReactCustomBlockImplementation<
147+
BlockConf["type"],
148+
BlockConf["propSchema"],
149+
BlockConf["content"]
150+
>),
151+
extensionsOrCreator?:
152+
| BlockNoteExtension<any>[]
153+
| (TOptions extends undefined
154+
? () => BlockNoteExtension<any>[]
155+
: (options: TOptions) => BlockNoteExtension<any>[]),
156+
): (
157+
options?: TOptions,
158+
) => BlockSpec<
159+
BlockConf["type"],
160+
BlockConf["propSchema"],
161+
BlockConf["content"]
162+
>;
120163
export function createReactBlockSpec<
121164
TName extends string,
122165
TProps extends PropSchema,

0 commit comments

Comments
 (0)