Skip to content

CherryMarkDown 结合 Vue 使用

1. 安装

  • 安装 vue3 自己百度下

  • 安装 cherry-markdown

bash
npm install cherry-markdown --save

自己封装一个 vue 组件

  • cherryMarkdown.vue
vue
<template>
  <div id="markdownbox"></div>
</template>

<script setup>
import "cherry-markdown/dist/cherry-markdown.css";
import CherryMarkdown from "cherry-markdown";

import { uploadAll } from "./single.js";
import { uploadAllMore } from "./more.js";
import { onMounted, ref } from "vue";
import { basicConfig } from "./config.js";
onMounted(() => {
  const cherryInstance = {
    el: document.querySelector("#markdownbox"),
    value: "",
    // 主题
    // 定义主题的作用范围,相同nameSpace的实例共享主题配置
    nameSpace: "cherry",
    themeSettings: {
      // 主题列表,用于切换主题
      themeList: [
        { className: "default", label: "默认" },
        { className: "dark", label: "黑" },
        { className: "light", label: "白" },
        { className: "green", label: "绿" },
        { className: "red", label: "粉" },
        { className: "violet", label: "紫" },
        { className: "blue", label: "蓝" },
      ],
      // 目前应用的主题
      mainTheme: "light",
      // 目前应用的代码块主题
      codeBlockTheme: "default",
    },
    // fileUpload: uploadAll, // 单文件上传
    /**
     * 上传文件的时候用来指定文件类型
     */
    fileTypeLimitMap: {
      video: "video/*",
      audio: "audio/*",
      image: "image/*",
      word: ".doc,.docx",
      pdf: ".pdf",
      file: "*",
    },
    /**
     * 上传文件的时候是否开启多选
     */
    multipleFileSelection: {
      video: true,
      audio: true,
      image: true,
      word: true,
      pdf: true,
      file: true,
    },
    /**
     * 多文件上传回调
     */
    callback: {
      fileUploadMulti: uploadAllMore,
    },
  };
  const resultconfig = Object.assign({}, basicConfig, cherryInstance);
  const cherry = new CherryMarkdown(resultconfig);
});
</script>

<style lang="scss" scoped>
iframe.cherry-dialog-iframe {
  width: 100%;
  height: 100%;
}
#markdownbox {
  width: 100%;
  height: 100%;
}
// 这里必须设置下,不然会撑不起屏幕
#app {
  width: 100%;
  height: 100vh;
}
</style>

config.js

  • 我这里封装了自定义按钮 一共就三步
  1. 写按钮

  2. 注册

  3. 使用

ts
// import "./pinyin/pinyin_dist.js";

/**
 * 自定义一个语法
 */
var CustomHookA = Cherry.createSyntaxHook(
  "codeBlock",
  Cherry.constants.HOOKS_TYPE_LIST.PAR,
  {
    makeHtml(str) {
      console.warn("custom hook", "hello");
      return str;
    },
    rule(str) {
      const regex = {
        begin: "",
        content: "",
        end: "",
      };
      regex.reg = new RegExp(regex.begin + regex.content + regex.end, "g");
      return regex;
    },
  }
);
/**
 * 自定义一个自定义菜单
 * 点第一次时,把选中的文字变成同时加粗和斜体
 * 保持光标选区不变,点第二次时,把加粗斜体的文字变成普通文本
 */
var customMenuA = Cherry.createMenuHook("加粗斜体", {
  iconName: "font",
  onClick: function (selection) {
    // 获取用户选中的文字,调用getSelection方法后,如果用户没有选中任何文字,会尝试获取光标所在位置的单词或句子
    let $selection = this.getSelection(selection) || "同时加粗斜体";
    // 如果是单选,并且选中内容的开始结束内没有加粗语法,则扩大选中范围
    if (!this.isSelections && !/^\s*(\*\*\*)[\s\S]+(\1)/.test($selection)) {
      this.getMoreSelection("***", "***", () => {
        const newSelection = this.editor.editor.getSelection();
        const isBoldItalic = /^\s*(\*\*\*)[\s\S]+(\1)/.test(newSelection);
        if (isBoldItalic) {
          $selection = newSelection;
        }
        return isBoldItalic;
      });
    }
    // 如果选中的文本中已经有加粗语法了,则去掉加粗语法
    if (/^\s*(\*\*\*)[\s\S]+(\1)/.test($selection)) {
      return $selection.replace(
        /(^)(\s*)(\*\*\*)([^\n]+)(\3)(\s*)($)/gm,
        "$1$4$7"
      );
    }
    /**
     * 注册缩小选区的规则
     *    注册后,插入“***TEXT***”,选中状态会变成“***【TEXT】***”
     *    如果不注册,插入后效果为:“【***TEXT***】”
     */
    this.registerAfterClickCb(() => {
      this.setLessSelection("***", "***");
    });
    return $selection.replace(/(^)([^\n]+)($)/gm, "$1***$2***$3");
  },
});
/**
 * 定义一个空壳,用于自行规划cherry已有工具栏的层级结构
 */
var customMenuB = Cherry.createMenuHook("实验室", {
  icon: {
    type: "svg",
    content:
      '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10" /><path d="M8 14s1.5 2 4 2 4-2 4-2" /><line x1="9" y1="9" x2="9.01" y2="9" /><line x1="15" y1="9" x2="15.01" y2="9" /></svg>',
    iconStyle: "width: 15px; height: 15px; vertical-align: middle;",
  },
});
/**
 * 定义一个自带二级菜单的工具栏
 */
var customMenuC = Cherry.createMenuHook("帮助中心", {
  iconName: "question",

  subMenuConfig: [
    {
      noIcon: true,
      name: "快捷键",
      onclick: (event) => {
        console.log(event);
      },
    },
    {
      noIcon: true,
      name: "联系我们",
      onclick: (event) => {
        console.log(event);
      },
    },
    {
      noIcon: true,
      name: "更新日志",
      onclick: (event) => {
        console.log(event);
      },
    },
  ],
});

/**
 * 定义带图表表格的按钮
 */
var customMenuTable = Cherry.createMenuHook("图表", {
  iconName: "trendingUp",
  subMenuConfig: [
    {
      noIcon: true,
      name: "折线图",
      onclick: (event) => {
        cherry.insert(
          '\n| :line:{"title": "折线图"} | Header1 | Header2 | Header3 | Header4 |\n| ------ | ------ | ------ | ------ | ------ |\n| Sample1 | 11 | 11 | 4 | 33 |\n| Sample2 | 112 | 111 | 22 | 222 |\n| Sample3 | 333 | 142 | 311 | 11 |\n'
        );
      },
    },
    {
      noIcon: true,
      name: "柱状图",
      onclick: (event) => {
        cherry.insert(
          '\n| :bar:{"title": "柱状图"} | Header1 | Header2 | Header3 | Header4 |\n| ------ | ------ | ------ | ------ | ------ |\n| Sample1 | 11 | 11 | 4 | 33 |\n| Sample2 | 112 | 111 | 22 | 222 |\n| Sample3 | 333 | 142 | 311 | 11 |\n'
        );
      },
    },
    {
      noIcon: true,
      name: "雷达图",
      onclick: (event) => {
        cherry.insert(
          '\n| :radar:{"title": "雷达图"} | 技能1 | 技能2 | 技能3 | 技能4 | 技能5 |\n| ------ | ------ | ------ | ------ | ------ | ------ |\n| 用户A | 90 | 85 | 75 | 80 | 88 |\n| 用户B | 75 | 90 | 88 | 85 | 78 |\n| 用户C | 85 | 78 | 90 | 88 | 85 |\n'
        );
      },
    },
    {
      noIcon: true,
      name: "热力图",
      onclick: (event) => {
        cherry.insert(
          '\n| :heatmap:{"title": "热力图"} | 周一 | 周二 | 周三 | 周四 | 周五 |\n| ------ | ------ | ------ | ------ | ------ | ------ |\n| 上午 | 10 | 20 | 30 | 40 | 50 |\n| 下午 | 15 | 25 | 35 | 45 | 55 |\n| 晚上 | 5 | 15 | 25 | 35 | 45 |\n'
        );
      },
    },
    {
      noIcon: true,
      name: "饼图",
      onclick: (event) => {
        cherry.insert(
          '\n| :pie:{"title": "饼图"} | 数值 |\n| ------ | ------ |\n| 苹果 | 40 |\n| 香蕉 | 30 |\n| 橙子 | 20 |\n| 葡萄 | 10 |\n'
        );
      },
    },
    {
      noIcon: true,
      name: "散点图",
      onclick: (event) => {
        cherry.insert(
          '\n| :scatter:{"title": "散点图", "cherry:mapping": {"x": "X", "y": "Y", "size": "Size", "series": "Series"}} | X | Y | Size | Series |\n| ------ | ------ | ------ | ------ | ------ |\n| A1 | 10 | 20 | 5 | S1 |\n| A2 | 15 | 35 | 8 | S1 |\n| B1 | 30 | 12 | 3 | S2 |\n| B2 | 25 | 28 | 6 | S2 |\n| C1 | 50 | 40 | 9 | S3 |\n| C2 | 60 | 55 | 7 | S3 |\n'
        );
      },
    },
  ],
});

/* (1) 自定义保存事件 */

var customMenuSave = Cherry.createMenuHook("保存", {
  noIcon: true,
  name: "保存",
  onClick: function (selection) {
    console.log(this);
    console.log(this.$cherry.getHtml());
    window.alert(this.$cherry.getHtml());
  },
});

const basicConfig = {
  id: "markdownbox",
  externals: {
    echarts: window.echarts,
    katex: window.katex,
    MathJax: window.MathJax,
  },
  isPreviewOnly: false,
  engine: {
    global: {
      htmlAttrWhiteList: "part|slot",
      flowSessionContext: false,
      // flowSessionCursor: 'default'
    },
    syntax: {
      link: {
        attrRender: (text, href) => {
          return ``;
        },
      },
      image: {
        videoWrapper: (link, type, defaultWrapper) => {
          console.log(type);
          return defaultWrapper;
        },
      },
      autoLink: {
        /** 生成的<a>标签追加target属性的默认值 空:在<a>标签里不会追加target属性, _blank:在<a>标签里追加target="_blank"属性 */
        target: "",
        /** 生成的<a>标签追加rel属性的默认值 空:在<a>标签里不会追加rel属性, nofollow:在<a>标签里追加rel="nofollow:在"属性*/
        rel: "",
        /** 是否开启短链接 */
        enableShortLink: true,
        /** 短链接长度 */
        shortLinkLength: 20,
        attrRender: (text, href) => {
          return ``;
        },
      },
      codeBlock: {
        theme: "twilight",
        lineNumber: true, // 默认显示行号
        expandCode: true,
        copyCode: true,
        editCode: false, // 默认不开启编辑功能
        changeLang: false, // 改变语言
        wrapperRender: (lang, code, html) => {
          return `<div class="custom-codeblock-wrapper language-${lang}" data-tips="可以自定义代码块外层容器">${html}</div>`;
        },
        customBtns: [
          {
            html: "<p>热</p>",
            onClick: (event, code, lang, dom) => {
              console.log(`【${lang}】: ${code}`);
              console.log(dom);
            },
          },
          {
            html: "自定义按钮2",
            onClick: (event, code, lang, dom) => {
              console.log(`【${lang}】: ${code}`);
              console.log(dom);
            },
          },
        ],
        customRenderer: {
          // 特殊配置“all”,会应用于所有语言
          // 'all': {
          //   render: (src, sign, cherryEngine, lang)=> {
          //     return `<p class="my-render">lang:${lang};code:${src}</p>`;
          //   }
          // }
        },
      },
      table: {
        enableChart: true,
      },
      fontEmphasis: {
        allowWhitespace: false, // 是否允许首尾空格
      },
      strikethrough: {
        needWhitespace: false, // 是否必须有前后空格
      },
      mathBlock: {
        engine: "MathJax", // katex或MathJax
        // engine: 'katex', // katex或MathJax
        src: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js", // 如果使用MathJax plugins,则需要使用该url通过script标签引入
        // src: 'https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js',
        // css: 'https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css', // 如果使用katex,则还需要引入css(如果是MathJax,则不需要)
      },
      inlineMath: {
        engine: "MathJax", // katex或MathJax
        // engine: 'katex', // katex或MathJax
      },
      emoji: {
        useUnicode: true,
        customResourceURL:
          "https://github.githubassets.com/images/icons/emoji/unicode/${code}.png?v8",
        upperCase: false,
      },
      htmlBlock: {
        removeTrailingNewline: false,
      },
      // toc: {
      //     tocStyle: 'nested'
      // }
      // 'header': {
      //   strict: false
      // }

      panel: {
        // 是否支持对齐语法
        enableJustify: true,
        // 是否支持信息面板语法
        enablePanel: true,
      },
      footnote: {
        /**
         * 脚注标号的配置
         */
        refNumber: {
          appendClass: "ref-number", // 添加到引用序号的类名
          // 脚注标号的内容
          render: (refNum, refTitle) => `[${refNum}]`,
          // 点击标号时回调
          clickRefNumberCallback: (event, refNum, refTitle, content) => {
            // console.log(refNum, refTitle, content);
          },
        },
        /**
         * 脚注列表的配置
         *  - refList: false 不渲染脚注列表
         */
        // refList: false,
        refList: {
          appendClass: "ref-list",
          title: {
            appendClass: "ref-list-title", // 添加到脚注列表标题的类名
            render: () => "", // 标题的内容,为空则渲染cherry默认的标题
          },
          listItem: {
            appendClass: "ref-list-item", // 添加到脚注列表单个脚注的类名
            render: (refNum, refTitle, content, refNumberLinkRender) => {
              return `${refNumberLinkRender(refNum, refTitle)}${content}`;
            },
          },
        },
        /**
         * hover到脚注标号时,显示一个卡片
         *  - bubbleCard: false 不响应hover事件
         */
        // bubbleCard: false,
        bubbleCard: {
          appendClass: "bubble-card", // 添加到卡片上的类名
          // 自定义渲染卡片内容
          render: (refNum, refTitle, content) => {
            return `
              <div class="cherry-ref-bubble-card__title">${refNum}. ${refTitle}</div>
              <div class="cherry-ref-bubble-card__content">${content}</div>
              <div class="cherry-ref-bubble-card__foot"></div>
            `;
          },
        },
      },
    },
    customSyntax: {
      // SyntaxHookClass
      CustomHook: {
        syntaxClass: CustomHookA,
        force: false,
        after: "br",
      },
    },
  },
  multipleFileSelection: {
    video: true,
    audio: false,
    image: true,
    word: false,
    pdf: true,
    file: true,
  },
  toolbars: {
    toolbar: [
      "bold",
      "italic",
      {
        strikethrough: [
          "strikethrough",
          "underline",
          "sub",
          "sup",
          "ruby",
          "customMenuAName",
        ],
      },
      "size",
      "|",
      "color",
      "header",
      "|",
      "ol",
      "ul",
      "checklist",
      "panel",
      "align",
      "detail",
      "|",
      "formula",
      {
        insert: [
          "image",
          "audio",
          "video",
          "link",
          "hr",
          "br",
          "code",
          "inlineCode",
          "formula",
          "toc",
          "table",
          "pdf",
          "word",
          "file",
        ],
      },
      "graph",
      // "proTable",
      // "customMenuTable",
      "togglePreview",
      "search",
      "shortcutKey",
      "codeTheme",
      {
        customMenuBName: ["ruby", "audio", "video", "customMenuAName"],
      },
      "customMenuCName",
    ],
    // (3) 添加
    toolbarRight: [
      "customMenuSave",
      "|",
      "export",
      "|",
      "changeLocale",
      "|",
      "wordCount",
    ],
    // bubble: [
    //   "bold",
    //   "italic",
    //   "underline",
    //   "strikethrough",
    //   "sub",
    //   "sup",
    //   "quote",
    //   "ruby",
    //   "|",
    //   "size",
    //   "color",
    // ], // array or false
    bubble: false, // array or false
    sidebar: ["mobilePreview", "copy", "theme"],
    toc: {
      // updateLocationHash: false, // 要不要更新URL的hash
      defaultModel: "full", // pure: 精简模式/缩略模式,只有一排小点; full: 完整模式,会展示所有标题
    },
    // (2) 添加
    customMenu: {
      customMenuAName: customMenuA,
      customMenuBName: customMenuB,
      customMenuCName: customMenuC,
      customMenuTable,
      customMenuSave,
    },
    shortcutKeySettings: {
      /** 是否替换已有的快捷键, true: 替换默认快捷键; false: 会追加到默认快捷键里,相同的shortcutKey会覆盖默认的 */
      isReplace: false,
      shortcutKeyMap: {
        "Alt-Digit1": {
          hookName: "header",
          aliasName: "标题",
        },
        "Control-Shift-KeyX": {
          hookName: "bold",
          aliasName: "加粗",
        },
      },
    },
  },

  previewer: {
    // 自定义markdown预览区域class
    // className: 'markdown'
    floatWhenClosePreviewer: true,
  },
  keydown: [],
  //extensions: [],
  callback: {
    onClickPreview: (event) => {
      console.log("onClickPreview", event);
    },
    afterAsyncRender: (md, html) => {
      // console.log("afterAsyncRender", md, html);
    },
    urlProcessor(url, srcType) {
      console.log(`url-processor`, url, srcType);
      return url;
    },
  },
  editor: {
    id: "cherry-text",
    name: "cherry-text",
    autoSave2Textarea: true,
    defaultModel: "edit&preview",
    showFullWidthMark: true, // 是否高亮全角符号 ·|¥|、|:|“|”|【|】|(|)|《|》
    showSuggestList: true, // 是否显示联想框
    maxUrlLength: 200, // url最大长度,超过则自动截断
    codemirror: {
      placeholder: "请输入",
    },
  },
  // cherry初始化后是否检查 location.hash 尝试滚动到对应位置
  autoScrollByHashAfterInit: true,
  // locale: 'en_US',
  themeSettings: {
    mainTheme: "default",
  },
};

export { basicConfig };

修改上传三部曲

多文件上传

ts
import axios from "axios";
// 多图上传文件核心
async function uploadAllMore(File, callback) {
  //  图片
  const imageRegex = /^image\/(jpg|jpeg|png|gif|bmp|webp|svg)$/i;
  // pdf
  const pdfRegex = /^application\/pdf$/i;
  // 视频
  const videoExtensions =
    /^video\/(mp4|avi|mov|wmv|flv|mkv|webm|mpeg|mpg|m4v)$/i;
  // word
  const wordMimeTypes = [
    "application/msword", // .doc
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .docx
  ];
  //  最后的结果
  const result = [];
  console.log(File);
  Object.entries(File).forEach(([attr, value]) => {
    const promise = new Promise((resolve, reject) => {
      if (imageRegex.test(File[attr].type)) {
        resolve(uploadImageAll(File[attr], callback));
      } else if (pdfRegex.test(File[attr].type)) {
        resolve(uploadFileMore(File[attr], callback));
      } else if (videoExtensions.test(File[attr].type)) {
        resolve(uploadVideoMore(File[attr], callback));
      } else if (wordMimeTypes.includes(File[attr].type)) {
        resolve(uploadWordMore(File[attr], callback));
      } else {
        window.alert("格式不对");
      }
    });
    result.push(promise);
  });

  Promise.all(result).then((item) => {
    callback(item);
  });
}

// 多图上传方法
async function uploadImageAll(File, callback) {
  let formData = new FormData();
  formData.append("image", File);
  formData.append("module", "cp");
  axios.defaults.headers.common["access-token"] = "你自己的token";

  let Filename = File.name; // 名字

  const dataresult = await axios({
    url: "你自己上传图片的接口",
    method: "post",
    data: formData,
  });
  if (dataresult.data.code == 200) {
    let obj = {
      url: dataresult.data.data.path_real,
      params: {
        name: `${Filename.replace(/\.[^.]+$/, "")}`,
        isBorder: true, // 是否显示边框,默认false
        isShadow: true, // 是否显示阴影,默认false
        isRadius: true, // 是否显示圆角,默认false
        width: "60%", // 图片的宽度,默认100%,可配置百分比,也可配置像素值
        height: "auto", // 图片的高度,默认auto
      },
    };
    return obj;
  } else {
    window.alert("上传失败");
  }
}

// 多文件上传PDF方法
async function uploadFileMore(File, callback) {
  let formData = new FormData();
  formData.append("file", File);
  formData.append("module", "monthly");
  axios.defaults.headers.common["access-token"] = "你自己的token";

  let Filename = File.name; // 名字

  const dataresult = await axios({
    url: "你自己上传pdf的接口",
    method: "post",
    data: formData,
  });
  if (dataresult.data.code == 200) {
    let obj = {
      url: dataresult.data.data.path_real,
      params: {
        name: `${Filename.replace(/\.[^.]+$/, "")}`,
        isBorder: true, // 是否显示边框,默认false
        isShadow: true, // 是否显示阴影,默认false
        isRadius: true, // 是否显示圆角,默认false
        width: "60%", // 图片的宽度,默认100%,可配置百分比,也可配置像素值
        height: "auto", // 图片的高度,默认auto
      },
    };
    return obj;
  } else {
    window.alert("上传失败");
  }
}

// 多文件上传video方法

async function uploadVideoMore(File, callback) {
  let formData = new FormData();
  formData.append("video", File);
  formData.append("module", "plan/video");
  axios.defaults.headers.common["access-token"] = "你自己的token";

  let Filename = File.name; // 名字

  const dataresult = await axios({
    url: "你自己是上传视频的接口",
    method: "post",
    data: formData,
  });
  if (dataresult.data.code == 200) {
    let obj = {
      url: dataresult.data.data.path_real,
      params: {
        name: `${Filename.replace(/\.[^.]+$/, "")}`,
        isBorder: true, // 是否显示边框,默认false
        isShadow: true, // 是否显示阴影,默认false
        isRadius: true, // 是否显示圆角,默认false
        width: "60%", // 图片的宽度,默认100%,可配置百分比,也可配置像素值
        height: "auto", // 图片的高度,默认auto
      },
    };
    return obj;
  } else {
    window.alert("上传失败");
  }
}

// 多文件上传word方法
async function uploadWordMore(File, callback) {
  let formData = new FormData();
  formData.append("file", File);
  formData.append("module", "monthly");
  axios.defaults.headers.common["access-token"] = "你自己的token";

  let Filename = File.name; // 名字

  const dataresult = await axios({
    url: "你自己是上传word的接口",
    method: "post",
    data: formData,
  });
  if (dataresult.data.code == 200) {
    let obj = {
      url: dataresult.data.data.path_real,
      params: {
        name: `${Filename.replace(/\.[^.]+$/, "")}`,
        isBorder: true, // 是否显示边框,默认false
        isShadow: true, // 是否显示阴影,默认false
        isRadius: true, // 是否显示圆角,默认false
        width: "60%", // 图片的宽度,默认100%,可配置百分比,也可配置像素值
        height: "auto", // 图片的高度,默认auto
      },
    };
    return obj;
  } else {
    window.alert("上传失败");
  }
}

export { uploadAllMore };

单文件上传

ts
import axios from "axios";

// 上传文件核心
function uploadAll(File, callback) {
  console.log(File.type);
  let type = File.type;
  // 图片
  const imageRegex = /^image\/(jpg|jpeg|png|gif|bmp|webp|svg)$/i;

  if (imageRegex.test(type)) {
    uploadImage(File, callback);
    return;
  }

  // PDF

  const pdfRegex = /^application\/pdf$/i;
  if (pdfRegex.test(type)) {
    uploadFile(File, callback);
    return;
  }

  // video
  const videoExtensions =
    /^video\/(mp4|avi|mov|wmv|flv|mkv|webm|mpeg|mpg|m4v)$/i;
  if (videoExtensions.test(type)) {
    uploadVideo(File, callback);
    return;
  }

  // word
  const wordMimeTypes = [
    "application/msword", // .doc
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .docx
  ];
  if (wordMimeTypes.includes(type)) {
    uploadWord(File, callback);
    return;
  }
}

// 单图上传图片接口
function uploadImage(File, callback) {
  let formData = new FormData();
  formData.append("image", File);
  formData.append("module", "cp");
  axios.defaults.headers.common["access-token"] = "你自己的token";
  axios({
    url: "你自己上传图片的接口",
    method: "post",
    data: formData,
  }).then((res) => {
    console.log(res);
    if (res.data.code == 200) {
      const url = res.data.data.path_real;
      console.log(url);
      callback(url, {
        name: File.name,
        isBorder: true, // 是否显示边框,默认false
        isShadow: true, // 是否显示阴影,默认false
        isRadius: true, // 是否显示圆角,默认false
        width: "60%", // 图片的宽度,默认100%,可配置百分比,也可配置像素值
        height: "auto", // 图片的高度,默认auto
      });
    } else {
      callback(false);
    }
  });
}

// 单文件pdf上传文件代码

function uploadFile(File, callback) {
  console.log(File);
  let formData = new FormData();
  formData.append("file", File);
  formData.append("module", "monthly");
  axios.defaults.headers.common["access-token"] = "你自己的token";
  axios({
    url: "你自己上传pdf的接口",
    method: "post",
    data: formData,
  }).then((res) => {
    console.log(res);
    if (res.data.code == 200) {
      const url = res.data.data.path_real;
      console.log(url);
      callback(url, {
        name: File.name,
        isBorder: true, // 是否显示边框,默认false
        isShadow: true, // 是否显示阴影,默认false
        isRadius: true, // 是否显示圆角,默认false
        width: "60%", // 图片的宽度,默认100%,可配置百分比,也可配置像素值
        height: "auto", // 图片的高度,默认auto
      });
    } else {
      callback(false);
    }
  });
}

// 单文件上传video方法

async function uploadVideo(File, callback) {
  let formData = new FormData();
  formData.append("video", File);
  formData.append("module", "plan/video");
  axios.defaults.headers.common["access-token"] = "你自己的token";

  let Filename = File.name; // 名字

  const dataresult = await axios({
    url: "你自己上传video的接口",
    method: "post",
    data: formData,
  });
  if (dataresult.data.code == 200) {
    let obj = {
      url: dataresult.data.data.path_real,
      params: {
        name: `${Filename.replace(/\.[^.]+$/, "")}`,
        isBorder: true, // 是否显示边框,默认false
        isShadow: true, // 是否显示阴影,默认false
        isRadius: true, // 是否显示圆角,默认false
        width: "60%", // 图片的宽度,默认100%,可配置百分比,也可配置像素值
        height: "auto", // 图片的高度,默认auto
      },
    };
    callback(obj);
  } else {
    window.alert("上传失败");
  }
}

// 单文件word上传

async function uploadWord(File, callback) {
  let formData = new FormData();
  formData.append("file", File);
  formData.append("module", "monthly");
  axios.defaults.headers.common["access-token"] = "你自己的token";
  axios({
    url: "你自己上传word的接口",
    method: "post",
    data: formData,
  }).then((res) => {
    console.log(res);
    if (res.data.code == 200) {
      const url = res.data.data.path_real;
      console.log(url);
      callback(url, {
        name: File.name,
        isBorder: true, // 是否显示边框,默认false
        isShadow: true, // 是否显示阴影,默认false
        isRadius: true, // 是否显示圆角,默认false
        width: "60%", // 图片的宽度,默认100%,可配置百分比,也可配置像素值
        height: "auto", // 图片的高度,默认auto
      });
    } else {
      callback(false);
    }
  });
}

export { uploadAll };