知与行后台管理端

Wangedit.jsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React from 'react';
  2. import E from 'wangeditor';
  3. import { fetch, apis } from '../../utils/request';
  4. /**
  5. * @param {*} props
  6. * @returns
  7. */
  8. class Wangedit extends React.Component {
  9. constructor(props, context) {
  10. super(props, context);
  11. this.state = {
  12. html: undefined,
  13. contenteditable: props.contenteditable == false ? false : true
  14. }
  15. this.editor = undefined;
  16. }
  17. render() {
  18. return (
  19. <div ref="editorElem" style={{ textAlign: 'left' }}>
  20. </div>
  21. );
  22. }
  23. componentDidMount() {
  24. const elem = this.refs.editorElem
  25. this.editor = new E(elem)
  26. // 使用 onchange 函数监听内容的变化
  27. this.editor.customConfig.onchange = html => {
  28. this.setState({ html })
  29. if (typeof this.props.onChange === 'function') {
  30. this.props.onChange(html)
  31. }
  32. }
  33. this.editor.customConfig.zIndex = 100
  34. this.editor.customConfig.uploadImgMaxLength = 1
  35. this.editor.customConfig.customUploadImg = function (files, insert) {
  36. if (!files.length) return
  37. const data = new FormData()
  38. data.append('file', files[0])
  39. fetch(apis.image.upload)({data}).then(insert)
  40. }
  41. this.editor.customConfig.menus = [
  42. 'head', // 标题
  43. 'bold', // 粗体
  44. 'fontSize', // 字号
  45. 'fontName', // 字体
  46. 'italic', // 斜体
  47. 'underline', // 下划线
  48. 'strikeThrough', // 删除线
  49. 'foreColor', // 文字颜色
  50. 'backColor', // 背景颜色
  51. 'list', // 列表
  52. 'justify', // 对齐方式
  53. 'quote', // 引用
  54. 'image', // 插入图片
  55. 'undo', // 撤销
  56. 'redo' // 重复
  57. ]
  58. // 过滤 word 字符
  59. this.editor.customConfig.pasteFilterStyle = false
  60. this.editor.customConfig.pasteTextHandle = function(content) {
  61. const regs = [
  62. /<!--\[if [\s\S]*?endif\]-->/ig,
  63. /<[a-zA-Z0-9]+\:[^>]+>[^>]*<\/[a-zA-Z0-9]+\:[^>]+>/ig,
  64. /<[a-zA-Z0-9]+\:[^>]+\/>/ig,
  65. /<style>[\s\S]*?<\/style>/ig,
  66. ]
  67. return regs.reduce((acc, reg) => {
  68. return acc.replace(reg, '')
  69. }, content)
  70. }
  71. this.editor.create()
  72. this.editor.$textElem.attr('contenteditable',this.state.contenteditable);
  73. this.editor.customConfig.uploadImgShowBase64 = true
  74. this.editor.txt.html(this.props.value)
  75. }
  76. componentDidUpdate(props, state) {
  77. if (this.props.value && !state.html) {
  78. if (this.editor) {
  79. this.editor.txt.html(this.props.value)
  80. }
  81. }
  82. }
  83. /**
  84. *增加这个 shouldComponentUpdate 生命函数
  85. 处理自动聚焦到富文本上
  86. *
  87. * @param {*} nextProps
  88. * @returns
  89. * @memberof Wangedit
  90. */
  91. shouldComponentUpdate(nextProps) {
  92. return nextProps.value !== this.editor.txt.html()
  93. }
  94. }
  95. export default Wangedit