tinymce富文本在vue3中使用

tinymce富文本在vue3中使用

tinymce富文本api说明

基于vue3的tinymce富文本

快速上手

1、引入tinymce

npm install tinymce
npm install @tinymce/tinymce-vue

2、将tinymce静态资源包复制到public中(其中包括皮肤和中文包)
3、将封装好的组件Tinymce包复制到components文件夹中

API

参数说明类型初始值
value富文本内容String
toolbar工具栏配置String, Array自行查看代码配置
toolbar插件配置String, Array自行查看代码配置
height富文本高度Number, String400
width富文本宽度Number, Stringauto
imageUploadUrl上传图片地址String
videoUploadUrl上传视频地址String
options一些配置包括readonlyobject{}
事件
事件名称说明回调参数
change富文本内容变化时的回调function(value)

注意:配置了imageUploadUrl和videoUploadUrl接口地址才能使用接口上传图片和视频功能

组件简单使用

  <Tinymce v-model:value="value" />
  import { Tinymce } from '/@/components/Tinymce/index';

配置 上传了+禁用+宽高

<Tinymce
  v-model:value="value"
  :imageUploadUrl="imageUploadUrl"
  :videoUploadUrl="videoUploadUrl"
  :options="{ readonly: true }"
  @change="handleChange"
/>

封装代码

<template>
  <div :class="prefixCls" :style="{ width: containerWidth }">
    <textarea
      :id="tinymceId"
      ref="elRef"
      :style="{ visibility: 'hidden' }"
      v-if="!initOptions.inline"
    ></textarea>
    <slot v-else></slot>
  </div>
</template>

<script lang="ts">
  import type { Editor, RawEditorSettings } from 'tinymce';
  import tinymce from 'tinymce/tinymce';
  import 'tinymce/themes/silver';
  import 'tinymce/icons/default/icons';
  import 'tinymce/plugins/advlist';
  import 'tinymce/plugins/anchor';
  import 'tinymce/plugins/autolink';
  import 'tinymce/plugins/autosave';
  import 'tinymce/plugins/code';
  import 'tinymce/plugins/codesample';
  import 'tinymce/plugins/directionality';
  import 'tinymce/plugins/fullscreen';
  import 'tinymce/plugins/hr';
  import 'tinymce/plugins/insertdatetime';
  import 'tinymce/plugins/link';
  import 'tinymce/plugins/lists';
  import 'tinymce/plugins/media';
  import 'tinymce/plugins/nonbreaking';
  import 'tinymce/plugins/noneditable';
  import 'tinymce/plugins/pagebreak';
  import 'tinymce/plugins/paste';
  import 'tinymce/plugins/preview';
  import 'tinymce/plugins/print';
  import 'tinymce/plugins/save';
  import 'tinymce/plugins/searchreplace';
  import 'tinymce/plugins/spellchecker';
  import 'tinymce/plugins/tabfocus';
  import 'tinymce/plugins/table';
  import 'tinymce/plugins/template';
  import 'tinymce/plugins/textpattern';
  import 'tinymce/plugins/visualblocks';
  import 'tinymce/plugins/visualchars';
  import 'tinymce/plugins/wordcount';
  import 'tinymce/plugins/image';
  import 'tinymce/plugins/charmap';
  import 'tinymce/plugins/imagetools';
  import 'tinymce/plugins/help';
  import 'tinymce/plugins/quickbars';
  import 'tinymce/plugins/importcss';
  import 'tinymce/plugins/toc';
  import 'tinymce/plugins/emoticons';

  import {
    defineComponent,
    computed,
    nextTick,
    ref,
    unref,
    watch,
    onDeactivated,
    onBeforeUnmount,
    PropType,
  } from 'vue';
  import ImgUpload from './ImgUpload.vue';
  import { toolbar, plugins } from './tinymce';
  import { buildShortUUID } from '/@/utils/uuid';
  import { bindHandlers } from './helper';
  import { onMountedOrActivated } from '@vben/hooks';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { isNumber } from '/@/utils/is';
  import { useLocale } from '/@/locales/useLocale';
  import { useAppStore } from '/@/store/modules/app';
  import { useUserStoreWithOut } from '/@/store/modules/user';

  const tinymceProps = {
    options: {
      type: Object as PropType<Partial<RawEditorSettings>>,
      default: () => ({}),
    },
    value: {
      type: String,
    },
    toolbar: {
      type: [String, Array] as PropType<string | string[]>,
      default: toolbar,
    },
    plugins: {
      type: [String, Array] as PropType<string | string[]>,
      default: plugins,
    },
    height: {
      type: [Number, String] as PropType<string | number>,
      required: false,
      default: 400,
    },
    width: {
      type: [Number, String] as PropType<string | number>,
      required: false,
      default: 'auto',
    },
    // 上传图片的url
    imageUploadUrl: {
      type: String,
      required: false,
    },
    // 上传视频接口
    videoUploadUrl: {
      type: String,
      required: false,
    },
  };
  export default defineComponent({
    name: 'Tinymce',
    components: { ImgUpload },
    inheritAttrs: false,
    props: tinymceProps,
    emits: ['change', 'update:value', 'inited', 'init-error'],
    setup(props, { emit, attrs }) {
      const userStore = useUserStoreWithOut();
      const editorRef = ref<Nullable<Editor>>(null);
      const fullscreen = ref(false);
      const tinymceId = ref<string>(buildShortUUID('tiny-vue'));
      const elRef = ref<Nullable<HTMLElement>>(null);

      const { prefixCls } = useDesign('tinymce-container');

      const appStore = useAppStore();

      const containerWidth = computed(() => {
        const width = props.width;
        if (isNumber(width)) {
          return `${width}px`;
        }
        return width;
      });

      const skinName = computed(() => {
        return appStore.getDarkMode === 'light' ? 'oxide' : 'oxide-dark';
      });
      const langName = computed(() => {
        const lang = useLocale().getLocale.value;
        return ['zh_CN', 'en'].includes(lang) ? lang : 'zh_CN';
      });
      // 如果传递了上传接口地址就展示接口上传功能
      const updateMedia = () => {
        const { imageUploadUrl, videoUploadUrl } = props;
        let result = {};
        if (imageUploadUrl) {
          result.images_upload_handler = function (blobInfo, succFun, failFun) {
            let xhr, formData;
            let file = blobInfo.blob(); // 转化为易于理解的file对象
            xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
            xhr.open('POST', imageUploadUrl);
            xhr.setRequestHeader('zj-unicom-token', userStore.getToken);
            xhr.onload = function () {
              let json;
              if (xhr.status != 200) {
                failFun('HTTP Error: ' + xhr.status);
                return;
              }
              json = JSON.parse(xhr.responseText);
              succFun(json.data);
            };
            formData = new FormData();
            formData.append('file', file, file.name);
            xhr.send(formData);
          };
        }
        if (videoUploadUrl) {
          result.file_picker_callback = function (callback) {
            // 模拟出一个input用于添加本地文件
            let input = document.createElement('input');
            input.setAttribute('type', 'file');
            input.setAttribute('accept', '.mp3, .mp4');
            input.click();
            input.onchange = function () {
              let file = this.files[0];
              let xhr = new XMLHttpRequest();
              xhr.withCredentials = false;
              xhr.open('POST', videoUploadUrl);
              xhr.setRequestHeader('zj-unicom-token', userStore.getToken);
              xhr.onload = function () {
                let json = JSON.parse(xhr.responseText);
                if (xhr.status != 200 || json.code != 200) {
                  callback(json.message || '上传失败');
                  return;
                }
                callback(json.data);
              };
              let formData = new FormData();
              formData.append('file', file, file.name);
              xhr.send(formData);
            };
          };
        }
        return result;
      };
      const initOptions = computed((): RawEditorSettings => {
        const { height, options, toolbar, plugins } = props;
        const publicPath = import.meta.env.VITE_PUBLIC_PATH || '/';
        return {
          selector: `#${unref(tinymceId)}`,
          height,
          toolbar,
          menubar: 'file edit insert view format table',
          plugins,
          fontsize_formats: '12px 14px 16px 18px 24px 36px 48px',
          language_url: publicPath + 'resource/tinymce/langs/' + langName.value + '.js',
          language: langName.value,
          branding: false,
          default_link_target: '_blank',
          link_title: false,
          object_resizing: false,
          auto_focus: true,
          skin: skinName.value,
          skin_url: publicPath + 'resource/tinymce/skins/ui/' + skinName.value,
          content_css:
            publicPath + 'resource/tinymce/skins/ui/' + skinName.value + '/content.min.css',
          ...options,
          setup: (editor: Editor) => {
            editorRef.value = editor;
            editor.on('init', (e) => initSetup(e));
          },
          deprecation_warnings: false,
          automatic_uploads: false,
          file_picker_types: 'media',
          ...updateMedia(),
        };
      });
      const disabled = computed(() => {
        const { options } = props;
        const getDisabled = (options && Reflect.get(options, 'readonly')) || attrs.disabled;
        const editor = unref(editorRef);
        if (editor) {
          editor.setMode(getDisabled ? 'readonly' : 'design');
        }
        return !!getDisabled || false;
      });

      watch(
        () => attrs.disabled,
        () => {
          const editor = unref(editorRef);
          if (!editor) {
            return;
          }
          editor.setMode(attrs.disabled ? 'readonly' : 'design');
        },
      );
      watch(
        () => editorRef.value,
        () => {
          const editor = unref(editorRef);
          if (!editor) {
            return;
          }
          editor.setMode(attrs.disabled ? 'readonly' : 'design');
        },
      );
      watch(
        () => appStore.getDarkMode,
        () => {
          destroy();
          initEditor();
        },
      );

      onMountedOrActivated(() => {
        if (!initOptions.value.inline) {
          tinymceId.value = buildShortUUID('tiny-vue');
        }
        nextTick(() => {
          setTimeout(() => {
            initEditor();
          }, 30);
        });
      });

      onBeforeUnmount(() => {
        destroy();
      });

      onDeactivated(() => {
        destroy();
      });

      function destroy() {
        if (tinymce !== null) {
          tinymce?.remove?.(unref(initOptions).selector!);
        }
      }

      function initEditor() {
        const el = unref(elRef);
        if (el) {
          el.style.visibility = '';
        }
        tinymce
          .init(unref(initOptions))
          .then((editor) => {
            emit('inited', editor);
          })
          .catch((err) => {
            emit('init-error', err);
          });
      }

      function initSetup(e) {
        const editor = unref(editorRef);
        if (!editor) {
          return;
        }
        const value = props.value || '';

        editor.setContent(value);
        bindModelHandlers(editor);
        bindHandlers(e, attrs, unref(editorRef));
      }

      function setValue(editor: Recordable, val: string, prevVal?: string) {
        if (
          editor &&
          typeof val === 'string' &&
          val !== prevVal &&
          val !== editor.getContent({ format: attrs.outputFormat })
        ) {
          editor.setContent(val);
        }
      }

      function bindModelHandlers(editor: any) {
        const modelEvents = attrs.modelEvents ? attrs.modelEvents : null;
        const normalizedEvents = Array.isArray(modelEvents) ? modelEvents.join(' ') : modelEvents;

        watch(
          () => props.value,
          (val: string, prevVal: string) => {
            setValue(editor, val || '', prevVal);
          },
          {
            immediate: true,
          },
        );

        editor.on(normalizedEvents ? normalizedEvents : 'change keyup undo redo', () => {
          const content = editor.getContent({ format: attrs.outputFormat });
          emit('update:value', content);
          emit('change', content);
        });

        editor.on('FullscreenStateChanged', (e) => {
          fullscreen.value = e.state;
        });
      }

      function handleInsertImg(url: string) {
        const editor = unref(editorRef);
        if (!editor || !url) {
          return;
        }
        editor.execCommand('mceInsertContent', false, `<img src="${url}"/>`);
        const content = editor?.getContent() ?? '';
        setValue(editor, content);
      }

      function handleImageUploading(name: string) {
        const editor = unref(editorRef);
        if (!editor) {
          return;
        }
        editor.execCommand('mceInsertContent', false, getUploadingImgName(name));
        const content = editor?.getContent() ?? '';
        setValue(editor, content);
      }

      function handleDone(name: string, url: string) {
        const editor = unref(editorRef);
        if (!editor) {
          return;
        }
        const content = editor?.getContent() ?? '';
        const val = content?.replace(getUploadingImgName(name), `<img src="${url}"/>`) ?? '';
        setValue(editor, val);
      }

      function getUploadingImgName(name: string) {
        return `[uploading:${name}]`;
      }

      return {
        prefixCls,
        containerWidth,
        initOptions,
        elRef,
        tinymceId,
        handleInsertImg,
        handleImageUploading,
        handleDone,
        editorRef,
        fullscreen,
        disabled,
      };
    },
  });
</script>

<style lang="less" scoped></style>

<style lang="less">
  @prefix-cls: ~'@{namespace}-tinymce-container';

  .@{prefix-cls} {
    position: relative;
    line-height: normal;

    textarea {
      visibility: hidden;
      z-index: -1;
    }
  }
</style>

文件包
在这里插入图片描述
官网地址:http://tinymce.ax-z.cn/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值