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方法。

当创建一个新的包含文本但不包含characterListContentBlock时,它将默认为提供的文本带有空CharacterMetadatacharacterList

总览

方法

属性

注意

Immutable Map API用于ContentBlock构造函数或设置属性

方法

getKey()

  1. getKey(): string

返回此ContentBlock的字符串键。 Block keys(块的键)是字母数字字符串。 建议使用generateRandomKey生成块密钥。

getType()

  1. getType(): DraftBlockType

返回此ContentBlock的类型。 类型值在很大程度上类似于块级HTML元素。

getText()

  1. getText(): string

返回此ContentBlock的纯文本。 此值不包含任何样式,装饰或HTML信息。

getCharacterList()

  1. getCharacterList(): List<CharacterMetadata>

返回一个CharacterMetadata对象的immutable List(不可变列表),ContentBlock中的每个字符。 (有关详细信息,请参见CharacterMetadata。)

该列表包含该块的所有样式和实体信息。

getLength()

  1. getLength(): number

返回ContentBlock的纯文本长度。

该值使用标准的JavaScript length属性作为字符串,因此不支持Unicode-代理对将被视为两个字符。

getDepth()

  1. getDepth(): number

返回此块的depth value(深度值)(如果有)。 当前仅用于列表项。

getInlineStyleAt()

  1. getInlineStyleAt(offset: number): DraftInlineStyle

返回此ContentBlock中给定偏移量的DraftInlineStyle值(一个OrderedSet <string>)。

getEntityAt()

  1. getEntityAt(offset: number): ?string

返回此ContentBlock中给定偏移量的实体键值(如果没有,则返回null

getData()

  1. getData(): Map<any, any>

返回块级元数据。

findStyleRanges()

  1. findStyleRanges(
  2. filterFn: (value: CharacterMetadata) => boolean,
  3. callback: (start: number, end: number) => void
  4. ): void

为该ContentBlock中的每个连续样式范围执行一个回调。

findEntityRanges()

  1. findEntityRanges(
  2. filterFn: (value: CharacterMetadata) => boolean,
  3. callback: (start: number, end: number) => void
  4. ): void

为该ContentBlock中每个连续范围的实体执行一个回调。

属性

注意

Immutable Map API用于ContentBlock构造函数或设置属性。

key

查看getKey().

text

查看getText().

type

查看getType().

characterList

查看getCharacterList().

depth

查看getDepth().

data

查看getData().