vditor轻量级md编辑器

vditor一种轻量级的Markdown编辑器

安装vditor
yarn add vditor 或者 npm install vditor --save

使用

<template>
  <div class="editorRef">
    <div ref="editorRef" class="vditor"></div>
  </div>
</template>
<script >
import { ElMessage } from "element-plus";
import {
  defineComponent,
  onMounted,
  onBeforeUnmount,
  ref,
  nextTick,
  toRefs,
  reactive,
} from "vue";
import Vditor from "vditor";
import "vditor/dist/index.css";
import { getApiUrlNew } from "@/utils/auth";
import upload from "@/utils/upload";
export default defineComponent({
  name: "Vditor",
  filters: {},
  props: {
    contentHtml: {
      type: String,
      default: "",
    },
  },
  setup(porps, content) {
    const state = reactive({
      isMobile: window.innerWidth <= 960,
    });
    const editorRef = ref();
    let instance;
    // 初始化 方法
    function init() {
      instance = new Vditor(editorRef.value, {
        width: state.isMobile ? "100%" : "100%",
        minHeight: 700,
        mode: "sv", //可选模式:sv, ir, wysiwyg
        toolbarConfig: {
          pin: true,
        },
        theme: "classic", //主题:classic, dark
        icon: "material", //图标风格:ant, material
        toolbar: [
          "emoji",
          "headings",
          "bold",
          "italic",
          "strike",
          "link",
          "|",
          "list",
          "ordered-list",
          "check",
          "outdent",
          "indent",
          "|",
          "quote",
          "line",
          "code",
          "inline-code",
          "insert-before",
          "insert-after",
          "|",
          "upload",
          "table",
          "|",
          "undo",
          "redo",
          "|",
          "edit-mode",
          {
            name: "more",
            toolbar: [
              "both",
              "code-theme",
              "content-theme",
              "export",
              "outline",
              "preview",
              "devtools",
              "info",
              "help",
            ],
          },
        ],
        counter: "999999", //允许输入的最大值
        typewriterMode: true,
        cache: {
          enable: false,
          actions: ["desktop", "tablet", "mobile", "mp-wechat", "zhihu"],
          delay: 1000,
          hljs: {
            enable: true,
            lineNumber: false,
            style: "github",
          },
          markdown: {
            autoSpace: false,
            codeBlockPreview: true,
            fixTermTypo: false,
            footnotes: true,
            linkBase: "",
            linkPrefix: "",
            listStyle: false,
            mark: false,
            mathBlockPreview: true,
            paragraphBeginningSpace: false,
            sanitize: true,
            toc: false,
          },
          math: {
            engine: "KaTeX",
            inlineDigit: false,
            macros: {},
          },
          width: "auto",
          mode: "both",
          // index: 999999,
          // outline:{
          //   enable:true,
          //   position:'left'
          // }
        },
        preview: {
          delay: 100,
          show: !state.isMobile,
        },
        after: () => {
         
            instance.setValue(porps.contentHtml);
         
        },
        input: (val) => {
          content.emit("update:contentHtml", val);
        },
        // 这里写上传
        upload: {
          max: 5 * 1024 * 1024,
          // linkToImgUrl: '',
          handler(file) {
            console.log(file);
            upload.uploadFile(getApiUrlNew(), file[0]).then((res) => {
              let type = file[0].type;
              let name = file[0].name;
              onloadCallback(res, type, name);
            });
          },
        },
      });
    }
    const onloadCallback = (oEvent, type, name) => {
      if (oEvent.code != 200) {
        return ElMessage({
          type: "error",
          message: "上传失败,请重新上传",
        });
      }

      let imgMdStr = "";
      if (type.indexOf("image") > -1) {
        imgMdStr = `![](${process.env.VUE_APP_BASE_HOST + oEvent.url})`;
      } else {
        imgMdStr = `![${name}](${process.env.VUE_APP_BASE_HOST + oEvent.url})`;
      }

      if (instance) {
        instance.insertValue(imgMdStr);
      }
    };
   
    // 初始化编辑器
    onMounted(() => {
      nextTick(() => {
        init();
      });
    });
    // 销毁
    onBeforeUnmount(() => {
      instance.destroy();
      instance = null;
    });
    // 获取编辑器内容
    function getEditValue() {
      return instance.getValue();
    }
    return {
      editorRef,
      ...toRefs(state),
      getEditValue,
    };
  },
  computed: {},
  components: {},
  data() {
    return {};
  },
  methods: {},
  mounted() {},
});
</script>
<style lang="scss" scoped>
</style>

详情可参考文档

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 3 中按需引入 v-md-editor 编辑器,你可以按照以下步骤进行操作: 1. 首先,安装 v-md-editor 包及其相关依赖: ``` npm install v-md-editor @kangc/v-md-editor @kangc/v-md-editor/lib/theme/style/vuepress.css ``` 2. 在你的 Vue 3 项目中,创建一个名为 `vmdeditor.js` 的文件,用于配置按需引入: ```javascript // vmdeditor.js import { createApp } from 'vue' import VMdEditor from '@kangc/v-md-editor' import '@kangc/v-md-editor/lib/style/base-editor.css' import vuepressTheme from '@kangc/v-md-editor/lib/theme/vuepress.js' // 引入你需要使用的编辑器组件 import { Vditor, CodeMirror, Prism, TextArea } from '@kangc/v-md-editor/lib/codemirror-editor' // 注册编辑器组件 VMdEditor.use(vuepressTheme, { Prism, CodeMirror, TextArea }) // 创建 Vue 应用实例 const app = createApp({}) app.use(VMdEditor) app.mount('#app') ``` 3. 在你的 Vue 3 项目中的入口文件(通常是 `main.js`)中引入 `vmdeditor.js` 文件: ```javascript // main.js import { createApp } from 'vue' import App from './App.vue' import './vmdeditor.js' // 引入 vmdeditor.js 文件 createApp(App).mount('#app') ``` 4. 现在,你可以在 Vue 组件中使用 v-md-editor 编辑器组件了。例如,在 `App.vue` 组件中使用 Vditor 编辑器: ```vue <template> <v-md-editor> <Vditor v-model="content" /> </v-md-editor> </template> <script> export default { data() { return { content: '' } } } </script> ``` 通过以上步骤,你可以按需引入 v-md-editor 编辑器,并在 Vue 3 项目中使用。记得根据你的需求选择合适的编辑器组件进行引入和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值