index.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <textarea :id="tinymceId" style="visibility: hidden" />
  3. </template>
  4. <script>
  5. import loadTinymce from '@/utils/loadTinymce'
  6. import { plugins, toolbar } from './config'
  7. import { debounce } from 'throttle-debounce'
  8. let num = 1
  9. export default {
  10. props: {
  11. id: {
  12. type: String,
  13. default: () => {
  14. num === 10000 && (num = 1)
  15. return `tinymce${+new Date()}${num++}`
  16. }
  17. },
  18. value: {
  19. default: ''
  20. }
  21. },
  22. data() {
  23. return {
  24. tinymceId: this.id
  25. }
  26. },
  27. mounted() {
  28. loadTinymce(tinymce => {
  29. // eslint-disable-next-line global-require
  30. require('./zh_CN')
  31. let conf = {
  32. selector: `#${this.tinymceId}`,
  33. language: 'zh_CN',
  34. menubar: 'file edit insert view format table',
  35. plugins,
  36. toolbar,
  37. height: 300,
  38. branding: false,
  39. object_resizing: false,
  40. end_container_on_empty_block: true,
  41. powerpaste_word_import: 'clean',
  42. code_dialog_height: 450,
  43. code_dialog_width: 1000,
  44. advlist_bullet_styles: 'square',
  45. advlist_number_styles: 'default',
  46. default_link_target: '_blank',
  47. link_title: false,
  48. nonbreaking_force_tab: true
  49. }
  50. conf = Object.assign(conf, this.$attrs)
  51. conf.init_instance_callback = editor => {
  52. if (this.value) editor.setContent(this.value)
  53. this.vModel(editor)
  54. }
  55. tinymce.init(conf)
  56. })
  57. },
  58. destroyed() {
  59. this.destroyTinymce()
  60. },
  61. methods: {
  62. vModel(editor) {
  63. // 控制连续写入时setContent的触发频率
  64. const debounceSetContent = debounce(250, editor.setContent)
  65. this.$watch('value', (val, prevVal) => {
  66. if (editor && val !== prevVal && val !== editor.getContent()) {
  67. if (typeof val !== 'string') val = val.toString()
  68. debounceSetContent.call(editor, val)
  69. }
  70. })
  71. editor.on('change keyup undo redo', () => {
  72. this.$emit('input', editor.getContent())
  73. })
  74. },
  75. destroyTinymce() {
  76. if (!window.tinymce) return
  77. const tinymce = window.tinymce.get(this.tinymceId)
  78. if (tinymce) {
  79. tinymce.destroy()
  80. }
  81. }
  82. }
  83. }
  84. </script>