| id | api-reference-content-state |
|---|---|
| title | ContentState |
| layout | docs |
| category | API Reference |
| next | api-reference-content-block |
| permalink | docs/api-reference-content-state.html |
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.
Static Methods
-
static createFromText(text: string): ContentState
-
static createFromBlockArray(blocks: Array): ContentState
Methods
-
getBlockMap()
-
getSelectionBefore()
-
getSelectionAfter()
-
getBlockForKey(key)
-
getKeyBefore(key)
-
getKeyAfter(key)
-
getBlockBefore(key)
-
getBlockAfter(key)
-
getBlocksAsArray()
-
getFirstBlock()
-
getLastBlock()
-
getPlainText(delimiter)
-
hasText()
Properties
Use Immutable Map API to set properties.
static createFromText(
text: string,
delimiter?: string
): 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.
static createFromBlockArray(blocks: Array<ContentBlock>): 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.
getBlockMap(): BlockMap
Returns the full ordered map of ContentBlock objects representing the state
of an entire document.
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.
getSelectionBefore(): SelectionState
Returns the SelectionState displayed in the editor before rendering blockMap.
When performing an undo action in the editor, the selectionBefore of the current
ContentState is used to place the selection range in the appropriate position.
getSelectionAfter(): SelectionState
Returns the SelectionState displayed in the editor after rendering blockMap.
When performing any action in the editor that leads to this blockMap being rendered,
the selection range will be placed in the selectionAfter position.
getBlockForKey(key: string): ContentBlock
Returns the ContentBlock corresponding to the given block key.
var {editorState} = this.state;
var startKey = editorState.getSelection().getStartKey();
var selectedBlockType = editorState
.getCurrentContent()
.getBlockForKey(startKey)
.getType();
getKeyBefore(key: string): ?string
Returns the key before the specified key in blockMap, or null if this is the first key.
getKeyAfter(key: string): ?string
Returns the key after the specified key in blockMap, or null if this is the last key.
getBlockBefore(key: string): ?ContentBlock
Returns the ContentBlock before the specified key in blockMap, or null if this is
the first key.
getBlockAfter(key: string): ?ContentBlock
Returns the ContentBlock after the specified key in blockMap, or null if this is the last key.
getBlocksAsArray(): Array<ContentBlock>
Returns the values of blockMap as an array.
You generally won't need to use this method, since getBlockMap provides an OrderedMap that you should use for iteration.
getFirstBlock(): ContentBlock
Returns the first ContentBlock.
getLastBlock(): ContentBlock
Returns the last ContentBlock.
getPlainText(delimiter?: string): string
Returns the full plaintext value of the contents, joined with a delimiter. If no
delimiter is specified, the line feed character (\u000A) is used.
hasText(): boolean
Returns whether the contents contain any text at all.
Use Immutable Map API to set properties.
See getBlockMap().
See getSelectionBefore().
See getSelectionAfter().