一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

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

服務(wù)器之家 - 編程語(yǔ)言 - JavaScript - React - React中使用Vditor自定義圖片詳解

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

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

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

安裝

npm install vditor -s

引用

導(dǎo)入依賴(lài)包

import Vditor from "vditor";

導(dǎo)入樣式

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

使用示例

export default class Vditor extends Component {
 constructor(props) {
   super(props);
   this.state = {
    editValue: ""
   };
 }
 componentDidMount = () => {
  //組件掛載完成之后調(diào)用 注意一定要在組件掛載完成之后調(diào)用 否則會(huì)找不到注入的DOM
  this.createVidtor({ value: this.state.editValue });
 }
 //創(chuàng)建編輯器 下面會(huì)詳解
 createVidtor = params => {
   let { value } = params;
   value = value ? value : " ";
   let that = this;
   const vditor = new Vditor("vditor", {
     height: 800,
     mode: "ir", //及時(shí)渲染模式
     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: "發(fā)布文章",
         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,初始化編輯器的時(shí)候使用自定義的ID即可
 render() {
  <div className="editorWrap">
    <div id="vditor" />
  </div>
 }
}

示例:

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

功能使用

新建對(duì)象

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

新建對(duì)象時(shí)第一個(gè)參數(shù)ID,要對(duì)應(yīng)上再render里面注入的ID

option參數(shù)

tip:只列舉一下常用參數(shù),其他的參數(shù)請(qǐng)參照 官方API

 

參數(shù) 說(shuō)明
height 配置編輯器高度
mode 編輯器模式
wysiwyg:所見(jiàn)即所得2
ir:及時(shí)渲染
sv:分屏模式
placeholder 占位符
toolbar 工具欄

 

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

默認(rèn)工具欄

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",
        ],
      }]

對(duì)應(yīng)工具欄展示:

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: "發(fā)布文章",
      className: "right",
      icon: `<img style="height: 16px" src="https://img.58cdn.com.cn/escstatic/docs/imgUpload/idocs/publish.svg"/>`,
      click() {
        that.publishDoc();
      }
    }
  ]
});
//tip:在調(diào)用本類(lèi)封裝的方法時(shí)提前把this賦值給其他方法內(nèi)的變量,在Vditor內(nèi)部改變了this指向

 

參數(shù) 說(shuō)明
hotkey 熱鍵配置
name 功能區(qū)分(唯一性)
tip 懸浮提示
className UI展示 right靠右
icon 按鈕圖標(biāo)
click 點(diǎn)擊事件

 

示例:

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

獲取值

saveDoc = () => {
 //在初始化時(shí)已經(jīng)把vditor賦值到this對(duì)象上 可直接通過(guò)getValue方法獲取當(dāng)前編輯器的值
  let mdValue = this.vditor && this.vditor.getValue();
 //獲取完值業(yè)務(wù)保存就行 這里不再詳細(xì)寫(xiě)本人的保存方法了
 ...
}

賦值

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

自定義圖片上傳

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);
   }
 }
});
//此接口里面調(diào)用的是自己的圖片上傳 業(yè)務(wù)方自行實(shí)現(xiàn)
handleImageUpload = (file, callback) => {
  const reader = new FileReader();
  let formdata = new FormData();
  formdata.append("files", file[0]);
  reader.onload = () => {
    // setTimeout 模擬異步上傳圖片
    // 當(dāng)異步上傳獲取圖片地址后,執(zhí)行callback回調(diào)(參數(shù)為imageUrl字符串),即可將圖片地址寫(xiě)入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]);
};

 

參數(shù) 說(shuō)明
accept 接收文件類(lèi)型(我這邊只做了圖片上傳)
multiple 是否多選
filename 格式化文件名
handler 點(diǎn)擊數(shù)觸發(fā)方法
url 配置此方法時(shí)可實(shí)現(xiàn)圖片粘貼并上傳

 

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

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

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

上傳完成后處理

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

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

總結(jié)

以上是本人在接入vditor編輯器是的一些使用總結(jié),更多相關(guān)React Vditor內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 无码骚夜夜精品 | 鬼吹灯之天星术免费观看 | 把女的下面扒开添视频 | 日韩精品视频观看 | 成人国产在线播放 | 黑人巨鞭大战白妞10级 | 91美女在线 | 国产99久久久国产精品成人 | 国产新疆成人a一片在线观看 | 三星w699 | 国产一区二区三区在线 | a级情欲片在线观看hd | 精品无码国产AV一区二区三区 | 免费成年网站 | 亚洲网红精品大秀在线观看 | jazz欧美人免费xxxxxx | 日韩永久在线观看免费视频 | 青青青青青操 | 91免费精品国自产拍在线可以看 | 禁忌h1v1怀孕 | 9久热久爱免费精品视频在线观看 | 欧美激情 亚洲 | 国产精品免费小视频 | 亚洲高清中文字幕 | 青青青草免费 | 成人资源影音先锋久久资源网 | 激情影院免费观看 | 盲井在线 | 国产亚洲精品精品国产亚洲综合 | 亚洲香蕉视频 | 狐媚小说 | 国产99热99 | 好逼365| 成年美女黄网站色视频大全免费 | 久久综合给会久久狠狠狠 | 国产成人cao在线 | 韩国三级在线观看 完整版 韩国三级视频网站 | 欧美白虎逼| 亚洲色图第四色 | 热久久最新 | 国产一级毛片外aaaa |