demo.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import Vditor from '../src/index'
  2. // import Vditor from '../dist/index.min'
  3. const LazyLoadImage = () => {
  4. const loadImg = (it) => {
  5. const testImage = document.createElement('img')
  6. testImage.src = it.getAttribute('data-src')
  7. testImage.addEventListener('load', () => {
  8. it.src = testImage.src
  9. it.style.backgroundImage = 'none'
  10. it.style.backgroundColor = 'transparent'
  11. })
  12. it.removeAttribute('data-src')
  13. }
  14. if (!('IntersectionObserver' in window)) {
  15. document.querySelectorAll('img').forEach((data) => {
  16. if (data.getAttribute('data-src')) {
  17. loadImg(data)
  18. }
  19. })
  20. return false
  21. }
  22. if (window.imageIntersectionObserver) {
  23. window.imageIntersectionObserver.disconnect()
  24. document.querySelectorAll('img').forEach(function (data) {
  25. window.imageIntersectionObserver.observe(data)
  26. })
  27. } else {
  28. window.imageIntersectionObserver = new IntersectionObserver((entries) => {
  29. entries.forEach((entrie) => {
  30. if ((typeof entrie.isIntersecting === 'undefined'
  31. ? entrie.intersectionRatio !== 0
  32. : entrie.isIntersecting) && entrie.target.getAttribute('data-src')) {
  33. loadImg(entrie.target)
  34. }
  35. })
  36. })
  37. document.querySelectorAll('img').forEach(function (data) {
  38. window.imageIntersectionObserver.observe(data)
  39. })
  40. }
  41. }
  42. new Vditor('vditor', {
  43. counter: 100,
  44. height: 300,
  45. editorName: 'vditor',
  46. tab: ' ',
  47. upload: {
  48. accept: 'image/*,.pdf',
  49. token: 'test',
  50. url: '/api/upload/editor',
  51. linkToImgUrl: '/api/upload/fetch',
  52. filename: name => {
  53. // ? \ / : | < > * [ ] white to -
  54. return name.replace(/\?|\\|\/|:|\||<|>|\*|\[|\]|\s+/g, '-')
  55. },
  56. },
  57. preview: {
  58. show: true,
  59. url: '/api/markdown',
  60. parse: () => {
  61. LazyLoadImage()
  62. },
  63. },
  64. classes: {
  65. preview: 'content-reset',
  66. },
  67. })
  68. window.vditorTest = new Vditor('vditorTest', {
  69. cache: false,
  70. height: 200,
  71. width: 500,
  72. counter: 100,
  73. resize: {
  74. enable: true,
  75. position: 'top',
  76. after: height => {
  77. console.log(`after resize, height change: ${height}`)
  78. },
  79. },
  80. placeholder: 'say sth...',
  81. lang: 'en_US',
  82. preview: {
  83. url: '/api/markdown',
  84. parse: (element) => {
  85. console.log(element)
  86. LazyLoadImage()
  87. },
  88. },
  89. hint: {
  90. emojiPath: 'https://static.hacpai.com/emoji/graphics',
  91. emojiTail: '<a href="https://hacpai.com/settings/function" target="_blank">设置常用表情</a>',
  92. emoji: {
  93. '+1': '👍',
  94. '-1': '👎',
  95. 'trollface': '/src/assets/emoji/trollface.png',
  96. 'huaji': '/src/assets/emoji/huaji.gif',
  97. },
  98. at: (key) => {
  99. console.log(`atUser: ${key}`)
  100. return [
  101. {
  102. value: '@88250',
  103. html: '<img src="https://img.hacpai.com/avatar/1353745196354_1535379434567.png?imageView2/1/w/52/h/52/interlace/0/q"> 88250',
  104. },
  105. {
  106. value: '@Vanessa',
  107. html: '<img src="https://img.hacpai.com/avatar/1353745196354_1535379434567.png?imageView2/1/w/52/h/52/interlace/0/q"> Vanessa',
  108. }]
  109. },
  110. },
  111. classes: {
  112. preview: 'content-reset',
  113. },
  114. focus: (val) => {
  115. console.log(`focus value: ${val}`)
  116. console.log(
  117. `focus cursor position:${JSON.stringify(vditor.getCursorPosition())}`)
  118. },
  119. blur: (val) => {
  120. console.log(`blur: ${val}`)
  121. },
  122. input: (val, mdElement) => {
  123. console.log('change:' + val, mdElement)
  124. },
  125. esc: (val) => {
  126. console.log(`esc: ${val}`)
  127. },
  128. ctrlEnter: (val) => {
  129. console.log(`ctrlEnter: ${val}`)
  130. },
  131. select: (val) => {
  132. console.log(`select: ${val}`)
  133. },
  134. toolbar: [
  135. {
  136. name: 'preview',
  137. tipPosition: 'ne',
  138. },
  139. 'br',
  140. {
  141. name: 'emoji',
  142. },
  143. 'strike',
  144. ],
  145. })