import React,{Component} from 'react';
import ReactQuill,{ Quill } from 'react-quill';
import 'react-quill/dist/quill.snow.css';
import { Button ,Modal,message} from 'antd';
import MYURL from '../api/config';
import { ImageDrop } from 'quill-image-drop-module';
Quill.register('modules/imageDrop', ImageDrop);
class Editer extends Component {
constructor(props) {
super(props)
this.state = { text: '' } // You can also pass a Quill Delta here
this.handleChange = this.handleChange.bind(this)
this.selectImage = this.selectImage.bind(this);
this.changeImageBeforeUpload = this.changeImageBeforeUpload.bind(this);
this.uploadForImage = this.uploadForImage.bind(this);
this.imageHandler = this.imageHandler.bind(this);
this.showUploadBox = this.showUploadBox.bind(this);
this.hideUploadBox =this.hideUploadBox.bind(this);
this.handleUpload =this.handleUpload.bind(this);
}
handleChange(value) {
// if (value) ReactQuill.getSelection().dangerouslyPasteHTML(value);
this.setState({ text: value })
};
modules={//富文本配置
toolbar:{
container:[
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
['bold', 'italic', 'underline', 'strike','blockquote'], // toggled buttons
['blockquote', 'code-block'],
// [{ 'header': 1 }, { 'header': 2 }], // custom button values
[{ 'script': 'sub'}, { 'script': 'super' }], // superscript/subscript
[{ 'indent': '-1'}, { 'indent': '+1' }], // outdent/indent
[{ 'direction': 'rtl' }], // text direction
[{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
[{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'},{ 'align': [] }],
[{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
[{ 'font': [] }],
[{ 'align': [] }],
['link', 'image', 'video'],
['clean'],
],
handlers: {
'image':this.showUploadBox.bind(this)
}
},
imageDrop: true,
};
showUploadBox(){
this.setState({
uploadBoxVisible:true
});
};
hideUploadBox(){
this.setState({
uploadBoxVisible:false
});
}; selectImage(){
this.refs.uploadInput.click();//点击modal的html结构中的input标签
}; changeImageBeforeUpload(e){
const file = e.target.files[0];
if (!file) {
return;
}
let src;
// 匹配类型为image/开头的字符串
if (file.type==="image/png"||file.type==="image/jpeg") {
src = URL.createObjectURL(file);
}else{
message.error("图片上传只支持JPG/PNG格式,请重新上传!");
return;
}
if (file.size/1024/1024>5) {
message.error("图片上传大小不要超过5MB,请重新上传!");
return;
}
this.setState({
src:src,
file:file
})
console.log('eeeeeee',window)
}
/*3.开始上传图片*/
handleUpload(){
let this_=this;
/*调用上传图片的封装方法*/
if(!this.state.file){
alert('请选择图片!!')
}else{
let fileServerAddr = MYURL.fileServer //服务器地址
let file =this.state.file.name
let size =this.state.file.size
this.uploadForImage(fileServerAddr,file,size,function (response) {//回调函数处理进度和后端返回值
console.log('res----?>',response)
if ((response && response.status === 200) ||(response && response.status === "200")) {
message.success("上传成功!");
this_.hideUploadBox();//隐藏弹框
console.log("response.data.url???=>",response.data.url)
this_.imageHandler(response.data.url);//处理插入图片到编辑器
}else if (response && response.status !== 200) {
message.error(response.msg)
}
},
localStorage.getItem("access_token"));
} };
uploadForImage(url,data,size,callback,token) {//data是数据列表
if (!data) {
alert('请选择图片!!')
console.log('未选择文件');
return;
}else{
let xhr = new XMLHttpRequest();
let formdata = new FormData();
formdata.append('file', data);
formdata.append('fileSize', size);
xhr.onload=() =>{
if(xhr.status === 200 ||xhr.statusn ==='200' ){
let response =JSON.parse(xhr.response)
console.log('res====',response)
callback(response);
}
};
// xhr.open('POST', url, true); // 第三个参数为async?,异步/同步
xhr.open('GET', url, true); // 第三个参数为async?,异步/同步
xhr.setRequestHeader("accessToken",token);
xhr.send(formdata);
} }
/*4.处理图片插入*/
imageHandler(url){
if (typeof this.reactQuillRef.getEditor !== 'function') return;
const quill = this.reactQuillRef.getEditor()
var range = quill.getSelection();
let index = range ? range.index : 0;
quill.insertEmbed(index, "image",url, Quill.sources.USER);//插入图片
quill.setSelection(index + 1);//光标位置加1
console.log("quill.getSelection.======",quill.getSelection().index)
};
render() {
return (
<div style={{maxHeight:"500px"}}>
<ReactQuill id="ddd" ref={(el) =>{this.reactQuillRef = el}} value={this.state.text} onChange={this.handleChange}
theme={"snow"} modules={this.modules} style={{height:"300px"}} />
<Modal
title="上传图片"
visible={this.state.uploadBoxVisible}
onCancel={this.hideUploadBox}
onOk={ this.handleUpload }
maskClosable={false}
width={500}
>
<div className="ImagaBox" >
<div>
<Button onClick={this.selectImage.bind(this)} style={{background:"#18ade4",border:"none",color:"#fff"}}>
选择图片
</Button>
<input ref="uploadInput" type='file' accept='image/*'
style={{width:"100px",border:"none",visibility:"hidden"}}
onChange={this.changeImageBeforeUpload.bind(this)}
/>
</div>
<div style={{textAlign:"center",margin:"10px 0"}}>
{this.state.src?
<img src={this.state.src} alt="" style={{maxWidth:"100%",height:"300px"}}/>
:
<div style={{background:"#f2f2f2",width:"100%",height:"300px"}}></div>
}
</div>
</div>
</Modal>
</div>
)
}
};
export default Editer;

react-quill 富文本编辑器 ---- 图片处理的更多相关文章

  1. Quill 富文本编辑器

    Quill 富文本编辑器 https://quilljs.com/ https://github.com/quilljs/quill https://github.com/quilljs/awesom ...

  2. 【重点突破】—— React实现富文本编辑器

    前言:富文本编辑器Rich Text Editor, 简称 RTE, 是一种可内嵌于浏览器,所见即所得的文本编辑器.   一.安装插件 react-draft-wysiwyg: 文本编辑器插件 dra ...

  3. 【React】富文本编辑器 清空文本内容 获取HTML

    富文本编辑器  React  传入 import React,{Component } from 'react'; import { Card, Button, Table, Form, Select ...

  4. Vue整合Quill富文本编辑器

    Quill介绍 Quill是一款开源的富文本编辑器,基于可扩展的架构设计,提供丰富的 API 进行定制.截止2021年1月,在github上面已有28.8k的star. Quill项目地址:https ...

  5. 轻量级quill富文本编辑器

    因为公司产品需要在移动端编辑文本,所以发现了这个轻量级的好东西,网上也没找到比较好的案例,就自己总结了下,有兴趣的直接复制代码运行看看就知道啦! 下面是quill.js的CDN加速地址: <!- ...

  6. quill富文本编辑器 API

    //1. 从第三个开始删除,删除4个 // console.log(this.quill.deleteText(2, 4)); // 12345678 1278 // 2.(返回对象)返回从第三个开始 ...

  7. Kindeditor JS 富文本编辑器图片上传指定路径

    js //================== KindEditor.ready(function (K) { var hotelid = $("#hotelid").val(); ...

  8. Vue2 封装的 Quill 富文本编辑器组件 Vue-Quill-Editor

    1.安装 npm install vue-quill-editor --save 2.使用 import { quillEditor } from 'vue-quill-editor' 3.组件中 & ...

  9. angular4 富文本编辑器

    使用quill富文本编辑器实现,angular项目中用到了ngx-quill插件. quill的GitHub地址:https://github.com/quilljs/quill ngx-quill的 ...

随机推荐

  1. 网络流板子/费用流板子 2018南京I题+2016青岛G题

    2018南京I题: dinic,链式前向星,数组队列,当前弧优化,不memset全部数组,抛弃满流点,bfs只找一条增广路,每次多路增广 #include <bits/stdc++.h> ...

  2. Maven将代码及依赖打成一个Jar包的方式

    Maven可以使用mvn package指令对项目进行打包,如果使用java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in ...

  3. linux 安装虚拟机

    如果虚拟机创建不了就重启电脑 重启时 按下F2 出现后 第二个 往下 有个默认的 那个那个 打开虚拟机 选择第一个 然后是选择语言选择软件里面的 软件选择选择 基本网页服务器(右侧选择 python ...

  4. tensorflow--mnist注解

    我自己对mnist官方例程进行了部分注解,希望分享出来有助于入门选手更好理解tensorflow的运行机制,可以拷贝到IDE再调试看看,看看具体数据流向还有一部分tensorflow里面用到的库.我用 ...

  5. 以慕课网日志分析为例-进入大数据Spark SQL的世界

    下载地址.请联系群主 第1章 初探大数据 本章将介绍为什么要学习大数据.如何学好大数据.如何快速转型大数据岗位.本项目实战课程的内容安排.本项目实战课程的前置内容介绍.开发环境介绍.同时为大家介绍项目 ...

  6. Microsoft Graph API -----起题 Graph API

    最近因为工作需要,接触学习使用了Microsoft Graph API.在看完Microsoft的Graph官方文档之后,也做了一些简单的案例,在Stack Overflow上做过一些回答.整体来说, ...

  7. OpenCV中的KNN

    一.K近邻 有两个类,红色.蓝色.我将红色点标记为0,蓝色点标记为1.还要创建25个训练数据,把它们分别标记为0或者1.Numpy中随机数产生器可以帮助我们完成这个任务 import cv2 impo ...

  8. nginx 10054报错问题解决方案

    使用nginx代理,端口8000.tomcat用于后端服务器,端口8080.nginx的error.log中报如下错误: 2018/09/21 09:08:06 [error] 12488#11600 ...

  9. CentOS系统下搭建tomcat服务器

    下载相应的linux版jdk和tomcat,本文讲解jdk版本jdk-7u79-linux-x64.tar.gz,tomcat版本apache-tomcat-7.0.69.tar.gz [配置jdk] ...

  10. VS2017打包注册IE插件及修改IE安全选项设置

    前言 最近项目需要在浏览器环境下读取员工身份证信息,要实现网页与硬件设备通信,考虑了几种实现方式: 1.借助ActiveX插件,通过程序库直接与设备通信. 优点:厂家提供了IE插件,开发简单 缺点:只 ...