ckeditor.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <div class="ckeditor" style="margin-bottom: 10px;">
  3. <textarea :id="id" :value="value"></textarea>
  4. </div>
  5. </template>
  6. <script>
  7. let inc = 0;
  8. export default {
  9. props: {
  10. value: {
  11. type: String
  12. },
  13. id: {
  14. type: String,
  15. default: () => `editor-${++inc}`
  16. },
  17. height: {
  18. type: String,
  19. default: "200px"
  20. },
  21. width: {
  22. type: String,
  23. default: "980px"
  24. },
  25. toolbar: {
  26. type: [String, Array],
  27. default: null
  28. },
  29. language: {
  30. type: String,
  31. default: "zh-cn"
  32. },
  33. extraplugins: {
  34. type: String,
  35. default: ""
  36. }
  37. },
  38. computed: {
  39. instance() {
  40. return window.CKEDITOR.instances[this.id];
  41. }
  42. },
  43. beforeUpdate() {
  44. if (this.value !== this.instance.getData()) {
  45. this.instance.setData(this.value);
  46. }
  47. },
  48. mounted() {
  49. let config = {
  50. toolbar: this.toolbar,
  51. language: this.language,
  52. height: this.height,
  53. width: this.width,
  54. extraPlugins: this.extraplugins,
  55. removeButtons:
  56. "Underline,Subscript,Superscript,Image,HorizontalRule,Unlink,Link,Scayt,Cut,Copy,Paste,PasteText,PasteFromWord,Maximize,Italic,Bold,NumberedList,BulletedList,Indent,Outdent,Blockquote,Styles,Format,About,RemoveFormat,Strike"
  57. };
  58. window.CKEDITOR.replace(this.id, config);
  59. this.instance.on("change", () => {
  60. let html = this.instance.getData();
  61. if (html !== this.value) {
  62. this.$emit("input", html);
  63. }
  64. });
  65. },
  66. beforeDestroy() {
  67. if (this.instance) {
  68. this.instance.focusManager.blur(true);
  69. this.instance.destroy();
  70. }
  71. }
  72. };
  73. </script>
  74. <style>
  75. .ckeditor::after {
  76. content: "";
  77. display: table;
  78. clear: both;
  79. }
  80. </style>