import Vditor from '../src/index'
import '../src/assets/less/index.less'
window.vditorTest = new Vditor('vditorTest', {
tab: '\t',
cache: false,
height: 200,
width: 500,
counter: 100,
resize: {
enable: true,
position: 'top',
after: height => {
console.log(`after resize, height change: ${height}`)
},
},
placeholder: 'say sth...',
lang: 'en_US',
preview: {
url: '/api/markdown',
parse: (element) => {
LazyLoadImage()
},
hljs: {
style: 'solarized-dark256',
lineNumber: true,
},
},
hint: {
emojiPath: 'https://cdn.jsdelivr.net/npm/vditor/dist/images/emoji',
emojiTail: '设置常用表情',
emoji: {
'+1': '👍',
'-1': '👎',
'trollface': 'https://cdn.jsdelivr.net/npm/vditor@1.3.1/dist/images/emoji/trollface.png',
'huaji': 'https://cdn.jsdelivr.net/npm/vditor@1.3.1/dist/images/emoji/huaji.gif',
},
at: (key) => {
console.log(`atUser: ${key}`)
return [
{
value: '@88250',
html: '
88250',
},
{
value: '@Vanessa',
html: '
Vanessa',
}]
},
},
focus: (val) => {
console.log(`focus value: ${val}`)
},
blur: (val) => {
console.log(`blur: ${val}`)
},
input: (val, mdElement) => {
console.log('change:' + val, mdElement)
},
esc: (val) => {
console.log(`esc: ${val}`)
console.log(
`cursor position:${JSON.stringify(vditorTest.getCursorPosition())}`)
},
ctrlEnter: (val) => {
console.log(`ctrlEnter: ${val}`)
},
select: (val) => {
console.log('select:', val)
},
upload: {
accept: 'image/*,.pdf',
token: 'test',
url: '/api/upload/editor',
linkToImgUrl: '/api/upload/fetch',
filename: name => {
// ? \ / : | < > * [ ] white to -
return name.replace(/\?|\\|\/|:|\||<|>|\*|\[|\]|\s+/g, '-')
},
},
toolbar: [
{
name: 'preview',
tipPosition: 'ne',
},
'both',
{
name: 'wysiwyg',
tipPosition: 'ne',
},
{
name: 'both',
tipPosition: 'ne',
},
'br',
{
name: 'emoji',
},
'strike',
'devtools',
'fullscreen',
{
name: 'download',
tipPosition: 'nw',
tip: '下载',
icon: '',
click: () => {
alert('download')
},
},
],
})
const LazyLoadImage = () => {
const loadImg = (it) => {
const testImage = document.createElement('img')
testImage.src = it.getAttribute('data-src')
testImage.addEventListener('load', () => {
it.src = testImage.src
it.style.backgroundImage = 'none'
it.style.backgroundColor = 'transparent'
})
it.removeAttribute('data-src')
}
if (!('IntersectionObserver' in window)) {
document.querySelectorAll('img').forEach((data) => {
if (data.getAttribute('data-src')) {
loadImg(data)
}
})
return false
}
if (window.imageIntersectionObserver) {
window.imageIntersectionObserver.disconnect()
document.querySelectorAll('img').forEach(function (data) {
window.imageIntersectionObserver.observe(data)
})
} else {
window.imageIntersectionObserver = new IntersectionObserver((entries) => {
entries.forEach((entrie) => {
if ((typeof entrie.isIntersecting === 'undefined'
? entrie.intersectionRatio !== 0
: entrie.isIntersecting) && entrie.target.getAttribute('data-src')) {
loadImg(entrie.target)
}
})
})
document.querySelectorAll('img').forEach(function (data) {
window.imageIntersectionObserver.observe(data)
})
}
}