ce48462768251068caff12591e65f0b31692a646.svn-base 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <div :class="{fullscreen:fullscreen}" class="tinymce-container" :style="{width:containerWidth}">
  3. <textarea :id="tinymceId" class="tinymce-textarea" />
  4. <div class="editor-custom-btn-container">
  5. <editorImage color="#1890ff" class="editor-upload-btn" @successCBK="imageSuccessCBK" />
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. /**
  11. * docs:
  12. * https://panjiachen.github.io/vue-element-admin-site/feature/component/rich-editor.html#tinymce
  13. */
  14. import editorImage from './components/EditorImage'
  15. import plugins from './plugins'
  16. import toolbar from './toolbar'
  17. import load from './dynamicLoadScript'
  18. // why use this cdn, detail see https://github.com/PanJiaChen/tinymce-all-in-one
  19. const tinymceCDN = 'https://cdn.jsdelivr.net/npm/tinymce-all-in-one@4.9.3/tinymce.min.js'
  20. export default {
  21. name: 'Tinymce',
  22. components: { editorImage },
  23. props: {
  24. id: {
  25. type: String,
  26. default: function() {
  27. return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
  28. }
  29. },
  30. value: {
  31. type: String,
  32. default: ''
  33. },
  34. toolbar: {
  35. type: Array,
  36. required: false,
  37. default() {
  38. return []
  39. }
  40. },
  41. menubar: {
  42. type: String,
  43. default: 'file edit insert view format table'
  44. },
  45. height: {
  46. type: [Number, String],
  47. required: false,
  48. default: 360
  49. },
  50. width: {
  51. type: [Number, String],
  52. required: false,
  53. default: 'auto'
  54. }
  55. },
  56. data() {
  57. return {
  58. hasChange: false,
  59. hasInit: false,
  60. tinymceId: this.id,
  61. fullscreen: false,
  62. languageTypeList: {
  63. 'en': 'en',
  64. 'zh': 'zh_CN',
  65. 'es': 'es_MX',
  66. 'ja': 'ja'
  67. }
  68. }
  69. },
  70. computed: {
  71. containerWidth() {
  72. const width = this.width
  73. if (/^[\d]+(\.[\d]+)?$/.test(width)) { // matches `100`, `'100'`
  74. return `${width}px`
  75. }
  76. return width
  77. }
  78. },
  79. watch: {
  80. value(val) {
  81. if (!this.hasChange && this.hasInit) {
  82. this.$nextTick(() =>
  83. window.tinymce.get(this.tinymceId).setContent(val || ''))
  84. }
  85. }
  86. },
  87. mounted() {
  88. this.init()
  89. },
  90. activated() {
  91. if (window.tinymce) {
  92. this.initTinymce()
  93. }
  94. },
  95. deactivated() {
  96. this.destroyTinymce()
  97. },
  98. destroyed() {
  99. this.destroyTinymce()
  100. },
  101. methods: {
  102. init() {
  103. // dynamic load tinymce from cdn
  104. load(tinymceCDN, (err) => {
  105. if (err) {
  106. this.$message.error(err.message)
  107. return
  108. }
  109. this.initTinymce()
  110. })
  111. },
  112. initTinymce() {
  113. const _this = this
  114. window.tinymce.init({
  115. selector: `#${this.tinymceId}`,
  116. language: this.languageTypeList['en'],
  117. height: this.height,
  118. body_class: 'panel-body ',
  119. object_resizing: false,
  120. toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
  121. menubar: this.menubar,
  122. plugins: plugins,
  123. end_container_on_empty_block: true,
  124. powerpaste_word_import: 'clean',
  125. code_dialog_height: 450,
  126. code_dialog_width: 1000,
  127. advlist_bullet_styles: 'square',
  128. advlist_number_styles: 'default',
  129. imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
  130. default_link_target: '_blank',
  131. link_title: false,
  132. nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
  133. init_instance_callback: editor => {
  134. if (_this.value) {
  135. editor.setContent(_this.value)
  136. }
  137. _this.hasInit = true
  138. editor.on('NodeChange Change KeyUp SetContent', () => {
  139. this.hasChange = true
  140. this.$emit('input', editor.getContent())
  141. })
  142. },
  143. setup(editor) {
  144. editor.on('FullscreenStateChanged', (e) => {
  145. _this.fullscreen = e.state
  146. })
  147. }
  148. // 整合七牛上传
  149. // images_dataimg_filter(img) {
  150. // setTimeout(() => {
  151. // const $image = $(img);
  152. // $image.removeAttr('width');
  153. // $image.removeAttr('height');
  154. // if ($image[0].height && $image[0].width) {
  155. // $image.attr('data-wscntype', 'image');
  156. // $image.attr('data-wscnh', $image[0].height);
  157. // $image.attr('data-wscnw', $image[0].width);
  158. // $image.addClass('wscnph');
  159. // }
  160. // }, 0);
  161. // return img
  162. // },
  163. // images_upload_handler(blobInfo, success, failure, progress) {
  164. // progress(0);
  165. // const token = _this.$store.getters.token;
  166. // getToken(token).then(response => {
  167. // const url = response.data.qiniu_url;
  168. // const formData = new FormData();
  169. // formData.append('token', response.data.qiniu_token);
  170. // formData.append('key', response.data.qiniu_key);
  171. // formData.append('file', blobInfo.blob(), url);
  172. // upload(formData).then(() => {
  173. // success(url);
  174. // progress(100);
  175. // })
  176. // }).catch(err => {
  177. // failure('出现未知问题,刷新页面,或者联系程序员')
  178. // console.log(err);
  179. // });
  180. // },
  181. })
  182. },
  183. destroyTinymce() {
  184. const tinymce = window.tinymce.get(this.tinymceId)
  185. if (this.fullscreen) {
  186. tinymce.execCommand('mceFullScreen')
  187. }
  188. if (tinymce) {
  189. tinymce.destroy()
  190. }
  191. },
  192. setContent(value) {
  193. window.tinymce.get(this.tinymceId).setContent(value)
  194. },
  195. getContent() {
  196. window.tinymce.get(this.tinymceId).getContent()
  197. },
  198. imageSuccessCBK(arr) {
  199. const _this = this
  200. arr.forEach(v => {
  201. window.tinymce.get(_this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" >`)
  202. })
  203. }
  204. }
  205. }
  206. </script>
  207. <style scoped>
  208. .tinymce-container {
  209. position: relative;
  210. line-height: normal;
  211. }
  212. .tinymce-container>>>.mce-fullscreen {
  213. z-index: 10000;
  214. }
  215. .tinymce-textarea {
  216. visibility: hidden;
  217. z-index: -1;
  218. }
  219. .editor-custom-btn-container {
  220. position: absolute;
  221. right: 4px;
  222. top: 4px;
  223. /*z-index: 2005;*/
  224. }
  225. .fullscreen .editor-custom-btn-container {
  226. z-index: 10000;
  227. position: fixed;
  228. }
  229. .editor-upload-btn {
  230. display: inline-block;
  231. }
  232. </style>