plugin.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. }
  63. editor.background = function(objectId) {
  64. var obj = document.getElementById('objectId');
  65. obj.style.backgroundColor = "red";
  66. }
  67. function addHeaders(xhttp, headers) {
  68. for (var key in headers) {
  69. if (headers.hasOwnProperty(key)) {
  70. xhttp.setRequestHeader(key, headers[key]);
  71. }
  72. }
  73. }
  74. function post(url, data, headers) {
  75. return new Promise(function(resolve, reject) {
  76. var xhttp = new XMLHttpRequest();
  77. var formData = new FormData();
  78. formData.append("file", data);
  79. formData.append("fileName", data.name);
  80. formData.append("type", "images");
  81. xhttp.open('POST', url);
  82. addHeaders(xhttp, headers);
  83. xhttp.onreadystatechange = function() {
  84. if (xhttp.readyState === 4) {
  85. if (xhttp.status === 200) {
  86. var requireData = JSON.parse(xhttp.responseText);
  87. resolve(requireData.imageUrl);
  88. } else {
  89. reject(JSON.parse(xhttp.responseText));
  90. }
  91. }
  92. };
  93. xhttp.send(formData);
  94. });
  95. }
  96. function uploadBasic(file) {
  97. var settings = editor.config.uploadimageConfig.settings;
  98. return post(settings.uploadUrl, file, settings.headers);
  99. }
  100. // editor.on('paste', function( evt ) {
  101. // //if ( !checkImage( evt.data.dataValue ) )
  102. // console.log('1231');
  103. // evt.cancel();
  104. // });
  105. CKEDITOR.on('instanceReady', function() {
  106. var iframeBase = document.querySelector('iframe').contentDocument.querySelector('html');
  107. var iframeBody = iframeBase.querySelector('body');
  108. iframeBody.ondragover = doNothing;
  109. iframeBody.ondrop = dropHandler;
  110. paddingToCenterBody = ((iframeBase.offsetWidth - iframeBody.offsetWidth) / 2) + 'px';
  111. iframeBase.style.height = '100%';
  112. iframeBase.style.width = '100%';
  113. iframeBase.style.overflowX = 'hidden';
  114. iframeBody.style.height = '100%';
  115. iframeBody.style.margin = '0';
  116. iframeBody.style.paddingLeft = paddingToCenterBody;
  117. iframeBody.style.paddingRight = paddingToCenterBody;
  118. });
  119. CKEDITOR.dialog.add("uploadimage",
  120. function(a) {
  121. return {
  122. title: "图片上传",
  123. minWidth: "660px",
  124. minHeight: "400px",
  125. contents: [{
  126. id: "tab1",
  127. label: "",
  128. title: "图片上传",
  129. expand: true,
  130. width: "420px",
  131. height: "",
  132. padding: 0,
  133. elements: [{
  134. type: "html",
  135. style: "width:100%;",
  136. html: '<div><input type="file" id="uploadImg" placeholder="请选择上传的图片" style="margin:10px 0px;"/></div>',
  137. onLoad: function(a) {
  138. CKEDITOR.document.getById("uploadImg").on('change', function() {
  139. var fileType = this.$.files[0].type;
  140. var fileTypeBoolean = (fileType == "image/jpeg" || fileType == "image/jpg" || fileType == "image/png" || fileType == "image/bmp");
  141. if (this.$.files.length == 1 && fileTypeBoolean) {
  142. var imgBtn = document.getElementById('uploadImgBtn');
  143. imgBtn.style.backgroundColor = "dodgerblue";
  144. } else {
  145. console.log(false);
  146. }
  147. });
  148. }
  149. },
  150. {
  151. type: "html",
  152. style: "width:100%;",
  153. html: '<div><button 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>',
  154. onLoad: function(a) {
  155. CKEDITOR.document.getById(this.domId).on('click', function() {
  156. var fileType = CKEDITOR.document.getById("uploadImg").$.files[0].type;
  157. var filesLength = CKEDITOR.document.getById("uploadImg").$.files.length;
  158. var fileTypeBoolean = (fileType == "image/jpeg" || fileType == "image/jpg" || fileType == "image/png" || fileType == "image/bmp");
  159. var dialog = this.getDialog();
  160. if (filesLength == 1 && fileTypeBoolean) {
  161. dialog._.editor.upload(CKEDITOR.document.getById("uploadImg").$.files[0]);
  162. var imgBtn = document.getElementById('uploadImgBtn');
  163. imgBtn.style.backgroundColor = "#d6d6d8";
  164. dialog.hide();
  165. }
  166. }, this);
  167. }
  168. },
  169. ]
  170. }],
  171. onOk: function() {},
  172. onShow: function() {}
  173. }
  174. })
  175. }
  176. });
  177. })();