README.md 4.4 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 7 8 9 10 11 12 13 14
## 更新记录

### v0.1 

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

- 自定义主题
- 单张图片/单个附件上传
- Markdown & Html 内容获取

璃白.'s avatar
璃白. 已提交
15 16 17 18
### v0.2

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

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

璃白.'s avatar
璃白. 已提交
22 23 24 25 26 27 28 29 30
### v0.3

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

- [focus事件](#)
- [blur事件](#)
- [ctrl+enter/command+enter快捷键](#)
- [夜间模式](#)

璃白.'s avatar
璃白. 已提交
31 32 33 34 35 36
# 使用

1. 通过script标签引入
```html
<script src="./markdown-editor.js"></script>
```
璃白.'s avatar
璃白. 已提交
37
2. 指定需要渲染的容器
璃白.'s avatar
璃白. 已提交
38 39 40 41 42 43 44 45
```html
<div id="app"></div>
```
3. 初始化实例
```js
new MdEditor({
    ...options
})
璃白.'s avatar
璃白. 已提交
46
```
璃白.'s avatar
璃白. 已提交
47 48 49 50 51 52

# options

| 属性 | 说明 | 类型 | 默认值 |
| ------ | ------ | ------ | ------ |
| el | 编辑器渲染的容器 | String | "#app"
璃白.'s avatar
璃白. 已提交
53
| value | 编辑器回显内容 | String \| Number | ""
璃白.'s avatar
璃白. 已提交
54
| themeOptions | 主题颜色配置 | Object | [themeOptions](#themeoptions)
璃白.'s avatar
璃白. 已提交
55
| toolsOptions | 顶部工具栏配置 | Object | [toolsOptions](#toolsoptions)
璃白.'s avatar
璃白. 已提交
56
| canAttachFile | 是否可以上传图片 | Boolean | true
璃白.'s avatar
璃白. 已提交
57
| canPreview | 是否开启预览 | Boolean | true
璃白.'s avatar
璃白. 已提交
58
| placeholder | placeholder | String | "请输入内容"
璃白.'s avatar
璃白. 已提交
59
| onChange | 获取编辑器markdown及html内容 | Function | function(res) {} [示例](#onchange)
璃白.'s avatar
璃白. 已提交
60
| onUpload | 上传文件钩子函数 | Function | function(file, callback) {} [示例](#onupload)
璃白.'s avatar
璃白. 已提交
61 62 63 64 65

# themeOptions

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

璃白.'s avatar
璃白. 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

# toolsOptions

| 属性 | 说明 | 类型 | 默认值 |
| ------ | ------ | ------ | ------ |
| bold | 加粗 | Boolean | true
| italic | 斜体 | Boolean | true
| quote | 引用 | Boolean | true
| code | 代码 | Boolean | true
| link | 链接 | Boolean | true
| ul | 无序列表 | Boolean | true
| ol | 有序列表 | Boolean | true
| task | 任务列表 | Boolean | true
| table | 表格 | Boolean | true
| fullScreen | 全屏模式 | Boolean | true


璃白.'s avatar
璃白. 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
# 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
璃白. 已提交
115 116 117
# onChange
用于获取markdown内容及编译后的html内容

璃白.'s avatar
璃白. 已提交
118 119 120 121 122 123 124 125 126 127

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

璃白.'s avatar
璃白. 已提交
128 129 130
# onUpload

上传或粘贴文件时会触发此函数
璃白.'s avatar
璃白. 已提交
131 132 133 134 135

```js
new MdEditor({
    ...,
    onUpload: function(file, callback) {
璃白.'s avatar
璃白. 已提交
136
        // do something with file
璃白.'s avatar
璃白. 已提交
137 138 139
        // ajax
        // ...
        // 得到图片的url
璃白.'s avatar
璃白. 已提交
140
        // 在callback函数中回传图片url,编辑器会将图片链接粘贴到内容里
璃白.'s avatar
璃白. 已提交
141 142 143 144
        callback(url)
    }
})
```
璃白.'s avatar
璃白. 已提交
145

璃白.'s avatar
璃白. 已提交
146 147 148 149 150 151 152
# Example
```html
<div id="app"></div>
    <script src="./markdown-editor.js"></script>
    <script>
      new MdEditor({
        el: "#app", // required
璃白.'s avatar
璃白. 已提交
153
        value: "回显的内容",
璃白.'s avatar
璃白. 已提交
154 155 156 157 158 159
        themeOptions: {
          borderColor: "#dbdbdb",
          borderColorActive: "#409eff",
          textColor: "#303030",
          textColorActive: "#000"
        },
璃白.'s avatar
璃白. 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172
        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
璃白. 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        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
璃白. 已提交
191
# License
璃白.'s avatar
璃白. 已提交
192 193

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