vue2 利用 tinymce富文本编辑器

打印 上一主题 下一主题

主题 553|帖子 553|积分 1659

注意:
在vue2中利用tinymce有版本限制的,最新版都是支持v3的,官方也阐明了;
vue2中不能利用@tinymce/tinymce-vue 为4以上的版本;

利用步骤:

1、vue项目中安装 tinymce;
npm install tinymce@5.1.0 -S
npm install @tinymce/tinymce-vue@3.0.1 -S
2、将富文本编译器封装成组件;
在src/components目次下新建一个tinymce文件夹(每个文件的代码在文章末了提供)

3、将组件引入到页面中进行利用;
哪个页面利用富文本编译器 就在哪个页面引入组件;
  1. <tinymce
  2.   v-model="addContent"
  3.   @input="(info) => tinymceInput(info)"
  4.    id="tinymceId"
  5. ></tinymce>
  6. import tinymce from '@/components/tinymce/index.vue' // 注意引入路径
  7. components: { tinymce },
  8. data () {
  9.   return {
  10.     addContent: '',
  11.   }
  12. },
  13. methods: {
  14.         tinymceInput (info) {
  15.        console.log(info,'info------------');
  16.      },
  17. },
复制代码
注意:可能会遇到的题目
import “tinymce/icons/default” 路径找不到需要升级tinymce版本
解决方法:
终端执行:npm i tinymce -S
代码:

index.vue

  1. <template>
  2.   <div class="tinymce-editor">
  3.     <editor
  4.       :id="id"
  5.       v-model="myValue"
  6.       :init="init"
  7.       :disabled="disabled"
  8.       @onClick="onClick"
  9.     ></editor>
  10.   </div>
  11. </template>
  12. <script>
  13. // 引入组件
  14. import tinymce from 'tinymce'
  15. import editor from '@tinymce/tinymce-vue'
  16. // 引入富文本编辑器主题的js和css
  17. import 'tinymce/skins/content/default/content.css'
  18. import 'tinymce/themes/silver'
  19. import 'tinymce/icons/default/icons' // 解决了icons.js 报错问题,不用定制icon可以忽略
  20. // 编辑器扩展插件plugins
  21. import 'tinymce/plugins/paste' // 粘贴插件
  22. import 'tinymce/plugins/image' // 上传图片插件
  23. import 'tinymce/plugins/media'// 插入视频插件
  24. import 'tinymce/plugins/link' // 超链接
  25. import 'tinymce/plugins/code' // 源码
  26. import 'tinymce/plugins/table' // 插入表格插件
  27. import 'tinymce/plugins/lists' // 列表插件
  28. import 'tinymce/plugins/wordcount' // 字数统计插件
  29. import 'tinymce/plugins/preview' // 预览
  30. // import 'tinymce/plugins/table'
  31. import 'tinymce/plugins/lists'
  32. import 'tinymce/plugins/colorpicker'
  33. import 'tinymce/plugins/textcolor'
  34. import "tinymce/plugins/code";
  35. import "tinymce/plugins/link";
  36. import "tinymce/plugins/advlist";
  37. import "tinymce/plugins/anchor";
  38. import "tinymce/plugins/codesample";
  39. import "tinymce/plugins/hr";
  40. import "tinymce/plugins/fullscreen";
  41. import "tinymce/plugins/textpattern";
  42. import "tinymce/plugins/searchreplace";
  43. import "tinymce/plugins/autolink";
  44. import "tinymce/plugins/directionality";
  45. import "tinymce/plugins/visualblocks";
  46. import "tinymce/plugins/visualchars";
  47. import "tinymce/plugins/template";
  48. import "tinymce/plugins/charmap";
  49. import "tinymce/plugins/nonbreaking";
  50. import "tinymce/plugins/insertdatetime";
  51. import "tinymce/plugins/autoresize";
  52. import "tinymce/plugins/pagebreak";
  53. import "tinymce/plugins/print";
  54. import "tinymce/plugins/save";
  55. import "tinymce/plugins/tabfocus";
  56. // import 'tinymce/plugins/contextmenu'
  57. // import "tinymce/plugins/imagetools";
  58. // import "tinymce/plugins/autosave";
  59. // import "tinymce/plugins/emoticons";
  60. // import "tinymce/plugins/spellchecker";
  61. import plugins from './plugins'
  62. import toolbar from './toolbar'
  63. export default {
  64.   name: 'tinymce',
  65.   components: { editor },
  66.   props: {
  67.     value: {
  68.       // 默认的富文本内容
  69.       type: String,
  70.       default: ''
  71.     },
  72.     id: {
  73.       type: String,
  74.       default: ''
  75.     },
  76.     disabled: {
  77.       // 禁用
  78.       type: Boolean,
  79.       default: false
  80.     },
  81.   },
  82.   data () {
  83.     return {
  84.       init: {
  85.         language_url: '/tinymce/langs/zh_CN.js', // 语言包路径
  86.         language: 'zh_CN', // 语言
  87.         skin_url: '/tinymce/skins/ui/oxide', // 主题路径
  88.         content_css: '/tinymce/skins/ui/oxide/content.css', // 为编辑区指定css文件,该参数的值是你的css文件路径,可使用绝对路径或相对路径。
  89.         min_height: 80,
  90.         // height: 500,
  91.         // width: this.calcWidth(),
  92.         plugins: plugins, // 指定需加载的插件
  93.         toolbar: toolbar, // 自定义工具栏
  94.         toolbar_mode: 'sliding', // 工具栏模式
  95.         // paste_data_images: true, // 允许粘贴图片
  96.         branding: false, // 不显示富文本支持方
  97.         statusbar: false, // 隐藏状态栏
  98.         menubar: false, // 禁用菜单栏
  99.         theme: 'silver', // 默认主题
  100.         placeholder: '请输入',
  101.         fontsize_formats: "12px 14px 16px 18px 20px 22px 24px 36px 48px 56px 72px",
  102.         font_formats: "宋体='宋体';仿宋='仿宋';微软雅黑='微软雅黑';楷体='楷体';隶书='隶书';幼圆='幼圆';Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Helvetica=helvetica;",
  103.         content_style: `
  104.         body { font-family:宋体,Arial,sans-serif; font-size:14px;line-height: 1.5; }` +
  105.         `table { width: 100% !important;}` + `td { border: 1px solid #ccc !important; }`,
  106.         zIndex: 1101,
  107.         style_formats_merge: false, //是否将style_formats设置中的样式附加到默认样式格式还是完全替换它们。true为附加
  108.         style_formats_autohide: true, // 隐藏当前不可用的样式列表
  109.         object_resizing: false, //禁用表格内联样式拖拽拉伸
  110.         table_resize_bars: false,//禁用表格单元格拖拽拉伸
  111.         forced_root_block: '', // 删除在tinymce中自动添加的p标签
  112.         force_br_newlines : true,
  113.         force_p_newlines : false,
  114.         // style_formats: [
  115.         //   // {
  116.         //   //   title: '首行缩进',
  117.         //   //   block: 'p',
  118.         //   //   styles: { 'text-indent': '2em' }
  119.         //   // },
  120.         //   {
  121.         //     title: 'Headings',
  122.         //     items: [
  123.         //       { title: 'Heading 1', format: 'h1' },
  124.         //       { title: 'Heading 2', format: 'h2' },
  125.         //       { title: 'Heading 3', format: 'h3' },
  126.         //       { title: 'Heading 4', format: 'h4' },
  127.         //       { title: 'Heading 5', format: 'h5' },
  128.         //       { title: 'Heading 6', format: 'h6' }
  129.         //     ]
  130.         //   },
  131.         //   // {
  132.         //   //   title: 'Inline',
  133.         //   //   items: [
  134.         //   //     { title: 'Bold', format: 'bold' },
  135.         //   //     { title: 'Italic', format: 'italic' },
  136.         //   //     { title: 'Underline', format: 'underline' },
  137.         //   //     { title: 'Strikethrough', format: 'strikethrough' },
  138.         //   //     { title: 'Superscript', format: 'superscript' },
  139.         //   //     { title: 'Subscript', format: 'subscript' },
  140.         //   //     { title: 'Code', format: 'code' }
  141.         //   //   ]
  142.         //   // },
  143.         //   {
  144.         //     title: 'Blocks',
  145.         //     items: [
  146.         //       { title: 'Paragraph', format: 'p' },
  147.         //       { title: 'Div', format: 'div' },
  148.         //       // { title: 'Blockquote', format: 'blockquote' },
  149.         //       // { title: 'Pre', format: 'pre' }
  150.         //     ]
  151.         //   },
  152.         //   {
  153.         //     title: 'Align',
  154.         //     items: [
  155.         //       { title: 'Left', format: 'alignleft' },
  156.         //       { title: 'Center', format: 'aligncenter' },
  157.         //       { title: 'Right', format: 'alignright' },
  158.         //       { title: 'Justify', format: 'alignjustify' }
  159.         //     ]
  160.         //   },
  161.         //   // {
  162.         //   //   title: "行高",
  163.         //   //   items: [
  164.         //   //     { title: "1", block: "p", styles: { "line-height": "1.0" } },
  165.         //   //     { title: "1.5", block: "p", styles: { "line-height": "1.5" } },
  166.         //   //     { title: "1.75", block: "p", styles: { "line-height": "1.75" } },
  167.         //   //     { title: "2", block: "p", styles: { "line-height": "2" } },
  168.         //   //     { title: "3", block: "p", styles: { "line-height": "3" } },
  169.         //   //     { title: "4", block: "p", styles: { "line-height": "4" } },
  170.         //   //     { title: "5", block: "p", styles: { "line-height": "5" } },
  171.         //   //   ],
  172.         //   // },
  173.         // ],
  174.         // init_instance_callback: (editor) => {
  175.         //   console.log(editor,'editor');
  176.         //     // 更改元素为Div
  177.         //     editor.execCommand('mceInsertContent', false, '<p></p>')
  178.         // },
  179.         // images_upload_handler: (blobInfo, success, failure) => {
  180.           // console.log(blobInfo, success, failure,'blobInfo');
  181.           // console.log(success, 'success');
  182.           // console.log(failure,'failure');
  183.           // const img = "data:image/jpeg;base64," + blobInfo.base64();
  184.           // success(img);
  185.         // },
  186.       },
  187.       myValue: this.value,
  188.     }
  189.   },
  190.   beforeDestroy () {
  191.     tinymce.remove()
  192.   },
  193.   created () {},
  194.   mounted () {
  195.     tinymce.init({})
  196.   },
  197.   computed: {},
  198.   watch: {
  199.     value(newValue) {
  200.       this.myValue = newValue
  201.     },
  202.     myValue(newValue) {
  203.       this.$emit('input', newValue)
  204.     },
  205.    
  206.   },
  207.   methods: {
  208.     onClick(e) {
  209.       this.$emit('onClick', e, tinymce)
  210.     },
  211.    
  212.     // clear() {
  213.     //   this.myValue = ''
  214.     // },
  215.     // calcWidth() {
  216.     //   return document.body.clientWidth / 2 + 'px'
  217.     // },
  218.   },
  219. }
  220. </script>
  221. <style lang="scss" scoped>
  222.   ::v-deep .tox .tox-tbtn--select {
  223.     width: 100px;
  224.   }
  225.   ::v-deep .tox .tox-sidebar-wrap {
  226.     padding: 5px;
  227.   }
  228. </style>
复制代码
plugins.js

  1. // autoresize 编辑器高度自适应
  2. // autosave 添加此插件跳跳转url时会弹出一个提示框(提示信息:重新加载此网站? 系统可能不会保存您所做的更改。)
  3. const plugins = ['advlist anchor autolink  code codesample colorpicker directionality fullscreen hr image insertdatetime link lists media nonbreaking pagebreak paste preview print save searchreplace tabfocus table template textcolor textpattern visualblocks visualchars wordcount autoresize']
  4. export default plugins
复制代码
toolbar.js

  1. // Here is a list of the toolbar
  2. // Detail list see https://www.tinymce.com/docs/advanced/editor-control-identifiers/#toolbarcontrols
  3. // const toolbar = ['searchreplace bold italic underline strikethrough alignleft aligncenter alignright outdent indent  blockquote undo redo removeformat subscript superscript code codesample', 'hr bullist numlist link image charmap preview anchor pagebreak insertdatetime media table emoticons forecolor backcolor fullscreen']
  4. // styleselect 行高
  5. // table 表格
  6. const toolbar = ['bold italic underline | fontselect | fontsizeselect | forecolor backcolor | removeformat']
  7. export default toolbar
复制代码
展示效果图:



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莫张周刘王

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表