plugin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. (function() {
  2. CKEDITOR.plugins.add('uploadimage', {
  3. requires: ["dialog"],
  4. init: function(editor) {
  5. editor.addCommand("uploadimage", new CKEDITOR.dialogCommand("uploadimage"));
  6. editor.ui.addButton("uploadimage", {
  7. label: "图片文件上传", //调用dialog时显示的名称
  8. command: "uploadimage",
  9. icon: this.path + "../image2/icons/image.png" //在toolbar中的图标
  10. });
  11. backends = {
  12. basic: {
  13. upload: uploadBasic,
  14. uploadImgUrl: "",
  15. required: ['uploadUrl'],
  16. init: function() {}
  17. }
  18. };
  19. var checkRequirement = function(condition, message) {
  20. if (!condition)
  21. throw Error("Assert failed" + (typeof message !== "undefined" ? ": " + message : ""));
  22. };
  23. function validateConfig() {
  24. var errorTemplate = 'uploadimageUpload Error: ->';
  25. checkRequirement(
  26. editor.config.hasOwnProperty('uploadimageConfig'),
  27. errorTemplate + "Missing required uploadimageConfig in CKEDITOR.config.js"
  28. );
  29. var backend = backends[editor.config.uploadimageConfig.backend];
  30. var suppliedKeys = Object.keys(editor.config.uploadimageConfig.settings);
  31. var requiredKeys = backend.required;
  32. var missing = requiredKeys.filter(function(key) {
  33. return suppliedKeys.indexOf(key) < 0
  34. });
  35. if (missing.length > 0) {
  36. throw 'Invalid Config: Missing required keys: ' + missing.join(', ')
  37. }
  38. }
  39. validateConfig();
  40. var backend = backends[editor.config.uploadimageConfig.backend];
  41. backend.init();
  42. function doNothing(e) {}
  43. function orPopError(err) {}
  44. function dropHandler(e) {
  45. e.preventDefault();
  46. var file = e.dataTransfer.files[0];
  47. backend.upload(file).then(insertImage, orPopError);
  48. alert("dohand")
  49. }
  50. editor.upload = up;
  51. function up(file) {
  52. backend.upload(file).then(insertImage, orPopError);
  53. }
  54. editor.apiUri = editor.config.uploadimageConfig.settings.downloadUrl;
  55. function insertImage(href) {
  56. var elem = editor.document.createElement('img', {
  57. attributes: {
  58. src: editor.apiUri + href
  59. }
  60. });
  61. editor.insertElement(elem);
  62. console.log(CKEDITOR.document.getById("uploadImg").$.files)
  63. }
  64. editor.background = function(objectId) {
  65. var obj = document.getElementById('objectId');
  66. obj.style.backgroundColor = "red";
  67. }
  68. function addHeaders(xhttp, headers) {
  69. for (var key in headers) {
  70. if (headers.hasOwnProperty(key)) {
  71. xhttp.setRequestHeader(key, headers[key]);
  72. }
  73. }
  74. }
  75. function post(url, data, headers) {
  76. return new Promise(function(resolve, reject) {
  77. var xhttp = new XMLHttpRequest();
  78. var formData = new FormData();
  79. formData.append("file", data);
  80. formData.append("fileName", data.name);
  81. formData.append("type", "images");
  82. xhttp.open('POST', url);
  83. addHeaders(xhttp, headers);
  84. xhttp.onreadystatechange = function() {
  85. if (xhttp.readyState === 4) {
  86. if (xhttp.status === 200) {
  87. var requireData = JSON.parse(xhttp.responseText);
  88. resolve('file'+requireData.data.relativeFilePath);
  89. } else {
  90. reject(JSON.parse(xhttp.responseText));
  91. }
  92. }
  93. };
  94. xhttp.send(formData);
  95. });
  96. }
  97. function addImg(file,editor){
  98. let reader = new FileReader();
  99. reader.readAsDataURL(file);
  100. reader.onload = function() {
  101. let result = this.result; //result为data url的形式
  102. // editor.$.activeElement.contentDocument.body.innerHTML+=("<img style='width:400px;' src="+result+" ><input style='display:inline-block;width:0' id='dingdandan'>")
  103. var elem = editor.document.createElement('img', {
  104. attributes: {
  105. src: result
  106. }
  107. });
  108. editor.insertElement(elem);
  109. }
  110. }
  111. editor.addImg = addImg;
  112. function uploadBasic(file) {
  113. var settings = editor.config.uploadimageConfig.settings;
  114. return post(settings.uploadUrl, file, settings.headers);
  115. }
  116. // editor.on('paste', function( evt ) {
  117. // //if ( !checkImage( evt.data.dataValue ) )
  118. // console.log('1231');
  119. // evt.cancel();
  120. // });
  121. CKEDITOR.on('instanceReady', function() {
  122. var iframeBase = document.querySelector('iframe').contentDocument.querySelector('html');
  123. var iframeBody = iframeBase.querySelector('body');
  124. iframeBody.ondragover = doNothing;
  125. iframeBody.ondrop = dropHandler;
  126. paddingToCenterBody = ((iframeBase.offsetWidth - iframeBody.offsetWidth) / 2) + 'px';
  127. iframeBase.style.height = '100%';
  128. iframeBase.style.width = '100%';
  129. iframeBase.style.overflowX = 'hidden';
  130. iframeBody.style.height = '100%';
  131. iframeBody.style.margin = '0';
  132. iframeBody.style.paddingLeft = paddingToCenterBody;
  133. iframeBody.style.paddingRight = paddingToCenterBody;
  134. });
  135. CKEDITOR.dialog.add("uploadimage",
  136. function(a) {
  137. return {
  138. title: "图片上传",
  139. minWidth: "660px",
  140. minHeight: "400px",
  141. contents: [{
  142. id: "tab1",
  143. label: "",
  144. title: "图片上传",
  145. expand: true,
  146. width: "420px",
  147. height: "",
  148. padding: 0,
  149. elements: [{
  150. type: "html",
  151. style: "width:100%;",
  152. html: '<div><input type="file" class="uploadImgfwb" id="uploadImg" placeholder="请选择上传的图片" style="margin:10px 0px;"/></div>',
  153. onLoad: function(a) {
  154. // CKEDITOR.document.getById("uploadImg").on('change', function() {
  155. // var fileType = this.$.files[0].type;
  156. // var fileTypeBoolean = (fileType == "image/jpeg" || fileType == "image/jpg" || fileType == "image/png" || fileType == "image/bmp");
  157. // if (this.$.files.length == 1 && fileTypeBoolean) {
  158. // var imgBtn = document.getElementById('uploadImgBtn');
  159. // imgBtn.style.backgroundColor = "dodgerblue";
  160. // } else {
  161. // console.log(false);
  162. // }
  163. // });
  164. $($(".uploadImgfwb")[$(".uploadImgfwb").length-1]).on('change', function() {
  165. var fileType = this.files[0].type;
  166. var fileTypeBoolean = (fileType == "image/jpeg" || fileType == "image/jpg" || fileType == "image/png" || fileType == "image/bmp");
  167. if (this.files.length == 1 && fileTypeBoolean) {
  168. $($('.uploadImgfwbBtn')[$(".uploadImgfwbBtn").length-1])[0].style.backgroundColor = "dodgerblue";
  169. } else {
  170. console.log(false);
  171. }
  172. });
  173. }
  174. },
  175. {
  176. type: "html",
  177. style: "width:100%;",
  178. html: '<div><button class="uploadImgfwbBtn" id="uploadImgBtn" style="border: 1px solid black;margin: 2px; padding: 2px;border-radius: 1px;background:#d6d6d8;margin:10px 0px;width:60px;text-align: center;">上传</button></div>',
  179. onLoad: function(a) {
  180. console.log(CKEDITOR.document.getById(this.domId));
  181. // CKEDITOR.document.getById(this.domId).on('click', function() {
  182. // var fileType = CKEDITOR.document.getById("uploadImg").$.files[0].type;
  183. // var filesLength = CKEDITOR.document.getById("uploadImg").$.files.length;
  184. // var fileTypeBoolean = (fileType == "image/jpeg" || fileType == "image/jpg" || fileType == "image/png" || fileType == "image/bmp");
  185. // var dialog = this.getDialog();
  186. // if (filesLength == 1 && fileTypeBoolean) {
  187. // dialog._.editor.upload(CKEDITOR.document.getById("uploadImg").$.files[0]);
  188. // // dialog._.editor.addImg(CKEDITOR.document.getById("uploadImg").$.files[0]);
  189. // var imgBtn = document.getElementById('uploadImgBtn');
  190. // imgBtn.style.backgroundColor = "#d6d6d8";
  191. // dialog.hide();
  192. // }
  193. // }, this);
  194. CKEDITOR.document.getById(this.domId).on('click', function() {
  195. var fileType = $($(".uploadImgfwb")[$(".uploadImgfwb").length-1])[0].files[0].type;
  196. var filesLength = $($(".uploadImgfwb")[$(".uploadImgfwb").length-1])[0].files.length;
  197. var fileTypeBoolean = (fileType == "image/jpeg" || fileType == "image/jpg" || fileType == "image/png" || fileType == "image/bmp");
  198. var dialog = this.getDialog();
  199. if (filesLength == 1 && fileTypeBoolean) {
  200. dialog._.editor.upload($($(".uploadImgfwb")[$(".uploadImgfwb").length-1])[0].files[0]);
  201. // dialog._.editor.addImg(CKEDITOR.document.getById("uploadImg").$.files[0]);
  202. var imgBtn = document.getElementById('uploadImgBtn');
  203. imgBtn.style.backgroundColor = "#d6d6d8";
  204. dialog.hide();
  205. }
  206. }, this);
  207. }
  208. },
  209. ]
  210. }],
  211. onOk: function() {},
  212. onShow: function() {}
  213. }
  214. })
  215. }
  216. });
  217. })();