You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.5 KiB

3 years ago
  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. // it will try to keep these URLs intact
  149. // https://www.tiny.cloud/docs-3x/reference/configuration/Configuration3x@convert_urls/
  150. // https://stackoverflow.com/questions/5196205/disable-tinymce-absolute-to-relative-url-conversions
  151. convert_urls: false
  152. // 整合七牛上传
  153. // images_dataimg_filter(img) {
  154. // setTimeout(() => {
  155. // const $image = $(img);
  156. // $image.removeAttr('width');
  157. // $image.removeAttr('height');
  158. // if ($image[0].height && $image[0].width) {
  159. // $image.attr('data-wscntype', 'image');
  160. // $image.attr('data-wscnh', $image[0].height);
  161. // $image.attr('data-wscnw', $image[0].width);
  162. // $image.addClass('wscnph');
  163. // }
  164. // }, 0);
  165. // return img
  166. // },
  167. // images_upload_handler(blobInfo, success, failure, progress) {
  168. // progress(0);
  169. // const token = _this.$store.getters.token;
  170. // getToken(token).then(response => {
  171. // const url = response.data.qiniu_url;
  172. // const formData = new FormData();
  173. // formData.append('token', response.data.qiniu_token);
  174. // formData.append('key', response.data.qiniu_key);
  175. // formData.append('file', blobInfo.blob(), url);
  176. // upload(formData).then(() => {
  177. // success(url);
  178. // progress(100);
  179. // })
  180. // }).catch(err => {
  181. // failure('出现未知问题,刷新页面,或者联系程序员')
  182. // console.log(err);
  183. // });
  184. // },
  185. })
  186. },
  187. destroyTinymce() {
  188. const tinymce = window.tinymce.get(this.tinymceId)
  189. if (this.fullscreen) {
  190. tinymce.execCommand('mceFullScreen')
  191. }
  192. if (tinymce) {
  193. tinymce.destroy()
  194. }
  195. },
  196. setContent(value) {
  197. window.tinymce.get(this.tinymceId).setContent(value)
  198. },
  199. getContent() {
  200. window.tinymce.get(this.tinymceId).getContent()
  201. },
  202. imageSuccessCBK(arr) {
  203. arr.forEach(v => window.tinymce.get(this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" >`))
  204. }
  205. }
  206. }
  207. </script>
  208. <style lang="scss" scoped>
  209. .tinymce-container {
  210. position: relative;
  211. line-height: normal;
  212. }
  213. .tinymce-container {
  214. ::v-deep {
  215. .mce-fullscreen {
  216. z-index: 10000;
  217. }
  218. }
  219. }
  220. .tinymce-textarea {
  221. visibility: hidden;
  222. z-index: -1;
  223. }
  224. .editor-custom-btn-container {
  225. position: absolute;
  226. right: 4px;
  227. top: 4px;
  228. /*z-index: 2005;*/
  229. }
  230. .fullscreen .editor-custom-btn-container {
  231. z-index: 10000;
  232. position: fixed;
  233. }
  234. .editor-upload-btn {
  235. display: inline-block;
  236. }
  237. </style>