提交 f400ef7d authored 作者: 钟是志's avatar 钟是志

wangEditor 修改

上级 84f11f90
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, forwardRef } from 'react';
import WangEditor from './wangEditorComponent'; import WangEditor from './wangEditorComponent';
export default function Index({ onChange, value, height }) { function Index({ onChange, value, height, domKey }) {
return (<WangEditor key={'cmsContent'} return (<WangEditor key={'cmsContent'}
height={height} height={height}
value={value} value={value}
domKey={domKey}
onChangeValue={onChange} onChangeValue={onChange}
/>); />);
} }
export default forwardRef((props, _ref) => {
console.log(props);
return <Index {...props}/>
})
...@@ -13,39 +13,44 @@ ...@@ -13,39 +13,44 @@
* value: string // 富文本的值 * value: string // 富文本的值
* 更多配置项 参考 http://www.wangeditor.com/doc/ * 更多配置项 参考 http://www.wangeditor.com/doc/
* */ * */
import React, {useState, useEffect} from 'react'; import React, { useState, useEffect } from 'react';
import WangEditor from './includes/wangEditor.min'; import WangEditor from './includes/wangEditor.min';
import uploadFile from './uploadFile'; import uploadFile from './uploadFile';
export default function Index( {
let editor = null;
export default function Index({
height, height,
zIndex, zIndex,
placeholder, placeholder,
focus, focus,
value, value,
key, domKey,
onChangeValue, onChangeValue,
} ){ }) {
useEffect(()=>{ useEffect(
const editor = new WangEditor(`#wangEditor${key}`); () => {
if(height){ editor = new WangEditor(`#wangEditor${domKey}`);
if (height) {
editor.config.height = height; // 编辑器高度 editor.config.height = height; // 编辑器高度
} }
if(zIndex){ if (zIndex) {
editor.config.zIndex = zIndex; // 编辑器 z-index editor.config.zIndex = zIndex; // 编辑器 z-index
} }
if(placeholder){ if (placeholder) {
editor.config.placeholder = placeholder; // placeholder editor.config.placeholder = placeholder; // placeholder
} }
if(focus){ if (focus) {
editor.config.focus = focus; editor.config.focus = focus;
} }
editor.config.onChange = function (newHtml) { editor.config.onchange = function(newHtml) {
onChangeValue(newHtml); onChangeValue(newHtml);
}; };
editor.config.onchangeTimeout = 500; // html内容改变时的 timeOut 配置
editor.config.customUploadImg = function(resultFiles, insertImgFn) { // 重写上传图片的方法 editor.config.customUploadImg = function(resultFiles, insertImgFn) { // 重写上传图片的方法
uploadFile({ file: resultFiles[0] }).then((y) => { uploadFile({ file: resultFiles[0] }).then((y) => {
if(y && y.url){ if (y && y.url) {
insertImgFn(y.url); insertImgFn(y.url);
} }
}); });
...@@ -54,7 +59,6 @@ export default function Index( { ...@@ -54,7 +59,6 @@ export default function Index( {
editor.config.pasteFilterStyle = false; // 关闭粘贴样式的过滤 editor.config.pasteFilterStyle = false; // 关闭粘贴样式的过滤
editor.config.uploadImgMaxSize = 3 * 1024 * 1024; // 上传的图片不能大于3M editor.config.uploadImgMaxSize = 3 * 1024 * 1024; // 上传的图片不能大于3M
editor.config.onchangeTimeout = 500; // html内容改变时的 timeOut 配置
editor.config.uploadImgMaxLength = 1; // 一次最多上传 1 个图片 editor.config.uploadImgMaxLength = 1; // 一次最多上传 1 个图片
editor.config.menus = [ // 配置菜单 editor.config.menus = [ // 配置菜单
'head', 'head',
...@@ -83,15 +87,26 @@ export default function Index( { ...@@ -83,15 +87,26 @@ export default function Index( {
]; ];
editor.create(); editor.create();
console.log(value);
if(value){ if(value && value.indexOf('wangEditorHtml') > -1) {
editor.txt.html(value);
}else{
editor.txt.html('<div class="wangEditorHtml">' + value + '</div>'); editor.txt.html('<div class="wangEditorHtml">' + value + '</div>');
} }
},[]);
return () => {
console.log('是否销毁了editor');
editor.destroy();
};
}, []);
// useEffect(() => {
// if (value && editor && editor.txt) {
// editor.txt.html('<div class="wangEditorHtml">' + value + '</div>');
// }
// }, [value]);
return ( return (
<div id={`wangEditor${key}`}> <div id={`wangEditor${domKey}`}>
</div> </div>
) );
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论