ckeditor.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. extrabuttons: {
  38. type: String,
  39. default: ""
  40. },
  41. readonly: {
  42. type: Boolean
  43. }
  44. },
  45. computed: {
  46. instance() {
  47. return window.CKEDITOR.instances[this.id];
  48. }
  49. },
  50. beforeUpdate() {
  51. if (this.value !== this.instance.getData()) {
  52. this.instance.setData(this.value);
  53. }
  54. },
  55. mounted() {
  56. var removePluginStr = "bidi";
  57. var removeButtonStr =
  58. "Image,Styles,Format,ShowBlocks,Iframe,PageBreak,Smiley,Flash,Language,JustifyBlock,JustifyRight,JustifyCenter,JustifyLeft,CreateDiv,CopyFormatting,ImageButton,Button,HiddenField,Select,Textarea,TextField,Radio,Checkbox,Form,BGColor,SelectAll,Replace,Find,Templates,Print,Preview,NewPage,Save,Underline,Subscript,Superscript,HorizontalRule,Unlink,Link,Scayt,Cut,Copy,Paste,PasteText,PasteFromWord,Maximize,Italic,Bold,NumberedList,BulletedList,Indent,Outdent,Blockquote,About,RemoveFormat,Strike";
  59. if (!this.extrabuttons || this.extrabuttons.indexOf("Font") == -1) {
  60. removeButtonStr = removeButtonStr + ",Font";
  61. }
  62. if (!this.extrabuttons || this.extrabuttons.indexOf("FontSize") == -1) {
  63. removeButtonStr = removeButtonStr + ",FontSize";
  64. }
  65. if (!this.extrabuttons || this.extrabuttons.indexOf("TextColor") == -1) {
  66. removeButtonStr = removeButtonStr + ",TextColor";
  67. }
  68. let config = {
  69. toolbar: this.toolbar,
  70. language: this.language,
  71. height: this.height,
  72. width: this.width,
  73. extraPlugins: this.extraplugins + ",base64image,pastebase64",
  74. removeButtons: removeButtonStr,
  75. removePlugins: removePluginStr
  76. };
  77. window.CKEDITOR.replace(this.id, config);
  78. this.instance.on("instanceReady", ev => {
  79. ev.editor.setReadOnly(this.readonly);
  80. });
  81. this.instance.on("change", () => {
  82. let html = this.instance.getData();
  83. if (html !== this.value) {
  84. this.$emit("input", html);
  85. }
  86. });
  87. },
  88. beforeDestroy() {
  89. if (this.instance) {
  90. this.instance.focusManager.blur(true);
  91. this.instance.destroy();
  92. }
  93. }
  94. };
  95. </script>
  96. <style>
  97. .ckeditor::after {
  98. content: "";
  99. display: table;
  100. clear: both;
  101. }
  102. </style>