index.vue 9.2 KB

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