- title: ContentState 内容状态
- 总览
- 静态方法
- 方法
getEntityMap()
getBlockMap()
getSelectionBefore()
getSelectionAfter()
getBlockForKey()
getKeyBefore()
getKeyAfter()
getBlockBefore()
getBlockAfter()
getBlocksAsArray()
getFirstBlock()
getLastBlock()
getPlainText()
getLastCreatedEntityKey()
hasText()
createEntity()
getEntity()
mergeEntityData()
replaceEntityData()
addEntity()
- 属性
id: api-reference-content-state
title: ContentState 内容状态
ContentState是一个Immutable Record,表示以下各项的完整状态:
- 编辑器的全部内容:文本,块和内联样式以及实体范围(entity ranges)
- 编辑器的两种selection states:渲染这些内容之前和之后。
ContentState
对象的最常见用法是通过EditorState.getCurrentContent()
,它提供了当前在编辑器中渲染的ContentState
。
EditorState
对象维护由ContentState
对象组成的撤消和重做堆栈。
总览
静态方法
-
- static createFromText(text: string): ContentState
-
- static createFromBlockArray(blocks: Array<ContentBlock>): ContentState
方法
-
- getEntityMap()
-
- getBlockMap()
-
- getSelectionBefore()
-
- getSelectionAfter()
-
- getBlockForKey(key)
-
- getKeyBefore(key)
-
- getKeyAfter(key)
-
- getBlockBefore(key)
-
- getBlockAfter(key)
-
- getBlocksAsArray()
-
- getFirstBlock()
-
- getLastBlock()
-
- getPlainText(delimiter)
-
- getLastCreatedEntityKey()
-
- hasText()
-
- createEntity(…)
-
- getEntity(…)
-
- mergeEntityData(…)
-
- replaceEntityData(…)
-
- addEntity(…)
属性
使用Immutable Map API设置属性。
例子
const editorState = EditorState.createEmpty();
const contentState = editorState.getCurrentContent();
const contentStateWithSelectionBefore = contentState.set(
'selectionBefore',
SelectionState.createEmpty(contentState.getBlockForKey('1pu4d')),
);
静态方法
createFromText()
static createFromText(
text: string,
delimiter?: string
): ContentState
从字符串生成ContentState
,并使用delimiter(定界符)将字符串拆分为ContentBlock
对象。
如果未提供定界符,则使用'\n'
。
createFromBlockArray()
static createFromBlockArray(
blocks: Array<ContentBlock>,
entityMap: ?OrderedMap
): ContentState
从ContentBlock
对象数组生成ContentState
。
默认的selectionBefore
和selectionAfter
状态将光标置于内容的开头。
方法
getEntityMap()
getEntityMap(): EntityMap
返回一个包含所有已创建的DraftEntity
记录的对象库。
在即将发布的v0.11.0中,返回的map(映射)将是DraftEntity
记录的Immutable ordered map(Immutable有序映射)。
在大多数情况下,您应该能够使用下面的便捷方法来定位特定的DraftEntity
记录或获取有关内容状态的信息。
getBlockMap()
getBlockMap(): BlockMap
返回表示整个文档状态的ContentBlock
对象的完整顺序图。
在大多数情况下,您应该能够使用下面的便捷方法来定位特定的ContentBlock
对象或获取有关内容状态的信息。
getSelectionBefore()
getSelectionBefore(): SelectionState
返回渲染blockMap
之前在编辑器中显示的SelectionState
在编辑器中执行撤消操作时,当前ContentState
的selectionBefore
用于将selection range(选择范围)放置在适当的位置。
getSelectionAfter()
getSelectionAfter(): SelectionState
返回渲染blockMap
后在编辑器中显示的SelectionState
。
当在编辑器中执行导致渲染此blockMap
的任何操作时,选择范围将放置在selectionAfter
位置。
getBlockForKey()
getBlockForKey(key: string): ContentBlock
返回与给定块键相对应的ContentBlock
。
例子
var {editorState} = this.state;
var startKey = editorState.getSelection().getStartKey();
var selectedBlockType = editorState
.getCurrentContent()
.getBlockForKey(startKey)
.getType();
getKeyBefore()
getKeyBefore(key: string): ?string
返回blockMap
中指定键之前的键;如果这是第一个键,则返回null
。
getKeyAfter()
getKeyAfter(key: string): ?string
在blockMap
中指定键之后返回键;如果这是最后一个键,则返回null
。
getBlockBefore()
getBlockBefore(key: string): ?ContentBlock
在blockMap
中指定键之前返回ContentBlock
;如果这是第一个键,则返回null
。
getBlockAfter()
getBlockAfter(key: string): ?ContentBlock
在blockMap
中指定键之后返回ContentBlock
;如果这是最后一个键,则返回null
。
getBlocksAsArray()
getBlocksAsArray(): Array<ContentBlock>
以数组形式返回blockMap
的值。
通常不需要使用此方法,因为getBlockMap
提供了应该用于迭代的OrderedMap
。
getFirstBlock()
getFirstBlock(): ContentBlock
返回第一个ContentBlock
。
getLastBlock()
getLastBlock(): ContentBlock
返回最后一个ContentBlock
。
getPlainText()
getPlainText(delimiter?: string): string
返回内容的完整纯文本值,并带有delimiter(定界符)。
如果未指定分隔符,则使用换行符(\u000A
)。
getLastCreatedEntityKey()
getLastCreatedEntityKey(): string
返回可用于引用最近创建的DraftEntity
记录的字符串键。
这是因为实体是通过ContentState
中的字符串键引用的。
应在CharacterMetadata
对象中使用字符串值来跟踪带注释字符的实体
hasText()
hasText(): boolean
返回内容是否完全包含任何文本。
createEntity()
createEntity(
type: DraftEntityType,
mutability: DraftEntityMutability,
data?: Object
): ContentState
返回已更新的ContentState
记录,以将新创建的DraftEntity
记录包含在EntityMap
中。
调用getLastCreatedEntityKey
以获取新创建的DraftEntity
记录的键。
getEntity()
getEntity(key: string): DraftEntityInstance
返回指定键的DraftEntityInstance
。
如果该键不存在实例,则抛出该异常。
mergeEntityData()
mergeEntityData(
key: string,
toMerge: {[key: string]: any}
): ContentState
由于DraftEntityInstance
对象是immutable(不可变的),因此您无法通过典型的可变方式来更新实体的元数据。
mergeData方法允许您将更新应用于指定的实体。
replaceEntityData()
replaceEntityData(
key: string,
newData: {[key: string]: any}
): ContentState
replaceData
方法与mergeData
方法类似,不同之处在于它将完全丢弃实例的现有数据值并将其替换为指定的newData
。
addEntity()
addEntity(instance: DraftEntityInstance): ContentState
在大多数情况下,您将使用contentState.createEntity()
。
这是一种便捷的方法,在典型的Draft
用法中您可能不需要。
在已经创建实例并且现在需要将其添加到实体存储的情况下,添加功能很有用。
如果要恢复ContentState
的原始JavaScript表示以进行编辑,则可能会发生这种情况。
属性
使用Immutable Map API设置属性。.
blockMap
查看getBlockMap()
.
selectionBefore
查看getSelectionBefore()
.
selectionAfter
查看getSelectionAfter()
.