demo.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // webpack.demo.js
  2. import Vditor from '../src/index'
  3. // import Vditor from 'vditor'
  4. // import Vditor from '../dist/index.min'
  5. const vditor = new Vditor('vditor', {
  6. height: 200,
  7. width: '50%',
  8. counter: 100,
  9. draggable: true,
  10. placeholder: 'say sth...',
  11. lang: 'en_US',
  12. preview: {
  13. url: '/api/markdown',
  14. parse: (element) => {
  15. console.log(element)
  16. },
  17. },
  18. hint: {
  19. emoji: {
  20. '+1': '👍',
  21. '-1': '👎',
  22. },
  23. at: (key) => {
  24. console.log(`atUser: ${key}`)
  25. return [
  26. {
  27. value: '@88250',
  28. html: '<img src="https://img.hacpai.com/avatar/1353745196354_1535379434567.png?imageView2/1/w/52/h/52/interlace/0/q"> 88250',
  29. },
  30. {
  31. value: '@Vanessa',
  32. html: '<img src="https://img.hacpai.com/avatar/1353745196354_1535379434567.png?imageView2/1/w/52/h/52/interlace/0/q"> Vanessa',
  33. }]
  34. },
  35. },
  36. classes: {
  37. preview: 'content-reset',
  38. },
  39. focus: (val) => {
  40. console.log(`focus value: ${val}`)
  41. console.log(
  42. `focus cursor position:${JSON.stringify(vditor.getCursorPosition())}`)
  43. },
  44. blur: (val) => {
  45. console.log(`blur: ${val}`)
  46. },
  47. input: (val, mdElement) => {
  48. console.log('change:' + val, mdElement)
  49. },
  50. esc: (val) => {
  51. console.log(`esc: ${val}`)
  52. },
  53. ctrlEnter: (val) => {
  54. console.log(`ctrlEnter: ${val}`)
  55. },
  56. select: (val) => {
  57. console.log(`select: ${val}`)
  58. },
  59. toolbar: [
  60. {
  61. name: 'preview',
  62. tipPosition: 'ne',
  63. },
  64. 'br'
  65. , {
  66. name: 'emoji',
  67. tail: '<a href="https://hacpai.com/settings/function" target="_blank">设置常用表情</a>',
  68. },
  69. 'strike',
  70. ],
  71. })
  72. const vditor2 = new Vditor('vditor2', {
  73. upload: {
  74. url: '/api/upload/editor',
  75. linkToImgUrl: '/api/fetch-upload',
  76. },
  77. preview: {
  78. show: true,
  79. url: '/api/markdown',
  80. parse: (element) => {
  81. console.log(element)
  82. },
  83. },
  84. })
  85. // vditor.insertVale('Hi, Vditor!')
  86. // vditor.focus()
  87. // console.log('vditor.getValue(): ' + vditor.getValue())
  88. // vditor.setSelection(4, 9)
  89. // console.log('vditor.getSelection(): ' + vditor.getSelection())
  90. // setTimeout(() => {
  91. // vditor.setValue('Hi, Markdown!')
  92. // vditor.renderPreview()
  93. // vditor.disabled()
  94. // }, 3000)
  95. //
  96. // setTimeout(() => {
  97. // vditor.enable()
  98. // vditor.setSelection(4, 12)
  99. // vditor.deleteValue()
  100. // }, 6000)
  101. //
  102. // setTimeout(() => {
  103. // vditor.setSelection(0, 4)
  104. // vditor.updateValue('Welcome')
  105. // vditor.blur()
  106. // }, 9000)