2ae6af689765fe57f2d8a72b7f5c8c86ffa938e9.svn-base 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <div class="tinymce-editor">
  3. <editor
  4. v-if="!reloading"
  5. v-model="myValue"
  6. :init="init"
  7. :disabled="disabled"
  8. @onClick="onClick">
  9. </editor>
  10. </div>
  11. </template>
  12. <script>
  13. import tinymce from 'tinymce/tinymce'
  14. import Editor from '@tinymce/tinymce-vue'
  15. import 'tinymce/themes/silver/theme'
  16. import 'tinymce/plugins/image'
  17. import 'tinymce/plugins/link'
  18. import 'tinymce/plugins/media'
  19. import 'tinymce/plugins/table'
  20. import 'tinymce/plugins/lists'
  21. import 'tinymce/plugins/contextmenu'
  22. import 'tinymce/plugins/wordcount'
  23. import 'tinymce/plugins/colorpicker'
  24. import 'tinymce/plugins/textcolor'
  25. import 'tinymce/plugins/fullscreen'
  26. import 'tinymce/icons/default'
  27. import { uploadAction,getFileAccessHttpUrl } from '@/api/manage'
  28. import { getVmParentByName } from '@/utils/util'
  29. export default {
  30. components: {
  31. Editor
  32. },
  33. props: {
  34. value: {
  35. type: String,
  36. required:false
  37. },
  38. triggerChange:{
  39. type: Boolean,
  40. default: false,
  41. required:false
  42. },
  43. disabled: {
  44. type: Boolean,
  45. default: false
  46. },
  47. plugins: {
  48. type: [String, Array],
  49. default: 'lists image link media table textcolor wordcount contextmenu fullscreen'
  50. },
  51. toolbar: {
  52. type: [String, Array],
  53. default: 'undo redo | formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists link unlink image media table | removeformat | fullscreen',
  54. branding:false
  55. }
  56. },
  57. data() {
  58. return {
  59. //初始化配置
  60. init: {
  61. language_url: '/tinymce/langs/zh_CN.js',
  62. language: 'zh_CN',
  63. skin_url: '/tinymce/skins/lightgray',
  64. height: 300,
  65. plugins: this.plugins,
  66. toolbar: this.toolbar,
  67. branding: false,
  68. menubar: false,
  69. toolbar_drawer: false,
  70. images_upload_handler: (blobInfo, success) => {
  71. let formData = new FormData()
  72. formData.append('file', blobInfo.blob(), blobInfo.filename());
  73. formData.append('biz', "jeditor");
  74. formData.append("jeditor","1");
  75. uploadAction(window._CONFIG['domianURL']+"/sys/common/upload", formData).then((res) => {
  76. if (res.success) {
  77. if(res.message == 'local'){
  78. const img = 'data:image/jpeg;base64,' + blobInfo.base64()
  79. success(img)
  80. }else{
  81. let img = getFileAccessHttpUrl(res.message)
  82. success(img)
  83. }
  84. }
  85. })
  86. }
  87. },
  88. myValue: this.value,
  89. reloading: false,
  90. }
  91. },
  92. mounted() {
  93. this.initATabsChangeAutoReload()
  94. },
  95. methods: {
  96. reload() {
  97. this.reloading = true
  98. this.$nextTick(() => this.reloading = false)
  99. },
  100. onClick(e) {
  101. this.$emit('onClick', e, tinymce)
  102. },
  103. //可以添加一些自己的自定义事件,如清空内容
  104. clear() {
  105. this.myValue = ''
  106. },
  107. /**
  108. * 自动判断父级是否是 <a-tabs/> 组件,然后添加事件监听,自动触发reload()
  109. *
  110. * 由于 tabs 组件切换会导致 tinymce 无法输入,
  111. * 只有重新加载才能使用(无论是vue版的还是jQuery版tinymce都有这个通病)
  112. */
  113. initATabsChangeAutoReload() {
  114. // 获取父级
  115. let tabs = getVmParentByName(this, 'ATabs')
  116. let tabPane = getVmParentByName(this, 'ATabPane')
  117. if (tabs && tabPane) {
  118. // 用户自定义的 key
  119. let currentKey = tabPane.$vnode.key
  120. // 添加事件监听
  121. tabs.$on('change', (key) => {
  122. // 切换到自己时执行reload
  123. if (currentKey === key) {
  124. this.reload()
  125. }
  126. })
  127. //update--begin--autor:liusq-----date:20210316------for:富文本编辑器tab父组件可能导致的赋值问题------
  128. this.reload()
  129. //update--end--autor:liusq-----date:20210316------for:富文本编辑器tab父组件可能导致的赋值问题------
  130. }else{
  131. //update--begin--autor:wangshuai-----date:20200724------for:富文本编辑器切换tab无法修改------
  132. let tabLayout = getVmParentByName(this, 'TabLayout')
  133. //update--begin--autor:liusq-----date:20210713------for:处理特殊情况excuteCallback不能使用------
  134. try {
  135. tabLayout.excuteCallback(() => {
  136. this.reload()
  137. })
  138. } catch (error) {
  139. if (tabLayout) {
  140. this.reload()
  141. }
  142. }
  143. //update--end--autor:liusq-----date:20210713------for:处理特殊情况excuteCallback不能使用------
  144. //update--begin--autor:wangshuai-----date:20200724------for:文本编辑器切换tab无法修改------
  145. }
  146. },
  147. },
  148. watch: {
  149. value(newValue) {
  150. this.myValue = (newValue == null ? '' : newValue)
  151. },
  152. myValue(newValue) {
  153. if(this.triggerChange){
  154. this.$emit('change', newValue)
  155. }else{
  156. this.$emit('input', newValue)
  157. }
  158. }
  159. }
  160. }
  161. </script>
  162. <style scoped>
  163. </style>