mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-04-21 13:51:59 +02:00
Add data property to document
. (#572)
* Add data property to `document`. Add `setDocumentDataOperation' to set document data. Change `Raw` serializer to take into account the document `data` property. Update docs and tests. * Make `setNode` operation working with document node. Rewrite `setDocumentdataOperation` using `setNode`. * Make 'setNodeByKey' working also with document node. * Get rid of `setDocumentDataOperation`.
This commit is contained in:
parent
9adfc63a36
commit
ca1e0d8c6d
@ -5,13 +5,14 @@
|
||||
import { Document } from 'slate'
|
||||
```
|
||||
|
||||
The top-level node in Slate's document model.
|
||||
The top-level node in Slate's document model.
|
||||
|
||||
Documents are made up of block nodes, inline nodes, and text nodes—just like in the DOM.
|
||||
Documents are made up of block nodes, inline nodes, and text nodes—just like in the DOM.
|
||||
|
||||
In some places, you'll see mention of "fragments", which are also `Document` objects, just that aren't attached to the main `State`. For example, when cutting-and-pasting a selection of content, that content will be referred to as a document "fragment".
|
||||
|
||||
- [Properties](#properties)
|
||||
- [`data`](#data)
|
||||
- [`nodes`](#nodes)
|
||||
- [Computed Properties](#computed-properties)
|
||||
- [`kind`](#kind)
|
||||
@ -30,6 +31,11 @@ Document({
|
||||
})
|
||||
```
|
||||
|
||||
### `data`
|
||||
`Immutable.Map`
|
||||
|
||||
Arbitrary data associated with the document. Defaults to an empty `Map`.
|
||||
|
||||
### `nodes`
|
||||
`Immutable.List`
|
||||
|
||||
|
@ -10,10 +10,11 @@ import './inline'
|
||||
* Dependencies.
|
||||
*/
|
||||
|
||||
import Data from './data'
|
||||
import Block from './block'
|
||||
import Node from './node'
|
||||
import generateKey from '../utils/generate-key'
|
||||
import { List, Record } from 'immutable'
|
||||
import { List, Map, Record } from 'immutable'
|
||||
|
||||
/**
|
||||
* Default properties.
|
||||
@ -22,6 +23,7 @@ import { List, Record } from 'immutable'
|
||||
*/
|
||||
|
||||
const DEFAULTS = {
|
||||
data: new Map(),
|
||||
key: null,
|
||||
nodes: new List(),
|
||||
}
|
||||
@ -45,6 +47,7 @@ class Document extends new Record(DEFAULTS) {
|
||||
if (properties instanceof Document) return properties
|
||||
|
||||
properties.key = properties.key || generateKey()
|
||||
properties.data = Data.create(properties.data)
|
||||
properties.nodes = Block.createList(properties.nodes)
|
||||
|
||||
return new Document(properties)
|
||||
|
@ -62,6 +62,7 @@ const Raw = {
|
||||
deserializeDocument(object, options) {
|
||||
return Document.create({
|
||||
key: object.key,
|
||||
data: object.data,
|
||||
nodes: Block.createList(object.nodes.map((node) => {
|
||||
return Raw.deserializeNode(node, options)
|
||||
}))
|
||||
@ -254,6 +255,7 @@ const Raw = {
|
||||
|
||||
serializeDocument(document, options = {}) {
|
||||
const object = {
|
||||
data: document.data.toJSON(),
|
||||
key: document.key,
|
||||
kind: document.kind,
|
||||
nodes: document.nodes
|
||||
@ -467,6 +469,7 @@ const Raw = {
|
||||
const ret = {}
|
||||
ret.nodes = object.nodes
|
||||
if (object.key) ret.key = object.key
|
||||
if (!isEmpty(object.data)) ret.data = object.data
|
||||
return ret
|
||||
},
|
||||
|
||||
@ -679,6 +682,7 @@ const Raw = {
|
||||
return {
|
||||
kind: 'state',
|
||||
document: {
|
||||
data: object.data,
|
||||
key: object.key,
|
||||
kind: 'document',
|
||||
nodes: object.nodes
|
||||
|
@ -348,7 +348,7 @@ function setNode(state, operation) {
|
||||
}
|
||||
|
||||
node = node.merge(properties)
|
||||
document = document.updateDescendant(node)
|
||||
document = node.kind === 'document' ? node : document.updateDescendant(node)
|
||||
state = state.merge({ document })
|
||||
return state
|
||||
}
|
||||
|
@ -255,8 +255,8 @@ export function setNodeByKey(transform, key, properties, options = {}) {
|
||||
transform.setNodeOperation(path, properties)
|
||||
|
||||
if (normalize) {
|
||||
const parent = document.getParent(key)
|
||||
transform.normalizeNodeByKey(parent.key, SCHEMA)
|
||||
const node = key === document.key ? document : document.getParent(key)
|
||||
transform.normalizeNodeByKey(node.key, SCHEMA)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: quote
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,4 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: true
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: one
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: line
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: line
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: quote
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: true
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: quote
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: true
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,4 +1,5 @@
|
||||
|
||||
data: {}
|
||||
nodes:
|
||||
- type: paragraph
|
||||
isVoid: false
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
kind: state
|
||||
document:
|
||||
data: {}
|
||||
kind: document
|
||||
nodes:
|
||||
- kind: block
|
||||
|
Loading…
x
Reference in New Issue
Block a user