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