Interfaces
7 min
slate works with pure json objects all it requires is that those json objects conform to certain interfaces for example, a text node in slate must obey the text interface interface text { text string } which means it must have a text property with a string of content but any other custom properties are also allowed, and completely up to you this lets you tailor your data to your specific domain and use case, adding whatever formatting logic you'd like, without slate getting in the way this interface based approach separates slate from most other rich text editors which require you to work with their hand rolled "model" classes and makes it much easier to reason about it also means that it avoids startup time penalties related to "initializing" the data model custom properties to take another example, the element node interface in slate is interface element { children node\[] } this is a very permissive interface all it requires is that the children property gets defined containing the element's child nodes but you can extend elements (or any other interface) with your custom properties that are specific to your domain for example, you might have "paragraph" and "link" elements const paragraph = { type 'paragraph', children \[ ], } const link = { type 'link', url 'https //example com', children \[ ] } the type and url properties are your custom api slate sees that they exist, but doesn't use them however, when slate renders a link element, you'll receive an object with the custom properties attached so that you can render it as \<a href={element url}>{element children}\</a> when getting started with slate, it's important to understand all of the interfaces it defines there are a handful of interfaces that are discussed in each of the guides helper functions in addition to the typing information, each interface in slate also exposes a series of helper functions that make them easier to work with for example, when working with nodes import { node } from 'slate' // get the string content of an element node const string = node string(element) // get the node at a specific path inside a root node const descendant = node get(value, path) or, when working with ranges import { range } from 'slate' // get the start and end points of a range in order const \[start, end] = range edges(range) // check if a range is collapsed to a single point if (range iscollapsed(range)) { // } there are many helper functions available for all common use cases when working with different interfaces when getting started it helps to read through all of them so you can often simplify complex logic into just a handful of lines of code custom helpers in addition to the built in helper functions, you might want to define your custom helper functions and expose them on your custom namespaces for example, if your editor supports images, you might want a helper that determines if an element is an image element const isimageelement = element => { return element type === 'image' && typeof element url === 'string' } you can define these as one off functions easily but you might also bundle them up into namespaces, just like the core interfaces do, and use them instead for example import { element } from 'slate' // you can use `myelement` everywhere to have access to your extensions export const myelement = { element, isimageelement, isparagraphelement, isquoteelement, } this makes it easy to reuse domain specific logic alongside the built in slate helpers