MceEditor.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <div>
  3. <textarea :id="Id"></textarea>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. const Id = Date.now();
  10. return {
  11. hasChange: false,
  12. hasInit: false,
  13. Id: Id,
  14. Editor: null,
  15. DefaultConfig: {
  16. language: 'zh_CN',
  17. // GLOBAL
  18. height: 500,
  19. theme: "modern",
  20. menubar: false,
  21. toolbar: `styleselect | fontselect | formatselect | fontsizeselect | forecolor backcolor | bold italic underline strikethrough | image media | table | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | preview removeformat hr | paste code link | undo redo | fullscreen `,
  22. plugins: `
  23. paste
  24. importcss
  25. image
  26. code
  27. table
  28. advlist
  29. fullscreen
  30. link
  31. media
  32. lists
  33. textcolor
  34. colorpicker
  35. hr
  36. preview
  37. `,
  38. // CONFIG
  39. forced_root_block: "p",
  40. force_p_newlines: true,
  41. importcss_append: true,
  42. // CONFIG: ContentStyle 这块很重要, 在最后呈现的页面也要写入这个基本样式保证前后一致, `table`和`img`的问题基本就靠这个来填坑了
  43. content_style: `
  44. * { padding:0; margin:0; }
  45. html, body { height:100%; }
  46. img { max-width:100%; display:block;height:auto; }
  47. a { text-decoration: none; }
  48. iframe { width: 100%; }
  49. p { line-height:1.6; margin: 0px; }
  50. table { word-wrap:break-word; word-break:break-all; max-width:100%; border:none; border-color:#999; }
  51. .mce-object-iframe { width:100%; box-sizing:border-box; margin:0; padding:0; }
  52. ul,ol { list-style-position:inside; }
  53. `,
  54. insert_button_items: "image link | inserttable",
  55. // CONFIG: Paste
  56. paste_retain_style_properties: "all",
  57. paste_word_valid_elements: "*[*]", // word需要它
  58. paste_data_images: true, // 粘贴的同时能把内容里的图片自动上传,非常强力的功能
  59. paste_convert_word_fake_lists: false, // 插入word文档需要该属性
  60. paste_webkit_styles: "all",
  61. paste_merge_formats: true,
  62. nonbreaking_force_tab: false,
  63. paste_auto_cleanup_on_paste: false,
  64. // CONFIG: Font
  65. fontsize_formats: "10px 11px 12px 14px 16px 18px 20px 24px",
  66. // CONFIG: StyleSelect
  67. style_formats: [
  68. {
  69. title: "首行缩进",
  70. block: "p",
  71. styles: { "text-indent": "2em" }
  72. },
  73. {
  74. title: "行高",
  75. items: [
  76. { title: "1", styles: { "line-height": "1" }, inline: "span" },
  77. {
  78. title: "1.5",
  79. styles: { "line-height": "1.5" },
  80. inline: "span"
  81. },
  82. { title: "2", styles: { "line-height": "2" }, inline: "span" },
  83. {
  84. title: "2.5",
  85. styles: { "line-height": "2.5" },
  86. inline: "span"
  87. },
  88. { title: "3", styles: { "line-height": "3" }, inline: "span" }
  89. ]
  90. }
  91. ],
  92. // FontSelect
  93. font_formats: `
  94. 微软雅黑=微软雅黑;
  95. 宋体=宋体;
  96. 黑体=黑体;
  97. 仿宋=仿宋;
  98. 楷体=楷体;
  99. 隶书=隶书;
  100. 幼圆=幼圆;
  101. Andale Mono=andale mono,times;
  102. Arial=arial, helvetica,
  103. sans-serif;
  104. Arial Black=arial black, avant garde;
  105. Book Antiqua=book antiqua,palatino;
  106. Comic Sans MS=comic sans ms,sans-serif;
  107. Courier New=courier new,courier;
  108. Georgia=georgia,palatino;
  109. Helvetica=helvetica;
  110. Impact=impact,chicago;
  111. Symbol=symbol;
  112. Tahoma=tahoma,arial,helvetica,sans-serif;
  113. Terminal=terminal,monaco;
  114. Times New Roman=times new roman,times;
  115. Trebuchet MS=trebuchet ms,geneva;
  116. Verdana=verdana,geneva;
  117. Webdings=webdings;
  118. Wingdings=wingdings,zapf dingbats`,
  119. // Tab
  120. tabfocus_elements: ":prev,:next",
  121. object_resizing: true,
  122. // Image
  123. imagetools_toolbar:
  124. "rotateleft rotateright | flipv fliph | editimage imageoptions"
  125. }
  126. };
  127. },
  128. props: {
  129. value: {
  130. default: "",
  131. type: String
  132. },
  133. config: {
  134. type: Object,
  135. default: () => {
  136. return {
  137. theme: "modern",
  138. height: 300
  139. };
  140. }
  141. },
  142. url: {
  143. default: "",
  144. type: String
  145. },
  146. accept: {
  147. default: "image/jpeg,image/png",
  148. type: String
  149. },
  150. maxSize: {
  151. default: 1024*1024*1000,
  152. type: Number
  153. },
  154. withCredentials: {
  155. default: false,
  156. type: Boolean
  157. }
  158. },
  159. watch: {
  160. value(val) {
  161. if (!this.hasChange && this.hasInit) {
  162. this.$nextTick(() =>
  163. window.tinymce.get(this.tinymceId).setContent(val || ''))
  164. }
  165. }
  166. },
  167. mounted() {
  168. this.init();
  169. },
  170. beforeDestroy() {
  171. // 销毁tinymce
  172. this.$emit("on-destroy");
  173. window.tinymce.remove(`#${this.Id}`);
  174. },
  175. methods: {
  176. init() {
  177. const self = this;
  178. this.Editor = window.tinymce.init({
  179. // 默认配置
  180. ...this.DefaultConfig,
  181. // 图片上传
  182. images_upload_handler: function(blobInfo, success, failure) {
  183. if (blobInfo.blob().size > self.maxSize) {
  184. failure("文件体积过大");
  185. }
  186. if (self.accept.indexOf(blobInfo.blob().type) > 0) {
  187. uploadPic();
  188. } else {
  189. failure("图片格式错误");
  190. }
  191. function uploadPic() {
  192. const xhr = new XMLHttpRequest();
  193. const formData = new FormData();
  194. xhr.withCredentials = self.withCredentials;
  195. xhr.open("POST", self.url);
  196. xhr.onload = function() {
  197. if (xhr.status !== 200) {
  198. // 抛出 'on-upload-fail' 钩子
  199. self.$emit("on-upload-fail");
  200. failure("上传失败: " + xhr.status);
  201. return;
  202. }
  203. const json = JSON.parse(xhr.responseText);
  204. /* eslint-disable-next-line */
  205. console.log(json);
  206. success(json.location);
  207. // 抛出 'on-upload-complete' 钩子
  208. self.$emit("on-upload-complete", [json, success, failure]);
  209. };
  210. formData.append("file", blobInfo.blob());
  211. xhr.send(formData);
  212. }
  213. },
  214. // prop内传入的的config
  215. ...this.config,
  216. // 挂载的DOM对象
  217. selector: `#${this.Id}`,
  218. init_instance_callback: (editor) => {
  219. if (self.value) {
  220. editor.setContent(self.value)
  221. }
  222. self.hasInit = true;
  223. editor.on('NodeChange Change KeyUp SetContent', () => {
  224. self.hasChange = true
  225. this.$emit('input', editor.getContent())
  226. })
  227. },
  228. setup: editor => {
  229. // 抛出 'on-ready' 事件钩子
  230. editor.on("init", () => {
  231. self.loading = false;
  232. self.$emit("on-ready");
  233. editor.setContent(self.value);
  234. });
  235. // 抛出 'input' 事件钩子,同步value数据
  236. editor.on("input change undo redo", () => {
  237. self.$emit("input", editor.getContent());
  238. });
  239. }
  240. });
  241. }
  242. }
  243. };
  244. </script>