1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-01 11:12:42 +02:00

Remove the deprecated Character model, and methods that reference it (#2016)

Closes #1999
This commit is contained in:
Zach Schneider
2018-08-03 18:24:05 -04:00
committed by Ian Storm Taylor
parent 1695f5a20c
commit 70d9e7951b
15 changed files with 2 additions and 382 deletions

View File

@@ -1,7 +1,6 @@
import {
Block,
Change,
Character,
Data,
Document,
History,
@@ -59,8 +58,6 @@ const Types = {
block: create('Block', v => Block.isBlock(v)),
blocks: create('List<Block>', v => Block.isBlockList(v)),
change: create('Change', v => Change.isChange(v)),
character: create('Character', v => Character.isCharacter(v)),
characters: create('List<Character>', v => Character.isCharacterList(v)),
data: create('Data', v => Data.isData(v)),
document: create('Document', v => Document.isDocument(v)),
history: create('History', v => History.isHistory(v)),

View File

@@ -7,7 +7,6 @@
const MODEL_TYPES = {
BLOCK: '@@__SLATE_BLOCK__@@',
CHANGE: '@@__SLATE_CHANGE__@@',
CHARACTER: '@@__SLATE_CHARACTER__@@',
DOCUMENT: '@@__SLATE_DOCUMENT__@@',
HISTORY: '@@__SLATE_HISTORY__@@',
INLINE: '@@__SLATE_INLINE__@@',

View File

@@ -1,7 +1,6 @@
import Block from './models/block'
import Change from './models/change'
import Changes from './changes'
import Character from './models/character'
import Data from './models/data'
import Document from './models/document'
import History from './models/history'
@@ -33,7 +32,6 @@ export {
Block,
Change,
Changes,
Character,
Data,
Document,
History,
@@ -61,7 +59,6 @@ export {
export default {
Block,
Changes,
Character,
Data,
Document,
History,

View File

@@ -1,175 +0,0 @@
import isPlainObject from 'is-plain-object'
import logger from 'slate-dev-logger'
import { List, Record, Set } from 'immutable'
import MODEL_TYPES, { isType } from '../constants/model-types'
/**
* Default properties.
*
* @type {Object}
*/
const DEFAULTS = {
marks: new Set(),
text: '',
}
/**
* Character.
*
* @type {Character}
*/
class Character extends Record(DEFAULTS) {
/**
* Create a `Character` with `attrs`.
*
* @param {Object|String|Character} attrs
* @return {Character}
*/
static create(attrs = {}) {
if (Character.isCharacter(attrs)) {
return attrs
}
if (typeof attrs == 'string') {
attrs = { text: attrs }
}
if (isPlainObject(attrs)) {
return Character.fromJSON(attrs)
}
throw new Error(
`\`Character.create\` only accepts objects, strings or characters, but you passed it: ${attrs}`
)
}
/**
* Create a list of `Characters` from `elements`.
*
* @param {String|Array<Object|Character|String>|List<Object|Character|String>} elements
* @return {List<Character>}
*/
static createList(elements = []) {
if (typeof elements == 'string') {
elements = elements.split('')
}
if (List.isList(elements) || Array.isArray(elements)) {
const list = new List(elements.map(Character.create))
return list
}
throw new Error(
`\`Block.createList\` only accepts strings, arrays or lists, but you passed it: ${elements}`
)
}
/**
* Create a `Character` from a JSON `object`.
*
* @param {Object} object
* @return {Character}
*/
static fromJSON(object) {
const { text, marks = [] } = object
if (typeof text != 'string') {
throw new Error('`Character.fromJSON` requires a block `text` string.')
}
const character = new Character({
text,
marks: new Set(marks),
})
return character
}
/**
* Alias `fromJS`.
*/
static fromJS = Character.fromJSON
/**
* Check if `any` is a `Character`.
*
* @param {Any} any
* @return {Boolean}
*/
static isCharacter = isType.bind(null, 'CHARACTER')
/**
* Check if `any` is a character list.
*
* @param {Any} any
* @return {Boolean}
*/
static isCharacterList(any) {
return List.isList(any) && any.every(item => Character.isCharacter(item))
}
/**
* Object.
*
* @return {String}
*/
get object() {
return 'character'
}
get kind() {
logger.deprecate(
'slate@0.32.0',
'The `kind` property of Slate objects has been renamed to `object`.'
)
return this.object
}
/**
* Return a JSON representation of the character.
*
* @return {Object}
*/
toJSON() {
const object = {
object: this.object,
text: this.text,
marks: this.marks.toArray().map(m => m.toJSON()),
}
return object
}
/**
* Alias `toJS`.
*/
toJS() {
return this.toJSON()
}
}
/**
* Attach a pseudo-symbol for type checking.
*/
Character.prototype[MODEL_TYPES.CHARACTER] = true
/**
* Export.
*
* @type {Character}
*/
export default Character

View File

@@ -3,7 +3,6 @@ import logger from 'slate-dev-logger'
import { List, Record, Set } from 'immutable'
import MODEL_TYPES, { isType } from '../constants/model-types'
import Character from './character'
import Mark from './mark'
/**
@@ -238,31 +237,6 @@ class Leaf extends Record(DEFAULTS) {
return this.object
}
/**
* Return leaf as a list of characters
*
* @return {List<Character>}
*/
getCharacters() {
logger.deprecate(
'slate@0.34.0',
'The `characters` property of Slate objects is deprecated'
)
const { marks } = this
const characters = Character.createList(
this.text.split('').map(char => {
return Character.create({
text: char,
marks,
})
})
)
return characters
}
/**
* Update a `mark` at leaf, replace with newMark
*

View File

@@ -207,7 +207,6 @@ class Node {
}
/**
<<<<<<< HEAD
* Create a point with `properties` relative to the node.
*
* @param {Object|Point} properties
@@ -222,9 +221,6 @@ class Node {
/**
* Create a range with `properties` relative to the node.
=======
* Create a new range with `properties` relative to the node.
>>>>>>> master
*
* @param {Object|Range} properties
* @return {Range}
@@ -481,47 +477,6 @@ class Node {
}, [])
}
/**
* Get all of the characters for every text node.
*
* @return {List<Character>}
*/
getCharacters() {
const characters = this.getTexts().flatMap(t => t.characters)
return characters
}
/**
* Get a list of the characters in a `range`.
*
* @param {Range} range
* @return {List<Character>}
*/
getCharactersAtRange(range) {
range = this.resolveRange(range)
if (range.isUnset) return List()
const { start, end } = range
if (start.key === end.key) {
const endText = this.getDescendant(end.key)
return endText.characters.slice(start.offset, end.offset)
}
return this.getTextsAtRange(range).flatMap(t => {
if (t.key === start.key) {
return t.characters.slice(start.offset)
}
if (t.key === end.key) {
return t.characters.slice(0, end.offset)
}
return t.characters
})
}
/**
* Get a child node.
*
@@ -2032,7 +1987,6 @@ class Node {
}
/**
<<<<<<< HEAD
* Resolve a `point`, relative to the node, ensuring that the keys and
* offsets in the point exist and that they are synced with the paths.
*
@@ -2047,8 +2001,6 @@ class Node {
}
/**
=======
>>>>>>> master
* Resolve a `range`, relative to the node, ensuring that the keys and
* offsets in the range exist and that they are synced with the paths.
*

View File

@@ -191,16 +191,6 @@ class Text extends Record(DEFAULTS) {
return this.leaves.reduce((string, leaf) => string + leaf.text, '')
}
/**
* Get the concatenated characters of the node;
*
* @returns {String}
*/
get characters() {
return this.leaves.flatMap(x => x.getCharacters())
}
/**
* Find the 'first' leaf at offset; By 'first' the alorighthm prefers `endOffset === offset` than `startOffset === offset`
* Corner Cases:

View File

@@ -448,18 +448,6 @@ class Value extends Record(DEFAULTS) {
)
}
/**
* Get the characters in the current selection.
*
* @return {List<Character>}
*/
get characters() {
return this.selection.isUnset
? new List()
: this.document.getCharactersAtRange(this.selection)
}
/**
* Get the marks of the current selection.
*