id: api-reference-content-block
title: ContentBlock 内容块
ContentBlock是Immutable Record,表示单个编辑器内容块的完整状态,包括
- 块的纯文本内容
- 类型, 例如paragraph, header, list item (段落,标题,列表项)
- 实体,内联样式和depth(深度)信息
ContentState
对象包含这些ContentBlock
对象的OrderedMap
,它们一起构成编辑器的全部内容。
ContentBlock
对象在很大程度上类似于块级HTML元素,例如段落和列表项。
可用的类型有:
- unstyled (未样式化)
- paragraph
- header-one
- header-two
- header-three
- header-four
- header-five
- header-six
- unordered-list-item (无序列表)
- ordered-list-item (有序列表)
- blockquote (块引用)
- code-block (代码块)
- atomic (原子)
可以使用构造函数直接创建新的ContentBlock
对象。
预期记录值将在下面详细说明。
表示样式和实体
characterList
字段是一个immutable List
,其中包含该块中每个字符的CharacterMetadata
对象。
这就是我们如何编码给定块的样式和实体。
通过大量使用这些列表和CharacterMetadata
对象的不变性和数据持久性,对内容的编辑通常对编辑器的内存占用影响很小。
通过以这种方式一起编码内联样式和实体,在ContentBlock上
执行编辑的函数可以在单个List对象上执行slices, concats和其他List
方法。
当创建一个新的包含文本但不包含characterList
的ContentBlock
时,它将默认为提供的文本带有空CharacterMetadata
的characterList
。
总览
方法
-
- getKey(): string
-
- getType(): DraftBlockType
-
- getText(): string
-
- getCharacterList(): List<CharacterMetadata>
-
- getLength(): number
-
- getDepth(): number
-
- getInlineStyleAt(offset: number): DraftInlineStyle
-
- getEntityAt(offset: number): ?string
-
- getData(): Map<any, any>
-
- findStyleRanges(filterFn: Function, callback: Function): void
-
- findEntityRanges(filterFn: Function, callback: Function): void
属性
注意
将Immutable Map API用于
ContentBlock
构造函数或设置属性
-
- key: string
-
- type: DraftBlockType
-
- text: string
-
- characterList: List<CharacterMetadata>
-
- depth: number
-
- data: Map<any, any>
方法
getKey()
getKey(): string
返回此ContentBlock
的字符串键。
Block keys(块的键)是字母数字字符串。
建议使用generateRandomKey
生成块密钥。
getType()
getType(): DraftBlockType
返回此ContentBlock
的类型。
类型值在很大程度上类似于块级HTML元素。
getText()
getText(): string
返回此ContentBlock
的纯文本。
此值不包含任何样式,装饰或HTML信息。
getCharacterList()
getCharacterList(): List<CharacterMetadata>
返回一个CharacterMetadata
对象的immutable List
(不可变列表),ContentBlock
中的每个字符。
(有关详细信息,请参见CharacterMetadata。)
该列表包含该块的所有样式和实体信息。
getLength()
getLength(): number
返回ContentBlock
的纯文本长度。
该值使用标准的JavaScript length
属性作为字符串,因此不支持Unicode-代理对将被视为两个字符。
getDepth()
getDepth(): number
返回此块的depth value(深度值)(如果有)。 当前仅用于列表项。
getInlineStyleAt()
getInlineStyleAt(offset: number): DraftInlineStyle
返回此ContentBlock
中给定偏移量的DraftInlineStyle
值(一个OrderedSet <string>
)。
getEntityAt()
getEntityAt(offset: number): ?string
返回此ContentBlock
中给定偏移量的实体键值(如果没有,则返回null
)
getData()
getData(): Map<any, any>
返回块级元数据。
findStyleRanges()
findStyleRanges(
filterFn: (value: CharacterMetadata) => boolean,
callback: (start: number, end: number) => void
): void
为该ContentBlock
中的每个连续样式范围执行一个回调。
findEntityRanges()
findEntityRanges(
filterFn: (value: CharacterMetadata) => boolean,
callback: (start: number, end: number) => void
): void
为该ContentBlock
中每个连续范围的实体执行一个回调。
属性
注意
将Immutable Map API用于
ContentBlock
构造函数或设置属性。
key
查看getKey()
.
text
查看getText()
.
type
查看getType()
.
characterList
查看getCharacterList()
.
depth
查看getDepth()
.
data
查看getData()
.