ckeditor.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div :style="divStyle">
  3. <div class="ckeditor">
  4. <textarea
  5. :id="id"
  6. :display="display"
  7. :value="value"
  8. class="el-textarea__inner"
  9. ></textarea>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. let inc = 0;
  15. export default {
  16. data() {
  17. return {
  18. divStyle: null
  19. };
  20. },
  21. props: {
  22. display: {
  23. type: String,
  24. default: "inline"
  25. },
  26. value: {
  27. type: String
  28. },
  29. id: {
  30. type: String,
  31. default: () => `editor-${++inc}`
  32. },
  33. height: {
  34. type: String,
  35. default: "300px"
  36. },
  37. width: {
  38. type: String,
  39. default: "500px"
  40. },
  41. language: {
  42. type: String,
  43. default: "zh-cn"
  44. },
  45. extraplugins: {
  46. type: String,
  47. default: ""
  48. }
  49. },
  50. computed: {
  51. instance() {
  52. return window.CKEDITOR.instances[this.id];
  53. }
  54. },
  55. beforeUpdate() {
  56. if (this.value !== this.instance.getData()) {
  57. this.instance.setData(this.value);
  58. }
  59. },
  60. mounted() {
  61. if (this.width) {
  62. this.divStyle = "width:" + this.width + ";";
  63. } else {
  64. this.divStyle = "width:500px";
  65. }
  66. this.divStyle = this.divStyle + "margin-top:-15px;";
  67. let config = {
  68. language: this.language,
  69. height: this.height,
  70. width: this.width,
  71. extraPlugins: this.extraplugins,
  72. toolbarGroups: [
  73. { name: "clipboard", groups: ["clipboard", "undo"] },
  74. {
  75. name: "editing",
  76. groups: ["find", "selection", "spellchecker", "editing"]
  77. },
  78. { name: "links", groups: ["links"] },
  79. { name: "insert", groups: ["insert"] },
  80. { name: "forms", groups: ["forms"] },
  81. { name: "tools", groups: ["tools"] },
  82. { name: "document", groups: ["mode", "document", "doctools"] },
  83. { name: "others", groups: ["others"] },
  84. "/",
  85. { name: "basicstyles", groups: ["basicstyles", "cleanup"] },
  86. {
  87. name: "paragraph",
  88. groups: ["list", "indent", "blocks", "align", "bidi", "paragraph"]
  89. },
  90. { name: "styles", groups: ["styles"] },
  91. { name: "colors", groups: ["colors"] },
  92. { name: "about", groups: ["about"] }
  93. ],
  94. removeButtons:
  95. "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"
  96. };
  97. if (this.display !== "inline") {
  98. window.CKEDITOR.replace(this.id, config);
  99. } else {
  100. window.CKEDITOR.inline(this.id, config);
  101. }
  102. this.instance.on("change", () => {
  103. let html = this.instance.getData();
  104. if (html !== this.value) {
  105. this.$emit("input", html);
  106. }
  107. });
  108. },
  109. beforeDestroy() {
  110. if (this.instance) {
  111. this.instance.focusManager.blur(true);
  112. this.instance.destroy();
  113. }
  114. }
  115. };
  116. </script>
  117. <style>
  118. .ckeditor::after {
  119. content: "";
  120. display: table;
  121. clear: both;
  122. }
  123. </style>