index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <textarea :id="tinymceId" style="visibility: hidden;"/>
  3. </template>
  4. <script>
  5. import loadTinymce from '@/utils/utilsJson/loadTinymce'
  6. import { plugins, toolbar } from './config'
  7. import { debounce } from 'throttle-debounce'
  8. import { getToken } from '../../utils/auth'
  9. let num = 1
  10. export default {
  11. props: {
  12. id: {
  13. type: String,
  14. default: () => {
  15. num === 10000 && (num = 1)
  16. return `tinymce${+new Date()}${num++}`
  17. }
  18. },
  19. value: {
  20. default: ''
  21. }
  22. },
  23. data() {
  24. return {
  25. tinymceId: this.id
  26. }
  27. },
  28. mounted() {
  29. loadTinymce((tinymce) => {
  30. // eslint-disable-next-line global-require
  31. require('./zh_CN')
  32. let conf = {
  33. selector: `#${this.tinymceId}`,
  34. language: 'zh_CN',
  35. // 字体列表
  36. font_formats:
  37. '微软雅黑=微软雅黑; 宋体=宋体; 黑体=黑体; 仿宋=仿宋; 楷体=楷体; 隶书=隶书; 幼圆=幼圆; Andale Mono=andale mono,times; Arial=arial,helvetica,sans-serif; Arial Black=arial black,avant garde; Book Antiqua=book antiqua,palatino; Comic Sans MS=comic sans ms,sans-serif; Courier New=courier new,courier; Georgia=georgia,palatino; Helvetica=helvetica; Impact=impact,chicago; Symbol=symbol; Tahoma=tahoma,arial,helvetica,sans-serif; Terminal=terminal,monaco; Times New Roman=times new roman,times; Trebuchet MS=trebuchet ms,geneva; Verdana=verdana,geneva; Webdings=webdings; Wingdings=wingdings,zapf dingbats',
  38. // 字体大小列表
  39. fontsize_formats: '12px 14px 16px 18px 24px 36px 48px',
  40. menubar: 'file edit insert view format table',
  41. plugins: plugins + ' image', // 添加 image 插件
  42. toolbar:toolbar ,
  43. height: 300,
  44. branding: false,
  45. object_resizing: false,
  46. end_container_on_empty_block: true,
  47. powerpaste_word_import: 'clean',
  48. code_dialog_height: 450,
  49. code_dialog_width: 1000,
  50. advlist_bullet_styles: 'square',
  51. advlist_number_styles: 'default',
  52. default_link_target: '_blank',
  53. link_title: false,
  54. nonbreaking_force_tab: true,
  55. // 图片上传配置
  56. images_upload_url: process.env.VUE_APP_BASE_API + '/common/upload',
  57. images_upload_handler: (blobInfo, success, failure) => {
  58. const formData = new FormData()
  59. formData.append('file', blobInfo.blob(), blobInfo.filename())
  60. fetch(process.env.VUE_APP_BASE_API + '/common/upload', {
  61. method: 'POST',
  62. headers: {
  63. Authorization: 'Bearer ' + getToken()
  64. },
  65. body: formData
  66. })
  67. .then((response) => {
  68. if (!response.ok) {
  69. throw new Error('Network response was not ok ' + response.statusText)
  70. }
  71. return response.json()
  72. })
  73. .then((data) => {
  74. success(data.url) // 假设服务器返回的 JSON 中包含图片的 URL
  75. })
  76. .catch((error) => {
  77. failure('上传失败: ' + error.message)
  78. })
  79. }
  80. }
  81. conf = Object.assign(conf, this.$attrs)
  82. conf.init_instance_callback = (editor) => {
  83. if (this.value) editor.setContent(this.value)
  84. this.vModel(editor)
  85. }
  86. tinymce.init(conf)
  87. })
  88. },
  89. destroyed() {
  90. this.destroyTinymce()
  91. },
  92. methods: {
  93. vModel(editor) {
  94. const debounceSetContent = debounce(250, editor.setContent)
  95. this.$watch('value', (val, prevVal) => {
  96. if (editor && val !== prevVal && val !== editor.getContent()) {
  97. if (typeof val !== 'string') val = val.toString()
  98. debounceSetContent.call(editor, val)
  99. }
  100. })
  101. editor.on('change keyup undo redo', () => {
  102. this.$emit('input', editor.getContent())
  103. })
  104. },
  105. destroyTinymce() {
  106. if (!window.tinymce) return
  107. const tinymce = window.tinymce.get(this.tinymceId)
  108. if (tinymce) {
  109. tinymce.destroy()
  110. }
  111. }
  112. }
  113. }
  114. </script>