vue3项目使用TinyMce配置说明

1、安装相关依赖

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

2、下载中文包

地址 Language Packages | Trusted Rich Text Editor | TinyMCE

在项目public文件夹下新建tinymce文件夹,
将下载的汉化包解压到此文件夹
然后在node_modules/tinymce中找到skins文件夹,也复制到public/tinymce里

3. 封装组件

在src/components下新建TinymceEditor.vue,并写入以下代码

 <template>
  <div>
    <Editor
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      :placeholder="placeholder"
      :id="tinymceId"
    ></Editor>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted, defineEmits, watch } from "vue";
import { upload } from "@/api/uploads";  // 自定义上传方法
import Editor from "@tinymce/tinymce-vue";
import tinymce from "tinymce/tinymce";
import "tinymce/themes/silver";
import "tinymce/themes/silver/theme";
import "tinymce/models/dom";
import "tinymce/icons/default";
import "tinymce/icons/default/icons";
// 引入编辑器插件
import "tinymce/plugins/code"; //编辑源码
import "tinymce/plugins/image"; //插入编辑图片
import "tinymce/plugins/media"; //插入视频
import "tinymce/plugins/link"; //超链接
import "tinymce/plugins/preview"; //预览
import "tinymce/plugins/template"; //模板
import "tinymce/plugins/table"; //表格
import "tinymce/plugins/pagebreak"; //分页
import "tinymce/plugins/lists"; //列
import "tinymce/plugins/advlist"; //列
import "tinymce/plugins/quickbars"; //快速工具条
import "tinymce/plugins/wordcount"; // 字数统计插件
import "@/assets/tinymce/langs/zh-Hans.js"; //下载后的语言包
import "tinymce/skins/content/default/content.css";
const emits = defineEmits(["update:value"]);
const props = defineProps({
  value: {
    type: String,
    default: "",
  },
  placeholder: {
    type: String,
    default: "",
  },
  height: {
    type: Number,
    default: 500,
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  plugins: {
    type: [String, Array],
    default:
      "code image media link preview table quickbars template pagebreak lists advlist",
  },
  toolbar: {
    type: [String, Array],
    default:
      "undo redo codesample bold italic underline strikethrough link alignleft aligncenter alignright alignjustify \
      bullist numlist outdent indent removeformat forecolor backcolor |formatselect fontselect fontsizeselect | \
      blocks fontfamily fontsize pagebreak lists image media table template preview | code selectall",
  },
  templates: {
    type: Array,
    default: () => [],
  },
  options: {
    type: Object,
    default: () => {},
  },
});
//用于接收外部传递进来的富文本
const myValue = ref(props.value);
const tinymceId = ref(
  "vue-tinymce-" + +new Date() + ((Math.random() * 1000).toFixed(0) + "")
);
const init = reactive({
  selector: "#" + tinymceId.value, //富文本编辑器的id,
  language_url: "/tinymce/langs/zh-Hans.js", // 语言包的路径,具体路径看自己的项目,文档后面附上中文js文件
  language: "zh-Hans", //语言
  skin_url: "/tinymce/skins/ui/oxide", // skin路径,具体路径看自己的项目
  content_css: "/tinymce/skins/content/default/content.css",
  menubar: true, //顶部菜单栏显示
  statusbar: true, // 底部的状态栏
  plugins: props.plugins,
  toolbar: props.toolbar,
  toolbar_mode: "sliding",
  font_formats:
    "Arial=arial,helvetica,sans-serif; 宋体=SimSun; 微软雅黑=Microsoft Yahei; Impact=impact,chicago;", //字体
  paste_convert_word_fake_lists: false, // 插入word文档需要该属性
  font_size_formats: "12px 14px 16px 18px 22px 24px 36px 72px", //文字大小
  height: props.height, //编辑器高度
  placeholder: props.placeholder,
  branding: false, //是否禁用“Powered by TinyMCE”
  image_dimensions: false, //去除宽高属性
  paste_webkit_styles: "all",
  paste_merge_formats: true,
  nonbreaking_force_tab: false,
  paste_auto_cleanup_on_paste: false,
  file_picker_types: "file",
  resize: true,
  elementpath: true,
  content_style: "",
  templates: props.templates,
  quickbars_selection_toolbar:
    "forecolor backcolor bold italic underline strikethrough link",
  quickbars_image_toolbar: "alignleft aligncenter alignright",
  quickbars_insert_toolbar: false,
  image_caption: true,
  image_advtab: true,
  convert_urls: false,
  images_upload_handler: function (blobInfo, progress) {
    return new Promise((resolve, reject) => {
      const data = new FormData();
      data.append("file", blobInfo.blob(), blobInfo.filename());
      update(data)
        .then((res) => {
          resolve(res.data.src);
        })
        .catch(() => {
          reject("Image upload failed");
        });
    });
  },
  // 文件上传
  file_picker_callback: (callback, value, meta) => {
    // Provide file and text for the link dialog
    if (meta.filetype == "file") {
      callback("mypage.html", { text: "My text" });
    }
    // Provide image and alt text for the image dialog
    if (meta.filetype == "image") {
      callback("myimage.jpg", { alt: "My alt text" });
    }
    // Provide alternative source and posted for the media dialog
    if (meta.filetype == "media") {
      callback("movie.mp4", { source2: "alt.ogg", poster: "image.jpg" });
    }
  },
  setup: function (editor) {
    editor.on("init", function () {
      this.getBody().style.fontSize = "14px";
    });
    editor.on("OpenWindow", function (e) {
      //FIX 编辑器在el-drawer中,编辑器的弹框无法获得焦点
      var D = document.querySelector(".el-drawer.open");
      var E = e.target.editorContainer;
      if (D && D.contains(E)) {
        var nowDA = document.activeElement;
        setTimeout(() => {
          document.activeElement.blur();
          nowDA.focus();
        }, 0);
      }
    });
  },
  ...props.options,
});
//监听外部传递进来的的数据变化
watch(
  () => props.value,
  () => {
    myValue.value = props.value;
    emits("update:value", myValue.value);
  }
);
//监听富文本中的数据变化
watch(
  () => myValue.value,
  () => {
    emits("update:value", myValue.value);
  }
);
//在onMounted中初始化编辑器
onMounted(() => {
  tinymce.init({});
});
</script>
<style></style>

4. 注册及使用组件

<template>
  <div>
    <TinymceEditor :value="formData.content" @update:value="updateContent" />
  </div>
</template>
<script setup lang="ts">
import TinymceEditor from "@/components/tinymce/index.vue";
const formData = reactive({
  content: "",
});
const updateContent = (v: string) => {
  formData.content = v;
};
</script>

 本文Tinymce版本

"@tinymce/tinymce-vue": "^5.1.1",
"tinymce": "^6.8.2",

Timymce文档

Tinymce中文文档:http://tinymce.ax-z.cn/
Tinymce官方文档:https://www.tiny.cloud/docs/
Tinymce github地址:https://github.com/tinymce/tinymce

 查看原文:vue3项目使用TinyMce配置说明

 关注公众号 "字节航海家" 及时获取最新内容

Vue 3项目使用TinyMCE,你可以按照以下步骤进行操作: 1. 安装TinyMCE:在终端中运行以下命令来安装TinyMCE的npm包: ``` npm install tinymce ``` 2. 创建一个TinyMCE组件:在Vue项目中创建一个新的组件,比如`TinyMCE.vue`。 3. 在TinyMCE.vue组件中引入和初始化TinyMCE编辑器: ```vue <template> <div> <textarea ref="editor"></textarea> </div> </template> <script> import { ref, onMounted } from 'vue'; import 'tinymce/tinymce'; import 'tinymce/icons/default'; import 'tinymce/themes/silver'; import 'tinymce/plugins/link'; import 'tinymce/plugins/paste'; export default { name: 'TinyMCE', setup() { const editorRef = ref(null); onMounted(() => { tinymce.init({ target: editorRef.value, plugins: ['link', 'paste'], toolbar: 'undo redo | bold italic | alignleft aligncenter alignright | code', height: 500, // 其他配置... }); }); return { editorRef, }; }, }; </script> ``` 在上面的代码中,我们引入了TinyMCE的必要资源,并在`onMounted`钩子函数中初始化了编辑器。 4. 在需要使用TinyMCE的页面中引入该组件: ```vue <template> <div> <h1>使用TinyMCE的页面</h1> <TinyMCE /> </div> </template> <script> import TinyMCE from '@/components/TinyMCE.vue'; export default { name: 'Page', components: { TinyMCE, }, }; </script> ``` 在上述代码中,我们将TinyMCE组件引入到需要使用的页面中。 这样,你就可以在Vue 3项目使用TinyMCE编辑器了。你可以根据需要调整TinyMCE配置和样式。注意,代码示例中的路径和文件名可能需要根据你的项目结构进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值