• VUE~富文本简单使用~tinymce


    引:

    基于Vue CLI 3脚手架搭建的项目整合tinymce 5富文本编辑器,vue cli 2版本及tinymce 4版本参考:https://blog.csdn.net/liub37/article/details/83310879
    做了些小修改,详情见github

     Github:https://github.com/liub1934/vue-use-tinymce

    示例:开发环境是vue2

    1、下载tinymce(富文本编辑器)

    npm install  tinymce@5.6.2  

    2、下载vue封装的控件

    npm install  @tinymce/tinymce-vue@3.2.0 

    3、下载中文语言包

    tinymce插件地址

     4、封装组件

    不进行封装也可以,按照官网上来就好。

    1. <template>
    2. <div class="tinymce-editor" v-if="editversion">
    3. <Editor v-model="editorValue" :init="editorInit" :disabled="disabled" @onClick="handleClick"/>
    4. </div>
    5. </template>
    6. <script>
    7. // 引入组件
    8. import tinymce from 'tinymce/tinymce'
    9. import Editor from '@tinymce/tinymce-vue'
    10. // 引入富文本编辑器主题的js和css
    11. import 'tinymce/themes/silver/theme.min.js'
    12. import 'tinymce/icons/default/icons.js'
    13. // 扩展插件
    14. import 'tinymce/plugins/image'
    15. import 'tinymce/plugins/link'
    16. import 'tinymce/plugins/code'
    17. import 'tinymce/plugins/table'
    18. import 'tinymce/plugins/lists'
    19. import 'tinymce/plugins/wordcount' // 字数统计插件
    20. import 'tinymce/plugins/media'
    21. import '@/assets/langs/zh_CN.js'//下载后的语言包的文职
    22. import { upload } from '@/http/upload'//上传文件方法
    23. export default {
    24. name: 'TinymceWmc',
    25. components: { Editor },
    26. props: {
    27. id: {
    28. type: String,
    29. default: 'tinymceEditor'
    30. },
    31. value: {
    32. type: String,
    33. default: ''
    34. },
    35. disabled: {
    36. type: Boolean,
    37. default: false
    38. },
    39. plugins: {
    40. type: [String, Array],
    41. default: 'link lists image code table wordcount media'
    42. },
    43. toolbar: {
    44. type: [String, Array],
    45. default: 'bold italic underline strikethrough | fontsizeselect | forecolor backcolor'
    46. }
    47. },
    48. data() {
    49. return {
    50. //判断组件是否已经加载过,如果加载过加不在渲染,如果重复加载渲染会导致,富文本不能使用
    51. editversion: false,
    52. editorInit: {
    53. language: 'zh_CN',
    54. skin_url: '/tinymce/skins/ui/oxide',
    55. height: 300,
    56. plugins: this.plugins,
    57. toolbar: this.toolbar,
    58. statusbar: true, // 底部的状态栏
    59. menubar: true, // 最上方的菜单
    60. branding: false, // 水印“Powered by TinyMCE”
    61. images_upload_handler: (blobInfo, success, failure) => {
    62. const isJPG = blobInfo.blob().type === 'image/jpeg' || blobInfo.blob().type === 'image/png' || blobInfo.blob().type === 'image/gif'
    63. const isLt2M = blobInfo.blob().size / 1024 / 1024 < 2
    64. if (!isJPG) {
    65. failure('上传头像封面只能是JPG、PNG、GIF 格式!')
    66. }
    67. if (!isLt2M) {
    68. failure('上传头像图片大小不能超过 2MB!')
    69. }
    70. const formData = new FormData()
    71. formData.set('file', blobInfo.blob())
    72. upload(formData).then(data => {
    73. console.log("富文本文件",data)
    74. if (data && data.code == 2000) {
    75. success(data.data.url)
    76. } else {
    77. failure(data.msg)
    78. },err=>{
    79. console.log("err富文本文件",err)
    80. })
    81. },
    82. file_picker_types: 'media',
    83. file_picker_callback: (callback, value, meta) => {
    84. if (meta.filetype == 'media') {
    85. const input = document.createElement('input') // 创建一个隐藏的input
    86. input.setAttribute('type', 'file')
    87. const that = this
    88. input.onchange = function() {
    89. const file = this.files[0] // 选取第一个文件
    90. that.uploadImg(file, 'video') // 上传视频拿到url
    91. if (that.uploaded) {
    92. callback(that.resVideo, { title: file.name }) // 将url显示在弹框输入框中
    93. } else {
    94. setTimeout(() => {
    95. callback(that.resVideo, { title: file.name })
    96. }, 2000)
    97. }
    98. }
    99. // 触发点击
    100. input.click()
    101. }
    102. },
    103. media_url_resolver: function(data, resolve) {
    104. try {
    105. const videoUri = encodeURI(data.url)
    106. const embedHtml = `<p>
    107. <span
    108. class="mce-object mce-object-video"
    109. data-mce-selected="1"
    110. data-mce-object="video"
    111. data-mce-p-width="100%"
    112. data-mce-p-height="auto"
    113. data-mce-p-controls="controls"
    114. data-mce-p-controlslist="nodownload"
    115. data-mce-p-allowfullscreen="true"
    116. data-mce-p-src=${videoUri} >
    117. <video src=${data.url} width="100%" height="auto" controls="controls" controlslist="nodownload">
    118. </video>
    119. </span>
    120. </p>
    121. <p style="text-align: left;"></p>`
    122. resolve({ html: embedHtml })
    123. } catch (e) {
    124. resolve({ html: '' })
    125. }
    126. }
    127. },
    128. editorId: this.id,
    129. editorValue: this.value,
    130. resVideo: null, // 返回的视频url
    131. uploaded: false // 有没有上传完成
    132. }
    133. },
    134. watch: {
    135. value(newValue) {
    136. console.log('bbbbbbbbbbbbbbbbbbbbb', newValue)
    137. this.editorValue = newValue
    138. },
    139. editorValue(newValue) {
    140. this.$emit('changeValue', newValue)
    141. }
    142. },
    143. mounted() {
    144. tinymce.init({})
    145. this.$nextTick(() => {
    146. this.editversion = true
    147. })
    148. },
    149. methods: {
    150. // https://github.com/tinymce/tinymce-vue => All available events
    151. handleClick(e) {
    152. this.$emit('onClick', e, tinymce)
    153. },
    154. clear() {
    155. this.editorValue = ''
    156. },
    157. // 上传视频拿到url
    158. uploadImg(file) {
    159. const content = file
    160. const formData = new FormData()
    161. formData.append('file', content)
    162. upload(formData).then(({ data }) => {
    163. if (data && data.code == 2000) {
    164. // success(data.data.url);
    165. this.resVideo = data.data.url
    166. this.uploaded = true
    167. }
    168. })
    169. }
    170. }
    171. }
    172. </script>
    173. <style>
    174. .tox-silver-sink {
    175. z-index: 13000 !important;
    176. }
    177. </style>

    5、使用组件

    1. <template>
    2. <div>
    3. <TinymceEditor
    4. :value="formData.content"
    5. @changeValue="changeValue" />
    6. </div>
    7. </template>
    8. <script>
    9. export default {
    10. methods: {
    11. //富文本
    12. changeValue(value) {
    13. this.formData.content= value
    14. },
    15. }
    16. }
    17. </script>

    官网https://www.tiny.cloud/docs/

  • 相关阅读:
    C++17 新特性
    卷积神经网络基础
    7.1.3 Selenium的用法2
    代码随想录训练营二刷第二十七天 | 39. 组合总和 40.组合总和II 131.分割回文串
    java-php-net-python-图书馆选择计算机毕业设计程序
    Jmeter 监听器 配置
    JenkinsNote-服务迁移
    STM32入门100步
    学习自定义SpringBoot Starter组件 (超详细的图文教程,从理论到实战)
    java-php-python-知道特产网计算机毕业设计
  • 原文地址:https://blog.csdn.net/weixin_41620505/article/details/125516176