index.vue 10 KB

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