reactjs - 将空 block 添加到 Draft.js 而不移动选择

标签 reactjs draftjs

在不更改 SelectionState 的情况下向 Draft.js 编辑器添加空的无样式 block (最后说)的最佳方法是什么?

最佳答案

这就是我最终所做的:

import { List } from 'immutable'
import {
  EditorState,
  ContentState,
  ContentBlock,
  genKey
} from 'draft-js'

const addEmptyBlock = (editorState) => {
  const newBlock = new ContentBlock({
    key: genKey(),
    type: 'unstyled',
    text: '',
    characterList: List()
  })

  const contentState = editorState.getCurrentContent()
  const newBlockMap = contentState.getBlockMap().set(newBlock.key, newBlock)

  return EditorState.push(
    editorState,
    ContentState
      .createFromBlockArray(newBlockMap.toArray())
      .set('selectionBefore', contentState.getSelectionBefore())
      .set('selectionAfter', contentState.getSelectionAfter())
  )
}

关于reactjs - 将空 block 添加到 Draft.js 而不移动选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39530561/

相关文章:

javascript - 草稿-js : auto line break

reactjs - 从 react 状态中删除 key 的正确方法

javascript - 如何知道 onclick 事件是从 mouseclick 还是按 Enter 键调用的

javascript - 当 readOnly 属性为 true 时,如何在 Draft.js 编辑器中将自定义渲染 block 设置为只读?

reactjs - 工具栏draftjs编辑器中的自定义按钮

javascript - 无法在 Draft.js 中设置 editorState (它看起来不可变,但没有错误)

reactjs - 使用 testing-library 和 jest 的 React 组件抛出的测试错误

reactjs - React-Redux 应用程序中的不变性 - Immutablejs 和 Typescript 冲突

reactjs - 如何在 React Native 中加密数据(使用 Expo)

draftjs - 我如何设计 Draft-js 编辑器本身?