static.js 3.9 KB

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