index.vue 3.0 KB

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