index.vue 9.1 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. <FileUpload :show-file-list="false" multiple @success="imageSuccessCBK" accept="image/*"></FileUpload>
  7. </div> -->
  8. </div>
  9. </template>
  10. <script>
  11. /**
  12. * docs:
  13. * https://panjiachen.github.io/vue-element-admin-site/feature/component/rich-editor.html#tinymce
  14. */
  15. // import "./tinymce-all-in-one"
  16. import editorImage from './components/EditorImage.vue'
  17. import plugins from './plugins'
  18. import toolbar from './toolbar'
  19. // import "tinymce/icons/default/icons";
  20. // import "tinymce/themes/silver/index";
  21. // import "tinymce/plugins/advlist/index"
  22. // import "tinymce/plugins/anchor/index"
  23. // import "tinymce/plugins/autolink/index"
  24. // import "tinymce/plugins/autoresize/index"
  25. // import "tinymce/plugins/autosave/index"
  26. // import "tinymce/plugins/charmap/index"
  27. // import "tinymce/plugins/code/index"
  28. // import "tinymce/plugins/codesample/index"
  29. // import "tinymce/plugins/directionality/index"
  30. // import "tinymce/plugins/emoticons/index"
  31. // import "tinymce/plugins/fullscreen/index"
  32. // import "tinymce/plugins/help/index"
  33. // import "tinymce/plugins/image/index"
  34. // import "tinymce/plugins/importcss/index"
  35. // import "tinymce/plugins/insertdatetime/index"
  36. // import "tinymce/plugins/link/index"
  37. // import "tinymce/plugins/lists/index"
  38. // import "tinymce/plugins/media/index"
  39. // import "tinymce/plugins/nonbreaking/index"
  40. // import "tinymce/plugins/pagebreak/index"
  41. // import "tinymce/plugins/preview/index"
  42. // import "tinymce/plugins/quickbars/index"
  43. // import "tinymce/plugins/save/index"
  44. // import "tinymce/plugins/searchreplace/index"
  45. // import "tinymce/plugins/table/index"
  46. // import "tinymce/plugins/template/index"
  47. // import "tinymce/plugins/visualblocks/index"
  48. // import "tinymce/plugins/visualchars/index"
  49. // import "tinymce/plugins/wordcount/index"
  50. import load from './dynamicLoadScript'
  51. import FileUpload from "@/components/FileUpload/index.vue";
  52. // window.globalVariables.apis = 'http://localhost:3001/'
  53. const baseURL = window.globalVariables.origin + '/tinymce'
  54. const tinymceCDN = baseURL + '/tinymce-all-in-one.js'
  55. import request from '@/utils/request';
  56. export default {
  57. name: 'Tinymce',
  58. components: {
  59. editorImage,
  60. FileUpload
  61. },
  62. props: {
  63. id: {
  64. type: String,
  65. default: function () {
  66. return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
  67. }
  68. },
  69. value: {
  70. type: String,
  71. default: ''
  72. },
  73. toolbar: {
  74. type: Array,
  75. required: false,
  76. default() {
  77. return []
  78. }
  79. },
  80. menubar: {
  81. type: String,
  82. default: 'file edit insert view format table'
  83. },
  84. height: {
  85. type: [Number, String],
  86. required: false,
  87. default: 360
  88. },
  89. width: {
  90. type: [Number, String],
  91. required: false,
  92. default: 'auto'
  93. }
  94. },
  95. data() {
  96. return {
  97. hasChange: false,
  98. hasInit: false,
  99. tinymceId: this.id,
  100. fullscreen: false,
  101. languageTypeList: {
  102. 'zh': 'zh_CN',
  103. // 'en': 'en',
  104. // 'es': 'es_MX',
  105. // 'ja': 'ja'
  106. }
  107. }
  108. },
  109. computed: {
  110. containerWidth() {
  111. const width = this.width
  112. if (/^[\d]+(\.[\d]+)?$/.test(width)) { // matches `100`, `'100'`
  113. return `${width}px`
  114. }
  115. return width
  116. }
  117. },
  118. watch: {
  119. value(val) {
  120. if (!this.hasChange && this.hasInit) {
  121. this.$nextTick(() =>
  122. window.tinymce.get(this.tinymceId).setContent(val || ''))
  123. }
  124. }
  125. },
  126. mounted() {
  127. this.init()
  128. },
  129. activated() {
  130. if (window.tinymce) {
  131. this.initTinymce()
  132. }
  133. },
  134. deactivated() {
  135. this.destroyTinymce()
  136. },
  137. destroyed() {
  138. this.destroyTinymce()
  139. },
  140. methods: {
  141. init() {
  142. // dynamic load tinymce from cdn
  143. load(tinymceCDN, (err) => {
  144. if (err) {
  145. this.$message.error(err.message)
  146. return
  147. }
  148. this.initTinymce()
  149. window.tinymce.baseURL = baseURL
  150. })
  151. },
  152. initTinymce() {
  153. const _this = this
  154. window.tinymce.init({
  155. selector: `#${this.tinymceId}`,
  156. language: this.languageTypeList['zh'],
  157. height: this.height,
  158. body_class: 'panel-body ',
  159. object_resizing: false,
  160. toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
  161. menubar: this.menubar,
  162. plugins: plugins,
  163. end_container_on_empty_block: true,
  164. powerpaste_word_import: 'clean',
  165. code_dialog_height: 450,
  166. code_dialog_width: 1000,
  167. advlist_bullet_styles: 'square',
  168. advlist_number_styles: 'default',
  169. // imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
  170. default_link_target: '_blank',
  171. link_title: false,
  172. nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
  173. init_instance_callback: editor => {
  174. if (_this.value) {
  175. editor.setContent(_this.value)
  176. }
  177. _this.hasInit = true
  178. editor.on('NodeChange Change KeyUp SetContent', () => {
  179. this.hasChange = true
  180. this.$emit('input', editor.getContent())
  181. })
  182. },
  183. setup(editor) {
  184. editor.on('FullscreenStateChanged', (e) => {
  185. _this.fullscreen = e.state
  186. })
  187. },
  188. // it will try to keep these URLs intact
  189. // https://www.tiny.cloud/docs-3x/reference/configuration/Configuration3x@convert_urls/
  190. // https://stackoverflow.com/questions/5196205/disable-tinymce-absolute-to-relative-url-conversions
  191. convert_urls: false,
  192. // 整合七牛上传
  193. // images_dataimg_filter(img) {
  194. // setTimeout(() => {
  195. // const $image = $(img);
  196. // $image.removeAttr('width');
  197. // $image.removeAttr('height');
  198. // if ($image[0].height && $image[0].width) {
  199. // $image.attr('data-wscntype', 'image');
  200. // $image.attr('data-wscnh', $image[0].height);
  201. // $image.attr('data-wscnw', $image[0].width);
  202. // $image.addClass('wscnph');
  203. // }
  204. // }, 0);
  205. // return img
  206. // },
  207. // images_upload_handler(blobInfo, success, failure, progress) {
  208. // progress(0);
  209. // const token = _this.$store.getters.token;
  210. // getToken(token).then(response => {
  211. // const url = response.data.qiniu_url;
  212. // const formData = new FormData();
  213. // formData.append('token', response.data.qiniu_token);
  214. // formData.append('key', response.data.qiniu_key);
  215. // formData.append('file', blobInfo.blob(), url);
  216. // upload(formData).then(() => {
  217. // success(url);
  218. // progress(100);
  219. // })
  220. // }).catch(err => {
  221. // failure('出现未知问题,刷新页面,或者联系程序员')
  222. // console.log(err);
  223. // });
  224. // },
  225. images_upload_handler: (blobInfo, success, failure, progress) => {
  226. const file = blobInfo.blob();
  227. return request({
  228. url: 'upload/main/file',
  229. timeout: 1000 * 60 * 2,
  230. transformRequest: [function (data, headers) {
  231. const formData = new FormData()
  232. Object.keys(data).forEach(key => formData.append(key, data[key]))
  233. return formData;
  234. }],
  235. data: {
  236. filedata: file
  237. },
  238. timeout: 1000 * 60,
  239. onUploadProgress(progressEvent) {
  240. progress(~~(progressEvent.loaded / progressEvent.total * 100 | 0))
  241. },
  242. }).then(res => {
  243. if (res.code === '1') {
  244. return success(window.globalVariables.oss + '/' + (res.data.url))
  245. } else {
  246. return failure('出现未知问题,刷新页面')
  247. }
  248. })
  249. },
  250. })
  251. },
  252. destroyTinymce() {
  253. const tinymce = window.tinymce.get(this.tinymceId)
  254. if (this.fullscreen) {
  255. tinymce.execCommand('mceFullScreen')
  256. }
  257. if (tinymce) {
  258. tinymce.destroy()
  259. }
  260. },
  261. setContent(value) {
  262. window.tinymce.get(this.tinymceId).setContent(value)
  263. },
  264. getContent() {
  265. window.tinymce.get(this.tinymceId).getContent()
  266. },
  267. imageSuccessCBK(arr) {
  268. console.log('arr :>> ', arr);
  269. arr.forEach(v => window.tinymce.get(this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" />`))
  270. }
  271. }
  272. }
  273. </script>
  274. <style lang="scss" scoped>
  275. .tinymce-container {
  276. position: relative;
  277. line-height: normal;
  278. }
  279. .tinymce-container {
  280. ::v-deep {
  281. .mce-fullscreen {
  282. z-index: 10000;
  283. }
  284. }
  285. }
  286. .tinymce-textarea {
  287. visibility: hidden;
  288. z-index: -1;
  289. }
  290. .editor-custom-btn-container {
  291. position: absolute;
  292. right: 4px;
  293. top: 4px;
  294. /*z-index: 2005;*/
  295. }
  296. .fullscreen .editor-custom-btn-container {
  297. z-index: 10000;
  298. position: fixed;
  299. }
  300. .editor-upload-btn {
  301. display: inline-block;
  302. }
  303. </style>