3

请检查下面的代码,我已经创建了 ImageButton 组件并加载到草稿 js(编辑器组件)

图片按钮.js

import React from 'react';
import PropTypes from 'prop-types';
import { EditorState, AtomicBlockUtils,convertToRaw,Entity  } from 'draft-js';
import { addNewBlock } from '../../util/model';
import { Block } from '../../util/constants';
import {imageEditorImg} from './../images';
import S3 from 'aws-s3';

var options = {
  accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY,
  secretAccessKey: process.env.REACT_APP_AWS_SECRET_KEY,
  bucketName: process.env.REACT_APP_BUCKET_NAME,
  region: process.env.REACT_APP_REGION,
  dirName: process.env.REACT_APP_DIR_NAME,
}

export default class ImageButton extends React.Component {

  static propTypes = {
    onChange: PropTypes.func,
    editorState: PropTypes.object,
  };

  constructor(props) {
    super(props);
    this.onClick = this.onClick.bind(this);
  }

  onClick() {
    this.input.value = null;
    this.input.click();
  }



  uploadImageCallBack(file) {
    return new Promise(
      (resolve, reject) => {
        const acceptedImageTypes = ['image/gif','image/jpeg','image/jpg','image/png','image/svg'];
        if(acceptedImageTypes.includes(file.type))
        {
            var getFileName = file.name.replace(/ /g,"_");
            var new_file = new File([file], file.lastModified+'_'+getFileName,{
              type: file.type
            });
           var getNewFileName = (file.lastModified+'_'+getFileName).replace(/\.[^/.]+$/, "");

            const S3Client = new S3(options);

            S3Client.uploadFile(new_file,getNewFileName)
            .then((data) => {
               console.log(data.location);

                resolve(data.location)
            })
            .catch((err) => {
                console.log(err)
                reject(err)
            })
        }
        else
        {
          console.log('Validation occured');
              let responseData={
                "data":{
                    "link":'',
                    "type":''
                },
                "status":400,
                "success" : false
            }
            resolve(responseData)
            toast.error(invalidFileFormate);
        }

      }
    );
  }

  addBlockQuote =(e) => {
    const { editorState, onChange } = this.props;
    const file = e.target.files[0];
    if (file.type.indexOf('image/') === 0) {
      console.log(file);
      console.log(editorState);

      const updateData = this.uploadImageCallBack(file);

      updateData.then(function(value) 
        {

            const contentStateWithEntity = Entity.create(
              "image",
              "IMMUTABLE",
              { src: value }
            );


            const contentState = editorState.getCurrentContent();

            const contentStateWithEntity = contentState.createEntity('image', 'IMMUTABLE', { src: value });
            const entityKey = contentStateWithEntity.getLastCreatedEntityKey();

            const newEditorState1 = EditorState.set(editorState, {
              currentContent: contentStateWithEntity
            });

            const newEditorState = AtomicBlockUtils.insertAtomicBlock(
              newEditorState1,
              entityKey,
              ' '
            );

            console.log(convertToRaw(editorState.getCurrentContent()), convertToRaw(newEditorState.getCurrentContent()), Entity.get(entityKey));

            onChange(newEditorState);


    }
  }


  render() {
    return (
      <div
        aria-selected = 'false'
        aria-label='rdw-image-control'
        className="rdw-image-wrapper"
        type="button"
        onClick={this.onClick}
        title="Add an Image"
        style={{cursor:'pointer',background:'none',border:'none',padding:'4px',maxWidth:'25px',height:'20px',margin:'4px'}}
      >
        <img alt="Upload image" src={imageEditorImg} />
        <input
          type="file"
          ref={(c) => { this.input = c; }}
          onChange={this.addBlockQuote}
          style={{ display: 'none' }}
        />
      </div>
    );
  }
}

我还使用 s3 客户端 npm 上传了图像,在获得承诺响应后,我在控制台日志中得到了有关已创建实体映射的结果,但图像未在编辑器中呈现(也生成了图形标签,但在该图像标签和图像跨度标签中丢失。

4

0 回答 0