index.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import WangEditor from 'wangeditor'
  2. import { onMounted, onBeforeUnmount, ref, watchEffect, watch, markRaw } from 'vue'
  3. import { formInputProps, fromInputEmits } from '../../form-input-props'
  4. import { useEditorConfig } from './use-editor-config'
  5. import { useFormInput } from '@cip/components/hooks/form-input'
  6. import { clearEmptyKey } from './util'
  7. export default {
  8. name: 'BasicEditor',
  9. props: formInputProps,
  10. emits: [...fromInputEmits],
  11. setup (props, context) {
  12. const { securityConfig, proxyValue, width, inputStyle } = useFormInput(props, context)
  13. const containerRef = ref()
  14. const editorConfig = useEditorConfig(securityConfig)
  15. const instance = ref()
  16. const getHtml = () => {
  17. const html = instance.value.txt.html()
  18. return html === '<p><br></p>' ? '' : html
  19. }
  20. // 老版本的el-table 在fixed时会创建2个实例,会导致更新错乱
  21. // 2.2.17后无次需求故去除此处判断代码
  22. const noChangeValue = (dom) => {
  23. // const noInstanceClass = props.config.fixed ? 'el-table__body-wrapper' : 'el-table__fixed-body-wrapper'
  24. // if (dom.parentElement.classList.contains('el-table') || dom.parentElement === document.body) return false
  25. // if (!dom.parentElement.classList.contains(noInstanceClass)) {
  26. // return noChangeValue(dom.parentElement)
  27. // } else {
  28. // return true
  29. // }
  30. return false
  31. }
  32. onMounted(() => {
  33. // 组件销毁时销毁editor的实例
  34. onBeforeUnmount(() => { destroyInstance() })
  35. // 实例化时判断是否在el-table__fixed-body-wrapper下
  36. createEditor()
  37. if (!props.tableData || (props.tableData && !noChangeValue(containerRef.value))) {
  38. watchEffect(() => {
  39. const old = getHtml()
  40. if (proxyValue.value !== old) { instance.value.txt.html(proxyValue.value) }
  41. })
  42. }
  43. // 得到config配置初始化及表单设计渲染
  44. watch(() => props.config, () => {
  45. if (props.showTemplate) { // ERROR:由于table对config进行了一次浅拷贝导致数据不停变化的临时处理方案
  46. // 重新渲染修改过config配置的editor,待优化
  47. createEditor()
  48. // 初始载入内容
  49. if (instance.value.txt.html() === '') instance.value.txt.html(proxyValue.value)
  50. }
  51. }, { deep: true })
  52. })
  53. const createEditor = () => {
  54. if (instance.value) instance.value.destroy()
  55. instance.value = markRaw(new WangEditor(containerRef.value))
  56. Object.assign(instance.value.config, clearEmptyKey({
  57. height: editorConfig.height,
  58. menus: editorConfig.menus,
  59. colors: editorConfig.colors,
  60. fontNames: editorConfig.fontNames,
  61. fontSizes: editorConfig.fontSizes,
  62. emotions: editorConfig.emotions,
  63. showFullScreen: editorConfig.showFullScreen,
  64. showMenuTooltips: editorConfig.showMenuTooltips,
  65. menuTooltipPosition: editorConfig.menuTooltipPosition,
  66. pasteFilterStyle: editorConfig.pasteFilterStyle,
  67. pasteIgnoreImg: editorConfig.pasteIgnoreImg,
  68. customUploadImg: editorConfig.customUploadImg,
  69. placeholder: editorConfig.placeholder,
  70. focus: false,
  71. zIndex: 0
  72. }), {
  73. onchange (val) {
  74. proxyValue.value = val
  75. }
  76. })
  77. instance.value.create()
  78. }
  79. const destroyInstance = () => {
  80. instance.value.destroy()
  81. instance.value = null
  82. }
  83. return () => (
  84. <div ref={containerRef} style={{ ...inputStyle.value, width: width.value }} />
  85. )
  86. }
  87. }