我在我的应用程序中使用基于草稿 js 的 react-draft-wysiwyg 文本编辑器。尝试通过编辑器图像控件上传图像时遇到一些问题。我正在使用自定义 FileUploader 组件。它将图像保存在服务器中,我也得到了包含上传图像 url 的正确响应。但是图像没有显示在编辑器中。这是我的编辑器组件代码:
import React from 'react';
import PropTypes from 'prop-types';
import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import FileUploader from 'containers/Globals/HtmlEditor/FileUploader';
const HTMLEditor =({editorState, onEditorStateChange}) => {
return (
<Editor
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName="editorClassName"
editorState={editorState}
onEditorStateChange={onEditorStateChange}
toolbar={{ image: {
className: 'editor_uploader',
component: FileUploader
}}}
hashtag={{
separator: ' ',
trigger: '#',
className: 'hashtag-className',
}}
mention={{
separator: ' ',
trigger: '@',
suggestions: [
],
}}
/>
);
}
HTMLEditor.propTypes = {
};
export default HTMLEditor;
下面是我在 TextEditor 中用于 FileUpload 的自定义Fileuploader组件。
import React, {Component} from 'react';
import Dropzone from 'react-dropzone';
import { connect } from 'react-redux';
import { uploadHtmlEditorImagesRequest } from './actions';
import { makeSelectHTMLEditorImageDetailInfo } from './selectors';
import { createStructuredSelector } from 'reselect';
const mapDispatchToProps = dispatch => ({
uploadHtmlEditorImagesRequest: (file) => dispatch(uploadHtmlEditorImagesRequest(file))
});
const mapStateToProps = createStructuredSelector({
image_file: makeSelectHTMLEditorImageDetailInfo()
});
const style = {
borderWidth: 2,
borderColor: 'black',
borderStyle: 'dashed',
borderRadius: 4,
margin: 30,
padding: 30,
width: 200,
transition: 'all 0.5s'
};
const activeStyle = {
borderStyle: 'solid',
backgroundColor: '#eee',
borderRadius: 8
};
class FileUploader extends Component {
constructor(props) {
super(props);
this.state = {
image_name: ''
}
}
onDrop = files => {
if (files) {
this.props.uploadHtmlEditorImagesRequest(files[0]);
}
};
uploadCallback(file) {
return new Promise(
(resolve, reject) => {
resolve({data: {link: "http://www.myfilmviews.com/wp-content/uploads/2012/12/scarlett_johansson.jpg"}});
}
);
}
componentWillReceiveProps(nextProps) {
if(nextProps.image_file.get('file_path')) {
nextProps.image_file
.entrySeq()
.map(([key, value]) => {
if(key==="file_path") {
this.setState(state => ({
image_name: value
}));
console.log(this.state.image_name)
this.uploadCallback();
}
})
.toArray();
}
}
render() {
return (
<div className="uploader">
<Dropzone
className="dropzone"
onDrop={this.onDrop}
style={style}
activeStyle={activeStyle}
multiple={false}
accept="image/*"
>
Drop files here or <br />
<span className="btn btn-link">Upload</span> {' '}
</Dropzone>
</div>
)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(FileUploader);
请指导我找到问题并修复。