index.js 5.5 KB

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