mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-01-18 05:59:13 +01:00
cleanup, set keys automatically
This commit is contained in:
parent
769f59798f
commit
0995c9cd96
File diff suppressed because one or more lines are too long
@ -10,13 +10,11 @@ import ReactDOM from 'react-dom'
|
||||
const state = {
|
||||
nodes: [
|
||||
{
|
||||
key: '1',
|
||||
kind: 'node',
|
||||
type: 'code',
|
||||
data: {},
|
||||
children: [
|
||||
{
|
||||
key: '2',
|
||||
type: 'text',
|
||||
ranges: [
|
||||
{
|
||||
@ -28,13 +26,11 @@ const state = {
|
||||
]
|
||||
},
|
||||
{
|
||||
key: '3',
|
||||
kind: 'node',
|
||||
type: 'paragraph',
|
||||
data: {},
|
||||
children: [
|
||||
{
|
||||
key: '4',
|
||||
type: 'text',
|
||||
ranges: [
|
||||
{
|
||||
@ -53,13 +49,7 @@ const state = {
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
selection: {
|
||||
anchorKey: '4',
|
||||
anchorOffset: 14,
|
||||
focusKey: '4',
|
||||
focusOffset: 14
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
import Text from './text'
|
||||
import uid from 'uid'
|
||||
import { Map, OrderedMap, Record } from 'immutable'
|
||||
|
||||
/**
|
||||
@ -28,10 +29,10 @@ class Node extends NodeRecord {
|
||||
|
||||
static create(object) {
|
||||
return new Node({
|
||||
key: object.key,
|
||||
type: object.type,
|
||||
children: Node.createMap(object.children),
|
||||
data: new Map(object.data),
|
||||
children: Node.createMap(object.children)
|
||||
key: uid(4),
|
||||
type: object.type
|
||||
})
|
||||
}
|
||||
|
||||
@ -43,10 +44,11 @@ class Node extends NodeRecord {
|
||||
*/
|
||||
|
||||
static createMap(array) {
|
||||
return new OrderedMap(array.reduce((map, node) => {
|
||||
map[node.key] = node.type == 'text'
|
||||
? Text.create(node)
|
||||
: Node.create(node)
|
||||
return new OrderedMap(array.reduce((map, object) => {
|
||||
const node = object.type == 'text'
|
||||
? Text.create(object)
|
||||
: Node.create(object)
|
||||
map[node.key] = node
|
||||
return map
|
||||
}, {}))
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
import convertRangesToCharacters from '../utils/convert-ranges-to-characters'
|
||||
import uid from 'uid'
|
||||
import { List, Record } from 'immutable'
|
||||
|
||||
/**
|
||||
@ -27,7 +28,7 @@ class Text extends TextRecord {
|
||||
static create(attrs) {
|
||||
const characters = convertRangesToCharacters(attrs.ranges)
|
||||
return new Text({
|
||||
key: attrs.key,
|
||||
key: uid(4),
|
||||
characters
|
||||
})
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import findOffsetKey from './find-offset-key'
|
||||
* Offset key splitter.
|
||||
*/
|
||||
|
||||
const SPLITTER = /^(\d+)(?:\.(\d+)-(\d+))?$/
|
||||
const SPLITTER = /^(\w+)(?:\.(\d+)-(\d+))?$/
|
||||
|
||||
/**
|
||||
* Find the selection anchor properties from a `node`.
|
||||
|
@ -9,7 +9,8 @@
|
||||
"lodash": "^4.13.1",
|
||||
"react": "^15.1.0",
|
||||
"to-camel-case": "^1.0.0",
|
||||
"ua-parser-js": "^0.7.10"
|
||||
"ua-parser-js": "^0.7.10",
|
||||
"uid": "0.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-core": "^6.9.1",
|
||||
|
Loading…
x
Reference in New Issue
Block a user