Vue中使用editor.md(1):简单使用

这篇具有很好参考价值的文章主要介绍了Vue中使用editor.md(1):简单使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

0. 背景

Vue项目中添加一个markdown编辑器,选择使用editor.md,记录在Vue项目中的简单使用。

1. 环境配置

1.1 下载editor.md

官网地址:http://pandao.github.io/editor.md/
Vue中使用editor.md(1):简单使用

项目文件解压后放入:public/static/
Vue中使用editor.md(1):简单使用

1.2 下载jQuery

下载地址:https://www.jsdelivr.com/package/npm/jquery?tab=files&version=1.12.0
Vue中使用editor.md(1):简单使用
下载后放入editor.md根目录下

1.3 下载scriptjs

cnpm install --save scriptjs

2. 实现

2.1 editor.md的配置文件

Vue中使用editor.md(1):简单使用

MarkdownConfig.js

const defaultConfig = {
  width: '100%',
  height: 600,
  path: '/static/editor.md/lib/',
  // theme: 'dark',
  // previewTheme: 'dark',
  // editorTheme: 'pastel-on-dark',
  markdown: '', // 默认填充内容
  lineWrapping: true, // 编辑框不换行
  codeFold: true, // 代码折叠
  placeholder: '请输入...',
  syncScrolling: true,
  saveHTMLToTextarea: true, // 保存 HTML 到 Textarea
  searchReplace: true,
  watch: true, // 实时预览
  htmlDecode: 'style,script,iframe|on*', // 开启 HTML 标签解析,为了安全性,默认不开启
  toolbar: true, // 工具栏
  previewCodeHighlight: true, // 预览 HTML 的代码块高亮,默认开启
  emoji: true,
  taskList: true,
  tocm: true, // Using [TOCM]
  tex: true, // 开启科学公式TeX语言支持,默认关闭
  flowChart: true, // 开启流程图支持,默认关闭
  sequenceDiagram: true, // 开启时序/序列图支持,默认关闭,
  dialogLockScreen: false, // 设置弹出层对话框不锁屏,全局通用,默认为true
  dialogShowMask: false, // 设置弹出层对话框显示透明遮罩层,全局通用,默认为true
  dialogDraggable: false, // 设置弹出层对话框不可拖动,全局通用,默认为true
  dialogMaskOpacity: 0.4, // 设置透明遮罩层的透明度,全局通用,默认值为0.1
  dialogMaskBgColor: '#000', // 设置透明遮罩层的背景颜色,全局通用,默认为#fff
  // imageUpload: false,
  // imageFormats: ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'],
  // imageUploadURL: 'http://127.0.0.1:8030/api/files/uploadSingleFile',
  // onload: function() {
  //    // this.fullscreen();
  //    // this.unwatch();
  //    // this.watch().fullscreen();
  //    // this.setMarkdown("#PHP");
  //    // this.width("100%");
  //    // this.height(480);
  //    // this.resize("100%", 640);
  // },
}
export {
  defaultConfig
}

2.2 editor.md的组件

Vue中使用editor.md(1):简单使用

<template>
  <div class="markdown-editor-box">
    <link rel="stylesheet" href="./static/editor.md/css/editormd.min.css">
    <div :id="editorId"></div>
  </div>
</template>

<script>
import scriptjs from 'scriptjs'
import { defaultConfig } from '@/config/MarkdownConfig'

export default {
  name: 'MarkdownEditor',
  props: {
    editorId: {
      type: String,
      default: 'markdown-editor'
    },
    onchange: {
      type: Function
    },
    config: {
      type: Object
    },
    initData: {
      type: String
    },
    initDataDelay: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      editor: null,
      editorLoaded: false
    }
  },
  methods: {
    fetchScript (url) {
      return new Promise(resolve => {
        scriptjs(url, () => {
          resolve()
        })
      })
    },
    getConfig () {
      return { ...defaultConfig, ...this.config }
    },
    getEditor () {
      return this.editor
    },
    getDoc () {
      return this.doc
    },
    watch () {
      return this.editor.watch()
    },
    unwatch () {
      return this.editor.unwatch()
    },
    previewing () {
      return this.editor.previewing()
    },
    getHTML () {
      return this.editor.getHTML()
    },
    getMarkdown () {
      return this.editor.getMarkdown()
    },
    setMarkdown (markdown) {
      return this.editor.setMarkdown(markdown)
    },
    initEditor () {
      (async () => {
        await this.fetchScript('./static/editor.md/jquery-1.10.2.min.js')
        await this.fetchScript('/static/editor.md/editormd.min.js')
        this.$nextTick(() => {
          const editor = window.editormd(this.editorId, this.getConfig())
          console.log('init editor ', editor)
          editor.on('load', () => {
            setTimeout(() => {
              this.editorLoaded = true
              this.initData && editor.setMarkdown(this.initData)
            }, this.initDataDelay)
          })
          this.onchange && editor.on('change', () => {
            const html = editor.getPreviewedHTML()
            this.onchange({
              markdown: editor.getMarkdown(),
              html: html,
              text: window.$(html).text()
            })
          })
          this.editor = editor
        })
      })()
    }
  },
  mounted () {
    this.initEditor()
  },
  watch: {
    initData: function (newVal) {
      if (newVal) {
        this.editorLoaded && this.editor.setMarkdown(newVal)
      }
    }
  }
}
</script>

<style scoped lang="less">

</style>

3. 测试

3.1 使用

在其他文件内使用

<markdown-editor ref="markdownView"></markdown-editor>

3.2 结果

Vue中使用editor.md(1):简单使用文章来源地址https://www.toymoban.com/news/detail-490559.html

x. 参考

  1. VUE 使用 editor.md (一)
  2. vue整合editor.md
  3. markdown编辑器之editormd使用整合

到了这里,关于Vue中使用editor.md(1):简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 给vue的页面添加背景图片

    1.在tempalate下面建设两个div,两个div建设在同一个div内 2.在第一个div内添加一个图片 img   :src=\\\"imgSrc\\\" width=\\\"100%\\\" height=\\\"100%\\\" alt=\\\"\\\" / 3.js里面定义图片的返回路径 在data {return内}因为存放路径没办法读取所以就 把类型写为require imgSrc:require( \\\'../../assets/images/bg2.jpg\\\' ) 4.给他俩添加styl

    2024年02月11日
    浏览(34)
  • 【unity】Runtime Editor的简单使用

    版本v2.26 定位、旋转、伸缩变形句柄 添加句柄预制体 位置:Battlehub→RTEditor→Content→Runtime→RTHandles→Prefabs PositionHandle(移动句柄) RotationHandle(旋转句柄) ScaleHandle(缩放句柄) 将预制体拖入场景中 下面以PositionHandle(移动句柄)为例,其余句柄使用方法都一样 选中编辑

    2024年02月09日
    浏览(28)
  • 简版的富文本编辑器、VUE+ElementUI 富文本编辑器 element ui富文本编辑器的使用(quill-editor) 不好用你来打我!全网醉简单!要复杂的别来!

    实现效果   1.安装插件 npm install vue-quill-editor --save 2.安装成功后在package.json中查看 3.在main.js中全局引入插件 4.页面实现 感谢老哥: ElementUI生成富文本编辑器 https://blog.csdn.net/keplerm/article/details/123379511?spm=1001.2101.3001.6650.9utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCom

    2024年02月16日
    浏览(55)
  • 一款好看的markdown编辑器:md-editor-v3

    md-editor-v3 的 github地址 及使用文档 md-editor-v3 预览效果地址 mavon-editor文本编辑器上传图片用法 Vue富文本编辑器-mavon-editor文本编辑器 v-md-editor使用 生成el-tree文章目录滚动跟随高亮 English | 中文 vue3 环境的 Markdown 编辑器,使用 jsx 和 typescript 语法开发,支持在 tsx 项目使用。 文

    2024年02月07日
    浏览(31)
  • 【C++】做一个飞机空战小游戏(四)——给游戏添加背景音乐(多线程技巧应用)

      [导读]本系列博文内容链接如下: 【C++】做一个飞机空战小游戏(一)——使用getch()函数获得键盘码值 【C++】做一个飞机空战小游戏(二)——利用getch()函数实现键盘控制单个字符移动 【C++】做一个飞机空战小游戏(三)——getch()函数控制任意造型飞机图标移动 【C++】做一个飞

    2024年02月14日
    浏览(31)
  • vue element ui table行点击添加自定义行背景色

       tablecpt组件,tablecpt/index.vue   引入tablecpt组件    mixin文件

    2024年02月11日
    浏览(36)
  • js-md5的简单使用

    MD5(单项散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2、MD3和MD4发展而来。 功能:         1.输入任意长度的信息,经过处理,输出32位的信息;                 2.不同的输入得到的不同的结果(唯一性);         3.根据32位的输入结果不

    2024年04月16日
    浏览(13)
  • monaco-editor基本使用以及monaco-editor封装成vue组件

    以vue2项目为例 安装依赖 配置vue.config.js 使用monaco-editor前,需要先准备一个容器来挂载monaco-editor实例 创建monaco-editor实例 使用monaco.editor.create方法创建monaco-editor实例,create方法的第一个参数接收一个dom元素,第二个参数可选,接收一个IStandaloneEditorConstructionOptions配置对象 关

    2024年02月06日
    浏览(32)
  • Vue3使用Monaco-editor

    Monaco-editor,一个vs code 编辑器,需要将其集成到项目。不说闲话了,直接上代码。  npm地址:https://www.npmjs.com/package/monaco-editor 中文文档:https://aydk.site/editor/ vite.config.ts  首先先封装个hook如下: @/hooks/useMonacoEditor.hook.ts  然后调用上面 useMonacoEditor 封装editor编辑器组件 @/com

    2024年02月06日
    浏览(35)
  • 【前端技巧】js-md5的简单使用

    @Author:Outman @Date:2023-03-27 MD5(单项散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2、MD3和MD4发展而来。 功能: 1.输入任意长度的信息,经过处理,输出32位的信息;2.不同的输入得到的不同的结果(唯一性);3.根据32位的输入结果不可能反推出输入的信息

    2024年02月12日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包