jest-puppeteer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import Vditor from '../src/index'
  2. import '../src/assets/less/index.less'
  3. window.vditorTest = new Vditor('vditorTest', {
  4. tab: '\t',
  5. cache: false,
  6. height: 200,
  7. width: 500,
  8. counter: 100,
  9. resize: {
  10. enable: true,
  11. position: 'top',
  12. after: height => {
  13. console.log(`after resize, height change: ${height}`)
  14. },
  15. },
  16. placeholder: 'say sth...',
  17. lang: 'en_US',
  18. preview: {
  19. url: '/api/markdown',
  20. parse: (element) => {
  21. LazyLoadImage()
  22. },
  23. hljs: {
  24. style: 'solarized-dark256',
  25. lineNumber: true,
  26. },
  27. },
  28. hint: {
  29. emojiPath: 'https://cdn.jsdelivr.net/npm/vditor/dist/images/emoji',
  30. emojiTail: '<a href="https://ld246.com/settings/function" target="_blank">设置常用表情</a>',
  31. emoji: {
  32. '+1': '👍',
  33. '-1': '👎',
  34. 'trollface': 'https://cdn.jsdelivr.net/npm/[email protected]/dist/images/emoji/trollface.png',
  35. 'huaji': 'https://cdn.jsdelivr.net/npm/[email protected]/dist/images/emoji/huaji.gif',
  36. },
  37. at: (key) => {
  38. console.log(`atUser: ${key}`)
  39. return [
  40. {
  41. value: '@88250',
  42. html: '<img src="https://img.ld246.com/avatar/1353745196354_1535379434567.png?imageView2/1/w/52/h/52/interlace/0/q"> 88250',
  43. },
  44. {
  45. value: '@Vanessa',
  46. html: '<img src="https://img.ld246.com/avatar/1353745196354_1535379434567.png?imageView2/1/w/52/h/52/interlace/0/q"> Vanessa',
  47. }]
  48. },
  49. },
  50. focus: (val) => {
  51. console.log(`focus value: ${val}`)
  52. },
  53. blur: (val) => {
  54. console.log(`blur: ${val}`)
  55. },
  56. input: (val, mdElement) => {
  57. console.log('change:' + val, mdElement)
  58. },
  59. esc: (val) => {
  60. console.log(`esc: ${val}`)
  61. console.log(
  62. `cursor position:${JSON.stringify(vditorTest.getCursorPosition())}`)
  63. },
  64. ctrlEnter: (val) => {
  65. console.log(`ctrlEnter: ${val}`)
  66. },
  67. select: (val) => {
  68. console.log('select:', val)
  69. },
  70. upload: {
  71. accept: 'image/*,.pdf',
  72. token: 'test',
  73. url: '/api/upload/editor',
  74. linkToImgUrl: '/api/upload/fetch',
  75. filename: name => {
  76. // ? \ / : | < > * [ ] white to -
  77. return name.replace(/\?|\\|\/|:|\||<|>|\*|\[|\]|\s+/g, '-')
  78. },
  79. },
  80. toolbar: [
  81. {
  82. name: 'preview',
  83. tipPosition: 'ne',
  84. },
  85. 'both',
  86. {
  87. name: 'wysiwyg',
  88. tipPosition: 'ne',
  89. },
  90. {
  91. name: 'both',
  92. tipPosition: 'ne',
  93. },
  94. 'br',
  95. {
  96. name: 'emoji',
  97. },
  98. 'strike',
  99. 'devtools',
  100. 'fullscreen',
  101. {
  102. name: 'download',
  103. tipPosition: 'nw',
  104. tip: '下载',
  105. icon: '<svg t="1566271629641" class="icon" viewBox="0 0 1092 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1960" xmlns:xlink="http://www.w3.org/1999/xlink" width="34.125" height="32"><defs><style type="text/css"></style></defs><path d="M1044.753067 902.3488H47.5136a47.5136 47.5136 0 0 0 0 94.890667h997.239467a45.806933 45.806933 0 0 0 33.5872-13.858134 47.5136 47.5136 0 0 0-33.5872-81.032533z m-522.4448-103.970133a33.450667 33.450667 0 0 0 23.825066 8.874666 33.450667 33.450667 0 0 0 23.825067-8.874666l302.2848-290.952534c7.509333-7.236267 8.874667-16.452267 3.6864-24.7808-5.188267-8.3968-32.768-7.714133-44.8512-7.714133h-118.784V47.5136c0-25.941333-13.789867-47.5136-47.445333-47.5136H427.4176c-33.655467 0-47.5136 21.572267-47.5136 47.5136v427.349333H237.431467c-12.014933 0-15.906133-0.6144-21.162667 7.7824-5.188267 8.328533-3.754667 17.544533 3.6864 24.7808l302.421333 290.952534z" p-id="1961"></path></svg>',
  106. click: () => {
  107. alert('download')
  108. },
  109. },
  110. ],
  111. })
  112. const LazyLoadImage = () => {
  113. const loadImg = (it) => {
  114. const testImage = document.createElement('img')
  115. testImage.src = it.getAttribute('data-src')
  116. testImage.addEventListener('load', () => {
  117. it.src = testImage.src
  118. it.style.backgroundImage = 'none'
  119. it.style.backgroundColor = 'transparent'
  120. })
  121. it.removeAttribute('data-src')
  122. }
  123. if (!('IntersectionObserver' in window)) {
  124. document.querySelectorAll('img').forEach((data) => {
  125. if (data.getAttribute('data-src')) {
  126. loadImg(data)
  127. }
  128. })
  129. return false
  130. }
  131. if (window.imageIntersectionObserver) {
  132. window.imageIntersectionObserver.disconnect()
  133. document.querySelectorAll('img').forEach(function (data) {
  134. window.imageIntersectionObserver.observe(data)
  135. })
  136. } else {
  137. window.imageIntersectionObserver = new IntersectionObserver((entries) => {
  138. entries.forEach((entrie) => {
  139. if ((typeof entrie.isIntersecting === 'undefined'
  140. ? entrie.intersectionRatio !== 0
  141. : entrie.isIntersecting) && entrie.target.getAttribute('data-src')) {
  142. loadImg(entrie.target)
  143. }
  144. })
  145. })
  146. document.querySelectorAll('img').forEach(function (data) {
  147. window.imageIntersectionObserver.observe(data)
  148. })
  149. }
  150. }