static.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Vditor.preview(document.getElementById('vditorPreview'))
  41. const vditor = new Vditor('vditor', {
  42. counter: 100,
  43. height: 300,
  44. hint: {
  45. emojiPath: 'https://static.hacpai.com/emoji/graphics',
  46. emojiTail: '<a href="https://hacpai.com/settings/function" target="_blank">设置常用表情</a>',
  47. emoji: {
  48. 'sd': '💔',
  49. 'huaji': 'https://unpkg.com/[email protected]/dist/images/emoji/huaji.gif',
  50. 'sym': '//static.b3log.org/images/brand/sym-128.png',
  51. 'pipe': '//static.b3log.org/images/brand/pipe-128.png',
  52. 'solo': '//static.b3log.org/images/brand/solo-128.png',
  53. 'wide': '//static.b3log.org/images/brand/wide-128.png',
  54. 'chainbook': '//static.b3log.org/images/brand/chainbook-128.png',
  55. 'vditor': '//static.b3log.org/images/brand/vditor-128.png',
  56. 'b3log': '//static.b3log.org/images/brand/b3log-128.png',
  57. 'bnd': '//static.b3log.org/images/brand/bnd-128.png',
  58. },
  59. },
  60. tab: '\t',
  61. upload: {
  62. accept: 'image/*,.wav',
  63. token: 'test',
  64. url: '/api/upload/editor',
  65. linkToImgUrl: '/api/upload/fetch',
  66. filename (name) {
  67. // ? \ / : | < > * [ ] white to -
  68. return name.replace(/\?|\\|\/|:|\||<|>|\*|\[|\]|\s+/g, '-')
  69. },
  70. handler (file) {
  71. console.log(file)
  72. return 'handler'
  73. },
  74. },
  75. preview: {
  76. mode: 'both',
  77. parse: () => {
  78. LazyLoadImage()
  79. },
  80. },
  81. })
  82. window.vditorTest = new Vditor('vditorTest', {
  83. cache: true,
  84. height: 200,
  85. width: 500,
  86. counter: 100,
  87. resize: {
  88. enable: true,
  89. position: 'top',
  90. after: height => {
  91. console.log(`after resize, height change: ${height}`)
  92. },
  93. },
  94. placeholder: 'say sth...',
  95. lang: 'en_US',
  96. preview: {
  97. url: '/api/markdown',
  98. parse: (element) => {
  99. console.log(element)
  100. LazyLoadImage()
  101. },
  102. },
  103. hint: {
  104. emojiPath: 'https://static.hacpai.com/emoji/graphics',
  105. emojiTail: '<a href="https://hacpai.com/settings/function" target="_blank">设置常用表情</a>',
  106. emoji: {
  107. '+1': '👍',
  108. '-1': '👎',
  109. 'trollface': 'https://unpkg.com/[email protected]/dist/images/emoji/trollface.png',
  110. 'huaji': 'https://unpkg.com/[email protected]/dist/images/emoji/huaji.gif',
  111. },
  112. at: (key) => {
  113. console.log(`atUser: ${key}`)
  114. return [
  115. {
  116. value: '@88250',
  117. html: '<img src="https://img.hacpai.com/avatar/1353745196354_1535379434567.png?imageView2/1/w/52/h/52/interlace/0/q"> 88250',
  118. },
  119. {
  120. value: '@Vanessa',
  121. html: '<img src="https://img.hacpai.com/avatar/1353745196354_1535379434567.png?imageView2/1/w/52/h/52/interlace/0/q"> Vanessa',
  122. }]
  123. },
  124. },
  125. focus: (val) => {
  126. console.log(`focus value: ${val}`)
  127. },
  128. blur: (val) => {
  129. console.log(`blur: ${val}`)
  130. },
  131. input: (val, mdElement) => {
  132. console.log('change:' + val, mdElement)
  133. },
  134. esc: (val) => {
  135. console.log(`esc: ${val}`)
  136. console.log(
  137. `cursor position:${JSON.stringify(vditorTest.getCursorPosition())}`)
  138. },
  139. ctrlEnter: (val) => {
  140. console.log(`ctrlEnter: ${val}`)
  141. },
  142. select: (val) => {
  143. console.log('select:', val)
  144. },
  145. upload: {
  146. accept: 'image/*,.pdf',
  147. token: 'test',
  148. url: '/api/upload/editor',
  149. linkToImgUrl: '/api/upload/fetch',
  150. filename: name => {
  151. // ? \ / : | < > * [ ] white to -
  152. return name.replace(/\?|\\|\/|:|\||<|>|\*|\[|\]|\s+/g, '-')
  153. },
  154. },
  155. toolbar: [
  156. {
  157. hotkey: '⌘-⇧-f',
  158. name: 'format',
  159. tipPosition: 'ne',
  160. tip: 'format',
  161. icon: '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="768" height="768" viewBox="0 0 768 768"><path d="M342 426v-84h426v84h-426zM342 256v-86h426v86h-426zM0 0h768v86h-768v-86zM342 598v-86h426v86h-426zM0 214l170 170-170 170v-340zM0 768v-86h768v86h-768z"></path></svg>',
  162. click: () => {
  163. alert('custom toolbar')
  164. },
  165. },
  166. {
  167. name: 'preview',
  168. tipPosition: 'ne',
  169. },
  170. {
  171. name: 'both',
  172. tipPosition: 'ne',
  173. },
  174. 'br',
  175. {
  176. name: 'emoji',
  177. },
  178. 'strike',
  179. {
  180. name: 'download',
  181. tipPosition: 'nw',
  182. tip: '下载',
  183. 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>',
  184. click:
  185. () => {
  186. alert('download')
  187. },
  188. },
  189. ],
  190. })