ckeditor.vue 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. default: "",
  13. },
  14. id: {
  15. type: String,
  16. default: () => `editor-${++inc}`,
  17. },
  18. height: {
  19. type: String,
  20. default: "200px",
  21. },
  22. width: {
  23. type: String,
  24. default: "980px",
  25. },
  26. toolbar: {
  27. type: [String, Array],
  28. default: null,
  29. },
  30. language: {
  31. type: String,
  32. default: "zh-cn",
  33. },
  34. extraplugins: {
  35. type: String,
  36. default: "",
  37. },
  38. extrabuttons: {
  39. type: String,
  40. default: "",
  41. },
  42. readonly: {
  43. type: Boolean,
  44. },
  45. },
  46. computed: {
  47. instance() {
  48. return window.CKEDITOR.instances[this.id];
  49. },
  50. },
  51. beforeUpdate() {
  52. if (this.value !== this.instance.getData()) {
  53. this.instance.setData(this.value);
  54. }
  55. },
  56. mounted() {
  57. var removePluginStr = "bidi,image";
  58. var removeButtonStr =
  59. "Table,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";
  60. if (!this.extrabuttons || this.extrabuttons.indexOf("Font") == -1) {
  61. removeButtonStr = removeButtonStr + ",Font";
  62. }
  63. if (!this.extrabuttons || this.extrabuttons.indexOf("FontSize") == -1) {
  64. removeButtonStr = removeButtonStr + ",FontSize";
  65. }
  66. if (!this.extrabuttons || this.extrabuttons.indexOf("TextColor") == -1) {
  67. removeButtonStr = removeButtonStr + ",TextColor";
  68. }
  69. let config = {
  70. toolbar: this.toolbar,
  71. language: this.language,
  72. height: this.height,
  73. width: this.width,
  74. extraPlugins: this.extraplugins + ",image2,base64image,pastebase64",
  75. removeButtons: removeButtonStr,
  76. removePlugins: removePluginStr,
  77. };
  78. window.CKEDITOR.replace(this.id, config);
  79. this.instance.on("instanceReady", (ev) => {
  80. ev.editor.setReadOnly(this.readonly);
  81. });
  82. this.instance.on("change", () => {
  83. let html = this.instance.getData();
  84. if (html !== this.value) {
  85. this.$emit("input", html);
  86. }
  87. });
  88. },
  89. beforeDestroy() {
  90. if (this.instance) {
  91. this.instance.focusManager.blur(true);
  92. this.instance.destroy();
  93. }
  94. },
  95. };
  96. </script>