index.vue 7.0 KB

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