Draft-JS - 如何使用一些不可编辑的文本创建自定义块

Draft-JS - How to create a custom block with some non-editable text

在 Draft-JS 中,我想要一个基本的自定义块,渲染一个 <h1> 元素。我想在 h1 之前添加一些用户无法编辑的文本。此处的文字是为了告知人们此块用于 Title。所以我想在不可编辑的块前面加上"TITLE"

在 Draft JS 中实现此目的的最佳方法是什么?

您可以通过在应该是 read-only:

的节点上应用 contentEditable={false}readOnly 属性 来实现您的目标
class MyCustomBlock extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div className="my-custom-block">
        <h1
          contentEditable={false} // <== !!!
          readOnly // <== !!!
        >
          Not editable title
        </h1>
        <div className="editable-area">
          <EditorBlock {...this.props} />
        </div>
      </div>
    );
  }
}

在下面的隐藏代码段中检查工作演示:

const {Editor, CharacterMetadata, DefaultDraftBlockRenderMap, ContentBlock, EditorBlock, genKey, ContentState, EditorState} = Draft;
const { List, Map, Repeat } = Immutable;

class MyCustomBlock extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div className="my-custom-block">
        <h1
          contentEditable={false}
          readOnly
        >
          Not editable title
        </h1>
        <div className="editable-area">
          <EditorBlock {...this.props} />
        </div>
      </div>
    );
  }
}

function blockRendererFn(contentBlock) {
  const type = contentBlock.getType();
  
  if (type === 'MyCustomBlock') {
    return {
      component: MyCustomBlock,
      props: {}
    };
  }
}

const RenderMap = new Map({
  MyCustomBlock: {
    element: 'div',
  }
}).merge(DefaultDraftBlockRenderMap);

const extendedBlockRenderMap = Draft.DefaultDraftBlockRenderMap.merge(RenderMap);

class Container extends React.Component {
  constructor(props) {
    super(props);

   this.state = {
      editorState: EditorState.createEmpty()
    };
  }

  _handleChange = (editorState) => {
    this.setState({ editorState });
  }
  
  _onAddCustomBlock = () => {
    const selection = this.state.editorState.getSelection();
    
   this._handleChange(addNewBlockAt(
      this.state.editorState,
      selection.getAnchorKey(),
      'MyCustomBlock'
    ))
  }

  render() {
    return (
      <div>
        <div className="container-root">
          <Editor
            placeholder="Type"
            blockRenderMap={extendedBlockRenderMap}
            blockRendererFn={blockRendererFn}
            editorState={this.state.editorState}
            onChange={this._handleChange}
          />
          </div>
          <button onClick={this._onAddCustomBlock}>
            ADD CUSTOM BLOCK
          </button>
      </div>
    );
  }
}

ReactDOM.render(<Container />, document.getElementById('react-root'));

const addNewBlockAt = (
  editorState,
  pivotBlockKey,
  newBlockType = 'unstyled',
  initialData = new Map({})
) => {
  const content = editorState.getCurrentContent();
  const blockMap = content.getBlockMap();
  const block = blockMap.get(pivotBlockKey);

  if (!block) {
    throw new Error(`The pivot key - ${ pivotBlockKey } is not present in blockMap.`);
  }

  const blocksBefore = blockMap.toSeq().takeUntil((v) => (v === block));
  const blocksAfter = blockMap.toSeq().skipUntil((v) => (v === block)).rest();
  const newBlockKey = genKey();

  const newBlock = new ContentBlock({
    key: newBlockKey,
    type: newBlockType,
    text: '',
    characterList: new List(),
    depth: 0,
    data: initialData,
  });

  const newBlockMap = blocksBefore.concat(
    [[pivotBlockKey, block], [newBlockKey, newBlock]],
    blocksAfter
  ).toOrderedMap();

  const selection = editorState.getSelection();

  const newContent = content.merge({
    blockMap: newBlockMap,
    selectionBefore: selection,
    selectionAfter: selection.merge({
      anchorKey: newBlockKey,
      anchorOffset: 0,
      focusKey: newBlockKey,
      focusOffset: 0,
      isBackward: false,
    }),
  });

  return EditorState.push(editorState, newContent, 'split-block');
};
body {
  font-family: Helvetica, sans-serif;
}

.container-root {
  border: 1px solid black;
  padding: 5px;
  margin: 5px;
}

.my-custom-block {
  background-color: cadetblue;
  margin: 15px 0;
  font-size: 16px;
  position: relative;
}

.editable-area {
  background-color: lightblue;
  height: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.0/Draft.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.7.0/Draft.css" rel="stylesheet"/>
<div id="react-root"></div>