当前位置:   article > 正文

TinyMce富文本处理器 在Vue3 vue-vite中使用(小白避坑 必看)_tinymce vue3

tinymce vue3

 (文章实时维护中,大家有问题直接问 )

最新更新时间: 2024-05-31

gitee. base project demo

我是觉得吧 tinymce这个富文本编辑器是最好用用起来可以说是非常丝滑、简单易懂

经典模式 |TinyMCE中文文档中文手册 (ax-z.cn)

  • 首先安装依赖:

这里先声明一下:TinyMce在4.0以后就不支持Vue2.0了, 所以我们需要固定版本,或者降低版本!否则会报错

vue3的下载命令如下

  1. $ npm install @tinymce/tinymce-vue -S
  1. $ npm install tinymce -S

vue2的下载命令如下

  1. $ npm install tinymce@5.1.0 -S
  1. $ npm install @tinymce/tinymce-vue@3.0.1 -S

编辑器本身是英文编辑器,所以还需要下载本地化文件(下载这个)语言包 |可信富文本编辑器 |TinyMCE

我们下一步 要在public下面创建一个"tinymce"目录如下

node_modules找到"tinymce"依赖下的"skins",将其拷贝到public目录下的"tinymce"之中, 同理将刚刚下载完成的"zh-CN"放置"public/langs"

创建components

我这里命名为""TEditor"" 

编写组件 ( copy 就完了 ) >>>

  1. <template>
  2. <div style="height: 100%; overflow: hidden">
  3. <editor
  4. v-model="myValue"
  5. :init="init"
  6. :enabled="enabled"
  7. :id="tinymceId"
  8. ></editor>
  9. </div>
  10. </template>
  11. <script setup>
  12. import {computed, reactive, watch, ref, nextTick, onMounted} from "vue"; //全屏
  13. import tinymce from "tinymce/tinymce";
  14. // import "tinymce/skins/content/default/content.css";
  15. import Editor from "@tinymce/tinymce-vue";
  16. import "tinymce/icons/default/icons";
  17. import "tinymce/models/dom"; // 一定要引入
  18. import "tinymce/themes/silver"; // 界面UI主题
  19. import "tinymce/plugins/image";
  20. import "tinymce/plugins/table";
  21. import "tinymce/plugins/lists"; // 列表插件
  22. import "tinymce/plugins/wordcount"; // 文字计数
  23. import "tinymce/plugins/preview"; // 预览
  24. import "tinymce/plugins/emoticons"; // emoji表情
  25. import "tinymce/plugins/emoticons/js/emojis.js"; //必须引入这个文件才有表情图库
  26. import "tinymce/plugins/code"; // 编辑源码
  27. import "tinymce/plugins/link"; // 链接插件
  28. import "tinymce/plugins/advlist"; //高级列表
  29. import "tinymce/plugins/codesample"; //代码示例
  30. import "tinymce/plugins/autoresize"; // 自动调整编辑器大小
  31. import "tinymce/plugins/quickbars"; // 光标处快捷提示
  32. import "tinymce/plugins/nonbreaking"; //插入不间断空格
  33. import "tinymce/plugins/searchreplace"; //查找替换
  34. import "tinymce/plugins/autolink"; //自动链接
  35. import "tinymce/plugins/directionality"; //文字方向
  36. import "tinymce/plugins/visualblocks"; //显示元素范围
  37. import "tinymce/plugins/visualchars"; //显示不可见字符
  38. import "tinymce/plugins/charmap"; // 特殊符号
  39. import "tinymce/plugins/nonbreaking"; //插入不间断空格
  40. import "tinymce/plugins/insertdatetime"; //插入日期时间
  41. import "tinymce/plugins/importcss"; //引入自定义样式的css文件
  42. import "tinymce/plugins/accordion"; // 可折叠数据手风琴模式
  43. import "tinymce/plugins/anchor"; //锚点
  44. import "tinymce/plugins/fullscreen";
  45. const emits = defineEmits(["update:modelValue", "setHtml"]);
  46. //这里我选择将数据定义在props里面,方便在不同的页面也可以配置出不同的编辑器,当然也可以直接在组件中直接定义
  47. const props = defineProps({
  48. value: {
  49. type: String,
  50. default: () => {
  51. return "";
  52. },
  53. },
  54. baseUrl: {
  55. type: String,
  56. default: "",
  57. },
  58. enabled: {
  59. type: Boolean,
  60. default: true,
  61. },
  62. // 编辑器初始可编辑状态
  63. editable_root: {
  64. type: Boolean,
  65. default: true,
  66. },
  67. plugins: {
  68. type: [String, Array],
  69. default:
  70. "importcss autoresize searchreplace autolink directionality code visualblocks visualchars fullscreen image link codesample table charmap nonbreaking anchor insertdatetime advlist lists wordcount charmap quickbars emoticons accordion",
  71. },
  72. knwlgId: {
  73. type: String,
  74. },
  75. toolbar: {
  76. type: [String, Array, Boolean],
  77. default: "undo redo | accordion accordionremove | blocks fontfamily fontsize| bold italic underline strikethrough ltr rtl | align numlist bullist | link image | table | lineheight outdent indent| forecolor backcolor removeformat | charmap emoticons | anchor codesample",
  78. },
  79. readonly: {
  80. type: Boolean,
  81. default: false,
  82. },
  83. minHeight: {
  84. type: Number,
  85. default: 630,
  86. },
  87. });
  88. const loading = ref(false);
  89. const tinymceId = ref(
  90. "vue-tinymce-" + +new Date() + ((Math.random() * 1000).toFixed(0) + "")
  91. );
  92. //定义一个对象 init初始化
  93. const init = reactive({
  94. selector: "#" + tinymceId.value, //富文本编辑器的id,
  95. language_url: "/tinymce/langs/zh_CN.js", // 语言包的路径,具体路径看自己的项目
  96. language: "zh_CN",
  97. skin_url: "/tinymce/skins/ui/oxide", // skin路径,具体路径看自己的项目
  98. editable_root: props.editable_root,
  99. height: 600,
  100. branding: false, // 是否禁用“Powered by TinyMCE”
  101. promotion: false, //去掉 upgrade
  102. // toolbar_sticky: true,
  103. // toolbar_sticky_offset: 100,
  104. menubar: "edit view insert format tools table",
  105. paste_data_images: true, //允许粘贴图像
  106. image_dimensions: false, //去除宽高属性
  107. plugins: props.plugins, //这里的数据是在props里面就定义好了的
  108. toolbar: props.toolbar, //这里的数据是在props里面就定义好了的
  109. // 取消图片资源路径转换
  110. convert_urls: false,
  111. // table边框位0是否展示网格线
  112. // visual: false,
  113. // 超链接默认打开方式
  114. link_default_target: "_blank",
  115. link_context_toolbar: true,
  116. // 默认快捷菜单
  117. quickbars_insert_toolbar: "image codesample table",
  118. // 选中图片的快捷提示
  119. quickbars_image_toolbar: "alignleft aligncenter alignright | rotateleft rotateright | imageoptions",
  120. editimage_toolbar: "rotateleft rotateright | flipv fliph | editimage imageoptions",
  121. // 文字样式
  122. font_family_formats:
  123. "Arial=arial,helvetica,sans-serif; 宋体=SimSun; 微软雅黑=Microsoft Yahei; Impact=impact,chicago;", //字体
  124. font_size_formats: "11px 12px 14px 16px 18px 24px 36px 48px 64px 72px", //文字大小
  125. image_caption: true,
  126. editimage_cors_hosts: ["picsum.photos"],
  127. noneditable_class: "mceNonEditable",
  128. toolbar_mode: "wrap", // 工具栏模式 floating / sliding / scrolling / wrap
  129. // 默认样式
  130. content_style:
  131. "body { font-family:Helvetica,Arial,sans-serif; font-size:16px }p {margin:3px; line-height:24px;}",
  132. image_advtab: true,
  133. importcss_append: true,
  134. paste_webkit_styles: "all",
  135. paste_merge_formats: true,
  136. nonbreaking_force_tab: false,
  137. paste_auto_cleanup_on_paste: false,
  138. file_picker_types: "file",
  139. // 选中文字的快捷提示
  140. quickbars_selection_toolbar:
  141. "bold italic | quicklink h2 h3 blockquote quickimage quicktable",
  142. // 编辑器高度自适应
  143. autoresize_bottom_margin: 20,
  144. // autoresize_overflow_padding: 16,
  145. min_height: props.minHeight,
  146. content_css: "/tinymce/skins/content/default/content.css", //以css文件方式自定义可编辑区域的css样式,css文件需自己创建并引入
  147. // setup: function (editor) {
  148. // },
  149. //图片上传 -实列 具体请根据官网补充-
  150. images_upload_handler: function (blobInfo, progress) {
  151. new Promise((resolve, reject) => {
  152. let file = blobInfo.blob();
  153. if (file.size / 1024 / 1024 > 200) {
  154. reject({
  155. message: "上传失败,图片大小请控制在 200M 以内",
  156. remove: true,
  157. });
  158. }
  159. const formData = new FormData();
  160. formData.append("file", file);
  161. console.log( formData)
  162. axios.post("/api/upload/upload", formData, {
  163. headers: {
  164. "Content-Type": "multipart/form-data",
  165. },
  166. onUploadProgress: (progressEvent) => {
  167. progress(
  168. Math.round((progressEvent.loaded / progressEvent.total) * 100)
  169. );
  170. },
  171. }).then((res) => {
  172. resolve(res.data.url);
  173. })
  174. .catch()
  175. });
  176. },
  177. });
  178. // 外部传递进来的数据变化
  179. const myValue = computed({
  180. get() {
  181. return props.modelValue;
  182. },
  183. set(val) {
  184. emits("update:modelValue", val);
  185. },
  186. });
  187. //监听富文本中的数据变化
  188. watch(
  189. () => myValue.value,
  190. () => {
  191. emits(
  192. "setHtml",
  193. tinymce.activeEditor.getContent({format: "text"}),
  194. myValue.value
  195. );
  196. }
  197. );
  198. // 设置编辑器只读模式
  199. watch(
  200. () => props.readonly,
  201. (newValue, oldValue) => {
  202. nextTick(() => {
  203. tinymce.activeEditor.mode.set(newValue ? "readonly" : "design");
  204. let iframeDom = document.querySelector("iframe");
  205. iframeDom &&
  206. (iframeDom.contentWindow.document.body.style.margin = newValue
  207. ? 0
  208. : "16px");
  209. });
  210. },
  211. {immediate: true}
  212. );
  213. //初始化编辑器
  214. onMounted(() => {
  215. tinymce.init({});
  216. });
  217. // 设置值
  218. const handleSetContent = (content) => {
  219. tinymce.activeEditor.setContent(content);
  220. };
  221. // 获取值
  222. const handleGetContent = () => {
  223. return tinymce.activeEditor.getContent();
  224. };
  225. defineExpose({
  226. handleSetContent,
  227. handleGetContent,
  228. });
  229. </script>
  230. <style lang="scss" scoped>
  231. :deep(.tox-tinymce) {
  232. border: 1px solid #dcdfe6;
  233. border-radius: 4px;
  234. .tox-statusbar {
  235. display: none;
  236. }
  237. }
  238. </style>
 引用组件
  1. <template>
  2. <div class="tinymce-box">
  3. <TEditor ></TEditor>
  4. </div>
  5. </template>
  6. <style scoped>
  7. .tinymce-box {
  8. width: 100%;
  9. }
  10. </style>
  11. <script lang="ts" setup>
  12. import TEditor from "@/components/TEditor.vue";
  13. </script>

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

闽ICP备14008679号