index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div class="tinymce-container editor-container">
  3. <textarea class="tinymce-textarea" :id="tinymceId"></textarea>
  4. </div>
  5. </template>
  6. <script>
  7. import plugins from './plugins'
  8. import toolbar from './toolbar'
  9. import { uploadPic } from '@/api/storage'
  10. export default {
  11. name: 'tinymce',
  12. props: {
  13. id: {
  14. type: String
  15. },
  16. value: {
  17. type: String,
  18. default: ''
  19. },
  20. toolbar: {
  21. type: Array,
  22. required: false,
  23. default() {
  24. return []
  25. }
  26. },
  27. menubar: {
  28. default: 'file edit insert view format table'
  29. },
  30. height: {
  31. type: Number,
  32. required: false,
  33. default: 360
  34. }
  35. },
  36. data() {
  37. return {
  38. hasChange: false,
  39. hasInit: false,
  40. tinymceId: this.id || 'vue-tinymce-' + +new Date()
  41. }
  42. },
  43. watch: {
  44. value(val) {
  45. if (!this.hasChange && this.hasInit) {
  46. this.$nextTick(() => window.tinymce.get(this.tinymceId).setContent(val))
  47. }
  48. }
  49. },
  50. mounted() {
  51. this.initTinymce()
  52. },
  53. activated() {
  54. this.initTinymce()
  55. },
  56. deactivated() {
  57. this.destroyTinymce()
  58. },
  59. methods: {
  60. initTinymce() {
  61. const _this = this
  62. window.tinymce.init({
  63. selector: `#${this.tinymceId}`,
  64. height: this.height,
  65. language: 'zh_CN',
  66. body_class: 'panel-body ',
  67. object_resizing: false,
  68. toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
  69. menubar: this.menubar,
  70. plugins: plugins,
  71. end_container_on_empty_block: true,
  72. powerpaste_word_import: 'clean',
  73. code_dialog_height: 450,
  74. code_dialog_width: 1000,
  75. advlist_bullet_styles: 'square',
  76. advlist_number_styles: 'default',
  77. default_link_target: '_blank',
  78. relative_urls : false,
  79. remove_script_host : false,
  80. document_base_url : 'http://xiaoyou.dgtis.com/',
  81. link_title: false,
  82. init_instance_callback: editor => {
  83. if (_this.value) {
  84. editor.setContent(_this.value)
  85. }
  86. _this.hasInit = true
  87. editor.on('NodeChange Change KeyUp', () => {
  88. this.hasChange = true
  89. this.$emit('input', editor.getContent({ format: 'raw' }))
  90. })
  91. },
  92. images_upload_handler: function(blobInfo, success, failure) {
  93. const formData = new FormData()
  94. formData.append('file', blobInfo.blob())
  95. uploadPic(formData).then(res => {
  96. var resurl=res.data.data.url;
  97. console.log(resurl);
  98. success(resurl)
  99. }).catch(() => {
  100. failure('上传失败,请重新上传')
  101. })
  102. }
  103. // 整合七牛上传
  104. // images_dataimg_filter(img) {
  105. // setTimeout(() => {
  106. // const $image = $(img);
  107. // $image.removeAttr('width');
  108. // $image.removeAttr('height');
  109. // if ($image[0].height && $image[0].width) {
  110. // $image.attr('data-wscntype', 'image');
  111. // $image.attr('data-wscnh', $image[0].height);
  112. // $image.attr('data-wscnw', $image[0].width);
  113. // $image.addClass('wscnph');
  114. // }
  115. // }, 0);
  116. // return img
  117. // },
  118. // images_upload_handler(blobInfo, success, failure, progress) {
  119. // progress(0);
  120. // const token = _this.$store.getters.token;
  121. // getToken(token).then(response => {
  122. // const url = response.data.qiniu_url;
  123. // const formData = new FormData();
  124. // formData.append('token', response.data.qiniu_token);
  125. // formData.append('key', response.data.qiniu_key);
  126. // formData.append('file', blobInfo.blob(), url);
  127. // upload(formData).then(() => {
  128. // success(url);
  129. // progress(100);
  130. // })
  131. // }).catch(err => {
  132. // failure('出现未知问题,刷新页面,或者联系程序员')
  133. // console.log(err);
  134. // });
  135. // },
  136. })
  137. },
  138. destroyTinymce() {
  139. if (window.tinymce.get(this.tinymceId)) {
  140. window.tinymce.get(this.tinymceId).destroy()
  141. }
  142. },
  143. setContent(value) {
  144. window.tinymce.get(this.tinymceId).setContent(value)
  145. },
  146. getContent() {
  147. window.tinymce.get(this.tinymceId).getContent()
  148. },
  149. imageSuccessCBK(arr) {
  150. const _this = this
  151. arr.forEach(v => {
  152. window.tinymce.get(_this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" >`)
  153. })
  154. }
  155. },
  156. destroyed() {
  157. this.destroyTinymce()
  158. }
  159. }
  160. </script>
  161. <style scoped>
  162. .tinymce-container {
  163. position: relative
  164. }
  165. .tinymce-textarea {
  166. visibility: hidden;
  167. z-index: -1;
  168. }
  169. .editor-custom-btn-container {
  170. position: absolute;
  171. right: 4px;
  172. top: 4px;
  173. /*z-index: 2005;*/
  174. }
  175. .editor-upload-btn {
  176. display: inline-block;
  177. }
  178. </style>