zoukankan      html  css  js  c++  java
  • vue整合SimpleMDE做编辑器

    1、安装simplemde

     npm install simplemde --save    //markdown编辑器
     npm install font-awesome --save    //FontAwesome字体图标
     npm install showdown --save    //转换markdown语法
     
    

    2、在conponents下创建以下目录结构

    20200727211837611

    index.vue,进行简单配置。具体配置参照官方文档

    <template>
      <div>
        <div style="680px;display:inline;float:left" class="simplemde-container">
          <textarea :id="id"/>
        </div>
        <div
          class="simplemde-html"
          style="display: inline;float: left;margin-left: 40px;font-size: 18px;"
          v-html="converterValue"
        ></div>
      </div>
    </template>
    
    <script>
    
    import "font-awesome/css/font-awesome.min.css";
    import "simplemde/dist/simplemde.min.css";
    import SimpleMDE from "simplemde";
    import showdown from "showdown";
    
    export default {
      name: "SimplemdeMd",
      props: {
        value: {
          type: String,
          default: ""
        },
        id: {
          type: String,
          required: false,
          default: "markdown-editor-" + +new Date()
        },
        placeholder: {
          type: String,
          default: ""
        }
      },
      data() {
        return {
          simplemde: null,
          hasChange: false,
          converterValue: ""
        };
      },
      watch: {
        value(val) {
          if (val === this.simplemde.value() && !this.hasChange) return;
          this.simplemde.value(val);
        }
      },
      mounted() {
        this.simplemde = new SimpleMDE({
          element: document.getElementById(this.id),
          autofocus: true,
          toolbar: [
            "bold",
            "italic",
            "heading",
            "strikethrough",
            "quote",
            "heading-smaller",
            "heading-bigger",
            "code",
            "quote",
            "unordered-list",
            "ordered-list",
            "clean-block",
            "link",
            "image",
            "table",
            "horizontal-rule",
            "preview",
            "side-by-side",
            "fullscreen",
            "guide"
          ],
          autoDownloadFontAwesome: false,
          spellChecker: false,
          insertTexts: {
            link: ["[", "]( )"]
          },
          placeholder: this.placeholder
        });
        if (this.value) {
          this.simplemde.value(this.value);
        }
        this.simplemde.codemirror.on("change", () => {
          if (this.hasChange) {
            this.hasChange = true;
          }
          this.converterValue = new showdown.Converter().makeHtml(
            this.simplemde.value()
          );
          this.$emit("input", this.simplemde.value());
        });
      },
      destroyed() {
        this.simplemde.toTextArea();
        this.simplemde = null;
      }
    };
    </script>
    
    <style scoped>
    .simplemde-container >>> .CodeMirror {
      min-height: 150px;
      line-height: 20px;
    }
    
    .simplemde-container >>> .CodeMirror-scroll {
      min-height: 150px;
    }
    
    .simplemde-container >>> .CodeMirror-code {
      padding-bottom: 40px;
    }
    
    .simplemde-container >>> .editor-statusbar {
      display: none;
    }
    
    .simplemde-container >>> .CodeMirror .CodeMirror-code .cm-link {
      color: #1890ff;
    }
    
    .simplemde-container >>> .CodeMirror .CodeMirror-code .cm-string.cm-url {
      color: #2d3b4d;
    }
    
    .simplemde-container
      >>> .CodeMirror
      .CodeMirror-code
      .cm-formatting-link-string.cm-url {
      padding: 0 2px;
      color: #e61e1e;
    }
    .simplemde-container >>> .editor-toolbar.fullscreen,
    .simplemde-container >>> .CodeMirror-fullscreen {
      z-index: 1003;
    }
    .simplemde-html >>> *{
      margin-top: 4px;
      margin-bottom: 4px 
    } 
    </style>
    
    

    3、使用

    <template>
      <div class="components-container">
        <div class="editor-container">
          <markdown-editor
            id="contentEditor"
            ref="contentEditor"
            v-model="content"
          ></markdown-editor>
        </div>
      </div>
    </template>
    <script>
    import MarkdownEditor from "@/components/MarkdownEditor";
    
    export default {
      components: { MarkdownEditor },
      data () {
        return {
          content: '',
          html: ''
        }
      },
      methods: {
        markdown2Html() {
          import('showdown').then(showdown => {
            const converter = new showdown.Converter()
            this.html = converter.makeHtml(this.content)
          })
        }
      }
    };
    </script>
    
    

    具体效果

    20200727211854256

  • 相关阅读:
    ubuntu 右键添加出现 Open in terminal 选项
    03创建DataFrame的5种方法
    pandas——read_excel()方法学习
    Win10修改编辑hosts文件无法保存怎么办
    pandas插入一行记录
    python中的日期类型转换
    解决Ubuntu安装VM Tools请确保您已登录客户机操作系统。在客户机中装载CD驱动器启动终端,使用tar解压缩安装程序,然后执行vmware-insall.pl安装VMware Tools。
    怎么保存退出 vim 编辑
    反射
    Oracle
  • 原文地址:https://www.cnblogs.com/xlwq/p/13387720.html
Copyright © 2011-2022 走看看