index.vue 2.7 KB

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