index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 editorImage from '../../views/propertyManagement/common/upImage.vue';
  16. import plugins from './plugins';
  17. import toolbar from './toolbar';
  18. import load from './dynamicLoadScript';
  19. // why use this cdn, detail see https://github.com/PanJiaChen/tinymce-all-in-one
  20. const tinymceCDN = 'https://cdn.jsdelivr.net/npm/tinymce-all-in-one@4.9.3/tinymce.min.js';
  21. export default {
  22. name: 'Tinymce',
  23. // components: { editorImage },
  24. props: {
  25. id: {
  26. type: String,
  27. default: function () {
  28. return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '');
  29. }
  30. },
  31. value: {
  32. type: String,
  33. default: ''
  34. },
  35. toolbar: {
  36. type: Array,
  37. required: false,
  38. default() {
  39. return [];
  40. }
  41. },
  42. height: {
  43. type: [Number, String],
  44. required: false,
  45. default: 360
  46. },
  47. width: {
  48. type: [Number, String],
  49. required: false,
  50. default: '100%'
  51. }
  52. },
  53. data() {
  54. return {
  55. hasChange: false,
  56. hasInit: false,
  57. tinymceId: this.id,
  58. fullscreen: false,
  59. languageTypeList: {
  60. en: 'en',
  61. zh: 'zh_CN',
  62. es: 'es_MX',
  63. ja: 'ja'
  64. }
  65. };
  66. },
  67. computed: {
  68. language() {
  69. return this.languageTypeList[this.$store.getters.language];
  70. },
  71. containerWidth() {
  72. const width = this.width;
  73. if (/^[\d]+(\.[\d]+)?$/.test(width)) {
  74. // matches `100`, `'100'`
  75. return `${width}px`;
  76. }
  77. return width;
  78. }
  79. },
  80. watch: {
  81. value(val) {
  82. if (!this.hasChange && this.hasInit) {
  83. this.$nextTick(() => window.tinymce.get(this.tinymceId).setContent(val || ''));
  84. }
  85. },
  86. language() {
  87. this.destroyTinymce();
  88. this.$nextTick(() => this.initTinymce());
  89. }
  90. },
  91. mounted() {
  92. this.init();
  93. },
  94. activated() {
  95. if (window.tinymce) {
  96. this.initTinymce();
  97. }
  98. },
  99. deactivated() {
  100. this.destroyTinymce();
  101. },
  102. destroyed() {
  103. this.destroyTinymce();
  104. },
  105. methods: {
  106. init() {
  107. // dynamic load tinymce from cdn
  108. load(tinymceCDN, (err) => {
  109. if (err) {
  110. this.$message.error(err.message);
  111. return;
  112. }
  113. this.initTinymce();
  114. });
  115. },
  116. initTinymce() {
  117. const _this = this;
  118. window.tinymce.init({
  119. // fontsize_formats: '8pt 10pt 12pt 13pt 14pt 15pt 16pt 17pt 18pt 19pt 20pt 24pt 36pt',
  120. language: 'zh_CN',
  121. selector: `#${this.tinymceId}`,
  122. //状态栏指的是编辑器最底下、左侧显示dom信息、右侧显示Tiny版权链接和调整大小的那一条。默认是显示的,设为false可将其隐藏
  123. statusbar: false,
  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: false,
  130. plugins: plugins,
  131. end_container_on_empty_block: true,
  132. powerpaste_word_import: 'clean',
  133. code_dialog_height: 450,
  134. code_dialog_width: '100%',
  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. deleteImage(val) {
  211. let content = window.tinymce.get(this.tinymceId).getContent();
  212. let reg = new RegExp(`<img class="wscnph" src="${val.response.data}" />`, 'g');
  213. let newContent = content.replace(reg, '');
  214. window.tinymce.get(this.tinymceId).setContent(newContent);
  215. },
  216. imageSuccessCBK(arr) {
  217. if (arr) {
  218. // 获取内容,允许上传图片的线上大小,个数
  219. let content = window.tinymce.get(this.tinymceId).getContent();
  220. let img = [];
  221. content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, function (match, capture) {
  222. img.push(capture);
  223. });
  224. if (img.length >= 3) {
  225. return this.$message.warning('最多插入三张图片');
  226. }
  227. arr.forEach((v) => window.tinymce.get(this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" >`));
  228. }
  229. }
  230. }
  231. };
  232. </script>
  233. <style lang="scss" scoped>
  234. .tinymce-container {
  235. position: relative;
  236. line-height: normal;
  237. }
  238. .tinymce-container {
  239. ::v-deep {
  240. .mce-fullscreen {
  241. z-index: 10000;
  242. }
  243. }
  244. }
  245. .tinymce-textarea {
  246. visibility: hidden;
  247. z-index: -1;
  248. }
  249. .editor-custom-btn-container {
  250. position: absolute;
  251. right: 4px;
  252. top: 4px;
  253. /*z-index: 2005;*/
  254. }
  255. .fullscreen .editor-custom-btn-container {
  256. z-index: 10000;
  257. position: fixed;
  258. }
  259. .editor-upload-btn {
  260. display: inline-block;
  261. }
  262. ::v-deep #mceu_28-body {
  263. display: none !important;
  264. }
  265. /deep/ .cell,
  266. .el-tooltip {
  267. &:hover {
  268. display: none;
  269. }
  270. }
  271. </style>