forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNoteSchema.ts
More file actions
107 lines (98 loc) · 3.11 KB
/
Copy pathBlockNoteSchema.ts
File metadata and controls
107 lines (98 loc) · 3.11 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
import {
defaultBlockSpecs,
defaultInlineContentSpecs,
defaultStyleSpecs,
} from "../blocks/defaultBlocks.js";
import type {
BlockNoDefaults,
PartialBlockNoDefaults,
} from "../schema/blocks/types.js";
import {
BlockSchema,
BlockSchemaFromSpecs,
BlockSpecs,
InlineContentSchema,
InlineContentSchemaFromSpecs,
InlineContentSpecs,
StyleSchema,
StyleSchemaFromSpecs,
StyleSpecs,
getBlockSchemaFromSpecs,
getInlineContentSchemaFromSpecs,
getStyleSchemaFromSpecs,
} from "../schema/index.js";
import type { BlockNoteEditor } from "./BlockNoteEditor.js";
function removeUndefined<T extends Record<string, any> | undefined>(obj: T): T {
if (!obj) {
return obj;
}
return Object.fromEntries(
Object.entries(obj).filter(([, value]) => value !== undefined)
) as T;
}
export class BlockNoteSchema<
BSchema extends BlockSchema,
ISchema extends InlineContentSchema,
SSchema extends StyleSchema
> {
public readonly blockSpecs: BlockSpecs;
public readonly inlineContentSpecs: InlineContentSpecs;
public readonly styleSpecs: StyleSpecs;
public readonly blockSchema: BSchema;
public readonly inlineContentSchema: ISchema;
public readonly styleSchema: SSchema;
// Helper so that you can use typeof schema.BlockNoteEditor
public readonly BlockNoteEditor: BlockNoteEditor<BSchema, ISchema, SSchema> =
"only for types" as any;
public readonly Block: BlockNoDefaults<BSchema, ISchema, SSchema> =
"only for types" as any;
public readonly PartialBlock: PartialBlockNoDefaults<
BSchema,
ISchema,
SSchema
> = "only for types" as any;
public static create<
BSpecs extends BlockSpecs = typeof defaultBlockSpecs,
ISpecs extends InlineContentSpecs = typeof defaultInlineContentSpecs,
SSpecs extends StyleSpecs = typeof defaultStyleSpecs
>(options?: {
/**
* A list of custom block types that should be available in the editor.
*/
blockSpecs?: BSpecs;
/**
* A list of custom InlineContent types that should be available in the editor.
*/
inlineContentSpecs?: ISpecs;
/**
* A list of custom Styles that should be available in the editor.
*/
styleSpecs?: SSpecs;
}) {
return new BlockNoteSchema<
BlockSchemaFromSpecs<BSpecs>,
InlineContentSchemaFromSpecs<ISpecs>,
StyleSchemaFromSpecs<SSpecs>
>(options);
// as BlockNoteSchema<
// BlockSchemaFromSpecs<BSpecs>,
// InlineContentSchemaFromSpecs<ISpecs>,
// StyleSchemaFromSpecs<SSpecs>
// >;
}
constructor(opts?: {
blockSpecs?: BlockSpecs;
inlineContentSpecs?: InlineContentSpecs;
styleSpecs?: StyleSpecs;
}) {
this.blockSpecs = removeUndefined(opts?.blockSpecs) || defaultBlockSpecs;
this.inlineContentSpecs =
removeUndefined(opts?.inlineContentSpecs) || defaultInlineContentSpecs;
this.styleSpecs = removeUndefined(opts?.styleSpecs) || defaultStyleSpecs;
this.blockSchema = getBlockSchemaFromSpecs(this.blockSpecs) as any;
this.inlineContentSchema = getInlineContentSchemaFromSpecs(
this.inlineContentSpecs
) as any;
this.styleSchema = getStyleSchemaFromSpecs(this.styleSpecs) as any;
}
}