ContentState is an Immutable Record that represents the full state of:

  • The entire contents of an editor: text, block and inline styles, and entity ranges.
  • Two selection states of an editor: before and after the rendering of these contents.

The most common use for the ContentState object is via EditorState.getCurrentContent(), which provides the ContentState currently being rendered in the editor.

An EditorState object maintains undo and redo stacks comprised of ContentState objects.

ContentState是一个代表着全部状态的不可变记录对象:

  • 编辑器的全部内容:文本、块和内联样式以及实体范围。
  • 编辑器的两个选择状态:渲染这些内容之前和之后。

对于ContentState对象,最常见的用法是通过EditorState.getCurrentContent(),它提供了当前在编辑器被渲染的ContentState.

EditorState对象维护由ContentState对象组成的撤销和重做堆栈。

Overview

Static Methods

createFromText()

  1. static createFromText(
  2. text: string,
  3. delimiter?: string
  4. ): ContentState

Generates a ContentState from a string, with a delimiter to split the string into ContentBlock objects. If no delimiter is provided, ‘\n‘ is used.

从一个字符串生成一个ContentState,用一个分隔符将字符串分割成ContentBlock对象。如果没有提供分隔符,则默认使用'\n'

createFromBlockArray()

  1. static createFromBlockArray(
  2. blocks: Array<ContentBlock>,
  3. entityMap: ?OrderedMap
  4. ): ContentState

Generates a ContentState from an array of ContentBlock objects. The default selectionBefore and selectionAfter states have the cursor at the start of the content.

从一个ContentBlock对象数组中生成一个ContentState。默认的selectionBeforeselectionAfter状态将光标放在内容的开头。

Methods

getEntityMap()

  1. getEntityMap(): EntityMap

Returns an object store containing all DraftEntity records that have been created. In upcoming v0.11.0 the map returned will be an Immutable ordered map of DraftEntity records.

返回一个对象store,其中包含已创建的所有DraftEntity记录。在即将到来的v0.11.0中,返回的映射将是一个不可变的、有序的DraftEntity记录映射。

In most cases, you should be able to use the convenience methods below to target specific DraftEntity records or obtain information about the state of the content.

在大多数情况下,您应该能够使用下面的便利方法来针对特定的DraftEntity记录或获取关于内容状态的信息。

getBlockMap()

  1. getBlockMap(): BlockMap

Returns the full ordered map of ContentBlock objects representing the state of an entire document.

返回表示代表整个文档状态的ContentBlock对象的完整有序映射。

In most cases, you should be able to use the convenience methods below to target specific ContentBlock objects or obtain information about the state of the content.

在大多数情况下,您应该能够使用下面的便利方法来针对特定的ContentBlock对象或获取关于内容状态的信息。

getSelectionBefore()

  1. getSelectionBefore(): SelectionState

Returns the SelectionState displayed in the editor before rendering blockMap.