• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a rich text editor for Vue 2.x

my-vue-editor

Travis Packagist Plugin on redmine.org

A rich text editor based on Vue2.x
中文文档

Demo

click here to see demo
More demo please refer to the example directory

Introduction

Our editor is based on vue-html5-editor secondary development. Thanks to its author PeakTai for providing a concise rich text editor plug-in, on the basis of which we have rewritten the native method and extended the functionality.

Install

npm install my-vue-editor

Introduced as a plug-in

import Vue from 'vue'
import myVueEditor from 'my-vue-editor'
Vue.use(myVueEditor, options)

Global introduction

<script src="serverpath/vue.js"></script>
<script src="serverpath/dist/my-vue-editor.js"></script>

Installed by the global variable myVueEditor

Vue.use(myVueEditor, options)

Use

<my-vue-editor :content="content" @change='ctnChange' @imageUpload='imageUpload'></my-vue-editor>

Configuration

Items       Type           Description
name     String Custom component name, the default is my-vue-editor
modules     Array Modules need to use
icons   Object     Covering the specified module's icon
commands Object     Custom command
shortcut Object     Custom shortcut
extendModules Array     Custom module
Any built-in module name Object     Overwrite the properties of the corresponding built-in module

example

Vue.use(myVueEditor, {
  // Overlay built-in module's icon
  icons: {
    image: 'iui-icon iui-icon-pic',
    indent: 'iui-icon iui-icon-insert'
  },
  // Modules in use
  modules: [
    'font',
    'bold',
    'italic',
    'underline',
    'linethrough',
    'ul',
    'indent',
    'align',
    'image',
    'quote',
    'todo',
    // This is a custom module
    'customSave'
  ],
  // Overlay image module's configuration
  image: {
    maxSize: 5120 * 1024,
    imgOccupyNewRow: true,
    compress: {
      width: 1600,
      height: 1600,
      quality: 0.8
    }
  },
  // Overlay font module's configuration
  font: {
    config: {
      'xx-large': {
        fontSize: 6,
        name: 'H1'
      },
      'medium': {
        fontSize: 3,
        name: 'H2'
      },
      'small': {
        fontSize: 2,
        name: 'H3'
      },
      default: 'medium'
    },
    // Modify the font module's module inspect mechanism to inspect via style and tag name
    inspect (add) {
      add('style', {
        fontSize: ['xx-large', 'x-large', 'large', 'medium', 'small']
      }).add('tag', 'font')
    }
  },
  // Overlay ul module's configuration
  ul: {
    // When the ul module is inspected, disabled all but itself
    exclude: 'ALL_BUT_MYSELF',
    // When the ul module is clicked, execute the following method
    handler (rh) {
     console.log('i am ul!')
     rh.editor.execCommand('insertUnorderedList')  
    }
  },
  // When the ul module is inspected, disabled image, todo and ul module
  quote: {
    exclude: ['image', 'todo', 'ul']
  },
  // Customize an command named getAllTexts that prints out all the text nodes under the current range object
  commands: {
    getAllTexts (rh, arg) {
      console.log(rh.getAllTextNodeInRange())
    }
  },
  shortcut: {
    // Custom a shortcut key, when you press the command + s, execute the save function
    saveNote: {
      metaKey: true,
      keyCode: 83,
      handler (editor) {
        save()
      }
    }
  },
  // Customize a module, a alert pops up when you click on the module icon
  extendModules: [{
     name: 'smile',
     icon: 'iui iui-icon-smile'
     handler (rh) {
      alert('smile~~')
     }
  }]
})

Event

Event name       Description         
change     Trigger when editor content changes, parameter is up-to-date content data
imageUpload Trigger when uploading images, parameters include the corresponding data of the image,
replaceSrcAfterUploadFinish:Used to replace the src attribute of img from the base64 format to the url returned by the server when the upload is successful)
deleteImgWhenUploadFail:Used to delete the current picture when the upload fails

Modify the built-in module

Add a parameter with a built-in module's name as it's key, will cover the built-in module's original properties(See all built-in modules and their configuration items in src/modules directory)

Take the image module as an example

Vue.user(myVueEditor, {
  image: {
    // Modify the image module's icon
    icon: 'iui-pic',
    // Cover the original compression parameters, so that the image is not compressed when uploaded
    compress: null,
    // Can not upload the same image repeatedly
    canUploadSameImage: false
  }
})

Cutom module

Extend the module with the extendModules configuration item We provide some common module configuration items

Item       Type           Description  
name     String module's name
icon     String module icon's className,The fontAwesome icon is used by default
exclude     String Array disabled modules When current module is inspected
When value is 'ALL' means disable all modules include current module
When value is'ALL_BUT_MYSELF', means disabled all modules but current module
When value is type of Array, Input module name to be disabled
inspect     Function module inspect,When the cursor is in the list, the list module is highlighted, that is, the list module is inspected, which is based on its UL label as a test basis
The first argument to the function is a method named add, which called to add the module's inspection basis. When there are multiple inspection bases, please call chaining
The first parameter of the add method indicates what path to test. The optional is 'tag' 'style' and 'attribute'
When parameter 1 is 'tag', parameter 2 passes in a tag name string
When parameter 1 is 'style', parameter 2 is an object with styleName as key and styleValue as value. Note styleName use the hump form(ex:fontSize),When there are multiple styleValue please use the form of Array
When parameter 1 is 'attribute', parameter 2 is an object whose key is attribute name and attribute value is value, and note that if any value is desired, pass in ''(ex:add('attribute', {'data-todo': ''}))
handler     Function What to do when you click the module
Parameter 1 is range-handler instance, through which can you get the Vue instance of the current editor and the method to operate range
We do not recommend operating range directly in the handler, but should encapsulate the range action as a command, calling the command via the execCommand method under the editor instance in the handler
tab     Vue instance Conventional modules use handlers for handling clicks, and if you want to implement UI (drop-down list of font modules) or more complex logic (left-to-right switching of align modules), use tab
init     Function Corresponds to the editor component life cycle created, the parameter 1 is the editor instance. Note that the execCommand method can not be used at this time
mounted     Function Corresponds to the editor component life cycle mounted, the parameter 1 is the editor instance.
updated     Function Corresponds to the editor component life cycle updated, the parameter 1 is the editor instance.
destroyed     Function Corresponds to the editor component life cycle beforeDestroy, the parameter 1 is the editor instance.

More Repositories

1

just-react

「React技术揭秘」 一本自顶向下的React源码分析书
JavaScript
6,120
star
2

big-react

跟着我,从0实现React18
TypeScript
3,703
star
3

fe-hunter

每天一道题,3个月后,你就是面试小能手,答题还能赚钱哦
JavaScript
1,667
star
4

second-curve

反内卷、高稳定、有前景的职业发展路径
TypeScript
186
star
5

react-design

《React设计原理》勘误、资料、交流
149
star
6

react18-demo

react18-demo for debug
JavaScript
43
star
7

react-interview

卡老师视频详解React面试题
35
star
8

Crimonland

一款第三人称射击游戏,拥有良好的打击感
JavaScript
21
star
9

react53

交互式React进阶课程
JavaScript
20
star
10

wx-robot

基于 wechat4u 、ThinkJs 、Vue 实现的微信机器人
JavaScript
9
star
11

react53-doc

五年React 三年模拟
8
star
12

sujs

Unopinionated, fast , simple web framework for node
JavaScript
7
star
13

feishu-robot

JavaScript
5
star
14

vue-image-lazyload

a lightful image lazyload plugin for Vue. 指令风格的轻量级滚动懒加载插件
Vue
4
star
15

chaoxing-mooc-helper

超星mooc的浏览器插件
JavaScript
4
star
16

city-picker

原生js实现的城市选择器
JavaScript
3
star
17

Summon

小朋友大朋友转动脑筋的益智游戏
JavaScript
2
star
18

betaSu.github.io

This is my blog~
HTML
2
star
19

LaGouApp

仿拉勾网的webApp
JavaScript
2
star
20

wxRobotApp

微信机器人app
JavaScript
1
star
21

kasong-school

卡颂课堂小鹅通插件
1
star
22

myAngularJs

I'm trying to build my own MVVM framework,which base on AngularJs
JavaScript
1
star
23

react-roadmap

2020年React学习路线图
1
star
24

zhihu

知乎爬虫,用于增长实验
JavaScript
1
star
25

doc-demo

前端知识点
1
star
26

algorithms-note

算法(第四版)学习笔记
Java
1
star
27

react-alpha-package

基于18.0.0-alpha,react与react-dom的包
JavaScript
1
star