Concepts
Interfaces
7min
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 richtext 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 be defined containing the element's child nodes but you can extend elements (or any other interface) with your own 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 there are your own custom api slate sees that they exist, but it doesn't ever use them for anything however, when it goes to render 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 them 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 lots of helper functions available for all of the common use cases when working with the different interfaces when getting started it pays to read through them all, because you can often simplify complex logic into just a handful of lines of code with them custom helpers in addition to the built in helper functions, you might want to define your own custom helper functions and expose them on your own 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
🤔
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.