react-富文本 react-editor react-contenteditable

自己写的一个简单的编辑器  尚在简洁 具体功能 加粗 斜体 下划线 链接地址 颜色字体大小 粘贴图片 excel  截屏 拖拽 上传 功能都有 默认值 是否显示工具栏 是否可编辑  测试通过能实现软件开发大部分需求 可自己继续添加其他需求 

样子:

部分代码:

/**
* Created by zhanglei on 2017/5/23.
*/
import React, { Component } from 'react';
import { Modal,message } from 'antd';
import PropTypes from 'prop-types';
import './edit.less'
import ToolBar from './toolBar'
export default class Editor extends Component {
static propTypes = {
className: PropTypes.string,
value:PropTypes.string,
editColor:PropTypes.string,
};
constructor(props){
super(props);
this.state={
value:null,
tableData:[],
linkModel:false,
visible:false,
isColor:false,
editValue:'',
myDisabled:false,
isEdit:true,
isFace:false,
isBackground:false,
linkValue:null,
isSent:true,
}
};
componentDidMount(){
document.addEventListener('click',this.documentClick);
};
moveToEnd = () =>{
const range = window.getSelection();
range.selectAllChildren(this.refs['inputText']);
range.collapseToEnd();
};
documentClick=(e)=>{
const { isColor, isBackground } = this.state;
if(isColor||isBackground){
let en = e.srcElement||e.target;
const name = '.color-content';
while(en){
if(en.className&&en.className === name.replace('.','')){
return;
}
en = en.parentNode;
}
this.setState({ isColor:false, isBackground:false });
}
};
componentWillUnmount(){
document.removeEventListener('click',this.documentClick)
}
onPaste=(e)=>{
let cbd = e.clipboardData;
if ( !(e.clipboardData && e.clipboardData.items) ) {
return ;
}
this.onParseOrDrop(cbd,e);
};
onDrop=(e)=>{
e.preventDefault();
let cbd = e.dataTransfer;
if ( !(e.dataTransfer && e.dataTransfer.items) ) {
return ;
}
this.onParseOrDrop(cbd,e);
};
onParseOrDrop = (cbd,e) =>{
let ua = window.navigator.userAgent;
if(cbd.items && cbd.items.length === 2 && cbd.items[0].kind === "string" && cbd.items[1].kind === "file" &&
cbd.types && cbd.types.length === 2 && cbd.types[0] === "text/plain" && cbd.types[1] === "Files" &&
ua.match(/Macintosh/i) && Number(ua.match(/Chrome\/(\d{2})/i)[1]) < 49){
return;
}
if(cbd.items&&cbd.items.length === 4){
document.createElement('table');
return false;
}
const myArray = Object.keys(cbd.items).map(item=>{
return cbd.items[item];
});
const arr = myArray.some((u)=>{
if(u.kind === 'file'&&u.type!=='image/jpeg'&&u.type!=='image/png'){
message.error('仅支持PNG或者JPG的图片格式上传');
return !1
}
return u.kind === 'file'&&(u.type==='image/jpeg'||u.type==='image/png')
});
if(!arr) return !1;
myArray.forEach(item=>{
if(item.kind === 'string'){
e.preventDefault();
return;
}
if(item.kind === "file"){
e.preventDefault();
const blob = item.getAsFile();
if (blob.size === 0) return;
this.bind_parse(blob);
}
})
};
bind_parse = (file) =>{
const reader = new FileReader();
reader.onload = ((e)=>{
const newImg = `<img class="edit-img" title='点击放大' src="${ e.target.result}"/>`;
const {onChange} = this.props;
if(onChange){
onChange( this.inputText.innerHTML+newImg );
}
});
reader.readAsDataURL(file);
};
textHtml=(e)=>{
e.preventDefault();
const {isEdit} = this.state;
let html =null;
if(isEdit){
html = this.refs['inputText'].innerHTML;
this.refs['inputText2'].value = html;
this.setState({ isEdit:false, myDisabled:true });
}else{
html = this.refs['inputText2'].value;
this.refs['inputText'].innerHTML = html;
this.setState({ isEdit:true, myDisabled:false });
}

};
emitChange = (e) => {
const { onChange } = this.props;
const html = e.target.innerHTML;
if (onChange && html !== this.lastHtml) {
onChange(html)
}
this.lastHtml = html;
};
shouldComponentUpdate(nextProps,nextState){
return nextProps.value !== this.props.value||
this.state.editValue !== nextState.editValue||
this.state.visible !== nextState.visible||
this.state.myDisabled !== nextState.myDisabled;
}
render() {
const { isEdit, visible, myDisabled } = this.state;
const { disabled, editColor } = this.props;
const isDisabled = !('disabled' in this.props) || !disabled;
return (
<div className="editor-full">
{
isDisabled &&
<ToolBar
id={ this.props.id }
myDisabled={ myDisabled }
bind_parse={(this.bind_parse)}
textHtml={(this.textHtml)}
/>
}
<div className="inputText" style={{ borderColor:editColor }}>
<div
className="input-content-edit"
onPaste={ this.onPaste }
onDrop={ this.onDrop }
onInput={this.emitChange}
contentEditable={ isDisabled }
onBlur={ this.emitChange }
dangerouslySetInnerHTML={{ __html:this.props.value }}
onClick={e =>{
if(e.target.title === '点击放大'){
this.setState({ url:e.target.src, visible:true })
}
}}
ref={ref =>this.inputText = ref}
style={{
display:isEdit ? 'block' : 'none',
height:isDisabled ? '200px' : 'auto'
}}>
</div>
<textarea style={{ display:isEdit ? 'none' : 'block' }} ref="inputText2" id="editor-con"/>
</div>
<Modal
className="chat-model"
style={{ width:'70%' }}
visible={ visible }
footer={ null }
onCancel={() =>this.setState({ visible:false })}
>
<img src={this.state.url} alt="img"/>
</Modal>
</div>

);
}
}
 
 

 

转载于:https://www.cnblogs.com/leijuan/p/7633123.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值