ckeditor.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. readonly: {
  38. type: Boolean
  39. }
  40. },
  41. computed: {
  42. instance() {
  43. return window.CKEDITOR.instances[this.id];
  44. }
  45. },
  46. beforeUpdate() {
  47. if (this.value !== this.instance.getData()) {
  48. this.instance.setData(this.value);
  49. }
  50. },
  51. mounted() {
  52. let config = {
  53. toolbar: this.toolbar,
  54. language: this.language,
  55. height: this.height,
  56. width: this.width,
  57. extraPlugins: this.extraplugins,
  58. removeButtons:
  59. "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"
  60. };
  61. window.CKEDITOR.replace(this.id, config);
  62. this.instance.on("instanceReady", ev => {
  63. ev.editor.setReadOnly(this.readonly);
  64. });
  65. this.instance.on("change", () => {
  66. let html = this.instance.getData();
  67. if (html !== this.value) {
  68. this.$emit("input", html);
  69. }
  70. });
  71. },
  72. beforeDestroy() {
  73. if (this.instance) {
  74. this.instance.focusManager.blur(true);
  75. this.instance.destroy();
  76. }
  77. }
  78. };
  79. </script>
  80. <style>
  81. .ckeditor::after {
  82. content: "";
  83. display: table;
  84. clear: both;
  85. }
  86. </style>