p-editor-tiny.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div class="tinymce-editor">
  3. <editor v-model="myValue" ref="editor" :init="init" :disabled="disabled" @onClick="onClick">
  4. </editor>
  5. </div>
  6. </template>
  7. <script>
  8. import tinymce from 'tinymce/tinymce'
  9. import 'tinymce/icons/default/icons.min.js'
  10. import 'tinymce/themes/silver'
  11. import 'tinymce/plugins/image' // 插入上传图片插件
  12. import 'tinymce/plugins/media' // 插入视频插件
  13. import 'tinymce/plugins/table' // 插入表格插件
  14. import 'tinymce/plugins/lists' // 列表插件
  15. import 'tinymce/plugins/wordcount' // 字数统计插件
  16. import 'tinymce/plugins/colorpicker'
  17. import 'tinymce/plugins/textcolor'
  18. export default {
  19. name:'p-editor',
  20. components: {
  21. Editor:() => import(/* webpackChunkName: "editor" */ '@tinymce/tinymce-vue')
  22. },
  23. props: {
  24. value: {
  25. type: String,
  26. default: ''
  27. },
  28. disabled: {
  29. type: Boolean,
  30. default: false
  31. },
  32. plugins: {
  33. type: [String, Array],
  34. default: 'lists image media table wordcount'
  35. },
  36. toolbar: {
  37. type: [String, Array],
  38. default: 'undo redo | formatselect | bold italic underline forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat'
  39. },
  40. imgType:{
  41. type:String,
  42. default:'base64'
  43. },
  44. height:{
  45. type:String,
  46. default(){
  47. return '100%'
  48. }
  49. }
  50. },
  51. data() {
  52. let _this = this;
  53. return {
  54. init: {
  55. language_url: 'tinymce/zh_CN.js',
  56. language: 'zh_CN',
  57. skin_url: 'tinymce/skins/ui/oxide',
  58. // skin_url: 'tinymce/skins/ui/oxide-dark',//暗色系
  59. height: this.height,
  60. plugins: this.plugins,
  61. toolbar: this.toolbar,
  62. branding: false,
  63. menubar: true,
  64. // 此处为图片上传处理函数
  65. images_upload_handler: (blobInfo, success) => {
  66. if(_this.imgType == 'upload'){
  67. let file = blobInfo.blob();
  68. if(file instanceof File){
  69. let f = new FormData();
  70. f.append('file',file);
  71. _this.$http({
  72. url:'/file/upload',
  73. method:'post',
  74. headers:{
  75. "Content-Type":'multipart/form-data'
  76. },
  77. data:f,
  78. }).then(res => {
  79. if(res.data.code == 200){
  80. console.log(res,res.data.data.url,success)
  81. // _this.formData.filePath = res.data.link;
  82. success(res.data.data.url)
  83. }
  84. }).catch(err => {
  85. throw(err);
  86. })
  87. }
  88. }else{
  89. let file = blobInfo.blob();
  90. const img = `data:${file.type};base64,` + blobInfo.base64()
  91. success(img)
  92. }
  93. }
  94. },
  95. myValue: this.value
  96. }
  97. },
  98. mounted() {
  99. tinymce.init({})
  100. },
  101. methods: {
  102. // 添加相关的事件,可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
  103. // 需要什么事件可以自己增加
  104. onClick(e) {
  105. this.$emit('onClick', e, tinymce)
  106. },
  107. // 可以添加一些自己的自定义事件,如清空内容
  108. clear() {
  109. this.value = ''
  110. }
  111. },
  112. watch: {
  113. value(newValue) {
  114. this.myValue = newValue
  115. },
  116. myValue(newValue) {
  117. this.$emit('input', newValue)
  118. }
  119. }
  120. }
  121. </script>
  122. <style>
  123. .tinymce-editor{
  124. height: 300px;
  125. }
  126. </style>