mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 21:21:21 +02:00
make create methods more helpful
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
|
|
||||||
|
import Data from './data'
|
||||||
|
import Inline from './inline'
|
||||||
import Node from './node'
|
import Node from './node'
|
||||||
|
import Text from './text'
|
||||||
import uid from 'uid'
|
import uid from 'uid'
|
||||||
import { Map, OrderedMap, Record } from 'immutable'
|
import Immutable, { Map, OrderedMap, Record } from 'immutable'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default properties.
|
* Default properties.
|
||||||
@@ -28,8 +31,15 @@ class Block extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
|
if (properties instanceof Block) return properties
|
||||||
|
if (properties instanceof Inline) return properties
|
||||||
|
if (properties instanceof Text) return properties
|
||||||
if (!properties.type) throw new Error('You must pass a block `type`.')
|
if (!properties.type) throw new Error('You must pass a block `type`.')
|
||||||
|
|
||||||
properties.key = uid(4)
|
properties.key = uid(4)
|
||||||
|
properties.data = Data.create(properties.data)
|
||||||
|
properties.nodes = Block.createMap(properties.nodes)
|
||||||
|
|
||||||
let block = new Block(properties)
|
let block = new Block(properties)
|
||||||
return block.normalize()
|
return block.normalize()
|
||||||
}
|
}
|
||||||
@@ -42,9 +52,12 @@ class Block extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static createMap(elements = []) {
|
static createMap(elements = []) {
|
||||||
return elements.reduce((map, element) => {
|
if (OrderedMap.isOrderedMap(elements)) return elements
|
||||||
return map.set(element.key, element)
|
return elements
|
||||||
}, new OrderedMap())
|
.map(Block.create)
|
||||||
|
.reduce((map, element) => {
|
||||||
|
return map.set(element.key, element)
|
||||||
|
}, new OrderedMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
import Mark from './mark'
|
||||||
import { List, Record, Set } from 'immutable'
|
import { List, Record, Set } from 'immutable'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,6 +25,8 @@ class Character extends CharacterRecord {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
|
if (properties instanceof Character) return properties
|
||||||
|
properties.marks = Mark.createSet(properties.marks)
|
||||||
return new Character(properties)
|
return new Character(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +38,8 @@ class Character extends CharacterRecord {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static createList(array = []) {
|
static createList(array = []) {
|
||||||
return new List(array)
|
if (List.isList(array)) return array
|
||||||
|
return new List(array.map(Character.create))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
import Block from './block'
|
||||||
import Node from './node'
|
import Node from './node'
|
||||||
import { OrderedMap, Record } from 'immutable'
|
import { OrderedMap, Record } from 'immutable'
|
||||||
|
|
||||||
@@ -24,6 +25,10 @@ class Document extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
|
if (properties instanceof Document) return properties
|
||||||
|
|
||||||
|
properties.nodes = Block.createMap(properties.nodes)
|
||||||
|
|
||||||
let document = new Document(properties)
|
let document = new Document(properties)
|
||||||
return document.normalize()
|
return document.normalize()
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,10 @@
|
|||||||
|
|
||||||
|
import Block from './block'
|
||||||
|
import Data from './data'
|
||||||
import Node from './node'
|
import Node from './node'
|
||||||
|
import Text from './text'
|
||||||
import uid from 'uid'
|
import uid from 'uid'
|
||||||
import { Map, OrderedMap, Record } from 'immutable'
|
import Immutable, { Map, OrderedMap, Record } from 'immutable'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Record.
|
* Record.
|
||||||
@@ -28,8 +31,15 @@ class Inline extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
|
if (properties instanceof Block) return properties
|
||||||
|
if (properties instanceof Inline) return properties
|
||||||
|
if (properties instanceof Text) return properties
|
||||||
if (!properties.type) throw new Error('You must pass an inline `type`.')
|
if (!properties.type) throw new Error('You must pass an inline `type`.')
|
||||||
|
|
||||||
properties.key = uid(4)
|
properties.key = uid(4)
|
||||||
|
properties.data = Data.create(properties.data)
|
||||||
|
properties.nodes = Inline.createMap(properties.nodes)
|
||||||
|
|
||||||
let inline = new Inline(properties)
|
let inline = new Inline(properties)
|
||||||
return inline.normalize()
|
return inline.normalize()
|
||||||
}
|
}
|
||||||
@@ -42,9 +52,12 @@ class Inline extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static createMap(elements = []) {
|
static createMap(elements = []) {
|
||||||
return elements.reduce((map, element) => {
|
if (OrderedMap.isOrderedMap(elements)) return elements
|
||||||
return map.set(element.key, element)
|
return elements
|
||||||
}, new OrderedMap())
|
.map(Inline.create)
|
||||||
|
.reduce((map, element) => {
|
||||||
|
return map.set(element.key, element)
|
||||||
|
}, new OrderedMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,20 +1,21 @@
|
|||||||
|
|
||||||
|
import Data from './data'
|
||||||
import { Map, Record, Set } from 'immutable'
|
import { Map, Record, Set } from 'immutable'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Record.
|
* Record.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const MarkRecord = new Record({
|
const DEFAULTS = {
|
||||||
data: new Map(),
|
data: new Map(),
|
||||||
type: null
|
type: null
|
||||||
})
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mark.
|
* Mark.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Mark extends MarkRecord {
|
class Mark extends Record(DEFAULTS) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new `Mark` with `properties`.
|
* Create a new `Mark` with `properties`.
|
||||||
@@ -24,7 +25,11 @@ class Mark extends MarkRecord {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
|
if (properties instanceof Mark) return properties
|
||||||
if (!properties.type) throw new Error('You must provide a `type` for the mark.')
|
if (!properties.type) throw new Error('You must provide a `type` for the mark.')
|
||||||
|
|
||||||
|
properties.data = Data.create(properties.data)
|
||||||
|
|
||||||
return new Mark(properties)
|
return new Mark(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +41,8 @@ class Mark extends MarkRecord {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static createSet(array = []) {
|
static createSet(array = []) {
|
||||||
return new Set(array)
|
if (Set.isSet(array)) return array
|
||||||
|
return new Set(array.map(Mark.create))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -5,20 +5,20 @@ import { Record } from 'immutable'
|
|||||||
* Record.
|
* Record.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const SelectionRecord = new Record({
|
const DEFAULTS = {
|
||||||
anchorKey: null,
|
anchorKey: null,
|
||||||
anchorOffset: 0,
|
anchorOffset: 0,
|
||||||
focusKey: null,
|
focusKey: null,
|
||||||
focusOffset: 0,
|
focusOffset: 0,
|
||||||
isBackward: false,
|
isBackward: false,
|
||||||
isFocused: false
|
isFocused: false
|
||||||
})
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selection.
|
* Selection.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Selection extends SelectionRecord {
|
class Selection extends Record(DEFAULTS) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new `Selection` with `properties`.
|
* Create a new `Selection` with `properties`.
|
||||||
@@ -28,6 +28,7 @@ class Selection extends SelectionRecord {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
|
if (properties instanceof Selection) return properties
|
||||||
return new Selection(properties)
|
return new Selection(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -59,6 +59,11 @@ class State extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
|
if (properties instanceof State) return properties
|
||||||
|
|
||||||
|
properties.document = Document.create(properties.document)
|
||||||
|
properties.selection = Selection.create(properties.selection)
|
||||||
|
|
||||||
return new State(properties)
|
return new State(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
import Character from './character'
|
||||||
import uid from 'uid'
|
import uid from 'uid'
|
||||||
import { List, Record } from 'immutable'
|
import { List, Record } from 'immutable'
|
||||||
|
|
||||||
@@ -25,7 +26,11 @@ class Text extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
|
if (properties instanceof Text) return properties
|
||||||
|
|
||||||
properties.key = uid(4)
|
properties.key = uid(4)
|
||||||
|
properties.characters = Character.createList(properties.characters)
|
||||||
|
|
||||||
return new Text(properties)
|
return new Text(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user