index.vue 8.0 KB

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