README.md 7.1 KB
Newer Older
Miykael_xxm's avatar
Miykael_xxm 已提交
1 2
# C Markdown 编辑器

璃白.'s avatar
璃白. 已提交
3
一款Markdown编辑器组件,支持使用markodwn语法来编写文档,同时支持图片上传等功能(当前版本仅支持单文件上传)
璃白.'s avatar
璃白. 已提交
4

Miykael_xxm's avatar
Miykael_xxm 已提交
5 6
## 更新记录

璃白.'s avatar
璃白. 已提交
7 8 9 10
### v0.4

2021-06-23 v0.4更新,添加功能:

璃白.'s avatar
璃白. 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24
- throttle属性
- zIndex属性
- maxLength属性
- showWordLimit属性
- rows属性
- filePathRule属性
- [onLoad事件](#onload)
- [onInput事件](#oninput)
- [setValue方法](#setvalue)
- [getValue方法](#getvalue)
- [focus方法](#focus)
- [blur方法](#blur)
- **移除canAttachFile属性**
- **移除footer,工具栏添加上传文件按钮**
璃白.'s avatar
璃白. 已提交
25
- 全屏模式添加工具栏
Miykael_xxm's avatar
Miykael_xxm 已提交
26

璃白.'s avatar
璃白. 已提交
27
### v0.3
Miykael_xxm's avatar
Miykael_xxm 已提交
28

璃白.'s avatar
璃白. 已提交
29 30 31 32 33 34
2021-06-17 v0.3更新,添加功能:

- [focus事件](#onfocus)
- [blur事件](#onblur)
- [ctrl+enter/command+enter快捷键](#onsubmit)
- [夜间模式](#themeoptions)
Miykael_xxm's avatar
Miykael_xxm 已提交
35

璃白.'s avatar
璃白. 已提交
36 37 38 39
### v0.2

2021-06-15 v0.2更新,添加功能:

璃白.'s avatar
璃白. 已提交
40 41
- [内容回显](#options)
- [顶部工具栏配置](#toolsoptions)
璃白.'s avatar
璃白. 已提交
42

璃白.'s avatar
璃白. 已提交
43 44 45 46 47 48 49
### v0.1 

2021-06-07 v0.1 初版更新,支持功能:

- 自定义主题
- 单张图片/单个附件上传
- Markdown & Html 内容获取
璃白.'s avatar
璃白. 已提交
50 51 52



璃白.'s avatar
璃白. 已提交
53 54 55 56 57 58
# 使用

1. 通过script标签引入
```html
<script src="./markdown-editor.js"></script>
```
璃白.'s avatar
璃白. 已提交
59
2. 指定需要渲染的容器
璃白.'s avatar
璃白. 已提交
60 61 62 63 64 65 66 67
```html
<div id="app"></div>
```
3. 初始化实例
```js
new MdEditor({
    ...options
})
璃白.'s avatar
璃白. 已提交
68
```
璃白.'s avatar
璃白. 已提交
69 70 71

# options

璃白.'s avatar
璃白. 已提交
72 73 74 75 76 77 78 79
| 属性 | 说明 | 类型 | 默认值 | 可选值 |
| ------ | ------ | ------ | ------ | ------ |
| el | 编辑器渲染的容器 | String | "#app" | |
| value | 编辑器回显内容 | String \| Number | "" | |
| zIndex | 全屏模式的层级 | String \| Number | 2000 | |
| maxLength | 最大字数限制 | String \| Number | 1000 | |
| showWordLimit | 是否显示当前字数 | Boolean | false | |
| rows | 编辑区行数 | String \| Number | 6 | 'auto' |
璃白.'s avatar
璃白. 已提交
80
| throttle | input事件的节流时间 | Number | 1000 |  |
璃白.'s avatar
璃白. 已提交
81
| filePathRule | 图片链接的格式限制 | RegExp | | |
璃白.'s avatar
璃白. 已提交
82
| themeOptions | 主题颜色配置 | Object | [themeOptions](#themeoptions)
璃白.'s avatar
璃白. 已提交
83 84
| toolsOptions | 顶部工具栏配置 | Object | [toolsOptions](#toolsoptions)
| canPreview | 是否开启预览 | Boolean | true
璃白.'s avatar
璃白. 已提交
85
| placeholder | placeholder | String | "请输入内容"
璃白.'s avatar
璃白. 已提交
86 87 88 89
| onLoad | 编辑器实例化 | Function | function(res) {} [示例](#onload)
| onInput | 输入事件 | Function | function(res) {} [示例](#oninput)
| onFocus | 获取焦点事件 | Function | function(res) {} [示例](#onfocus)
| onBlur | 失去焦点事件 | Function | function(res) {} [示例](#onblur)
璃白.'s avatar
璃白. 已提交
90
| onChange | 获取编辑器markdown及html内容 | Function | function(res) {} [示例](#onchange)
璃白.'s avatar
璃白. 已提交
91
| onUpload | 上传文件钩子函数 | Function | function(file, callback) {} [示例](#onupload)
璃白.'s avatar
璃白. 已提交
92
| onsubmit | 快捷键函数 | Function | function(file, callback) {} [示例](#onsubmit)
璃白.'s avatar
璃白. 已提交
93

璃白.'s avatar
璃白. 已提交
94 95 96
# 方法

| 方法 | 说明 | 类型 | 示例 |
璃白.'s avatar
璃白. 已提交
97
| ------ | ------ | ------ | ------ |
璃白.'s avatar
璃白. 已提交
98 99 100 101 102
| focus | 使编辑器获取焦点 | Function | [focus](#focus) |
| blur | 使编辑器失去焦点 | Function | [blur](#focus) |
| getValue | 获取编辑器的内容 | Function | [getValue](#getvalue) |
| setValue | 设置编辑器的内容 | Function | [setValue](#setvalue) |

璃白.'s avatar
璃白. 已提交
103 104 105 106
# themeOptions

| 属性 | 说明 | 类型 | 默认值 |
| ------ | ------ | ------ | ------ |
璃白.'s avatar
璃白. 已提交
107
| dark | 夜间模式 | Boolean | false
璃白.'s avatar
璃白. 已提交
108 109 110 111
| borderColor | 编辑器边框默认颜色 | String | "#dbdbdb"
| borderColorActive | 编辑器边框激活颜色 | String | "#409eff"
| textColor | 编辑器文字默认颜色 | String | "#303030"
| textColorActive | 编辑器文字激活颜色 | String | "#000"
璃白.'s avatar
璃白. 已提交
112

璃白.'s avatar
璃白. 已提交
113 114 115 116 117 118 119 120

# toolsOptions

| 属性 | 说明 | 类型 | 默认值 |
| ------ | ------ | ------ | ------ |
| bold | 加粗 | Boolean | true
| italic | 斜体 | Boolean | true
| quote | 引用 | Boolean | true
璃白.'s avatar
璃白. 已提交
121
| code | 代码块 | Boolean | true
璃白.'s avatar
璃白. 已提交
122 123 124 125 126
| link | 链接 | Boolean | true
| ul | 无序列表 | Boolean | true
| ol | 有序列表 | Boolean | true
| task | 任务列表 | Boolean | true
| table | 表格 | Boolean | true
璃白.'s avatar
璃白. 已提交
127
| file | 上传文件 | Boolean | true
璃白.'s avatar
璃白. 已提交
128 129 130
| fullScreen | 全屏模式 | Boolean | true


璃白.'s avatar
璃白. 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143
# onLoad
编辑器实例化后触发


```js
new MdEditor({
    ...,
    onLoad: function() {
        console.log('ready') // { text: "...", html: "..." }
    }
})
```

璃白.'s avatar
璃白. 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
# onFocus
编辑器获取焦点时触发


```js
new MdEditor({
    ...,
    onFocus: function(res) {
        console.log(res) // { text: "...", html: "..." }
    }
})
```

# onBlur
编辑器失去焦点时触发


```js
new MdEditor({
    ...,
    onBlur: function(res) {
        console.log(res) // { text: "...", html: "..." }
    }
})
```

璃白.'s avatar
璃白. 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182
# onInput
用于获取markdown内容及编译后的html内容


```js
new MdEditor({
    ...,
    onInput: function(res) {
        console.log(res) // { text: "...", html: "..." }
    }
})
```

璃白.'s avatar
璃白. 已提交
183 184 185
# onChange
用于获取markdown内容及编译后的html内容

璃白.'s avatar
璃白. 已提交
186 187 188 189 190 191 192 193 194 195

```js
new MdEditor({
    ...,
    onChange: function(res) {
        console.log(res) // { text: "...", html: "..." }
    }
})
```

璃白.'s avatar
璃白. 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208
# onSubmit
按下ctrl+enter/command+enter组合键时触发


```js
new MdEditor({
    ...,
    onSubmit: function(res) {
        console.log(res) // { text: "...", html: "..." }
    }
})
```

璃白.'s avatar
璃白. 已提交
209 210 211
# onUpload

上传或粘贴文件时会触发此函数
璃白.'s avatar
璃白. 已提交
212 213 214 215 216

```js
new MdEditor({
    ...,
    onUpload: function(file, callback) {
璃白.'s avatar
璃白. 已提交
217
        // do something with file
璃白.'s avatar
璃白. 已提交
218 219 220
        // ajax
        // ...
        // 得到图片的url
璃白.'s avatar
璃白. 已提交
221
        // 在callback函数中回传图片url,编辑器会将图片链接粘贴到内容里
璃白.'s avatar
璃白. 已提交
222 223 224 225
        callback(url)
    }
})
```
璃白.'s avatar
璃白. 已提交
226

璃白.'s avatar
璃白. 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
# getValue

获取编辑器内容

```js
const myEditor = new MdEditor({
    ...
    }
})

const value = myEditor.getValue()
console.log(value)



myEditor.getValue(value => {
    console.log(value)
})
```

# setValue

设置编辑器内容

```js
const myEditor = new MdEditor({
    ...
    }
})

myEditor.setValue('this is new content')
```

# focus

使编辑器获取焦点

```js
const myEditor = new MdEditor({
    ...
    }
})

myEditor.focus()
```

# blur

使编辑器失去焦点

```js
const myEditor = new MdEditor({
    ...
    }
})

myEditor.blur()
```

璃白.'s avatar
璃白. 已提交
286 287 288 289 290 291 292
# Example
```html
<div id="app"></div>
    <script src="./markdown-editor.js"></script>
    <script>
      new MdEditor({
        el: "#app", // required
璃白.'s avatar
璃白. 已提交
293
        value: "回显的内容",
璃白.'s avatar
璃白. 已提交
294 295 296 297 298 299
        themeOptions: {
          borderColor: "#dbdbdb",
          borderColorActive: "#409eff",
          textColor: "#303030",
          textColorActive: "#000"
        },
璃白.'s avatar
璃白. 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312
        toolsOptions: {
          bold: true,
          italic: false,
          quote: true,
          code: true,
          link: false,
          ul: true,
          ol: true,
          task: true,
          table: false,
          fullScreen: false
        },
        canPreview: true,
璃白.'s avatar
璃白. 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        canAttachFile: true,
        placeholder: "请输入内容",
        onChange: function(res) {
          const { text, html } = res
          // submit(text)
          // render(html)
        },
        onUpload: function(file, callback) {
          ajax.post('http://example.com', {
              file: file
          }).then(res=>{
              callback(res.url)
          })
        }
      });
    </script>
```

璃白.'s avatar
璃白. 已提交
331
# License
璃白.'s avatar
璃白. 已提交
332 333

[MIT](https://codechina.csdn.net/codechina_dev/markdown-editor/-/blob/master/LICENSE)