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.
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 extending the editor object:
// Create a custom editor plugin function that will augment the editor.constwithCustom= editor => {return editor}constApp= () => {// Wrap the editor with our new `withCustom` plugin.consteditor=useMemo(() =>withCustom(withReact(createEditor())), [])const [value,setValue] =useState([ { type:'paragraph', children: [{ text:'A line of text in a paragraph.' }], }, ])constrenderElement=useCallback(props => {switch (props.element.type) {case'code':return <CodeElement {...props} />default:return <DefaultElement {...props} /> } }, [])constrenderLeaf=useCallback(props => {return <Leaf {...props} /> }, [])return ( <Slateeditor={editor} value={value} onChange={value =>setValue(value)}> <EditablerenderElement={renderElement}renderLeaf={renderLeaf}onKeyDown={event => {if (!event.ctrlKey) {return }switch (event.key) {case'`': {event.preventDefault()const [match] =Editor.nodes(editor, {match: n =>n.type ==='code', })constisCodeActive=!!matchTransforms.setNodes( editor, { type: isCodeActive ?null:'code' }, { match: n =>Editor.isBlock(editor, n) } )break }case'b': {event.preventDefault()Transforms.setNodes( editor, { bold:true }, { match: n =>Text.isText(n), split:true } )break } } }} /> </Slate> )}
Since we haven't yet defined (or overridden) any commands in withCustom, nothing will change yet. Our app will still function exactly as it did before.
However, now we can start extract bits of logic into reusable methods:
constwithCustom= editor => {const { exec } = editoreditor.exec= command => {// Define a command to toggle the bold formatting.if (command.type ==='toggle_bold_mark') {constisActive=CustomEditor.isBoldMarkActive(editor)Transforms.setNodes( editor, { bold: isActive ?null:true }, { match: n =>Text.isText(n), split:true } ) }// Define a command to toggle the code block formatting.elseif (command.type ==='toggle_code_block') {constisActive=CustomEditor.isCodeBlockActive(editor)Transforms.setNodes( editor, { type: isActive ?null:'code' }, { match: n =>Editor.isBlock(editor, n) } ) }// Otherwise, fall back to the built-in `exec` logic for everything else.else {exec(command) } }return editor}// Define our own custom set of helpers for active-checking queries.constCustomEditor= {isBoldMarkActive(editor) {const [match] =Editor.nodes(editor, {match: n =>n.bold ===true, universal:true, })return!!match },isCodeBlockActive(editor) {const [match] =Editor.nodes(editor, {match: n =>n.type ==='code', })return!!match },}constApp= () => {consteditor=useMemo(() =>withCustom(withReact(createEditor())), [])const [value,setValue] =useState([ { type:'paragraph', children: [{ text:'A line of text in a paragraph.' }], }, ])constrenderElement=useCallback(props => {switch (props.element.type) {case'code':return <CodeElement {...props} />default:return <DefaultElement {...props} /> } }, [])constrenderLeaf=useCallback(props => {return <Leaf {...props} /> }, [])return ( <Slateeditor={editor} value={value} onChange={value =>setValue(value)}> <EditablerenderElement={renderElement}renderLeaf={renderLeaf}onKeyDown={event => {if (!event.ctrlKey) {return }// Replace the `onKeyDown` logic with our new commands.switch (event.key) {case'`': {event.preventDefault()editor.exec({ type:'toggle_code_block' })break }case'b': {event.preventDefault()editor.exec({ type:'toggle_bold_mark' })break } } }} /> </Slate> )}
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:
constApp= () => {consteditor=useMemo(() =>withCustom(withReact(createEditor())), [])const [value,setValue] =useState([ { type:'paragraph', children: [{ text:'A line of text in a paragraph.' }], }, ])constrenderElement=useCallback(props => {switch (props.element.type) {case'code':return <CodeElement {...props} />default:return <DefaultElement {...props} /> } }, [])constrenderLeaf=useCallback(props => {return <Leaf {...props} /> }, [])return (// Add a toolbar with buttons that call the same methods. <Slateeditor={editor} value={value} onChange={value =>setValue(value)}> <div> <buttononMouseDown={event => {event.preventDefault()editor.exec({ type:'toggle_bold_mark' }) }} > Bold </button> <buttononMouseDown={event => {event.preventDefault()editor.exec({ type:'toggle_code_block' }) }} > Code Block </button> </div> <Editableeditor={editor}renderElement={renderElement}renderLeaf={renderLeaf}onKeyDown={event => {if (!event.ctrlKey) {return }switch (event.key) {case'`': {event.preventDefault()editor.exec({ type:'toggle_code_block' })break }case'b': {event.preventDefault()editor.exec({ type:'toggle_bold_mark' })break } } }} /> </Slate> )}
That's the benefit of extracting the logic.
And you don't necessarily need to define it all in the same plugin. You can use the plugin pattern to add logic and behaviors to an editor from elsewhere.
For example, you can use the slate-history package to add a history stack to your editor, like so:
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.
That's why plugins are awesome. They let you get really expressive while also making your codebase easier to manage. And since Slate is built with plugins as a primary consideration, using them is dead simple!