demo.js 2.5 KB

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