Concepts

Plugins

3min
you've already seen how the behaviors of slate editors can be overridden these overrides can also be packaged up into "plugins" to be reused, tested and shared this is one of the most powerful aspects of slate's architecture a plugin is simply a function that takes an editor object and returns it after it has augmented it in some way for example, a plugin that marks image nodes as "void" const withimages = editor => { const { isvoid } = editor editor isvoid = element => { return element type === 'image' ? true isvoid(element) } return editor } and then to use the plugin, simply import { createeditor } from 'slate' const editor = withimages(createeditor()) this plugin composition model makes slate extremely easy to extend! helper functions in addition to the plugin functions, you might want to expose helper functions that are used alongside your plugins for example import { editor, element } from 'slate' const myeditor = { editor, insertimage(editor, url) { const element = { type 'image', url, children \[{ text '' }] } transforms insertnodes(editor, element) }, } const myelement = { element, isimageelement(value) { return element iselement(element) && element type === 'image' }, } then you can use myeditor and myelement everywhere and have access to all your helpers in one place
🤔
Have a question?
Our super-smart AI,knowledgeable support team and an awesome community will get you an answer in a flash.
To ask a question or participate in discussions, you'll need to authenticate first.