• 业务型 编辑器组件的封装(复制即可使用)


    使用需要安装  wangeditor  npm i --save wangeditor

    1. import React from 'react';
    2. import E from 'wangeditor';
    3. import './index.less'
    4. class EditorElem extends React.Component {
    5. constructor(props) {
    6. super(props);
    7. this.isChange = false;
    8. this.state = {
    9. }
    10. }
    11. componentDidMount() {
    12. const elemBody = this.refs.editorElemBody;
    13. this.editor = new E(elemBody)
    14. this.initEditor()
    15. }
    16. componentWillReceiveProps(nextProps) {
    17. if (nextProps.value != this.props.value) {
    18. if (!this.isChange) {
    19. this.editor.txt.html(nextProps.value)
    20. }
    21. this.isChange = false;
    22. }
    23. }
    24. initEditor() {
    25. // 使用 onchange 函数监听内容的变化,并实时更新到 state 中
    26. this.editor.config.onchange = html => {
    27. this.isChange = true;
    28. // console.log(editor.txt.html())
    29. let editorContent = this.editor.txt.html();
    30. this.props.onChange(editorContent)
    31. // 不给延时,会导致详情调整过的内容修改后组件数据更新不了
    32. setTimeout(() => {
    33. this.isChange = false
    34. }, 50);
    35. }
    36. this.editor.config.menus = [
    37. 'head', // 标题
    38. 'bold', // 粗体
    39. 'fontSize', // 字号
    40. 'fontName', // 字体
    41. 'italic', // 斜体
    42. 'underline', // 下划线
    43. 'strikeThrough', // 删除线
    44. 'indent',
    45. 'lineHeight',
    46. 'foreColor', // 文字颜色
    47. 'backColor', // 背景颜色
    48. 'link', // 插入链接
    49. 'list', // 列表
    50. 'todo',
    51. 'justify', // 对齐方式
    52. 'quote', // 引用
    53. 'emoticon', // 表情
    54. 'image', // 插入图片
    55. 'table', // 表格
    56. 'video', // 插入视频
    57. 'code', // 插入代码
    58. 'splitLine',
    59. 'undo', // 撤销
    60. 'redo' // 重复
    61. ]
    62. this.editor.config.colors = ['#999', '#666', '#000000',
    63. '#eeece0',
    64. '#1c487f',
    65. '#4d80bf',
    66. '#c24f4a',
    67. '#8baa4a',
    68. '#7b5ba1',
    69. '#46acc8',
    70. '#f9963b',
    71. '#ffffff'];
    72. // editor.config.uploadImgShowBase64 = true;
    73. this.editor.config.pasteIgnoreImg = true;
    74. this.editor.config.uploadImgServer = `${configs.host.test}/api/FileUpload/Upload`; // 上传图片到服务器
    75. this.editor.config.uploadFileName = 'fileName'
    76. this.editor.config.uploadImgParams = {
    77. merchantId: localStorage.getItem('MerchantId'),
    78. Directory: 'Image'
    79. }
    80. // 限制一次最多上传 1 张图片
    81. this.editor.config.uploadImgMaxLength = 1;
    82. // 将 timeout 时间改为 3s
    83. // this.editor.config.uploadImgTimeout = 3000;
    84. this.props.zIndex && (this.editor.config.zIndex = this.props.zIndex);
    85. this.editor.config.uploadImgHeaders = {
    86. Accept: 'multipart/form-data',
    87. // Authorization: `Bearer ${localStorage.getItem('access_token')}`,
    88. // MerchantId: localStorage.getItem('MerchantId')
    89. }
    90. this.editor.config.uploadImgHooks = {
    91. before: function (xhr, editor, files) {
    92. // 图片上传之前触发
    93. },
    94. success: function (xhr, editor, result) {
    95. // 图片上传并返回结果,图片插入成功之后触发
    96. },
    97. fail: function (xhr, editor, result) {
    98. // 图片上传并返回结果,但图片插入错误时触发
    99. },
    100. error: function (xhr, editor) {
    101. // 图片上传出错时触发
    102. },
    103. // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
    104. // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
    105. customInsert: function (insertImg, result, editor) {
    106. // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
    107. // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
    108. // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
    109. var url = result.data;
    110. insertImg(url);
    111. // result 必须是一个 JSON 格式字符串!!!否则报错
    112. }
    113. }
    114. this.editor.create()
    115. this.props.value && this.editor.txt.html(this.props.value);
    116. // 开启编辑功能
    117. if (this.props.disabled) {
    118. this.editor.disable()
    119. }
    120. // this.editor.$textElem.attr('contenteditable', this.props.disabled ? false : true)
    121. }
    122. render() {
    123. return (
    124. <div className="text-area" >
    125. <div
    126. style={{
    127. // height: 335,
    128. }}
    129. ref="editorElemBody" className="editorElem-body">
    130. div>
    131. div>
    132. )
    133. }
    134. }
    135. export default EditorElem;
    1. .editorElem-body{
    2. /* table 样式 */
    3. table {
    4. border-top: 1px solid #ccc;
    5. border-left: 1px solid #ccc;
    6. }
    7. table td,
    8. table th {
    9. border-bottom: 1px solid #ccc;
    10. border-right: 1px solid #ccc;
    11. padding: 3px 5px;
    12. }
    13. table th {
    14. border-bottom: 2px solid #ccc;
    15. text-align: center;
    16. }
    17. /* blockquote 样式 */
    18. blockquote {
    19. display: block;
    20. border-left: 8px solid #d0e5f2;
    21. padding: 5px 10px;
    22. margin: 10px 0;
    23. line-height: 1.4;
    24. font-size: 100%;
    25. background-color: #f1f1f1;
    26. }
    27. /* code 样式 */
    28. code {
    29. display: inline-block;
    30. *display: inline;
    31. *zoom: 1;
    32. background-color: #f1f1f1;
    33. border-radius: 3px;
    34. padding: 3px 5px;
    35. margin: 0 3px;
    36. }
    37. pre code {
    38. display: block;
    39. }
    40. /* ul ol 样式 */
    41. ul, ol {
    42. margin: 10px 0 10px 20px;
    43. }
    44. }
    1. {/* 编辑器组件 ---开始 */}
    2. .formItemLayout2} label="编辑器组件">
    3. {getFieldDecorator("editorValue", {
    4. rules: [{ required: true, message: "请填写内容" }],
    5. initialValue: detailData.editorValue,
    6. })()}
    7. {/* 编辑器组件 ---结束 */}

    使用便捷,无需关注内部实现和定义一堆函数,只需要传入value值 即可回显数据

    可以触发form的表单验证,无需额外在提交的时候验证是否有值进行message提示

  • 相关阅读:
    【解刊】IEEE(Trans)系列,CCF-A类顶刊,国人友好,无需版面费!
    【RTOS训练营】I2C和UART知识和预习安排 + 晚课提问
    解析treeSet集合进行自定义类的排序
    【RabbitMQ】SpringBoot整合RabbitMQ实现延时队列
    C++ 班级通讯录管理系统
    通俗讲解傅里叶变换
    论如何在使用RedisStandaloneConfiguration时让JedisConnectionFactory用上JedisPoolConfig
    小程序中实现用户的登录与注册
    基于LVM和NFS的cinder多后端
    Linux--CentOS6和CentOS7的区别
  • 原文地址:https://blog.csdn.net/qq_36947128/article/details/136173836