xheditor 封装成React组件

本文介绍了一个基于React的富文本编辑器组件实现,重点展示了如何集成图片上传功能,并通过自定义插件来扩展编辑器的功能。代码示例中包含了使用XMLHttpRequest进行文件上传的具体实现。

人狠话不多,直接上代码:

xheditor.js

import React, { Component } from 'react';


export default class Xheditor extends Component {

  constructor(props) {
    super(props);
  }
  componentDidMount(){

      const _this=this;
      var allPlugins={
          uploadimg:{
             c:'xheIcon xheBtnImg',
             t:'插入图片',
             i:function(){
                  console.log('load over');
                  $('#imgstore').change(function(e){

                      var value=e.target.value;
                      console.log(value);

                      const xhr = new XMLHttpRequest();
                      xhr.open('POST', window.Retail+'index.upload/index?alias='+window.alias);
                      const data = new FormData();
                      data.append('file', this.files[0]);
                      xhr.send(data);
                      xhr.addEventListener('load', () => {
                        // console.log(xhr.responseText)
                        const response = JSON.parse(xhr.responseText);
                        if(response && response.code === 0){
                          if(response.data && response.data.url){
                             console.log(response.data.url);

                             var html="<img src='"+response.data.url+"'/>";

                             _this.xh.pasteHTML(html)
                          }
                        }
                      });
                      xhr.addEventListener('error', () => {
                        // console.log(xhr.responseText)
                        if(xhr.responseText){
                          const error = JSON.parse(xhr.responseText);
                          console.log(error);
                        }
                      });
                  })

             },
             e:function(){

                $('#imgstore').click();

             }
          }
      };
      this.xh=$('#elm4').xheditor({
        tools:'Cut,Copy,Paste,Pastetext,|,Blocktag,FontSize,Bold,Italic,SelectAll,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,uploadimg,Hr,Emot,Code,map,|,Source,Print,Fullscreen',
        upImgUrl: window.Retail+'index.upload/index?alias='+window.alias,
        upImgExt:"jpg,jpeg,gif,png",
        plugins:allPlugins

      });

      this.xh.pasteHTML(this.props.content+" ");
  }
  getContent(e){
       
      var content=this.xh.getSource();
      return content
  }
  render() {
    
    return (
  	   	<div>
  	         <textarea runat="server" name="elm4" id="elm4" ref='xh' name="elm4" rows="12" cols="80" style= {{width: "100%",height:'800px'}}/>
             <input id="imgstore" type="file" style={{display:'none'}} />
  	    </div>
    );
  }
}

父组件配置:

组件引用:
<Xheditor ref='xheditor' content={this.state.xhcontent}/>

父组件调用子组件方法获得内容:
values['content'] = this.refs.xheditor.getContent();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值