README.md 3.9 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
# 使用

1. 通过script标签引入
```html
<script src="./markdown-editor.js"></script>
```
璃白.'s avatar
璃白. 已提交
28
2. 指定需要渲染的容器
璃白.'s avatar
璃白. 已提交
29 30 31 32 33 34 35 36
```html
<div id="app"></div>
```
3. 初始化实例
```js
new MdEditor({
    ...options
})
璃白.'s avatar
璃白. 已提交
37
```
璃白.'s avatar
璃白. 已提交
38 39 40 41 42 43

# options

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

# themeOptions

| 属性 | 说明 | 类型 | 默认值 |
| ------ | ------ | ------ | ------ |
| borderColor | 编辑器边框默认颜色 | String | "#dbdbdb"
| borderColorActive | 编辑器边框激活颜色 | String | "#409eff"
| textColor | 编辑器文字默认颜色 | String | "#303030"
| textColorActive | 编辑器文字激活颜色 | String | "#000"
璃白.'s avatar
璃白. 已提交
61

璃白.'s avatar
璃白. 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

# 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
璃白. 已提交
79 80 81
# onChange
用于获取markdown内容及编译后的html内容

璃白.'s avatar
璃白. 已提交
82 83 84 85 86 87 88 89 90 91

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

璃白.'s avatar
璃白. 已提交
92 93 94
# onUpload

上传或粘贴文件时会触发此函数
璃白.'s avatar
璃白. 已提交
95 96 97 98 99

```js
new MdEditor({
    ...,
    onUpload: function(file, callback) {
璃白.'s avatar
璃白. 已提交
100
        // do something with file
璃白.'s avatar
璃白. 已提交
101 102 103
        // ajax
        // ...
        // 得到图片的url
璃白.'s avatar
璃白. 已提交
104
        // 在callback函数中回传图片url,编辑器会将图片链接粘贴到内容里
璃白.'s avatar
璃白. 已提交
105 106 107 108
        callback(url)
    }
})
```
璃白.'s avatar
璃白. 已提交
109

璃白.'s avatar
璃白. 已提交
110 111 112 113 114 115 116
# Example
```html
<div id="app"></div>
    <script src="./markdown-editor.js"></script>
    <script>
      new MdEditor({
        el: "#app", // required
璃白.'s avatar
璃白. 已提交
117
        value: "回显的内容",
璃白.'s avatar
璃白. 已提交
118 119 120 121 122 123
        themeOptions: {
          borderColor: "#dbdbdb",
          borderColorActive: "#409eff",
          textColor: "#303030",
          textColorActive: "#000"
        },
璃白.'s avatar
璃白. 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136
        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
璃白. 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        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
璃白. 已提交
155
# License
璃白.'s avatar
璃白. 已提交
156 157

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