index.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 } = 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. const noChangeValue = (dom) => {
  21. const noInstanceClass = props.config.fixed ? 'el-table__body-wrapper' : 'el-table__fixed-body-wrapper'
  22. if (dom.parentElement.classList.contains('el-table') || dom.parentElement === document.body) return false
  23. if (!dom.parentElement.classList.contains(noInstanceClass)) {
  24. return noChangeValue(dom.parentElement)
  25. } else {
  26. return true
  27. }
  28. }
  29. onMounted(() => {
  30. // 组件销毁时销毁editor的实例
  31. onBeforeUnmount(() => { destroyInstance() })
  32. // 实例化时判断是否在el-table__fixed-body-wrapper下
  33. createEditor()
  34. if (!props.tableData || (props.tableData && !noChangeValue(containerRef.value))) {
  35. watchEffect(() => {
  36. const old = getHtml()
  37. if (proxyValue.value !== old) { instance.value.txt.html(proxyValue.value) }
  38. })
  39. }
  40. // 得到config配置初始化及表单设计渲染
  41. watch(() => props.config, () => {
  42. if (props.showTemplate) { // ERROR:由于table对config进行了一次浅拷贝导致数据不停变化的临时处理方案
  43. // 重新渲染修改过config配置的editor,待优化
  44. createEditor()
  45. // 初始载入内容
  46. if (instance.value.txt.html() === '') instance.value.txt.html(proxyValue.value)
  47. }
  48. }, { deep: true })
  49. })
  50. const createEditor = () => {
  51. if (instance.value) instance.value.destroy()
  52. instance.value = markRaw(new WangEditor(containerRef.value))
  53. Object.assign(instance.value.config, clearEmptyKey({
  54. height: editorConfig.height,
  55. menus: editorConfig.menus,
  56. colors: editorConfig.colors,
  57. fontNames: editorConfig.fontNames,
  58. fontSizes: editorConfig.fontSizes,
  59. emotions: editorConfig.emotions,
  60. showFullScreen: editorConfig.showFullScreen,
  61. showMenuTooltips: editorConfig.showMenuTooltips,
  62. menuTooltipPosition: editorConfig.menuTooltipPosition,
  63. pasteFilterStyle: editorConfig.pasteFilterStyle,
  64. pasteIgnoreImg: editorConfig.pasteIgnoreImg,
  65. customUploadImg: editorConfig.customUploadImg,
  66. placeholder: editorConfig.placeholder,
  67. focus: false,
  68. zIndex: 0
  69. }), {
  70. onchange (val) {
  71. proxyValue.value = val
  72. }
  73. })
  74. instance.value.create()
  75. }
  76. const destroyInstance = () => {
  77. instance.value.destroy()
  78. instance.value = null
  79. }
  80. return () => (
  81. <div ref={containerRef} style="width:100%" />
  82. )
  83. }
  84. }