index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div :id="id" />
  3. </template>
  4. <script>
  5. // deps for editor
  6. // import 'codemirror/lib/codemirror.css' // codemirror
  7. // import 'tui-editor/dist/tui-editor.css' // editor ui
  8. // import 'tui-editor/dist/tui-editor-contents.css' // editor content
  9. // import Editor from 'tui-editor'
  10. // import defaultOptions from './default-options'
  11. import 'codemirror/lib/codemirror.css'
  12. import '@toast-ui/editor/dist/toastui-editor.css'
  13. import Editor from '@toast-ui/editor'
  14. import defaultOptions from './default-options'
  15. export default {
  16. name: 'MarkdownEditor',
  17. props: {
  18. value: {
  19. type: String,
  20. default: ''
  21. },
  22. id: {
  23. type: String,
  24. required: false,
  25. default() {
  26. return 'markdown-editor-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
  27. }
  28. },
  29. options: {
  30. type: Object,
  31. default() {
  32. return defaultOptions
  33. }
  34. },
  35. mode: {
  36. type: String,
  37. default: 'markdown'
  38. },
  39. height: {
  40. type: String,
  41. required: false,
  42. default: '300px'
  43. },
  44. language: {
  45. type: String,
  46. required: false,
  47. default: 'en_US' // https://github.com/nhnent/tui.editor/tree/master/src/js/langs
  48. }
  49. },
  50. data() {
  51. return {
  52. editor: null
  53. }
  54. },
  55. computed: {
  56. editorOptions() {
  57. const options = Object.assign({}, defaultOptions, this.options)
  58. options.initialEditType = this.mode
  59. options.height = this.height
  60. options.language = this.language
  61. return options
  62. }
  63. },
  64. watch: {
  65. value(newValue, preValue) {
  66. if (newValue !== preValue && newValue !== this.editor.getValue()) {
  67. this.editor.setValue(newValue)
  68. }
  69. },
  70. language(val) {
  71. this.destroyEditor()
  72. this.initEditor()
  73. },
  74. height(newValue) {
  75. this.editor.height(newValue)
  76. },
  77. mode(newValue) {
  78. this.editor.changeMode(newValue)
  79. }
  80. },
  81. mounted() {
  82. this.initEditor()
  83. },
  84. destroyed() {
  85. this.destroyEditor()
  86. },
  87. methods: {
  88. initEditor() {
  89. this.editor = new Editor({
  90. el: document.getElementById(this.id),
  91. ...this.editorOptions
  92. })
  93. if (this.value) {
  94. this.editor.setValue(this.value)
  95. }
  96. this.editor.on('change', () => {
  97. this.$emit('input', this.editor.getValue())
  98. })
  99. },
  100. destroyEditor() {
  101. if (!this.editor) return
  102. this.editor.off('change')
  103. this.editor.remove()
  104. },
  105. setValue(value) {
  106. this.editor.setValue(value)
  107. },
  108. getValue() {
  109. return this.editor.getValue()
  110. },
  111. setHtml(value) {
  112. this.editor.setHtml(value)
  113. },
  114. getHtml() {
  115. return this.editor.getHtml()
  116. }
  117. }
  118. }
  119. </script>