1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-21 14:41:23 +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

@@ -31,7 +31,6 @@
* [Block](./reference/slate/block.md)
* [Change](./reference/slate/change.md)
* [Character](./reference/slate/character.md)
* [Data](./reference/slate/data.md)
* [Document](./reference/slate/document.md)
* [Inline](./reference/slate/inline.md)

View File

@@ -39,14 +39,6 @@ Ensure that a value is an immutable `List` of Slate [`Block`](../slate/block.md)
Ensure that a value is a Slate `Change`.
### `character`
Ensure that a value is a Slate `Character`.
### `characters`
Ensure that a value is an immutable `List` of Slate [`Character`](../slate/character.md) objects.
### `data`
Ensure that a value is a Slate `Data`.

View File

@@ -1,70 +0,0 @@
# `Character`
```js
import { Character } from 'slate'
```
A character in a [`Text`](./text.md) node.
Characters are how Slate associates [`Marks`](./mark.md) with a range of text, for formatting.
## Properties
```js
Character({
marks: Immutable.Set<Mark>,
text: String
})
```
### `object`
`String`
A string with a value of `'character'`.
### `marks`
`Immutable.Set`
A set of [`Marks`](./mark.md) attached to the character.
### `text`
`String`
The text string of the character.
## Static Methods
### `Character.create`
`Character.create(properties: Object) => Character`
Create a character from a plain Javascript object of `properties`.
### `Character.createList`
`Character.createList(array: Array) => List`
Create a list of characters from a plain Javascript `array`.
### `Character.fromJSON`
`Character.fromJSON(object: Object) => Character`
Create a character from a JSON `object`.
### `Character.isCharacter`
`Character.isCharacter(maybeCharacter: Any) => Boolean`
Returns a boolean if the passed in argument is a `Character`.
## Instance Methods
### `toJSON`
`toJSON() => Object`
Returns a JSON representation of the character.

View File

@@ -4,7 +4,7 @@
import { Mark } from 'slate'
```
A formatting mark that can be associated with [`Characters`](./character.md). Marks are how Slate represents rich formatting like **bold** or _italic_.
A formatting mark that can be associated with characters. Marks are how Slate represents rich formatting like **bold** or _italic_.
## Properties

View File

@@ -73,18 +73,6 @@ Get all of the bottom-most [`Block`](./block.md) nodes in a `range`.
Get all of the bottom-most [`Block`](./block.md) nodes by `type`.
### `getCharacters`
`getCharacters() => List`
Get a list of all of the [`Characters`](./character.md) in the node.
### `getCharactersAtRange`
`getCharactersAtRange(range: Range) => List`
Get a list of all of the [`Characters`](./character.md) in a `range`.
### `getChild`
`getChild(path: List|Array) => Node|Void`

View File

@@ -10,15 +10,10 @@ A text node in a Slate [`Document`](./document.md). Text nodes are always the bo
```js
Text({
characters: Immutable.List<Character>,
key: String
key: String,
})
```
### `characters`
A list of [`Characters`](./character.md) with associated [`Marks`](./mark.md) that make up the text node's content.
### `key`
`String`

View File

@@ -117,12 +117,6 @@ Get a list of the lowest-depth [`Inline`](./inline.md) nodes in the current sele
Get a list of the [`Text`](./text.md) nodes in the current selection.
### `characters`
`List`
Get a list of the [`Character`](./character.md) objects in the current selection.
### `hasUndos`
`Boolean`

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.
*