textArea.vue 4.8 KB

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