当前位置:   article > 正文

若依前后端分离版本使用tinymce富文本编辑器_若依富文本编辑器组件

若依富文本编辑器组件

tinymce中文文档地址:TinyMCE中文文档中文手册 (ax-z.cn)

tinymce官网地址:API Reference | Docs | TinyMCE

安装及使用

我项目里是vue2,tinymce使用的是5.10.3版本。

1.安装tinymce。我这里指定版本安装。
  1. npm install @tinymce/tinymce-vue@3.0.1 -S
  2. npm install tinymce@5.10.3 -S
2.新建一个TinyEditor.vue组件,配置功能及语言包。

将node-modules中的tinymce代码复制到public目录下plugin下的文件可以都删了。

下载中文包语言包 |值得信赖的富文本编辑器 |微小的MCE (tiny.cloud)

将下载好的中文包解压后放到public下,在init引用语言包时,注意地址。

  1. <template>
  2. <div className="tinymce-box">
  3. <Editor v-model="contentValue" :init="init" :disabled="disabled" @onClick="onClick"/>
  4. </div>
  5. </template>
  6. <script>
  7. //引入tinymce编辑器
  8. import Editor from '@tinymce/tinymce-vue'
  9. //下面这三行解决tinymce编辑器变成只读模式
  10. import tinymce from 'tinymce/tinymce'
  11. import 'tinymce/themes/silver/theme'
  12. import 'tinymce/icons/default/icons'
  13. export default {
  14. name: 'TEditor',
  15. components: {
  16. Editor
  17. },
  18. props: {
  19. value: {
  20. type: String,
  21. default: ''
  22. },
  23. disabled: {
  24. type: Boolean,
  25. default: false
  26. },
  27. },
  28. data() {
  29. return {
  30. init: {
  31. // 配置信息
  32. selector: "#textarea",
  33. height: 400,
  34. language_url: "/tinymce/langs/zh-Hans.js", // 中文包的存放路径
  35. language: "zh-Hans",
  36. toolbar_mode: "none",
  37. plugins: "print preview searchreplace autolink directionality visualblocks visualchars fullscreen image imagetools link template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount textpattern autosave kityformula-editor",
  38. toolbar: "fullscreen undo redo restoredraft | cut copy paste pastetext | forecolor backcolor bold italic underline strikethrough link anchor | alignleft aligncenter alignright alignjustify outdent indent | \\\n" +
  39. " styleselect formatselect fontselect fontsizeselect | bullist numlist | blockquote subscript superscript removeformat | \\\n" +
  40. " table image resize charmap hr pagebreak insertdatetime print preview | code selectall searchreplace visualblocks | indent2em lineheight formatpainter axupimgs | kityformula-editor",
  41. skin_url: '/tinymce/skins/ui/oxide', //皮肤:浅色
  42. autosave_ask_before_unload:false, //去掉网页关闭时的弹窗
  43. //字体
  44. font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;', //字体样式
  45. // image_advtab: true,
  46. // a11y_advanced_options: true,
  47. //引入公式
  48. external_plugins: {
  49. 'kityformula-editor': '/tinymce/plugins/kityformula-editor/plugin.min.js',
  50. },
  51. //图片上传按钮显示
  52. file_picker_types: 'image',
  53. image_caption: true,
  54. // 图片上传(下面两个都是图片上传方法,根据需要使用过一个即可)
  55. images_upload_handler: (blobInfo, success, failure) => { //此选项允许你使用自定义函数代替TinyMCE来处理上传操作。该自定义函数可接受四个参数:如果未使用此配置,TinyMCE将使用ajax每次上传一个图片,并在成功返回结果后调用成功回调函数。
  56. const img = 'data:image/jpeg;base64,' + blobInfo.base64()
  57. success(img)
  58. console.log(success)
  59. console.log(failure)
  60. const formData = new FormData()
  61. formData.append('file', blobInfo.blob())
  62. },
  63. file_picker_callback: function (cb, value, meta) {
  64. var input = document.createElement('input');
  65. input.setAttribute('type', 'file');
  66. input.setAttribute('accept', 'image/*');
  67. /*
  68. Note: In modern browsers input[type="file"] is functional without
  69. even adding it to the DOM, but that might not be the case in some older
  70. or quirky browsers like IE, so you might want to add it to the DOM
  71. just in case, and visually hide it. And do not forget do remove it
  72. once you do not need it anymore.
  73. */
  74. input.onchange = function () {
  75. var file = this.files[0];
  76. var reader = new FileReader();
  77. reader.onload = function () {
  78. /*
  79. Note: Now we need to register the blob in TinyMCEs image blob
  80. registry. In the next release this part hopefully won't be
  81. necessary, as we are looking to handle it internally.
  82. */
  83. var id = 'blobid' + (new Date()).getTime();
  84. var blobCache = tinymce.activeEditor.editorUpload.blobCache;
  85. var base64 = reader.result.split(',')[1];
  86. var blobInfo = blobCache.create(id, file, base64);
  87. blobCache.add(blobInfo);
  88. /* call the callback and populate the Title field with the file name */
  89. cb(blobInfo.blobUri(), {title: file.name});
  90. };
  91. reader.readAsDataURL(file);
  92. };
  93. input.click();
  94. },
  95. statusbar: false,//直接设置为false 去掉编辑器下方的广告
  96. },
  97. contentValue: this.value
  98. }
  99. },
  100. watch: {
  101. value(newValue) {
  102. this.contentValue = newValue
  103. },
  104. contentValue(newValue) {
  105. this.$emit('input', newValue)
  106. },
  107. },
  108. methods: {
  109. // 添加相关的事件,可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
  110. onClick(e){
  111. this.$emit('onClick', e, tinymce)
  112. },
  113. //清空内容
  114. clear() {
  115. this.contentValue = ''
  116. },
  117. },
  118. }
  119. </script>
  120. <style lang="scss">
  121. figure.image {
  122. display: inline-block;
  123. border: 1px solid gray;
  124. margin: 0 2px 0 1px;
  125. background: #f5f2f0;
  126. }
  127. figure.align-left {
  128. float: left;
  129. }
  130. figure.align-right {
  131. float: right;
  132. }
  133. figure.image img {
  134. margin: 8px 8px 0 8px;
  135. }
  136. figure.image figcaption {
  137. margin: 6px 8px 6px 8px;
  138. text-align: center;
  139. }
  140. </style>
3.在main.js里全局引入封装的组件。
  1. // tinymce富文本编辑器
  2. import TinyEditor from '@/views/components/TinymceEditor.vue'
  3. Vue.component('TinyEditor',TinyEditor)
4.在需要的界面中,直接使用<TinyEditor>标签使用即可。

例如:

<TinyEditor ref="tinymce"  v-model="item.answer" ></TinyEditor>

问题及解决:

问题1:如果富文本编辑器放在了el-dialog弹窗中,会导致在弹窗中点击编辑器功能按钮,点击无反应。
原因:tinymce的功能按钮弹窗的z-index低于el-dialog。

解决:将tinymce中的skin.min.css中的z-index后的数值添加4个0,如果值是-1,不需要改。

改完如果还不好使,将这个文件复制到node-modules中的tinymce的skin.min.css文件替换。

问题2:添加公式按钮

解决:kityformula-editor 数学公式 | TinyMCE中文文档中文手册 (ax-z.cn)到这个网址下载公式插件包,然后放到public下的tinymce下的plugins里。然后在自己第二步封装的组件TinyEditor.vue中的init中添加如下代码:plugins和toolbar里添加该功能按钮,external_plugins来引用这个插件。

  1. plugins: "kityformula-editor",
  2. toolbar: "kityformula-editor",
  3. external_plugins: {
  4. 'kityformula-editor': '/tinymce/plugins/kityformula-editor/plugin.min.js',
  5. },

然后修改公式插件中的plugin.js和plugin.min.js中的baseURL地址(我这里使用的本地环境,所以直接写死了本地的localhost)。

实现效果:

问题3:去掉富文本编辑器下的广告。

解决:在tinymce的init中添加如下代码即可。

statusbar: false,//直接设置为false  去掉编辑器下方的广告

效果:

问题4:富文本编辑器界面可能会出现如下弹窗,提示申请key。

你如果不使用付费的插件,可以忽略这个问题,通过css样式隐藏掉即可。

解决:在public下的index.html中加入css样式隐藏掉。

问题5:图片无法拖拽四角拉伸改变大小。

(这个问题百度不到答案,我在使用的时候也找了半天,才发现是其他引用的问题)

解决:图片拉伸是tinymce自带的功能,如果无法拉伸图片,请确认封装的TinyEditor.vue中是否只有这个引入

删除其他没用的引用,例如:

问题6:界面控制台出现expected expression,got '<'  错误

解决:检查报错后面的js路径是否使用正确。贴图这个问题指的是语言包路径错误。

最后,如有其他问题请参考这位大佬的文章:富文本编辑器小技巧大杂烩之TinyMce_tinymce去除边框-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/733866
推荐阅读
相关标签
  

闽ICP备14008679号