index.js 6.2 KB

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