README.md 8.7 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
### v0.6

璃白.'s avatar
璃白. 已提交
9
2021-07-19 v0.6更新,添加功能:
璃白.'s avatar
璃白. 已提交
10

璃白.'s avatar
璃白. 已提交
11 12 13
- 优化工具栏表格交互
- 工具栏添加[格式刷](#toolsoptions)
- 添加[md语法说明](#toolsoptions)
璃白.'s avatar
璃白. 已提交
14
- 添加代码语言选择
璃白.'s avatar
璃白. 已提交
15
- 添加height属性
璃白.'s avatar
璃白. 已提交
16

璃白.'s avatar
璃白. 已提交
17 18 19 20 21 22
### v0.5

2021-06-29 v0.5更新,添加功能:

- tab快捷键
- tabSize属性
璃白.'s avatar
璃白. 已提交
23 24 25
- [代码风格配置](#themeoptions)
- [toggleTab方法](#toggletab)
- [toggleFullScreen方法](#togglefullscreen)
璃白.'s avatar
璃白. 已提交
26

璃白.'s avatar
璃白. 已提交
27 28 29 30
### v0.4

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

璃白.'s avatar
璃白. 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44
- throttle属性
- zIndex属性
- maxLength属性
- showWordLimit属性
- rows属性
- filePathRule属性
- [onLoad事件](#onload)
- [onInput事件](#oninput)
- [setValue方法](#setvalue)
- [getValue方法](#getvalue)
- [focus方法](#focus)
- [blur方法](#blur)
- **移除canAttachFile属性**
- **移除footer,工具栏添加上传文件按钮**
璃白.'s avatar
璃白. 已提交
45
- 全屏模式添加工具栏
Miykael_xxm's avatar
Miykael_xxm 已提交
46

璃白.'s avatar
璃白. 已提交
47
### v0.3
Miykael_xxm's avatar
Miykael_xxm 已提交
48

璃白.'s avatar
璃白. 已提交
49 50 51 52 53 54
2021-06-17 v0.3更新,添加功能:

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

璃白.'s avatar
璃白. 已提交
56 57 58 59
### v0.2

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

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

璃白.'s avatar
璃白. 已提交
63 64 65 66 67 68 69
### v0.1 

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

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



璃白.'s avatar
璃白. 已提交
73 74 75 76 77 78
# 使用

1. 通过script标签引入
```html
<script src="./markdown-editor.js"></script>
```
璃白.'s avatar
璃白. 已提交
79
2. 指定需要渲染的容器
璃白.'s avatar
璃白. 已提交
80 81 82 83 84 85 86 87
```html
<div id="app"></div>
```
3. 初始化实例
```js
new MdEditor({
    ...options
})
璃白.'s avatar
璃白. 已提交
88
```
璃白.'s avatar
璃白. 已提交
89 90 91

# options

璃白.'s avatar
璃白. 已提交
92 93 94 95 96 97 98 99
| 属性 | 说明 | 类型 | 默认值 | 可选值 |
| ------ | ------ | ------ | ------ | ------ |
| el | 编辑器渲染的容器 | String | "#app" | |
| value | 编辑器回显内容 | String \| Number | "" | |
| zIndex | 全屏模式的层级 | String \| Number | 2000 | |
| maxLength | 最大字数限制 | String \| Number | 1000 | |
| showWordLimit | 是否显示当前字数 | Boolean | false | |
| rows | 编辑区行数 | String \| Number | 6 | 'auto' |
璃白.'s avatar
璃白. 已提交
100
| height | 编辑器高度 | String \| Number |  |  |
璃白.'s avatar
璃白. 已提交
101
| tabSize | tab键缩紧空格数 | String \| Number | 2
璃白.'s avatar
璃白. 已提交
102
| throttle | input事件的节流时间 | Number | 1000 |  |
璃白.'s avatar
璃白. 已提交
103
| filePathRule | 图片链接的格式限制 | RegExp | [示例](#filepathrule) | |
璃白.'s avatar
璃白. 已提交
104
| themeOptions | 主题颜色配置 | Object | [themeOptions](#themeoptions)
璃白.'s avatar
璃白. 已提交
105 106
| toolsOptions | 顶部工具栏配置 | Object | [toolsOptions](#toolsoptions)
| canPreview | 是否开启预览 | Boolean | true
璃白.'s avatar
璃白. 已提交
107
| placeholder | placeholder | String | "请输入内容"
璃白.'s avatar
璃白. 已提交
108 109 110 111
| onLoad | 编辑器实例化 | Function | function(res) {} [示例](#onload)
| onInput | 输入事件 | Function | function(res) {} [示例](#oninput)
| onFocus | 获取焦点事件 | Function | function(res) {} [示例](#onfocus)
| onBlur | 失去焦点事件 | Function | function(res) {} [示例](#onblur)
璃白.'s avatar
璃白. 已提交
112
| onChange | 获取编辑器markdown及html内容 | Function | function(res) {} [示例](#onchange)
璃白.'s avatar
璃白. 已提交
113
| onUpload | 上传文件钩子函数 | Function | function(file, callback) {} [示例](#onupload)
璃白.'s avatar
璃白. 已提交
114
| onsubmit | 快捷键函数 | Function | function(file, callback) {} [示例](#onsubmit)
璃白.'s avatar
璃白. 已提交
115

璃白.'s avatar
璃白. 已提交
116 117 118
# 方法

| 方法 | 说明 | 类型 | 示例 |
璃白.'s avatar
璃白. 已提交
119
| ------ | ------ | ------ | ------ |
璃白.'s avatar
璃白. 已提交
120 121 122 123
| focus | 使编辑器获取焦点 | Function | [focus](#focus) |
| blur | 使编辑器失去焦点 | Function | [blur](#focus) |
| getValue | 获取编辑器的内容 | Function | [getValue](#getvalue) |
| setValue | 设置编辑器的内容 | Function | [setValue](#setvalue) |
璃白.'s avatar
璃白. 已提交
124 125
| toggleTab | 切换编辑和预览tab | Function | [toggleTab](#toggletab) |
| toggleFullScreen | 切换编辑器全屏模式 | Function | [toggleFullScreen](#togglefullscreen) |
璃白.'s avatar
璃白. 已提交
126

璃白.'s avatar
璃白. 已提交
127 128
# themeOptions

璃白.'s avatar
璃白. 已提交
129 130
| 属性 | 说明 | 类型 | 默认值 | 可选值 |
| ------ | ------ | ------ | ------ | ------ |
璃白.'s avatar
璃白. 已提交
131
| dark | 夜间模式 | Boolean | false
璃白.'s avatar
璃白. 已提交
132 133 134 135
| borderColor | 编辑器边框默认颜色 | String | "#dbdbdb"
| borderColorActive | 编辑器边框激活颜色 | String | "#409eff"
| textColor | 编辑器文字默认颜色 | String | "#303030"
| textColorActive | 编辑器文字激活颜色 | String | "#000"
璃白.'s avatar
璃白. 已提交
136
| codeTheme | 代码主题色配置 | String | "light" | 'light', 'dark', 'atom-one-dark' |
璃白.'s avatar
璃白. 已提交
137

璃白.'s avatar
璃白. 已提交
138 139 140

# toolsOptions

璃白.'s avatar
璃白. 已提交
141 142
| 属性 | 说明 | 类型 | 默认值 | 说明 |
| ------ | ------ | ------ | ------ | ------ |
璃白.'s avatar
璃白. 已提交
143 144 145
| bold | 加粗 | Boolean | true
| italic | 斜体 | Boolean | true
| quote | 引用 | Boolean | true
璃白.'s avatar
璃白. 已提交
146
| format | 格式刷 | Boolean | true | 支持标题、加粗、斜体、引用、代码块、无序列表、任务列表 |
璃白.'s avatar
璃白. 已提交
147
| code | 代码块 | Boolean | true
璃白.'s avatar
璃白. 已提交
148 149 150 151 152
| link | 链接 | Boolean | true
| ul | 无序列表 | Boolean | true
| ol | 有序列表 | Boolean | true
| task | 任务列表 | Boolean | true
| table | 表格 | Boolean | true
璃白.'s avatar
璃白. 已提交
153
| file | 上传文件 | Boolean | true
璃白.'s avatar
璃白. 已提交
154
| help | markdown语法 | Boolean | true
璃白.'s avatar
璃白. 已提交
155 156 157
| fullScreen | 全屏模式 | Boolean | true


璃白.'s avatar
璃白. 已提交
158 159 160 161 162 163 164 165 166
# filePathRule
用于限制可上传文件的路径规则

```js
new MdEditor({
    ...,
    filePathRule: /^https:\/\/csdn\.net/
})

璃白.'s avatar
璃白. 已提交
167 168
onInput/onChange事件中会返回invalidList[]列表内容为不符合filePathRule规则的地址

璃白.'s avatar
璃白. 已提交
169 170 171
```


璃白.'s avatar
璃白. 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184
# onLoad
编辑器实例化后触发


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

璃白.'s avatar
璃白. 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
# 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
璃白. 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223
# onInput
用于获取markdown内容及编译后的html内容


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

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

璃白.'s avatar
璃白. 已提交
227 228 229 230 231 232 233 234 235 236

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

璃白.'s avatar
璃白. 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249
# onSubmit
按下ctrl+enter/command+enter组合键时触发


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

璃白.'s avatar
璃白. 已提交
250 251 252
# onUpload

上传或粘贴文件时会触发此函数
璃白.'s avatar
璃白. 已提交
253 254 255 256 257

```js
new MdEditor({
    ...,
    onUpload: function(file, callback) {
璃白.'s avatar
璃白. 已提交
258
        // do something with file
璃白.'s avatar
璃白. 已提交
259 260 261
        // ajax
        // ...
        // 得到图片的url
璃白.'s avatar
璃白. 已提交
262
        // 在callback函数中回传图片url,编辑器会将图片链接粘贴到内容里
璃白.'s avatar
璃白. 已提交
263 264 265 266
        callback(url)
    }
})
```
璃白.'s avatar
璃白. 已提交
267

璃白.'s avatar
璃白. 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
# 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
璃白. 已提交
327 328 329 330 331 332 333 334 335 336 337
# toggleTab

切换编辑器编辑和预览tab

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

myEditor.toggleTab() // 默认取反
璃白.'s avatar
璃白. 已提交
338 339
myEditor.toggleTab({"edit")
myEditor.toggleTab("preview")
璃白.'s avatar
璃白. 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
```

# toggleFullScreen

切换编辑器全屏模式

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

myEditor.toggleFullScreen() // 默认取反
myEditor.toggleFullScreen(true)
myEditor.toggleFullScreen(false)
```

璃白.'s avatar
璃白. 已提交
357 358 359 360 361 362 363
# Example
```html
<div id="app"></div>
    <script src="./markdown-editor.js"></script>
    <script>
      new MdEditor({
        el: "#app", // required
璃白.'s avatar
璃白. 已提交
364
        value: "回显的内容",
璃白.'s avatar
璃白. 已提交
365 366 367 368 369 370
        themeOptions: {
          borderColor: "#dbdbdb",
          borderColorActive: "#409eff",
          textColor: "#303030",
          textColorActive: "#000"
        },
璃白.'s avatar
璃白. 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383
        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
璃白. 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
        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
璃白. 已提交
402
# License
璃白.'s avatar
璃白. 已提交
403 404

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