You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's look at our custom alert block from the demo, and go over each field to explain how it works:
25
+
It returns a function that you can call to create an instance of your custom block, or a `BlockSpec`. This `BlockSpec` then gets passed into your [BlockNote schema](/docs/features/custom-schemas#creating-your-own-schema) to add the block to the editor. This function may also take arbitrary options, which you can find out more about [below](/docs/features/custom-schemas/custom-blocks#block-config-options).
26
+
27
+
Let's look at our custom alert block from the demo, and go over everything we pass to `createReactBlockSpec`:
25
28
26
29
```typescript
27
-
constAlert=createReactBlockSpec(
30
+
constcreateAlert=createReactBlockSpec(
28
31
{
29
32
type: "alert",
30
33
propSchema: {
@@ -54,8 +57,6 @@ type BlockConfig = {
54
57
type:string;
55
58
content:"inline"|"none";
56
59
readonly propSchema:PropSchema;
57
-
isSelectable?:boolean;
58
-
hardBreakShortcut?:"shift+enter"|"enter"|"none";
59
60
};
60
61
```
61
62
@@ -106,12 +107,6 @@ If you do not want the prop to have a default value, you can define it as an obj
`isSelectable?:` Can be set to false in order to make the block non-selectable, both using the mouse and keyboard. This also helps with being able to select non-editable content within the block. Should only be set to false when `content` is `none` and defaults to true.
110
-
111
-
`hardBreakShortcut?:` Defines which keyboard shortcut should be used to insert a hard break into the block's inline content. Defaults to `"shift+enter"`.
@@ -152,6 +156,69 @@ type ReactCustomBlockImplementation = {
152
156
153
157
-`element`: The HTML element that's being parsed.
154
158
159
+
`runsBefore?:` If this block has parsing or extensions that need to be given priority over any other blocks, you can pass their `type`s in an array here.
160
+
161
+
`meta?:` An object for setting various generic properties of the block.
162
+
163
+
-`hardBreakShortcut?:` Defines which keyboard shortcut should be used to insert a hard break into the block's inline content. Defaults to `"shift+enter"`.
164
+
165
+
-`selectable?:` Can be set to false in order to make the block non-selectable, both using the mouse and keyboard. This also helps with being able to select non-editable content within the block. Should only be set to false when `content` is `none` and defaults to true.
166
+
167
+
-`fileBlockAccept?:` For custom file blocks, this specifies which MIME types are accepted when uploading a file. All file blocks should specify this property, and should use a [`FileBlockWrapper`](https://github.com/TypeCellOS/BlockNote/blob/main/packages/react/src/blocks/File/helpers/render/FileBlockWrapper.tsx)/[`ResizableFileBlockWrapper`](https://github.com/TypeCellOS/BlockNote/blob/main/packages/react/src/blocks/File/helpers/render/ResizableFileBlockWrapper.tsx) component in their `render` functions (see next subsection).
168
+
169
+
-`code?:` Whether this block contains [code](https://prosemirror.net/docs/ref/#model.NodeSpec.code).
170
+
171
+
-`defining?:` Whether this block is [defining](https://prosemirror.net/docs/ref/#model.NodeSpec.defining).
172
+
173
+
-`isolating?:` Whether this block is [isolating](https://prosemirror.net/docs/ref/#model.NodeSpec.isolating).
174
+
175
+
### Block Extensions
176
+
177
+
While the example on this page doesn't use it, `createReactBlockSpec` takes a third, optional argument `extensions`. This is for adding editor `extensions` that are specific to the block, which you can find out more about [here](/docs/features/extensions).
178
+
179
+
Block extensions are typically things like e.g. adding keyboard shortcuts to change the current block type to a custom block. For a table of contents block, an extension could also add a ProseMirror plugin to scan for headings to put in the ToC.
180
+
181
+
### Block Config Options
182
+
183
+
In some cases, you may want to have a customizable block config. For example, you may want to be able to have a code block with syntax highlighting for either web or embedded code, or a heading block with a flexible number of heading levels. You can use the same API for this use case, with some minor changes:
184
+
185
+
```typescript
186
+
// Arbitrary options that your block can take, e.g. number of heading levels or
// Creates an instance of the custom block and adds it to the schema.
211
+
customBlock: createCustomBlock(options),
212
+
},
213
+
});
214
+
```
215
+
216
+
You can see that instead of passing plain objects for the config and implementation, we instead pass functions. These take the block options as an argument, and return the config and implementation objects respectively. Additionally, the function for creating the config is wrapped in a `createBlockConfig` function.
217
+
218
+
Also notice that for the example on this page, we create a new Alert block instance by simply calling `createAlert()` with no arguments. When a custom block takes options though, you can pass them in when creating an instance, as shown above.
219
+
220
+
To see a full example of block options being used, check out the [built-in heading block](https://github.com/TypeCellOS/BlockNote/blob/main/packages/core/src/blocks/Heading/block.ts).
221
+
155
222
## Adding Custom Blocks to the Editor
156
223
157
224
Finally, create a BlockNoteSchema using the definition of your custom blocks:
Let's look at our custom mentions tag from the demo, and go over each field to explain how it works:
24
+
It returns an instance of your custom inline content, or an `InlineContentSpec`. This `InlineContentSpec` then gets passed into your [BlockNote schema](/docs/features/custom-schemas#creating-your-own-schema) to add the inline content to the editor.
25
+
26
+
Let's look at our custom mentions tag from the demo, and go over everything we pass to `createReactInlineContentSpec`:
25
27
26
28
```typescript
27
29
const Mention =createReactInlineContentSpec(
@@ -59,8 +61,7 @@ type CustomInlineContentConfig = {
59
61
`content:``styled` if your custom inline content should contain [`StyledText`](/docs/foundations/document-structure#inline-content-objects), `none` if not.
60
62
61
63
<Callouttype="info">
62
-
_In the mentions demo, we want each mention to be a single, non-editable
63
-
element, so we set `content` to `"none"`._
64
+
_In the mentions demo, we want each mention to be a single, non-editable element, so we set `content` to `"none"`._
64
65
</Callout>
65
66
66
67
`propSchema:` The `PropSchema` specifies the props that the inline content supports. Inline content props (properties) are data stored with your inline content in the document, and can be used to customize its appearance or behavior.
@@ -95,8 +96,7 @@ If you do not want the prop to have a default value, you can define it as an obj
95
96
-`values?:` Specifies an array of values that the prop can take, for example, to limit the value to a list of pre-defined strings. If `values` is not defined, BlockNote assumes the prop can be any value of `PrimitiveType`.
96
97
97
98
<Callouttype="info">
98
-
_In the mentions demo, we add a `user` prop for the user that's being
99
-
mentioned._
99
+
_In the mentions demo, we add a `user` prop for the user that's being mentioned._
@@ -124,11 +133,20 @@ type ReactCustomInlineContentImplementation = {
124
133
125
134
-`draggable:` Specifies whether the inline content can be dragged within the editor. If set to `true`, the inline content will be draggable. Defaults to `false` if not specified. If this is true, you should add `data-drag-handle` to the DOM element that should function as the drag handle.
126
135
136
+
`toExternalHTML?:` This component is used whenever the inline content is being exported to HTML for use outside BlockNote, for example when copying it to the clipboard. If it's not defined, BlockNote will just use `render` for the HTML conversion. Takes the same props as `render`.
137
+
138
+
<Callouttype="info">
139
+
_Note that your component passed to `toExternalHTML` is rendered and serialized in a separate React root, which means you can't use hooks that rely on React Contexts._
140
+
</Callout>
141
+
142
+
`parse?:` The `parse` function defines how to parse HTML content into your inline content, for example when pasting contents from the clipboard. If the element should be parsed into your custom inline content, you return the props that the block should be given. Otherwise, return `undefined`. Takes a single argument:
143
+
144
+
-`element`: The HTML element that's being parsed.
145
+
127
146
`meta?.draggable?:` Whether the inline content should be draggable.
128
147
129
148
<Callouttype="info">
130
-
_Note that since inline content is, by definition, inline, your component
131
-
should also return an HTML inline element._
149
+
_Note that since inline content is, by definition, inline, your component should also return an HTML inline element._
Copy file name to clipboardExpand all lines: docs/content/docs/features/custom-schemas/custom-styles.mdx
+18-5Lines changed: 18 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,10 +18,12 @@ Use the `createReactStyleSpec` function to create a custom style type. This func
18
18
function createReactStyleSpec(
19
19
styleConfig:CustomStyleConfig,
20
20
styleImplementation:ReactStyleImplementation,
21
-
);
21
+
):StyleSpec;
22
22
```
23
23
24
-
Let's look at our custom font style from the demo, and go over each field to explain how it works:
24
+
It returns an instance of your custom inline content, or a `StyleSpec`. This `StyleSpec` then gets passed into your [BlockNote schema](/docs/features/custom-schemas#creating-your-own-schema) to add the style to the editor.
25
+
26
+
Let's look at our custom font style from the demo, and go over everything we pass to `createReactStyleSpec`:
25
27
26
28
```typescript
27
29
exportconst Font =createReactStyleSpec(
@@ -67,6 +69,11 @@ type ReactCustomStyleImplementation = {
@@ -76,12 +83,18 @@ type ReactCustomStyleImplementation = {
76
83
77
84
-`contentRef:` A React `ref` to mark the editable element.
78
85
86
+
`toExternalHTML?:` This component is used whenever the style is being exported to HTML for use outside BlockNote, for example when copying it to the clipboard. If it's not defined, BlockNote will just use `render` for the HTML conversion. Takes the same props as `render`.
87
+
79
88
<Callouttype="info">
80
-
_Note that in contrast to Custom Blocks and Inline Content, the `render`
81
-
function of Custom Styles cannot access React Context or other state. They
82
-
should be plain React functions analogous to the example._
89
+
_Note that your component passed to `toExternalHTML` is rendered and
90
+
serialized in a separate React root, which means you can't use hooks that rely
91
+
on React Contexts._
83
92
</Callout>
84
93
94
+
`parse?:` The `parse` function defines how to parse HTML content into your style, for example when pasting contents from the clipboard. If the element should be parsed into your custom style, you return a `string` or `true`. If the `propSchema` is `"string"`, you should likewise return a string value, or `true` otherwise. Returning `undefined` will not parse the style from the HTML element. Takes a single argument:
95
+
96
+
-`element`: The HTML element that's being parsed.
97
+
85
98
## Adding Custom Style to the Editor
86
99
87
100
Finally, create a BlockNoteSchema using the definition of your custom style:
Copy file name to clipboardExpand all lines: docs/content/docs/features/custom-schemas/index.mdx
+48-11Lines changed: 48 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,35 +28,72 @@ Text Styles are properties that can be applied to a piece of text, such as bold,
28
28
29
29
## Creating your own schema
30
30
31
-
Once you have defined your custom blocks (see the links above), inline content or styles, you can create a schema and pass this to the initialization of the editor.
31
+
Once you have defined your custom blocks (see the links above), inline content or styles, you can create a schema and pass this to the initialization of the editor. There are two ways to create a new schema.
32
+
33
+
### Extending an existing schema
34
+
35
+
You can call `BlockNoteSchema.extend` to add custom blocks, inline content, or styles to an existing schema. While this works for any existing schema, it's most common to use this to extend the default schema.
36
+
37
+
```typescript
38
+
// Creates an instance of the default schema when nothing is passed to
39
+
// `BlockNoteSchema.create`.
40
+
const schema =BlockNoteSchema.create()
41
+
// Adds custom blocks, inline content, or styles to the default schema.
42
+
.extend({
43
+
blockSpecs: {
44
+
// Add your own custom blocks:
45
+
customBlock: CustomBlock,
46
+
...
47
+
},
48
+
inlineContentSpecs: {
49
+
// Add your own custom inline content:
50
+
customInlineContent: CustomInlineContent,
51
+
...
52
+
},
53
+
styleSpecs: {
54
+
// Add your own custom styles:
55
+
customStyle: CustomStyle,
56
+
...
57
+
},
58
+
});
59
+
```
60
+
61
+
### Creating a schema from scratch
62
+
63
+
Passing custom blocks, inline content, or styles directly into `BlockNoteSchema.create` will produce a new schema with only the things you pass. This can be useful if you only need a few basic things from the default schema, and intend to implement everything else yourself.
32
64
33
65
```typescript
34
66
const schema =BlockNoteSchema.create({
35
67
blockSpecs: {
36
-
//enable the default blocks if desired
37
-
...defaultBlockSpecs,
68
+
//Add only the default paragraph block:
69
+
paragraph: defaultBlockSpecs.paragraph,
38
70
39
71
// Add your own custom blocks:
40
-
// customBlock: CustomBlock,
72
+
customBlock: CustomBlock,
73
+
...
41
74
},
42
75
inlineContentSpecs: {
43
-
//enable the default inline content if desired
44
-
...defaultInlineContentSpecs,
76
+
//Add only the default text inline content:
77
+
text: defaultInlineContentSpecs.text,
45
78
46
79
// Add your own custom inline content:
47
-
// customInlineContent: CustomInlineContent,
80
+
customInlineContent: CustomInlineContent,
81
+
...
48
82
},
49
83
styleSpecs: {
50
-
//enable the default styles if desired
51
-
...defaultStyleSpecs,
84
+
//Add only the default bold style:
85
+
bold: defaultStyleSpecs.bold,
52
86
53
87
// Add your own custom styles:
54
-
// customStyle: CustomStyle
88
+
customStyle: CustomStyle,
89
+
...
55
90
},
56
91
});
57
92
```
58
93
59
-
You can then pass this to the instantiation of your BlockNoteEditor (`BlockNoteEditor.create` or `useCreateBlockNote`):
94
+
## Using your own schema
95
+
96
+
Once you've created an instance of your schema using `BlockNoteSchema.create` or `BlockNoteSchema.extend`, you can pass it to the `schema` option of your BlockNoteEditor (`BlockNoteEditor.create` or `useCreateBlockNote`):
0 commit comments