🐛 修复@用户异步不生效问题
一款Markdown编辑器组件,支持使用markodwn语法来编写文档,同时支持图片上传等功能
地址:https://csdnimg.cn/release/markdown-editor/1.0.0/markdown-editor.js
2022-03-01 v1.0更新,添加功能:
2021-11-11 v0.9更新,添加功能:
2021-09-03 v0.8更新,添加功能:
2021-08-05 v0.7更新,添加功能:
2021-07-19 v0.6更新,添加功能:
2021-06-29 v0.5更新,添加功能:
2021-06-23 v0.4更新,添加功能:
2021-06-17 v0.3更新,添加功能:
2021-06-15 v0.2更新,添加功能:
2021-06-07 v0.1 初版更新,支持功能:
<script src="./markdown-editor.js"></script>
import MdEditor from './markdown-editor.js'
<div id="app"></div>
new MdEditor({ ...options })
用于限制可上传文件的路径规则
new MdEditor({ ..., filePathRule: /^https:\/\/csdn\.net/ }) onInput/onChange事件中会返回invalidList[]列表,内容为不符合filePathRule规则的地址
编辑器实例化后触发
new MdEditor({ ..., onLoad: function() { console.log('ready') // { text: "...", html: "..." } } })
编辑器获取焦点时触发
new MdEditor({ ..., onFocus: function(res) { console.log(res) // { text: "...", html: "..." } } })
编辑器失去焦点时触发
new MdEditor({ ..., onBlur: function(res) { console.log(res) // { text: "...", html: "..." } } })
用于获取markdown内容及编译后的html内容
new MdEditor({ ..., onInput: function(res) { console.log(res) // { text: "...", html: "..." } } })
new MdEditor({ ..., onChange: function(res) { console.log(res) // { text: "...", html: "..." } } })
按下ctrl+enter/command+enter组合键时触发
new MdEditor({ ..., onSubmit: function(res) { console.log(res) // { text: "...", html: "..." } } })
上传或粘贴文件时会触发此函数
new MdEditor({ ..., onUpload: function(file, type, callback) { // type 返回 img/file/video // do something with file // ajax // ... // 得到图片的url // 在callback函数中回传图片url,编辑器会将图片链接粘贴到内容里 callback(url) } })
按下@键时触发此函数,通过回传用户列表可生成用户选择弹窗
new MdEditor({ ..., queryUserList: function(val, callback) { const list = [ { nickname: "王一博", username: 'cpongo4', url: "https://weibo.com/u/5492443184", avatar: "https://img2.baidu.com/it/u=298051053,3773223854&fm=26&fmt=auto&gp=0.jpg" }, { nickname: "王俊凯", username: 'cpongo5', url: "https://weibo.com/tfwangjunkai", avatar: "https://img1.baidu.com/it/u=2378425879,2273515018&fm=26&fmt=auto&gp=0.jpg" }, { nickname: "易烊千玺", username: 'cpongo6', url: "https://weibo.com/tfyiyangqianxi", avatar: "https://img0.baidu.com/it/u=2227200088,1939721201&fm=26&fmt=auto&gp=0.jpg" }, { nickname: "白敬亭", username: 'cpongo7', url: "https://weibo.com/u/2112496475", avatar: "https://img1.baidu.com/it/u=3265411836,2089649447&fm=11&fmt=auto&gp=0.jpg" } ]; callback(list); } })
通过返回的链接信息把链接渲染成卡片样式
renderLinks: function(val, callback) { const newLinks = val.map(item => { // 调用接口获取链接的信息 item.title = "指定的可执行文件cmd.exe未能运行,某某函数不正确-后端-CSDN问答"; item.icon = "https://g.csdnimg.cn/static/logo/favicon32.ico"; item.description = "CSDN问答为您找到指定的可执行文件cmd.exe未能运行,某某函数不正确相关问题答案,如果想了解更多关于指定的可执行文件cmd.exe未能运行,某某函数不正确 asp.net、c# 技术问题等相关问答,请访问CSDN问答。"; return item; }); callback(newLinks); },
获取编辑器内容
const myEditor = new MdEditor({ ... } }) const value = myEditor.getValue() console.log(value) 或 myEditor.getValue(value => { console.log(value) })
设置编辑器内容
const myEditor = new MdEditor({ ... } }) myEditor.setValue('this is new content')
使编辑器获取焦点
const myEditor = new MdEditor({ ... } }) myEditor.focus()
使编辑器失去焦点
const myEditor = new MdEditor({ ... } }) myEditor.blur()
注册工具栏按钮
const myEditor = new MdEditor({ ... } }) myEditor.registerTools([ { name: "test", // icon: "https://img1.baidu.com/it/u=1522221441,2900868227&fm=26&fmt=auto&gp=0.jpg", tip: "自定义的按钮", click: function() { // 点击按钮的事件 } } ]);
切换编辑器编辑和预览tab
const myEditor = new MdEditor({ ... } }) myEditor.toggleTab() // 默认取反 myEditor.toggleTab({"edit") myEditor.toggleTab("preview")
切换编辑器全屏模式
const myEditor = new MdEditor({ ... } }) myEditor.toggleFullScreen() // 默认取反 myEditor.toggleFullScreen(true) myEditor.toggleFullScreen(false)
<div id="app"></div> <script src="./markdown-editor.js"></script> <script> new MdEditor({ el: "#app", // required value: "回显的内容", themeOptions: { borderColor: "#dbdbdb", borderColorActive: "#409eff", textColor: "#303030", textColorActive: "#000" }, toolsOptions: { bold: true, italic: false, quote: true, code: true, link: false, ul: true, ol: true, task: true, table: false, fullScreen: false }, canPreview: true, 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>
MIT
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
Awesome-Markdown-Editor
一款Markdown编辑器组件,支持使用markodwn语法来编写文档,同时支持图片上传等功能
地址:https://csdnimg.cn/release/markdown-editor/1.0.0/markdown-editor.js
更新记录
v1.0
2022-03-01 v1.0更新,添加功能:
v0.9
2021-11-11 v0.9更新,添加功能:
v0.8
2021-09-03 v0.8更新,添加功能:
v0.7
2021-08-05 v0.7更新,添加功能:
v0.6
2021-07-19 v0.6更新,添加功能:
v0.5
2021-06-29 v0.5更新,添加功能:
v0.4
2021-06-23 v0.4更新,添加功能:
v0.3
2021-06-17 v0.3更新,添加功能:
v0.2
2021-06-15 v0.2更新,添加功能:
v0.1
2021-06-07 v0.1 初版更新,支持功能:
使用
options
方法
themeOptions
toolsOptions
filePathRule
用于限制可上传文件的路径规则
onLoad
编辑器实例化后触发
onFocus
编辑器获取焦点时触发
onBlur
编辑器失去焦点时触发
onInput
用于获取markdown内容及编译后的html内容
onChange
用于获取markdown内容及编译后的html内容
onSubmit
按下ctrl+enter/command+enter组合键时触发
onUpload
上传或粘贴文件时会触发此函数
queryUserList
按下@键时触发此函数,通过回传用户列表可生成用户选择弹窗
renderLinks
通过返回的链接信息把链接渲染成卡片样式
getValue
获取编辑器内容
setValue
设置编辑器内容
focus
使编辑器获取焦点
blur
使编辑器失去焦点
registertools
注册工具栏按钮
toggleTab
切换编辑器编辑和预览tab
toggleFullScreen
切换编辑器全屏模式
Example
License
MIT