激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - React - React中使用Vditor自定義圖片詳解

React中使用Vditor自定義圖片詳解

2021-12-20 16:04淺夏晴空 React

這篇文章主要介紹了React中使用Vditor自定義圖片詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

安裝

npm install vditor -s

引用

導入依賴包

import Vditor from "vditor";

導入樣式

import "vditor/src/assets/scss/index.scss";

使用示例

export default class Vditor extends Component {
 constructor(props) {
   super(props);
   this.state = {
    editValue: ""
   };
 }
 componentDidMount = () => {
  //組件掛載完成之后調用 注意一定要在組件掛載完成之后調用 否則會找不到注入的DOM
  this.createVidtor({ value: this.state.editValue });
 }
 //創建編輯器 下面會詳解
 createVidtor = params => {
   let { value } = params;
   value = value ? value : " ";
   let that = this;
   const vditor = new Vditor("vditor", {
     height: 800,
     mode: "ir", //及時渲染模式
     placeholder: "React Vditor",
     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",
       "|",
       "fullscreen",
       "edit-mode",
       {
         name: "more",
         toolbar: [
           "both",
           "code-theme",
           "content-theme",
           "export",
           "outline",
           "preview",
           "devtools",
           "info",
           "help"
         ]
       },
       "|",
       {
         hotkey: "?-S",
         name: "save",
         tipPosition: "s",
         tip: "保存",
         className: "right",
         icon: `<img style="height: 16px" src="https://img.58cdn.com.cn/escstatic/docs/imgUpload/idocs/save.svg"/>`,
         click() {
           that.saveDoc();
         }
       },
       {
         hotkey: "",
         name: "publish",
         tipPosition: "s",
         tip: "發布文章",
         className: "right",
         icon: `<img style="height: 16px" src="https://img.58cdn.com.cn/escstatic/docs/imgUpload/idocs/publish.svg"/>`,
         click() {
           that.publishDoc();
         }
       }
     ],
     after() {
       vditor.setValue(value);
     },
     blur() {
       that.saveDoc();
     },
     upload: {
       accept: "image/*",
       multiple: false,
       filename(name) {
         return name
           .replace(/[^(a-zA-Z0-9u4e00-u9fa5.)]/g, "")
           .replace(/[?\/:|<>*[]()$%{}@~]/g, "")
           .replace("/\s/g", "");
       },
       handler(files) {
         function callback(path) {
           let name = files[0] && files[0].name;
           let succFileText = "";
           if (vditor && vditor.vditor.currentMode === "wysiwyg") {
             succFileText += `
 <img           } else {
             succFileText += ` 
![${name}](${path})`;
           }
           document.execCommand("insertHTML", false, succFileText);
         }
         that.handleImageUpload(files, callback);
       },
       url(files) {
         that.handleImageUpload(files);
       }
     }
   });
   this.vditor = vditor;
   return vditor;
 };
 //首先需要在render里面注入DOM,可自定義注入DOM的ID,初始化編輯器的時候使用自定義的ID即可
 render() {
  <div className="editorWrap">
    <div id="vditor" />
  </div>
 }
}

示例:

React中使用Vditor自定義圖片詳解

功能使用

新建對象

const vditor = new Vditor("vditor", ...option);

新建對象時第一個參數ID,要對應上再render里面注入的ID

option參數

tip:只列舉一下常用參數,其他的參數請參照 官方API

 

參數 說明
height 配置編輯器高度
mode 編輯器模式
wysiwyg:所見即所得2
ir:及時渲染
sv:分屏模式
placeholder 占位符
toolbar 工具欄

 

Tip:如果要自定義工具欄的話,一定要加上默認的工具欄,不然只展示自定義的了

默認工具欄

tip:此為源碼里面copy 不用更改可直接使用,官方已定義好了快捷鍵和功能

toolbar: [
      "emoji",
      "headings",
      "bold",
      "italic",
      "strike",
      "link",
      "|",
      "list",
      "ordered-list",
      "check",
      "outdent",
      "indent",
      "|",
      "quote",
      "line",
      "code",
      "inline-code",
      "insert-before",
      "insert-after",
      "|",
      "upload",
      "record",
      "table",
      "|",
      "undo",
      "redo",
      "|",
      "fullscreen",
      "edit-mode",
      {
        name: "more",
        toolbar: [
          "both",
          "code-theme",
          "content-theme",
          "export",
          "outline",
          "preview",
          "devtools",
          "info",
          "help",
        ],
      }]

對應工具欄展示:

React中使用Vditor自定義圖片詳解

自定義按鈕

let that = this;
const vditor = new Vditor("vditor", {
  toolbar: [
    {
      hotkey: "?-S",
      name: "save",
      tipPosition: "s",
      tip: "保存",
      className: "right",
      icon: `<img style="height: 16px" src="https://img.58cdn.com.cn/escstatic/docs/imgUpload/idocs/save.svg"/>`,
      click() {
        that.saveDoc();
      }
    },
    {
      hotkey: "",
      name: "publish",
      tipPosition: "s",
      tip: "發布文章",
      className: "right",
      icon: `<img style="height: 16px" src="https://img.58cdn.com.cn/escstatic/docs/imgUpload/idocs/publish.svg"/>`,
      click() {
        that.publishDoc();
      }
    }
  ]
});
//tip:在調用本類封裝的方法時提前把this賦值給其他方法內的變量,在Vditor內部改變了this指向

 

參數 說明
hotkey 熱鍵配置
name 功能區分(唯一性)
tip 懸浮提示
className UI展示 right靠右
icon 按鈕圖標
click 點擊事件

 

示例:

React中使用Vditor自定義圖片詳解

獲取值

saveDoc = () => {
 //在初始化時已經把vditor賦值到this對象上 可直接通過getValue方法獲取當前編輯器的值
  let mdValue = this.vditor && this.vditor.getValue();
 //獲取完值業務保存就行 這里不再詳細寫本人的保存方法了
 ...
}

賦值

let { value } = params;
value = value ? value : " ";
//如果是空值的話 最好給一個空格 以免編輯器初始化時報錯
const vditor = new Vditor("vditor", {
 // value: value,
 after() {
   vditor.setValue(value);
 }
});
//tip:雖說官方也提供value直接賦值 但是在React里面不生效,就需要在after里面去調用setValue來完成賦值

自定義圖片上傳

const vditor = new Vditor("vditor", {
 upload: {
   accept: "image/*",
   multiple: false,
   filename(name) {
     return name
       .replace(/[^(a-zA-Z0-9u4e00-u9fa5.)]/g, "")
       .replace(/[?\/:|<>*[]()$%{}@~]/g, "")
       .replace("/\s/g", "");
   },
   handler(files) {
     function callback(path) {
       let name = files[0] && files[0].name;
       let succFileText = "";
       if (vditor && vditor.vditor.currentMode === "wysiwyg") {
         succFileText += `
 <img       } else {
         succFileText += ` 
![${name}](${path})`;
       }
       document.execCommand("insertHTML", false, succFileText);
     }
     that.handleImageUpload(files, callback);
   },
   url(files, callback) {
     that.handleImageUpload(files, callback);
   }
 }
});
//此接口里面調用的是自己的圖片上傳 業務方自行實現
handleImageUpload = (file, callback) => {
  const reader = new FileReader();
  let formdata = new FormData();
  formdata.append("files", file[0]);
  reader.onload = () => {
    // setTimeout 模擬異步上傳圖片
    // 當異步上傳獲取圖片地址后,執行callback回調(參數為imageUrl字符串),即可將圖片地址寫入markdown
    new Promise(resolve => {
      this.props.dispatch({
        type: "docManager/imageUpload",
        payload: { resolve, username: myInfo.userId, formdata }
      });
    }).then(res => {
      let imgurl = res.result.path;
      callback(imgurl);
    });
  };
  reader.readAsDataURL(file[0]);
};

 

參數 說明
accept 接收文件類型(我這邊只做了圖片上傳)
multiple 是否多選
filename 格式化文件名
handler 點擊數觸發方法
url 配置此方法時可實現圖片粘貼并上傳

 

React中使用Vditor自定義圖片詳解

上傳完成后接口返回的CDN地址

React中使用Vditor自定義圖片詳解

上傳完成后處理

handler(files) {
  function callback(path) {
    let name = files[0] && files[0].name;
    let succFileText = "";
    //上傳完成獲取當前編輯器模式 根據不同模式拼接不同的展示標簽
    if (vditor && vditor.vditor.currentMode === "wysiwyg") {
      succFileText += `
 <img    } else {
      succFileText += ` 
![${name}](${path})`;
    }
    //拼接完直接插入到鼠標選中位置
    document.execCommand("insertHTML", false, succFileText);
  }
  that.handleImageUpload(files, callback);
}

React中使用Vditor自定義圖片詳解

總結

以上是本人在接入vditor編輯器是的一些使用總結,更多相關React Vditor內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://segmentfault.com/a/1190000038640152

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一级美国一级 | 国产精品成人久久 | 视频一区二区精品 | 九九综合视频 | 伊人手机在线观看 | 毛片在线免费视频 | 91精品久久香蕉国产线看观看 | a级高清免费毛片av在线 | 日韩字幕在线 | 久久久免费观看完整版 | 激情综合在线 | 精品黑人一区二区三区国语馆 | 91av在线免费播放 | 久久91亚洲精品久久91综合 | 国产精品自拍99 | 日韩精品 | 国产妇女乱码一区二区三区 | 91精品国产乱码久 | wwwxxx免费视频 | 毛片视频网站 | 亚洲精品久久久久久久久久 | 色网免费观看 | 成人毛片网站 | 草人人 | 色婷婷久久久久久 | 国产亚洲精品视频中文字幕 | 99视频观看 | 欧美性猛交一区二区三区精品 | 国产一区二区三区四区五区精品 | 狠狠色噜噜狠狠狠米奇9999 | 免费国产在线观看 | 72pao成人国产永久免费视频 | 国产网站黄 | 综合图区亚洲 | 在线成人av观看 | 国产99精品视频 | 国产99精品视频 | 成品片a免人视频 | 一级做a爱片毛片免费 | 国产精品久久久久久久久久iiiii | 91极品视频在线观看 |