index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. times: null,
  56. hasChange: false,
  57. hasInit: false,
  58. tinymceId: this.id,
  59. fullscreen: false,
  60. languageTypeList: {
  61. en: 'en',
  62. zh: 'zh_CN',
  63. es: 'es_MX',
  64. ja: 'ja'
  65. },
  66. thisImg: ''
  67. };
  68. },
  69. computed: {
  70. language() {
  71. return this.languageTypeList[this.$store.getters.language];
  72. },
  73. containerWidth() {
  74. const width = this.width;
  75. if (/^[\d]+(\.[\d]+)?$/.test(width)) {
  76. // matches `100`, `'100'`
  77. return `${width}px`;
  78. }
  79. return width;
  80. }
  81. },
  82. watch: {
  83. value(val) {
  84. // console.log('tinymceValue============', val);
  85. if (!this.hasChange && this.hasInit) {
  86. this.$nextTick(() => {
  87. window.tinymce.get(this.tinymceId).setContent(val || '');
  88. });
  89. }
  90. },
  91. language() {
  92. this.destroyTinymce();
  93. this.$nextTick(() => this.initTinymce());
  94. }
  95. },
  96. mounted() {
  97. this.init();
  98. },
  99. activated() {
  100. if (window.tinymce) {
  101. this.initTinymce();
  102. }
  103. },
  104. deactivated() {
  105. this.destroyTinymce();
  106. },
  107. destroyed() {
  108. this.destroyTinymce();
  109. },
  110. methods: {
  111. init() {
  112. // dynamic load tinymce from cdn
  113. // load(tinymceCDN, (err) => {
  114. // if (err) {
  115. // this.$message.error(err.message);
  116. // return;
  117. // }
  118. // this.initTinymce();
  119. // });
  120. this.initTinymce();
  121. },
  122. onPaste(event) {
  123. const items = (event.clipboardData || window.clipboardData).items;
  124. if (items[0].type.indexOf('image') !== -1) {
  125. const file = items[0].getAsFile();
  126. const formData = new FormData();
  127. formData.append('file', file);
  128. // 上传图片
  129. this.$http.post('/sc-community/upload/uploadFile', formData).then((res) => {
  130. if (res.status == 0) {
  131. let content = window.tinymce.get(this.tinymceId).getContent();
  132. let newContent = content.replace(/<img *src="data.*?\/>/g, `<img src='${res.data}' />`);
  133. window.tinymce.get(this.tinymceId).setContent(newContent);
  134. // resolve && resolve();
  135. } else {
  136. this.$message.error(res.msg);
  137. }
  138. });
  139. }
  140. // else {
  141. // this.$message({type:'warning',message:'只能复制纯文本或图片',duration:3000});
  142. // }
  143. return;
  144. new Promise((resolve) => {
  145. const items = (event.clipboardData || window.clipboardData).items;
  146. if (items[0].type.indexOf('image') !== -1) {
  147. const file = items[0].getAsFile();
  148. const formData = new FormData();
  149. formData.append('file', file);
  150. // 上传图片
  151. this.$http.post('/sc-community/upload/uploadFile', formData).then((res) => {
  152. if (res.status === 0) {
  153. let img = `<img class="wscnph" src="${res.data}" />`;
  154. window.tinymce.get(this.tinymceId).insertContent(img);
  155. resolve && resolve();
  156. } else {
  157. this.$message.error(res.msg);
  158. }
  159. });
  160. } else {
  161. this.$message.error('只能复制文字图片,样式无法复制请手动排版');
  162. }
  163. }).then(() => {
  164. let content = window.tinymce.get(this.tinymceId).getContent();
  165. let newContent = content.replace(/<img *src="data.*?\/>/g, '');
  166. window.tinymce.get(this.tinymceId).setContent(newContent);
  167. });
  168. },
  169. initTinymce() {
  170. const _this = this;
  171. window.tinymce.init({
  172. // fontsize_formats: '8pt 10pt 12pt 13pt 14pt 15pt 16pt 17pt 18pt 19pt 20pt 24pt 36pt',
  173. language: 'zh_CN',
  174. selector: `#${this.tinymceId}`,
  175. //状态栏指的是编辑器最底下、左侧显示dom信息、右侧显示Tiny版权链接和调整大小的那一条。默认是显示的,设为false可将其隐藏
  176. statusbar: false,
  177. height: this.height,
  178. branding: false, //去掉底部文本
  179. body_class: 'panel-body ',
  180. object_resizing: false,
  181. toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
  182. menubar: false,
  183. plugins: plugins,
  184. //只保留标签
  185. valid_elements: 'img[src],span[style],div,em,p,strong,blockquote',
  186. end_container_on_empty_block: true,
  187. powerpaste_word_import: 'clean',
  188. code_dialog_height: 450,
  189. code_dialog_width: '100%',
  190. default_link_target: '_blank',
  191. link_title: false,
  192. init_instance_callback: (editor) => {
  193. if (_this.value) {
  194. editor.setContent(_this.value);
  195. }
  196. _this.hasInit = true;
  197. editor.on('NodeChange Change KeyUp SetContent', () => {
  198. this.times = null;
  199. this.hasChange = true;
  200. // const val = editor.getContent().replace(/<p><img\s?src="data.*?<\/p>/g, '')
  201. this.$emit('input', editor.getContent());
  202. });
  203. editor.on('paste', (evt) => {
  204. // 监听粘贴事件
  205. this.onPaste(evt);
  206. });
  207. },
  208. setup(editor) {
  209. editor.on('FullscreenStateChanged', (e) => {
  210. _this.fullscreen = e.state;
  211. });
  212. },
  213. convert_urls: false,
  214. paste_data_images: false // 粘贴的同时能把内容里的图片自动上传
  215. // images_upload_handler(blobInfo, success, failure, progress) {
  216. // }
  217. });
  218. },
  219. destroyTinymce() {
  220. const tinymce = window.tinymce.get(this.tinymceId);
  221. if (this.fullscreen) {
  222. tinymce.execCommand('mceFullScreen');
  223. }
  224. if (tinymce) {
  225. tinymce.destroy();
  226. }
  227. },
  228. setContent(value) {
  229. window.tinymce.get(this.tinymceId).setContent(value);
  230. },
  231. getContent() {
  232. window.tinymce.get(this.tinymceId).getContent();
  233. },
  234. deleteImage(val) {
  235. let content = window.tinymce.get(this.tinymceId).getContent();
  236. let reg = new RegExp(`<img class="wscnph" src="${val.response.data}" />`, 'g');
  237. let newContent = content.replace(reg, '');
  238. window.tinymce.get(this.tinymceId).setContent(newContent);
  239. },
  240. imageSuccessCBK(arr) {
  241. // if (arr) {
  242. // // 获取内容,允许上传图片的线上大小,个数
  243. // let content = window.tinymce.get(this.tinymceId).getContent();
  244. // let img = [];
  245. // content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, function (match, capture) {
  246. // img.push(capture);
  247. // });
  248. // // if (img.length >= 3) {
  249. // // return this.$message.warning('最多插入三张图片');
  250. // // }
  251. // arr.forEach((v) => window.tinymce.get(this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" />`));
  252. // }
  253. }
  254. }
  255. };
  256. </script>
  257. <style lang="scss" scoped>
  258. /deep/ .wscnph {
  259. width: 200px !important;
  260. height: 100px;
  261. vertical-align: middle;
  262. }
  263. .tinymce-container {
  264. position: relative;
  265. line-height: normal;
  266. }
  267. .tinymce-container {
  268. ::v-deep {
  269. .mce-fullscreen {
  270. z-index: 10000;
  271. }
  272. }
  273. }
  274. .tinymce-textarea {
  275. visibility: hidden;
  276. z-index: -1;
  277. }
  278. .editor-custom-btn-container {
  279. position: absolute;
  280. right: 4px;
  281. top: 4px;
  282. /*z-index: 2005;*/
  283. }
  284. .fullscreen .editor-custom-btn-container {
  285. z-index: 10000;
  286. position: fixed;
  287. }
  288. .editor-upload-btn {
  289. display: inline-block;
  290. }
  291. ::v-deep #mceu_28-body {
  292. display: none !important;
  293. }
  294. /deep/ .cell,
  295. .el-tooltip {
  296. &:hover {
  297. display: none;
  298. }
  299. }
  300. </style>