1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-31 02:49:56 +02:00

Fix cycle reference in slate-core (#1881)

* Switch to isType

* Fix fromJSON in circular reference

* Remove un-necessary files

* fix characters

* Import from node.js

* Export from block... models file
This commit is contained in:
Jinxuan Zhu
2018-06-10 20:23:11 -04:00
committed by Ian Storm Taylor
parent e04021f02e
commit b26535fd94
11 changed files with 55 additions and 97 deletions

View File

@@ -21,6 +21,18 @@ const MODEL_TYPES = {
VALUE: '@@__SLATE_VALUE__@@',
}
/**
* Export type identification function
*
* @param {string} type
* @param {any} any
* @return {boolean}
*/
export function isType(type, any) {
return !!(any && any[MODEL_TYPES[type]])
}
/**
* Export.
*

View File

@@ -1,9 +1,3 @@
/**
* Prevent circular dependencies.
*/
import './document'
/**
* Dependencies.
*/
@@ -12,8 +6,7 @@ import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { List, Map, Record } from 'immutable'
import MODEL_TYPES from '../constants/model-types'
import Node from './node'
import MODEL_TYPES, { isType } from '../constants/model-types'
import generateKey from '../utils/generate-key'
/**
@@ -108,8 +101,8 @@ class Block extends Record(DEFAULTS) {
key,
type,
isVoid: !!isVoid,
data: new Map(data),
nodes: new List(nodes.map(Node.fromJSON)),
data: Map(data),
nodes: Block.createChildren(nodes),
})
return block
@@ -128,9 +121,7 @@ class Block extends Record(DEFAULTS) {
* @return {Boolean}
*/
static isBlock(any) {
return !!(any && any[MODEL_TYPES.BLOCK])
}
static isBlock = isType.bind(null, 'BLOCK')
/**
* Check if `any` is a block list.
@@ -221,15 +212,6 @@ class Block extends Record(DEFAULTS) {
Block.prototype[MODEL_TYPES.BLOCK] = true
/**
* Mix in `Node` methods.
*/
Object.getOwnPropertyNames(Node.prototype).forEach(method => {
if (method == 'constructor') return
Block.prototype[method] = Node.prototype[method]
})
/**
* Export.
*

View File

@@ -4,7 +4,7 @@ import logger from 'slate-dev-logger'
import pick from 'lodash/pick'
import { List } from 'immutable'
import MODEL_TYPES from '../constants/model-types'
import MODEL_TYPES, { isType } from '../constants/model-types'
import Changes from '../changes'
import Operation from './operation'
import apply from '../operations/apply'
@@ -31,9 +31,7 @@ class Change {
* @return {Boolean}
*/
static isChange(any) {
return !!(any && any[MODEL_TYPES.CHANGE])
}
static isChange = isType.bind(null, 'CHANGE')
/**
* Create a new `Change` with `attrs`.

View File

@@ -2,7 +2,7 @@ import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { List, Record, Set } from 'immutable'
import MODEL_TYPES from '../constants/model-types'
import MODEL_TYPES, { isType } from '../constants/model-types'
/**
* Default properties.
@@ -104,9 +104,7 @@ class Character extends Record(DEFAULTS) {
* @return {Boolean}
*/
static isCharacter(any) {
return !!(any && any[MODEL_TYPES.CHARACTER])
}
static isCharacter = isType.bind(null, 'CHARACTER')
/**
* Check if `any` is a character list.

View File

@@ -1,10 +1,3 @@
/**
* Prevent circular dependencies.
*/
import './block'
import './inline'
/**
* Dependencies.
*/
@@ -13,8 +6,7 @@ import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { List, Map, Record } from 'immutable'
import Node from './node'
import MODEL_TYPES from '../constants/model-types'
import MODEL_TYPES, { isType } from '../constants/model-types'
import generateKey from '../utils/generate-key'
/**
@@ -78,7 +70,7 @@ class Document extends Record(DEFAULTS) {
const document = new Document({
key,
data: new Map(data),
nodes: new List(nodes.map(Node.fromJSON)),
nodes: Document.createChildren(nodes),
})
return document
@@ -97,9 +89,7 @@ class Document extends Record(DEFAULTS) {
* @return {Boolean}
*/
static isDocument(any) {
return !!(any && any[MODEL_TYPES.DOCUMENT])
}
static isDocument = isType.bind(null, 'DOCUMENT')
/**
* Object.
@@ -176,15 +166,6 @@ class Document extends Record(DEFAULTS) {
Document.prototype[MODEL_TYPES.DOCUMENT] = true
/**
* Mix in `Node` methods.
*/
Object.getOwnPropertyNames(Node.prototype).forEach(method => {
if (method == 'constructor') return
Document.prototype[method] = Node.prototype[method]
})
/**
* Export.
*

View File

@@ -4,7 +4,7 @@ import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { List, Record, Stack } from 'immutable'
import MODEL_TYPES from '../constants/model-types'
import MODEL_TYPES, { isType } from '../constants/model-types'
/**
* Debug.
@@ -84,9 +84,7 @@ class History extends Record(DEFAULTS) {
* @return {Boolean}
*/
static isHistory(any) {
return !!(any && any[MODEL_TYPES.HISTORY])
}
static isHistory = isType.bind(null, 'HISTORY')
/**
* Object.

View File

@@ -1,9 +1,3 @@
/**
* Prevent circular dependencies.
*/
import './document'
/**
* Dependencies.
*/
@@ -12,8 +6,7 @@ import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { List, Map, Record } from 'immutable'
import Node from './node'
import MODEL_TYPES from '../constants/model-types'
import MODEL_TYPES, { isType } from '../constants/model-types'
import generateKey from '../utils/generate-key'
/**
@@ -109,7 +102,7 @@ class Inline extends Record(DEFAULTS) {
type,
isVoid: !!isVoid,
data: new Map(data),
nodes: new List(nodes.map(Node.fromJSON)),
nodes: Inline.createChildren(nodes),
})
return inline
@@ -128,9 +121,7 @@ class Inline extends Record(DEFAULTS) {
* @return {Boolean}
*/
static isInline(any) {
return !!(any && any[MODEL_TYPES.INLINE])
}
static isInline = isType.bind(null, 'INLINE')
/**
* Check if `any` is a list of inlines.
@@ -221,15 +212,6 @@ class Inline extends Record(DEFAULTS) {
Inline.prototype[MODEL_TYPES.INLINE] = true
/**
* Mix in `Node` methods.
*/
Object.getOwnPropertyNames(Node.prototype).forEach(method => {
if (method == 'constructor') return
Inline.prototype[method] = Node.prototype[method]
})
/**
* Export.
*

View File

@@ -2,7 +2,7 @@ import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { List, Record, Set } from 'immutable'
import MODEL_TYPES from '../constants/model-types'
import MODEL_TYPES, { isType } from '../constants/model-types'
import Character from './character'
import Mark from './mark'
@@ -98,9 +98,7 @@ class Leaf extends Record(DEFAULTS) {
* @return {Boolean}
*/
static isLeaf(any) {
return !!(any && any[MODEL_TYPES.LEAF])
}
static isLeaf = isType.bind(null, 'LEAF')
/**
* Check if `any` is a list of leaves.

View File

@@ -2,7 +2,7 @@ import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { Map, Record, Set } from 'immutable'
import MODEL_TYPES from '../constants/model-types'
import MODEL_TYPES, { isType } from '../constants/model-types'
import Data from './data'
import memoize from '../utils/memoize'
@@ -137,9 +137,7 @@ class Mark extends Record(DEFAULTS) {
* @return {Boolean}
*/
static isMark(any) {
return !!(any && any[MODEL_TYPES.MARK])
}
static isMark = isType.bind(null, 'MARK')
/**
* Check if `any` is a set of marks.

View File

@@ -3,10 +3,11 @@ import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { List, OrderedSet, Set } from 'immutable'
import Block from './block'
import Data from './data'
import Document from './document'
import Block from './block'
import Inline from './inline'
import Document from './document'
import { isType } from '../constants/model-types'
import Range from './range'
import Text from './text'
import generateKey from '../utils/generate-key'
@@ -74,7 +75,7 @@ class Node {
static createList(elements = []) {
if (List.isList(elements) || Array.isArray(elements)) {
const list = new List(elements.map(Node.create))
const list = List(elements.map(Node.create))
return list
}
@@ -165,11 +166,8 @@ class Node {
*/
static isNode(any) {
return (
Block.isBlock(any) ||
Document.isDocument(any) ||
Inline.isInline(any) ||
Text.isText(any)
return !!['BLOCK', 'DOCUMENT', 'INLINE', 'TEXT'].find(type =>
isType(type, any)
)
}
@@ -2128,6 +2126,21 @@ memoize(Node.prototype, [
'getFirstInvalidDescendant',
])
/**
* Mix in `Node` methods.
*/
Object.getOwnPropertyNames(Node.prototype).forEach(method => {
if (method == 'constructor') return
Block.prototype[method] = Node.prototype[method]
Inline.prototype[method] = Node.prototype[method]
Document.prototype[method] = Node.prototype[method]
})
Block.createChildren = Node.createList
Inline.createChildren = Node.createList
Document.createChildren = Node.createList
/**
* Export.
*

View File

@@ -5,7 +5,7 @@ import { List, OrderedSet, Record, Set, is } from 'immutable'
import Character from './character'
import Mark from './mark'
import Leaf from './leaf'
import MODEL_TYPES from '../constants/model-types'
import MODEL_TYPES, { isType } from '../constants/model-types'
import generateKey from '../utils/generate-key'
import memoize from '../utils/memoize'
@@ -114,9 +114,7 @@ class Text extends Record(DEFAULTS) {
* @return {Boolean}
*/
static isText(any) {
return !!(any && any[MODEL_TYPES.TEXT])
}
static isText = isType.bind(null, 'TEXT')
/**
* Check if `any` is a list of texts.