当前位置:   article > 正文

tinymce富文本编辑器(vue)_tinymce-vue

tinymce-vue

TinyMC编辑器简介

TinyMCE是一款易用、且功能强大的所见即所得的富文本编辑器。跟其他富文本编辑器相比,有着丰富的插件,支持多种语言,能够满足日常的业务需求并且免费。

TinyMCE的优势:

开源可商用,基于LGPL2.1
插件丰富,自带插件基本涵盖日常所需功能
接口丰富,可扩展性强,有能力可以无限拓展功能
界面好看,符合现代审美
提供经典、内联、沉浸无干扰三种模式(详见“介绍与入门”)
对标准支持优秀(自v5开始)
多语言支持,官网可下载几十种语言。
下图为开启全部功能的截图

TinyMCE中文文档地址:TinyMCE中文文档中文手册

1、安装

vue-cli版本:3.x+

安装tinymce

npm install tinymce -S

安装tinymce-vue

npm install @tinymce/tinymce-vue -S

这两个组件安装完之后,在public目录下新建文件夹static/tinymce,目录建好后,找到node_modules文件夹下的tinymce/skins目录,将skins目录复制到我们创建的static/tinymce文件夹内,如下图所示​

​​

 vue-cli版本:2.x

安装tinymce

npm install tinymce -S

安装tinymce-vue

npm install @tinymce/tinymce-vue@3.0.1 -S 

安装之后,在 node_modules 中找到 tinymce/skins 目录,然后将 skins 目录拷贝到 static 目录下

注意: 如果是使用 vue-cli 3.x 构建的 typescript 项目,就放到 public 目录下,文中所有 static 目录相关都这样处理

tinymce 默认是英文界面,所以还需要下载一个中文语言包

然后将这个语言包放到 static 目录下,为了结构清晰,我包了一层 tinymce 目录

这个是vue-cli2项目的放法

这个是vue-cli3项目的放法

2、配置中文语言

到官网下载中文语言包 zh_CN.js

在刚才创建的static/tinymce文件夹内再新建langs文件夹,用来存放我们下载的中文语言包,如下图所示

 vue-cli2.x 同理

​​3.组件

  1. <template>
  2. <!-- 富文本 -->
  3. <div>
  4. <editor v-model="content" :init="init" :disabled="disabled"></editor>
  5. </div>
  6. </template>
  7. <script>
  8. import tinymce from "tinymce/tinymce";
  9. import Editor from "@tinymce/tinymce-vue";
  10. import "tinymce/icons/default/icons";
  11. import "tinymce/themes/silver";
  12. import "tinymce/plugins/image";
  13. import "tinymce/plugins/media";
  14. import "tinymce/plugins/table";
  15. import "tinymce/plugins/lists";
  16. import "tinymce/plugins/contextmenu";
  17. import "tinymce/plugins/wordcount";
  18. import "tinymce/plugins/colorpicker";
  19. import "tinymce/plugins/textcolor";
  20. import "tinymce/plugins/preview";
  21. import "tinymce/plugins/code";
  22. import "tinymce/plugins/link";
  23. import "tinymce/plugins/advlist";
  24. import "tinymce/plugins/codesample";
  25. import "tinymce/plugins/hr";
  26. import "tinymce/plugins/fullscreen";
  27. import "tinymce/plugins/textpattern";
  28. import "tinymce/plugins/searchreplace";
  29. import "tinymce/plugins/autolink";
  30. import "tinymce/plugins/directionality";
  31. import "tinymce/plugins/visualblocks";
  32. import "tinymce/plugins/visualchars";
  33. import "tinymce/plugins/template";
  34. import "tinymce/plugins/charmap";
  35. import "tinymce/plugins/nonbreaking";
  36. import "tinymce/plugins/insertdatetime";
  37. import "tinymce/plugins/imagetools";
  38. import "tinymce/plugins/autosave";
  39. import "tinymce/plugins/autoresize";
  40. export default {
  41. components: {
  42. Editor
  43. },
  44. props: {
  45. value: {
  46. type: String,
  47. default: ""
  48. },
  49. disabled: {
  50. type: Boolean,
  51. default: false
  52. },
  53. plugins: {
  54. type: [String, Array],
  55. default:
  56. "preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap hr nonbreaking insertdatetime advlist lists wordcount imagetools textpattern autosave autoresize"
  57. },
  58. toolbar: {
  59. type: [String, Array],
  60. default:
  61. "code undo redo restoredraft | cut copy paste pastetext | forecolor backcolor bold italic underline strikethrough link codesample | alignleft aligncenter alignright alignjustify outdent indent formatpainter | \
  62. styleselect formatselect fontselect fontsizeselect | bullist numlist | blockquote subscript superscript removeformat | \
  63. table image media charmap hr pagebreak insertdatetime | fullscreen preview"
  64. }
  65. },
  66. data() {
  67. return {
  68. //初始化配置
  69. init: {
  70. //menubar: true, // 菜单栏显隐
  71. language_url: "/static/tinymce/langs/zh_CN.js",
  72. //language_url: '../../static/tinymce/langs/zh_CN.js', // vue-cli2.x
  73. language: "zh_CN",
  74. skin_url: "/static/tinymce/skins/ui/oxide",
  75. //skin_url: '../../static/tinymce/skins/ui/oxide', // vue-cli2.x
  76. //content_css: '../../static/tinymce/skins/content/default/content.css',// vue-cli2.x
  77. height: 770,
  78. min_height: 770,
  79. max_height: 770,
  80. toolbar_mode: "wrap",
  81. plugins: this.plugins,
  82. toolbar: this.toolbar,
  83. content_style: "p {margin: 5px 0;}",
  84. fontsize_formats: "12px 14px 16px 18px 24px 36px 48px 56px 72px",
  85. font_formats:
  86. "微软雅黑=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;",
  87. branding: false,
  88. // 图片上传
  89. images_upload_handler: (blobInfo, success, failure) => {
  90. // const img = 'data:image/jpeg;base64,' + blobInfo.base64()
  91. // success(img)
  92. const formData = new FormData()
  93. formData.append('file', blobInfo.blob())
  94. reserveTableFoodDescribe(formData).then(res => {
  95. if (res.code === '10000') {
  96. const file = res.data
  97. success(file.url)
  98. return
  99. }
  100. failure('上传失败')
  101. }).catch(() => {
  102. failure('上传出错')
  103. })
  104. }
  105. },
  106. content: this.value
  107. };
  108. },
  109. mounted() {
  110. tinymce.init({});
  111. },
  112. methods: {
  113. },
  114. watch: {
  115. value(newValue) {
  116. this.content = newValue;
  117. },
  118. content(newValue) {
  119. this.$emit("input", newValue);
  120. }
  121. }
  122. };
  123. </script>
  124. <style scoped lang="scss">
  125. </style>

4.组件使用

  1. import Editor from "@/components/imcoder-tinymce";
  2. components: { Editor },
  3. <editor v-model="yourContent"></editor>

5.配置工具项

TinyMCE菜单配置详解

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

闽ICP备14008679号