index.vue 7.1 KB

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