id: api-reference-modifier

title: Modifier 修改器

Modifier模块是一组实用函数的static set(静态集合),其中封装了ContentState对象上的常见编辑操作。 强烈建议您使用这些方法进行编辑操作

给定任何受影响实体的可变性类型,这些方法还应适当地删除或修改实体范围。

一般情况下,这些方法都接受带有相关参数的ContentState对象,并返回ContentState对象。 如果未实际执行编辑,则返回的ContentState将与输入对象相同

总览

方法

静态方法

replaceText()

  1. replaceText(
  2. contentState: ContentState,
  3. rangeToReplace: SelectionState,
  4. text: string,
  5. inlineStyle?: DraftInlineStyle,
  6. entityKey?: ?string
  7. ): ContentState

用提供的字符串替换此ContentState的指定范围,并将内联样式和实体关键字应用于整个插入的字符串。

示例:在Facebook上,当用提及Abraham Lincoln的方式替换@abraham lincoln时,整个旧范围是要替换的目标,并且提及实体应应用于插入的字符串

insertText()

  1. insertText(
  2. contentState: ContentState,
  3. targetRange: SelectionState,
  4. text: string,
  5. inlineStyle?: DraftInlineStyle,
  6. entityKey?: ?string
  7. ): ContentState

replaceText相同,但强制将目标范围折叠,以便不替换任何字符。 这只是为了方便,因为文本编辑经常是插入而不是替换

moveText()

  1. moveText(
  2. contentState: ContentState,
  3. removalRange: SelectionState,
  4. targetRange: SelectionState
  5. ): ContentState

将”removal(删除)”范围移动到”target”范围,替换目标文本。

replaceWithFragment()

  1. replaceWithFragment(
  2. contentState: ContentState,
  3. targetRange: SelectionState,
  4. fragment: BlockMap
  5. ): ContentState

“fragment(片段)”是block map的一部分,实际上仅是OrderedMap <string,ContentBlock>ContentState对象的完整block map大致相同。

此方法将用fragment(片段)替换”target”范围。

示例:粘贴内容时,我们将粘贴转换为片段以插入到编辑器中,然后使用此方法添加它。

removeRange()

  1. removeRange(
  2. contentState: ContentState,
  3. rangeToRemove: SelectionState,
  4. removalDirection: DraftRemovalDirection
  5. ): ContentState

从编辑器中删除整个文本范围。 删除方向对于正确的实体删除行为很重要

splitBlock()

  1. splitBlock(
  2. contentState: ContentState,
  3. selectionState: SelectionState
  4. ): ContentState

将选定的块分成两个块。 仅当selection处于折叠状态时才应使用

applyInlineStyle()

  1. applyInlineStyle(
  2. contentState: ContentState,
  3. selectionState: SelectionState,
  4. inlineStyle: string
  5. ): ContentState

将指定的嵌入式样式应用于整个选定范围(selected range)。

removeInlineStyle()

  1. removeInlineStyle(
  2. contentState: ContentState,
  3. selectionState: SelectionState,
  4. inlineStyle: string
  5. ): ContentState

从整个选定范围中删除指定的嵌入式内联样式。

setBlockType()

  1. setBlockType(
  2. contentState: ContentState,
  3. selectionState: SelectionState,
  4. blockType: DraftBlockType
  5. ): ContentState

设置所有选定块的块类型。

setBlockData()

  1. setBlockData(
  2. contentState: ContentState,
  3. selectionState: SelectionState,
  4. blockData: Map<any, any>
  5. ): ContentState

设置所有选定块的块数据。

mergeBlockData()

  1. mergeBlockData(
  2. contentState: ContentState,
  3. selectionState: SelectionState,
  4. blockData: Map<any, any>
  5. ): ContentState

更新所有选定块的块数据。

applyEntity()

  1. applyEntity(
  2. contentState: ContentState,
  3. selectionState: SelectionState,
  4. entityKey: ?string
  5. ): ContentState

将实体应用于整个选定范围,或者如果entityKeynull,则从该范围中删除所有实体