在Draftjs未知DraftEntity钥匙插入介质时

问题描述 投票:0回答:1

我是新来Draft.js和我努力使简单的编辑器具有以下this example,我的代码是不是完全一样的例子,但它的样子。但是当我在audiovideo按钮调用makeMedia点击我得到这个错误:

  Unknown DraftEntity key

这是我的编辑:

import React, { Component } from 'react'
import {Editor, EditorState, RichUtils,CompositeDecorator,AtomicBlockUtils,Entity } from 'draft-js'
import 'draft-js/dist/Draft.css';

 class MyEditor extends Component {
constructor(props) {
    super(props)
    this.state = { editorState: EditorState.createEmpty()}
    this.onChange = this.onChange.bind(this)
    this.makeMedia = this.makeMedia.bind(this)
}
onChange(editorState) {
    this.setState({ editorState })
}
makeMedia(type){
    const {editorState} = this.state;
    const contentState = editorState.getCurrentContent();
    const contentStateWithEntity = contentState.createEntity(
        type,
        'IMMUTABLE',
        {}
    )
    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
    const newEditorState = EditorState.set(editorState,{currentContent:contentStateWithEntity})
    this.setState({
        editorState:AtomicBlockUtils.insertAtomicBlock(
            newEditorState,
            entityKey,
            ''
        )
    })
}
render() {
    return (
        <div className="editor">
            <button onClick={()=>this.makeMedia('audio')}>audio</button>
            <button onClick={()=>this.makeMedia('video')}>video</button>
            <Editor
            blockRendererFn={mediaBlockRenderer}
                customStyleMap={colorStyleMap}
                editorState={this.state.editorState}
                onChange={this.onChange}
                ref={(element) => { this.editor = element }}
            />
        </div>
    )
}
 }
 function mediaBlockRenderer(block) {
 console.log(block.getType())
    if (block.getType() === 'atomic') {
      return {
        component: Media,
        editable: false,
      };
    }

    return null;
  }

  const Audio = (props)=>{
  return <audio controls/>
 }
const Video = (props)=>{
return <Video controls/>
 }
 const Media = (props)=>{
   let media;
   const entity = props.contentState.getEntity(
    props.block.getEntityAt(0)
   )
   const type = entity.getType();
    if(type === 'audio'){
    media = <Audio/>
   }
   else if (type === 'video'){
    media = <Video/>
  }
   return media;
}
export default MyEditor
draftjs
1个回答
2
投票

我解决这个问题,将一个空间,让我转换:

editorState:AtomicBlockUtils.insertAtomicBlock(
        newEditorState,
        entityKey,
        ''
    )

editorState:AtomicBlockUtils.insertAtomicBlock(
        newEditorState,
        entityKey,
        ' ' //ading space here
    )
© www.soinside.com 2019 - 2024. All rights reserved.