textArea.vue 4.3 KB

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