textArea.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div>
  3. <article
  4. :id="id"
  5. v-loading="loading"
  6. element-loading-text="数据上传中,请稍后"
  7. element-loading-spinner="el-icon-loading"
  8. >
  9. <div v-show="isEmpty && !edit" class="text-edit">
  10. {{ emptyText }}
  11. <el-button type="text" @click="ImmediateAddition">{{ inputButton }}</el-button>
  12. </div>
  13. <div v-show="edit">
  14. <el-tooltip effect="dark" content="单击‘编辑’,失去焦点 ‘保存’" placement="bottom">
  15. <editor v-model="inputValue" :class="'tinymce_'+id" :init="init" @input="changeText" />
  16. </el-tooltip>
  17. </div>
  18. <div v-show="!isEmpty && !edit">
  19. <pre class="text-pre" @click="ImmediateAddition" v-html="handlerText(value)" />
  20. </div>
  21. <div :id="'inputUpload_'+id" style="display: none" @click.stop="blur_textarea" />
  22. <div v-show="edit" class="control">
  23. <el-button size="small" @click="edit=false">取消</el-button>
  24. <el-button type="primary" size="small" @click.stop="blur_textarea">确定</el-button>
  25. </div>
  26. </article>
  27. </div>
  28. </template>
  29. <script>
  30. import { getContainImgHTMLNode } from '@/utils/handleTinymce'
  31. import tinymce from 'tinymce/tinymce'
  32. import Editor from '@tinymce/tinymce-vue'
  33. import 'tinymce/themes/silver/theme'
  34. import 'tinymce/icons/default/icons'
  35. export default {
  36. components: {
  37. Editor
  38. },
  39. props: {
  40. id: {
  41. type: String,
  42. default: '',
  43. required: true
  44. },
  45. value: {
  46. type: String,
  47. default: '',
  48. required: false
  49. },
  50. height: {
  51. type: Number,
  52. default: 200,
  53. required: false
  54. },
  55. emptyText: {
  56. type: String,
  57. default: '',
  58. required: false
  59. },
  60. inputButton: {
  61. type: String,
  62. default: '',
  63. required: false
  64. }
  65. },
  66. data() {
  67. return {
  68. loading: false,
  69. inputValue: '',
  70. edit: false,
  71. init: {
  72. auto_focus: true,
  73. language_url: '/tinymce/langs/zh_CN.js',
  74. language: 'zh_CN',
  75. skin_url: '/tinymce/skins/ui/oxide', // 编辑器需要一个skin才能正常工作,所以要设置一个skin_url指向之前复制出来的skin文件
  76. height: this.height,
  77. browser_spellcheck: true, // 拼写检查
  78. branding: false, // 去水印
  79. elementpath: false, // 禁用编辑器底部的状态栏
  80. statusbar: false, // 隐藏编辑器底部的状态栏
  81. paste_data_images: true, // 允许粘贴图像
  82. menubar: false, // 隐藏最上方menu
  83. fontsize_formats: '14px 16px 18px 20px 24px 26px 28px 30px 32px 36px', // 字体大小
  84. file_picker_types: 'image',
  85. images_upload_credentials: true,
  86. plugins: 'lists table textcolor wordcount contextmenu', // 引入插件
  87. toolbar: 'bold italic underline strikethrough | fontsizeselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent table | undo redo | removeformat formatselect'
  88. }
  89. }
  90. },
  91. computed: {
  92. isEmpty() {
  93. return this.inputValue === null || this.inputValue.replace(/\s+/g, '') === ''
  94. }
  95. },
  96. watch: {
  97. value: {
  98. handler(newV, oldV) {
  99. this.inputValue = newV
  100. },
  101. immediate: true
  102. }
  103. },
  104. mounted() {
  105. document.body.addEventListener('click', e => {
  106. if (!document.getElementById(this.id)) {
  107. return false
  108. }
  109. const isContain = document.getElementById(this.id).contains(e.target)
  110. if (!isContain) {
  111. document.getElementById(`inputUpload_${this.id}`).click()
  112. }
  113. })
  114. tinymce.init({ selector: `.tinymce_${this.id}` })
  115. },
  116. methods: {
  117. ImmediateAddition() { // 立即添加(编辑)
  118. this.edit = true
  119. },
  120. changeText(e) { // 富文本内容改变
  121. this.inputValue = e
  122. },
  123. async blur_textarea() {
  124. if (this.edit) {
  125. this.loading = true
  126. try {
  127. this.inputValue = await getContainImgHTMLNode(this.inputValue)
  128. } catch (error) {
  129. this.loading = false
  130. throw error
  131. }
  132. this.loading = false
  133. this.edit = false
  134. this.$emit('update:value', this.inputValue)
  135. this.$emit('change', this.inputValue)
  136. }
  137. },
  138. handlerText(val) {
  139. if (val) {
  140. const reg = new RegExp(/<\/?p[^>]*>/gi)
  141. return val.replace(reg, '')
  142. }
  143. }
  144. }
  145. }
  146. </script>
  147. <style scoped lang="scss">
  148. article {
  149. padding: 0 30px 20px 30px;
  150. }
  151. .text-edit {
  152. color: #666666;
  153. font-size: 14px;
  154. display: flex;
  155. align-items: center;
  156. justify-content: flex-start;
  157. }
  158. .text-pre {
  159. white-space:pre-line;
  160. font-size: 14px;
  161. color: #333B4A;
  162. cursor: pointer;
  163. }
  164. /deep/ textarea {
  165. width: calc(100% - 40px);
  166. margin: auto;
  167. }
  168. .control {
  169. display: flex;
  170. justify-content: flex-end;
  171. margin-top: 10px;
  172. }
  173. </style>