提交 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,85 +13,100 @@ ...@@ -13,85 +13,100 @@
* 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( {
height,
zIndex,
placeholder,
focus,
value,
key,
onChangeValue,
} ){
useEffect(()=>{
const editor = new WangEditor(`#wangEditor${key}`);
if(height){
editor.config.height = height; // 编辑器高度
}
if(zIndex){
editor.config.zIndex = zIndex; // 编辑器 z-index
}
if(placeholder){
editor.config.placeholder = placeholder; // placeholder
}
if(focus){
editor.config.focus = focus;
}
editor.config.onChange = function (newHtml) {
onChangeValue(newHtml);
};
editor.config.customUploadImg = function(resultFiles, insertImgFn) { // 重写上传图片的方法 let editor = null;
uploadFile({ file: resultFiles[0] }).then((y) => { export default function Index({
if(y && y.url){ height,
insertImgFn(y.url); zIndex,
} placeholder,
}); focus,
// insertImgFn(imgUrl); value,
}; domKey,
onChangeValue,
}) {
useEffect(
() => {
editor = new WangEditor(`#wangEditor${domKey}`);
if (height) {
editor.config.height = height; // 编辑器高度
}
if (zIndex) {
editor.config.zIndex = zIndex; // 编辑器 z-index
}
if (placeholder) {
editor.config.placeholder = placeholder; // placeholder
}
if (focus) {
editor.config.focus = focus;
}
editor.config.onchange = function(newHtml) {
onChangeValue(newHtml);
};
editor.config.pasteFilterStyle = false; // 关闭粘贴样式的过滤 editor.config.onchangeTimeout = 500; // html内容改变时的 timeOut 配置
editor.config.uploadImgMaxSize = 3 * 1024 * 1024; // 上传的图片不能大于3M
editor.config.onchangeTimeout = 500; // html内容改变时的 timeOut 配置
editor.config.uploadImgMaxLength = 1; // 一次最多上传 1 个图片
editor.config.menus = [ // 配置菜单
'head',
'bold',
'fontSize',
'fontName',
'italic',
'underline',
'strikeThrough',
'indent',
'lineHeight',
'foreColor',
'backColor',
'link',
'list',
'justify',
'quote',
// 'emoticon',
'image',
'video',
'table',
// 'code',
'splitLine',
// 'undo',
// 'redo',
];
editor.create(); editor.config.customUploadImg = function(resultFiles, insertImgFn) { // 重写上传图片的方法
uploadFile({ file: resultFiles[0] }).then((y) => {
if (y && y.url) {
insertImgFn(y.url);
}
});
// insertImgFn(imgUrl);
};
if(value){ editor.config.pasteFilterStyle = false; // 关闭粘贴样式的过滤
editor.txt.html('<div class="wangEditorHtml">' + value + '</div>'); editor.config.uploadImgMaxSize = 3 * 1024 * 1024; // 上传的图片不能大于3M
} editor.config.uploadImgMaxLength = 1; // 一次最多上传 1 个图片
},[]); editor.config.menus = [ // 配置菜单
'head',
'bold',
'fontSize',
'fontName',
'italic',
'underline',
'strikeThrough',
'indent',
'lineHeight',
'foreColor',
'backColor',
'link',
'list',
'justify',
'quote',
// 'emoticon',
'image',
'video',
'table',
// 'code',
'splitLine',
// 'undo',
// 'redo',
];
editor.create();
console.log(value);
if(value && value.indexOf('wangEditorHtml') > -1) {
editor.txt.html(value);
}else{
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论