react+ueditor(百度編輯器 )

http://uso.oschina.io/react-ueditor-demo/

https://github.com/uso51984/react-ueditor

react+ueditor(百度編輯器 )

更新日志 2018-10-14 (方案一)

由于百度的富文本編輯器ueditor沒(méi)有模塊化支持,所以在commonjs或者es6中無(wú)法正常的引用。 所以在react中需要單獨(dú)做處理,一種方案是修改源碼來(lái)支持, 這種方案缺點(diǎn)就是需要修改源碼地方比較多, 不利于更新ueditor的版本,具體說(shuō)明在以前的更新也就是方案二種有說(shuō)明。這里我們說(shuō)另外一種我覺(jué)得更好的方法, 動(dòng)態(tài)加載ueditorjs. 這里上一些關(guān)鍵代碼

  static defaultProps = {
    value: '',
    onChange: () => { },
    ueditorUrl: 'https://uso.oschina.io/react-ueditor-demo/ueditor.all.js',
    ueditorConfigUrl: 'https://uso.oschina.io/react-ueditor-demo/ueditor.config.js',
    ueditorHomeUrl: '',
    ueditorIframeUrl: '',
    editorConfig: {}, // ueditor 默認(rèn)值
    className: '',
    prefix: 'bee',
  }

 loadUEditorScript() {
    if (window[UEDITOR_LOADED_KEY] !== undefined) {
      return;
    }
    window[UEDITOR_LOADED_KEY] = 1; // 加載中
    let {
      ueditorHomeUrl,
      ueditorIframeUrl,
      ueditorUrl,
      ueditorConfigUrl,
    } = this.props;

    window.UEDITOR_HOME_URL = ueditorHomeUrl;
    window.UEDITOR_IFRAME_URL = ueditorIframeUrl;

    this.createScriptDom(ueditorConfigUrl, () => {
      this.createScriptDom(ueditorUrl, () => {
        window[UEDITOR_LOADED_KEY] = 2; // 加載完成
      });
    });
  }

  createScriptDom(url, callback) {
    const scriptDom = document.createElement('script');
    scriptDom.type = 'text/javascript';
    scriptDom.async = true;
    scriptDom.src = url;

    scriptDom.onload = function () {
      callback();
    }
    document.body.appendChild(scriptDom);
  }

更新日志 2018-3-24(方案二)

鑒于大家都在問(wèn)怎么樣能用import 導(dǎo)入。 2018年3月24號(hào) 23點(diǎn),抽時(shí)間處理了一下。 demo已更新到代碼庫(kù),目的拋磚引玉。下面說(shuō)一下解決思路;
由于ueditor源碼并非模塊化設(shè)計(jì),所以我們不能直接使用,只能修改源碼。 其實(shí)修改源碼也相對(duì)簡(jiǎn)單。 對(duì)外把 UE 變量export default UE
然后相關(guān)依賴(lài)的變量比如ueditor.config.js 里面的UEDITOR_CONFIG, I18N文件的對(duì)象配置等等依賴(lài)變量, 也從全局變量, 修改為模塊成為主文件的依賴(lài), 類(lèi)似在
ueditor.all.js加入以下代碼。 當(dāng)然我個(gè)人覺(jué)得最好的還是下載源文件修改編譯成自己想要的

import I18N from './lang/zh-cn/zh-cn';
import UEDITOR_CONFIG from './ueditor.config';

組件代碼

import React from 'react';
import UE from '../../ueditor/ueditor.all';

class Ueditor extends React.Component {
  static defaultProps = {
    config: {}
  }

  constructor(props){
    super(props);
    this.state = {
    };
  }

  componentDidMount(){
    this.initEditor()
  }

  componentWillUnmount() {
    // 組件卸載后,清除放入庫(kù)的id
    UE.delEditor(this.props.id);
  }

  initEditor() {
    const { id, config } = this.props;
    const ueEditor = UE.getEditor(this.props.id, config);
    const self = this;

    ueEditor.ready((ueditor) => {
      if (!ueditor) {
        UE.delEditor(id);
        self.initEditor();
      }
    })
  }

  render(){
    return (
      <div id={this.props.id} name="content" type="text/plain"></div>
    )
  }
}
export default Ueditor;

調(diào)用

import Ueditor from '../base/Ueditor.js'
<Ueditor  id="content" height="200" /> 

獲取輸入內(nèi)容

<button onClick={this.testSubmit}>保存</button>
testSubmit(){
    console.log(UE.getEditor('content').getContent())
}

調(diào)試中遇到問(wèn)題

  • 前端切換路由后,第二次進(jìn)入頁(yè)面不能渲染出編輯器

在組件卸載后清除編輯器庫(kù)中存在的id

  componentWillUnmount() {
    // 組件卸載后,清除放入庫(kù)的id
    UE.delEditor(this.props.id);
  }

參考文獻(xiàn): http://blog.csdn.net/wei_shining/article/details/52344263

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,828評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,097評(píng)論 4 61
  • ヾ(??3?)ノ床前明月光,低頭思念家,舉頭望明月,低頭思故鄉(xiāng),每縫佳節(jié)倍思親。 每次想到家二眼淚汪汪,相聚是美好...
    天使龍寶閱讀 429評(píng)論 0 1
  • 說(shuō)起長(zhǎng)生不老,沒(méi)有誰(shuí)比古代帝王更想長(zhǎng)生不老。名利權(quán)利雙收的帝王,享盡人間榮華富貴,到了老年將至的時(shí)候,無(wú)不渴望能長(zhǎng)...
    0fcae5e7e00f閱讀 2,868評(píng)論 0 0
  • 從醫(yī)是服務(wù)行業(yè),的確不假。他們服務(wù)的對(duì)象是一類(lèi)特殊群體,是需要幫助的群體?!耙晃换颊邅?lái)了,他需要幫助,這個(gè)時(shí)候出現(xiàn)...
    生活那些事er總是有你閱讀 280評(píng)論 0 0

友情鏈接更多精彩內(nèi)容