mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-03 12:12:39 +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:
committed by
Ian Storm Taylor
parent
e04021f02e
commit
b26535fd94
@@ -21,6 +21,18 @@ const MODEL_TYPES = {
|
|||||||
VALUE: '@@__SLATE_VALUE__@@',
|
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.
|
* Export.
|
||||||
*
|
*
|
||||||
|
@@ -1,9 +1,3 @@
|
|||||||
/**
|
|
||||||
* Prevent circular dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import './document'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dependencies.
|
* Dependencies.
|
||||||
*/
|
*/
|
||||||
@@ -12,8 +6,7 @@ import isPlainObject from 'is-plain-object'
|
|||||||
import logger from 'slate-dev-logger'
|
import logger from 'slate-dev-logger'
|
||||||
import { List, Map, Record } from 'immutable'
|
import { List, Map, Record } from 'immutable'
|
||||||
|
|
||||||
import MODEL_TYPES from '../constants/model-types'
|
import MODEL_TYPES, { isType } from '../constants/model-types'
|
||||||
import Node from './node'
|
|
||||||
import generateKey from '../utils/generate-key'
|
import generateKey from '../utils/generate-key'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,8 +101,8 @@ class Block extends Record(DEFAULTS) {
|
|||||||
key,
|
key,
|
||||||
type,
|
type,
|
||||||
isVoid: !!isVoid,
|
isVoid: !!isVoid,
|
||||||
data: new Map(data),
|
data: Map(data),
|
||||||
nodes: new List(nodes.map(Node.fromJSON)),
|
nodes: Block.createChildren(nodes),
|
||||||
})
|
})
|
||||||
|
|
||||||
return block
|
return block
|
||||||
@@ -128,9 +121,7 @@ class Block extends Record(DEFAULTS) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isBlock(any) {
|
static isBlock = isType.bind(null, 'BLOCK')
|
||||||
return !!(any && any[MODEL_TYPES.BLOCK])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if `any` is a block list.
|
* Check if `any` is a block list.
|
||||||
@@ -221,15 +212,6 @@ class Block extends Record(DEFAULTS) {
|
|||||||
|
|
||||||
Block.prototype[MODEL_TYPES.BLOCK] = true
|
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.
|
* Export.
|
||||||
*
|
*
|
||||||
|
@@ -4,7 +4,7 @@ import logger from 'slate-dev-logger'
|
|||||||
import pick from 'lodash/pick'
|
import pick from 'lodash/pick'
|
||||||
import { List } from 'immutable'
|
import { List } from 'immutable'
|
||||||
|
|
||||||
import MODEL_TYPES from '../constants/model-types'
|
import MODEL_TYPES, { isType } from '../constants/model-types'
|
||||||
import Changes from '../changes'
|
import Changes from '../changes'
|
||||||
import Operation from './operation'
|
import Operation from './operation'
|
||||||
import apply from '../operations/apply'
|
import apply from '../operations/apply'
|
||||||
@@ -31,9 +31,7 @@ class Change {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isChange(any) {
|
static isChange = isType.bind(null, 'CHANGE')
|
||||||
return !!(any && any[MODEL_TYPES.CHANGE])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new `Change` with `attrs`.
|
* Create a new `Change` with `attrs`.
|
||||||
|
@@ -2,7 +2,7 @@ import isPlainObject from 'is-plain-object'
|
|||||||
import logger from 'slate-dev-logger'
|
import logger from 'slate-dev-logger'
|
||||||
import { List, Record, Set } from 'immutable'
|
import { List, Record, Set } from 'immutable'
|
||||||
|
|
||||||
import MODEL_TYPES from '../constants/model-types'
|
import MODEL_TYPES, { isType } from '../constants/model-types'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default properties.
|
* Default properties.
|
||||||
@@ -104,9 +104,7 @@ class Character extends Record(DEFAULTS) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isCharacter(any) {
|
static isCharacter = isType.bind(null, 'CHARACTER')
|
||||||
return !!(any && any[MODEL_TYPES.CHARACTER])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if `any` is a character list.
|
* Check if `any` is a character list.
|
||||||
|
@@ -1,10 +1,3 @@
|
|||||||
/**
|
|
||||||
* Prevent circular dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import './block'
|
|
||||||
import './inline'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dependencies.
|
* Dependencies.
|
||||||
*/
|
*/
|
||||||
@@ -13,8 +6,7 @@ import isPlainObject from 'is-plain-object'
|
|||||||
import logger from 'slate-dev-logger'
|
import logger from 'slate-dev-logger'
|
||||||
import { List, Map, Record } from 'immutable'
|
import { List, Map, Record } from 'immutable'
|
||||||
|
|
||||||
import Node from './node'
|
import MODEL_TYPES, { isType } from '../constants/model-types'
|
||||||
import MODEL_TYPES from '../constants/model-types'
|
|
||||||
import generateKey from '../utils/generate-key'
|
import generateKey from '../utils/generate-key'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,7 +70,7 @@ class Document extends Record(DEFAULTS) {
|
|||||||
const document = new Document({
|
const document = new Document({
|
||||||
key,
|
key,
|
||||||
data: new Map(data),
|
data: new Map(data),
|
||||||
nodes: new List(nodes.map(Node.fromJSON)),
|
nodes: Document.createChildren(nodes),
|
||||||
})
|
})
|
||||||
|
|
||||||
return document
|
return document
|
||||||
@@ -97,9 +89,7 @@ class Document extends Record(DEFAULTS) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isDocument(any) {
|
static isDocument = isType.bind(null, 'DOCUMENT')
|
||||||
return !!(any && any[MODEL_TYPES.DOCUMENT])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Object.
|
* Object.
|
||||||
@@ -176,15 +166,6 @@ class Document extends Record(DEFAULTS) {
|
|||||||
|
|
||||||
Document.prototype[MODEL_TYPES.DOCUMENT] = true
|
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.
|
* Export.
|
||||||
*
|
*
|
||||||
|
@@ -4,7 +4,7 @@ import isPlainObject from 'is-plain-object'
|
|||||||
import logger from 'slate-dev-logger'
|
import logger from 'slate-dev-logger'
|
||||||
import { List, Record, Stack } from 'immutable'
|
import { List, Record, Stack } from 'immutable'
|
||||||
|
|
||||||
import MODEL_TYPES from '../constants/model-types'
|
import MODEL_TYPES, { isType } from '../constants/model-types'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Debug.
|
* Debug.
|
||||||
@@ -84,9 +84,7 @@ class History extends Record(DEFAULTS) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isHistory(any) {
|
static isHistory = isType.bind(null, 'HISTORY')
|
||||||
return !!(any && any[MODEL_TYPES.HISTORY])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Object.
|
* Object.
|
||||||
|
@@ -1,9 +1,3 @@
|
|||||||
/**
|
|
||||||
* Prevent circular dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import './document'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dependencies.
|
* Dependencies.
|
||||||
*/
|
*/
|
||||||
@@ -12,8 +6,7 @@ import isPlainObject from 'is-plain-object'
|
|||||||
import logger from 'slate-dev-logger'
|
import logger from 'slate-dev-logger'
|
||||||
import { List, Map, Record } from 'immutable'
|
import { List, Map, Record } from 'immutable'
|
||||||
|
|
||||||
import Node from './node'
|
import MODEL_TYPES, { isType } from '../constants/model-types'
|
||||||
import MODEL_TYPES from '../constants/model-types'
|
|
||||||
import generateKey from '../utils/generate-key'
|
import generateKey from '../utils/generate-key'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,7 +102,7 @@ class Inline extends Record(DEFAULTS) {
|
|||||||
type,
|
type,
|
||||||
isVoid: !!isVoid,
|
isVoid: !!isVoid,
|
||||||
data: new Map(data),
|
data: new Map(data),
|
||||||
nodes: new List(nodes.map(Node.fromJSON)),
|
nodes: Inline.createChildren(nodes),
|
||||||
})
|
})
|
||||||
|
|
||||||
return inline
|
return inline
|
||||||
@@ -128,9 +121,7 @@ class Inline extends Record(DEFAULTS) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isInline(any) {
|
static isInline = isType.bind(null, 'INLINE')
|
||||||
return !!(any && any[MODEL_TYPES.INLINE])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if `any` is a list of inlines.
|
* Check if `any` is a list of inlines.
|
||||||
@@ -221,15 +212,6 @@ class Inline extends Record(DEFAULTS) {
|
|||||||
|
|
||||||
Inline.prototype[MODEL_TYPES.INLINE] = true
|
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.
|
* Export.
|
||||||
*
|
*
|
||||||
|
@@ -2,7 +2,7 @@ import isPlainObject from 'is-plain-object'
|
|||||||
import logger from 'slate-dev-logger'
|
import logger from 'slate-dev-logger'
|
||||||
import { List, Record, Set } from 'immutable'
|
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 Character from './character'
|
||||||
import Mark from './mark'
|
import Mark from './mark'
|
||||||
|
|
||||||
@@ -98,9 +98,7 @@ class Leaf extends Record(DEFAULTS) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isLeaf(any) {
|
static isLeaf = isType.bind(null, 'LEAF')
|
||||||
return !!(any && any[MODEL_TYPES.LEAF])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if `any` is a list of leaves.
|
* Check if `any` is a list of leaves.
|
||||||
|
@@ -2,7 +2,7 @@ import isPlainObject from 'is-plain-object'
|
|||||||
import logger from 'slate-dev-logger'
|
import logger from 'slate-dev-logger'
|
||||||
import { Map, Record, Set } from 'immutable'
|
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 Data from './data'
|
||||||
import memoize from '../utils/memoize'
|
import memoize from '../utils/memoize'
|
||||||
|
|
||||||
@@ -137,9 +137,7 @@ class Mark extends Record(DEFAULTS) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isMark(any) {
|
static isMark = isType.bind(null, 'MARK')
|
||||||
return !!(any && any[MODEL_TYPES.MARK])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if `any` is a set of marks.
|
* Check if `any` is a set of marks.
|
||||||
|
@@ -3,10 +3,11 @@ import isPlainObject from 'is-plain-object'
|
|||||||
import logger from 'slate-dev-logger'
|
import logger from 'slate-dev-logger'
|
||||||
import { List, OrderedSet, Set } from 'immutable'
|
import { List, OrderedSet, Set } from 'immutable'
|
||||||
|
|
||||||
import Block from './block'
|
|
||||||
import Data from './data'
|
import Data from './data'
|
||||||
import Document from './document'
|
import Block from './block'
|
||||||
import Inline from './inline'
|
import Inline from './inline'
|
||||||
|
import Document from './document'
|
||||||
|
import { isType } from '../constants/model-types'
|
||||||
import Range from './range'
|
import Range from './range'
|
||||||
import Text from './text'
|
import Text from './text'
|
||||||
import generateKey from '../utils/generate-key'
|
import generateKey from '../utils/generate-key'
|
||||||
@@ -74,7 +75,7 @@ class Node {
|
|||||||
|
|
||||||
static createList(elements = []) {
|
static createList(elements = []) {
|
||||||
if (List.isList(elements) || Array.isArray(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
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,11 +166,8 @@ class Node {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static isNode(any) {
|
static isNode(any) {
|
||||||
return (
|
return !!['BLOCK', 'DOCUMENT', 'INLINE', 'TEXT'].find(type =>
|
||||||
Block.isBlock(any) ||
|
isType(type, any)
|
||||||
Document.isDocument(any) ||
|
|
||||||
Inline.isInline(any) ||
|
|
||||||
Text.isText(any)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2128,6 +2126,21 @@ memoize(Node.prototype, [
|
|||||||
'getFirstInvalidDescendant',
|
'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.
|
* Export.
|
||||||
*
|
*
|
||||||
|
@@ -5,7 +5,7 @@ import { List, OrderedSet, Record, Set, is } from 'immutable'
|
|||||||
import Character from './character'
|
import Character from './character'
|
||||||
import Mark from './mark'
|
import Mark from './mark'
|
||||||
import Leaf from './leaf'
|
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 generateKey from '../utils/generate-key'
|
||||||
import memoize from '../utils/memoize'
|
import memoize from '../utils/memoize'
|
||||||
|
|
||||||
@@ -114,9 +114,7 @@ class Text extends Record(DEFAULTS) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static isText(any) {
|
static isText = isType.bind(null, 'TEXT')
|
||||||
return !!(any && any[MODEL_TYPES.TEXT])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if `any` is a list of texts.
|
* Check if `any` is a list of texts.
|
||||||
|
Reference in New Issue
Block a user