Executing Commands
Up until now, everything we've learned has been about how to write one-off logic for your specific Slate editor. But one of the most powerful things about Slate is that it lets you model your specific rich text "domain" however you'd like, and write less one-off code.
In the previous guides we've written some useful code to handle formatting code blocks and bold marks. And we've hooked up the onKeyDown handler to invoke that code. But we've always done it using the built-in Editor helpers directly, instead of using "commands".
Slate lets you augment the built-in editor object to handle your own custom rich text commands. And you can even use pre-packaged "plugins" which add a given set of functionality.
Let's see how this works.
We'll start with our app from earlier:
1
const App = () => {
2
const editor = useMemo(() => withReact(createEditor()), [])
3
const [value, setValue] = useState([
4
{
5
type: 'paragraph',
6
children: [{ text: 'A line of text in a paragraph.' }],
7
},
8
])
9
10
const renderElement = useCallback(props => {
11
switch (props.element.type) {
12
case 'code':
13
return <CodeElement {...props} />
14
default:
15
return <DefaultElement {...props} />
16
}
17
}, [])
18
19
const renderLeaf = useCallback(props => {
20
return <Leaf {...props} />
21
}, [])
22
23
return (
24
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
25
<Editable
26
renderElement={renderElement}
27
renderLeaf={renderLeaf}
28
onKeyDown={event => {
29
if (!event.ctrlKey) {
30
return
31
}
32
33
switch (event.key) {
34
case '`': {
35
event.preventDefault()
36
const [match] = Editor.nodes(editor, {
37
match: n => n.type === 'code',
38
})
39
Transforms.setNodes(
40
editor,
41
{ type: match ? null : 'code' },
42
{ match: n => Editor.isBlock(editor, n) }
43
)
44
break
45
}
46
47
case 'b': {
48
event.preventDefault()
49
Transforms.setNodes(
50
editor,
51
{ bold: true },
52
{ match: n => Text.isText(n), split: true }
53
)
54
break
55
}
56
}
57
}}
58
/>
59
</Slate>
60
)
61
}
Copied!
It has the concept of "code blocks" and "bold formatting". But these things are all defined in one-off cases inside the onKeyDown handler. If you wanted to reuse that logic elsewhere you'd need to extract it.
We can instead implement these domain-specific concepts by creating custom helper functions:
1
// Define our own custom set of helpers.
2
const CustomEditor = {
3
isBoldMarkActive(editor) {
4
const [match] = Editor.nodes(editor, {
5
match: n => n.bold === true,
6
universal: true,
7
})
8
9
return !!match
10
},
11
12
isCodeBlockActive(editor) {
13
const [match] = Editor.nodes(editor, {
14
match: n => n.type === 'code',
15
})
16
17
return !!match
18
},
19
20
toggleBoldMark(editor) {
21
const isActive = CustomEditor.isBoldMarkActive(editor)
22
Transforms.setNodes(
23
editor,
24
{ bold: isActive ? null : true },
25
{ match: n => Text.isText(n), split: true }
26
)
27
},
28
29
toggleCodeBlock(editor) {
30
const isActive = CustomEditor.isCodeBlockActive(editor)
31
Transforms.setNodes(
32
editor,
33
{ type: isActive ? null : 'code' },
34
{ match: n => Editor.isBlock(editor, n) }
35
)
36
},
37
}
38
39
const App = () => {
40
const editor = useMemo(() => withReact(createEditor()), [])
41
const [value, setValue] = useState([
42
{
43
type: 'paragraph',
44
children: [{ text: 'A line of text in a paragraph.' }],
45
},
46
])
47
48
const renderElement = useCallback(props => {
49
switch (props.element.type) {
50
case 'code':
51
return <CodeElement {...props} />
52
default:
53
return <DefaultElement {...props} />
54
}
55
}, [])
56
57
const renderLeaf = useCallback(props => {
58
return <Leaf {...props} />
59
}, [])
60
61
return (
62
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
63
<Editable
64
renderElement={renderElement}
65
renderLeaf={renderLeaf}
66
onKeyDown={event => {
67
if (!event.ctrlKey) {
68
return
69
}
70
71
// Replace the `onKeyDown` logic with our new commands.
72
switch (event.key) {
73
case '`': {
74
event.preventDefault()
75
CustomEditor.toggleCodeBlock(editor)
76
break
77
}
78
79
case 'b': {
80
event.preventDefault()
81
CustomEditor.toggleBoldMark(editor)
82
break
83
}
84
}
85
}}
86
/>
87
</Slate>
88
)
89
}
Copied!
Now our commands are clearly defined and you can invoke them from anywhere we have access to our editor object. For example, from hypothetical toolbar buttons:
1
const App = () => {
2
const editor = useMemo(() => withReact(createEditor()), [])
3
const [value, setValue] = useState([
4
{
5
type: 'paragraph',
6
children: [{ text: 'A line of text in a paragraph.' }],
7
},
8
])
9
10
const renderElement = useCallback(props => {
11
switch (props.element.type) {
12
case 'code':
13
return <CodeElement {...props} />
14
default:
15
return <DefaultElement {...props} />
16
}
17
}, [])
18
19
const renderLeaf = useCallback(props => {
20
return <Leaf {...props} />
21
}, [])
22
23
return (
24
// Add a toolbar with buttons that call the same methods.
25
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
26
<div>
27
<button
28
onMouseDown={event => {
29
event.preventDefault()
30
CustomEditor.toggleBoldMark(editor)
31
}}
32
>
33
Bold
34
</button>
35
<button
36
onMouseDown={event => {
37
event.preventDefault()
38
CustomEditor.toggleCodeBlock(editor)
39
}}
40
>
41
Code Block
42
</button>
43
</div>
44
<Editable
45
editor={editor}
46
renderElement={renderElement}
47
renderLeaf={renderLeaf}
48
onKeyDown={event => {
49
if (!event.ctrlKey) {
50
return
51
}
52
53
switch (event.key) {
54
case '`': {
55
event.preventDefault()
56
CustomEditor.toggleCodeBlock(editor)
57
break
58
}
59
60
case 'b': {
61
event.preventDefault()
62
CustomEditor.toggleBoldMark(editor)
63
break
64
}
65
}
66
}}
67
/>
68
</Slate>
69
)
70
}
Copied!
That's the benefit of extracting the logic.
And there you have it! We just added a ton of functionality to the editor with very little work. And we can keep all of our command logic tested and isolated in a single place, making the code easier to maintain.
Last modified 7mo ago
Copy link