1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-26 00:27:28 +02:00
This commit is contained in:
Sunny Hirai
2021-04-01 18:13:48 -07:00
45 changed files with 733 additions and 911 deletions

View File

@@ -1,4 +1,4 @@
# Nodes: Editor, Elements and Texts
# Nodes
The most important types are the `Node` objects:
@@ -8,7 +8,7 @@ The most important types are the `Node` objects:
These three interfaces are combined together to form a tree—just like the DOM. For example, here's a simple plaintext value:
```js
```javascript
const editor = {
children: [
{
@@ -39,7 +39,7 @@ A Slate document is a nested and recursive structure. In a document, elements ca
The top-level node in a Slate document is the `Editor` itself. It encapsulates all of the richtext "content" of the document. Its interface is:
```ts
```typescript
interface Editor {
children: Node[]
...
@@ -52,16 +52,15 @@ We'll cover its functionality later, but the important part as far as nodes are
Elements make up the middle layers of a richtext document. They are the nodes that are custom to your own domain. Their interface is:
```ts
```typescript
interface Element {
children: Node[]
[key: string]: unknown
}
```
You can define custom elements for any type of content you want. For example you might have paragraphs and quotes in your data model which are differentiated by a `type` property:
```js
```javascript
const paragraph = {
type: 'paragraph',
children: [...],
@@ -75,7 +74,7 @@ const quote = {
It's important to note that you can use _any_ custom properties you want. The `type` property in that example isn't something Slate knows or cares about. If you were defining your own "link" nodes, you might have a `url` property:
```js
```javascript
const link = {
type: 'link',
url: 'https://example.com',
@@ -85,7 +84,7 @@ const link = {
Or maybe you want to give all of your nodes ID an property:
```js
```javascript
const paragraph = {
id: 1,
type: 'paragraph',
@@ -105,7 +104,7 @@ But in certain cases, like for links, you might want to make them "inline" flowi
> 🤖 This is a concept borrowed from the DOM's behavior, see [Block Elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements) and [Inline Elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements).
You can define which nodes are treated as inline nodes by overriding the `editor.isInline` function. (By default it always returns `false`.) Note that inline nodes cannot be the first or last child of a parent block, nor can it be next to another inline node in the children array. Slate will automatically space these with `{ text: '' }` children by default with [`normalizeNode`](https://docs.slatejs.org/concepts/10-normalizing#built-in-constraints).
You can define which nodes are treated as inline nodes by overriding the `editor.isInline` function. \(By default it always returns `false`.\) Note that inline nodes cannot be the first or last child of a parent block, nor can it be next to another inline node in the children array. Slate will automatically space these with `{ text: '' }` children by default with [`normalizeNode`](https://docs.slatejs.org/concepts/10-normalizing#built-in-constraints).
Elements can either contain block elements or inline elements intermingled with text nodes as children. But elements **cannot** contain some children that are blocks and some that are inlines.
@@ -117,22 +116,21 @@ Elements default to being non-void, meaning that their children are fully editab
> 🤖 This is a concept borrowed from the HTML spec, see [Void Elements](https://www.w3.org/TR/2011/WD-html-markup-20110405/syntax.html#void-element).
You can define which elements are treated as void by overriding the `editor.isVoid` function. (By default it always returns `false`.)
You can define which elements are treated as void by overriding the `editor.isVoid` function. \(By default it always returns `false`.\)
## `Text`
Text nodes are the lowest-level nodes in the tree, containing the text content of the document, along with any formatting. Their interface is:
```ts
```typescript
interface Text {
text: string
[key: string]: unknown
}
```
For example, a string of bold text:
```js
```javascript
const text = {
text: 'A string of bold text',
bold: true,