plugin.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 + "templates.png" //在toolbar中的图标
  10. });
  11. backends = {
  12. basic: {
  13. upload: uploadBasic,
  14. uploadImgUrl:"",
  15. required: ['uploadUrl'],
  16. init: function() {
  17. }
  18. }
  19. };
  20. var checkRequirement = function(condition, message) {
  21. if (!condition)
  22. throw Error("Assert failed" + (typeof message !== "undefined" ? ": " + message : ""));
  23. };
  24. function validateConfig() {
  25. var errorTemplate = 'uploadimageUpload Error: ->';
  26. checkRequirement(
  27. editor.config.hasOwnProperty('uploadimageConfig'),
  28. errorTemplate + "Missing required uploadimageConfig in CKEDITOR.config.js"
  29. );
  30. var backend = backends[editor.config.uploadimageConfig.backend];
  31. var suppliedKeys = Object.keys(editor.config.uploadimageConfig.settings);
  32. var requiredKeys = backend.required;
  33. var missing = requiredKeys.filter(function(key) {
  34. return suppliedKeys.indexOf(key) < 0
  35. });
  36. if (missing.length > 0) {
  37. throw 'Invalid Config: Missing required keys: ' + missing.join(', ')
  38. }
  39. }
  40. validateConfig();
  41. var backend = backends[editor.config.uploadimageConfig.backend];
  42. backend.init();
  43. function doNothing(e) { }
  44. function orPopError(err) { }
  45. function dropHandler(e) {
  46. e.preventDefault();
  47. var file = e.dataTransfer.files[0];
  48. backend.upload(file).then(insertImage, orPopError);
  49. alert("dohand")
  50. }
  51. editor.upload = up;
  52. function up(file) {
  53. backend.upload(file).then(insertImage, orPopError);
  54. }
  55. editor.apiUri = editor.config.uploadimageConfig.settings.downloadUrl;
  56. function insertImage(href) {
  57. var elem = editor.document.createElement('img', {
  58. attributes: {
  59. src: editor.apiUri+href
  60. }
  61. });
  62. editor.insertElement(elem);
  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(requireData.imageUrl);
  89. } else {
  90. reject(JSON.parse(xhttp.responseText));
  91. }
  92. }
  93. };
  94. xhttp.send(formData);
  95. });
  96. }
  97. function uploadBasic(file) {
  98. var settings = editor.config.uploadimageConfig.settings;
  99. return post(settings.uploadUrl, file, settings.headers);
  100. }
  101. // editor.on('paste', function( evt ) {
  102. // //if ( !checkImage( evt.data.dataValue ) )
  103. // console.log('1231');
  104. // evt.cancel();
  105. // });
  106. CKEDITOR.on('instanceReady', function() {
  107. var iframeBase = document.querySelector('iframe').contentDocument.querySelector('html');
  108. var iframeBody = iframeBase.querySelector('body');
  109. iframeBody.ondragover = doNothing;
  110. iframeBody.ondrop = dropHandler;
  111. paddingToCenterBody = ((iframeBase.offsetWidth - iframeBody.offsetWidth) / 2) + 'px';
  112. iframeBase.style.height = '100%';
  113. iframeBase.style.width = '100%';
  114. iframeBase.style.overflowX = 'hidden';
  115. iframeBody.style.height = '100%';
  116. iframeBody.style.margin = '0';
  117. iframeBody.style.paddingLeft = paddingToCenterBody;
  118. iframeBody.style.paddingRight = paddingToCenterBody;
  119. });
  120. CKEDITOR.dialog.add("uploadimage",
  121. function(a) {
  122. return {
  123. title: "图片上传",
  124. minWidth: "660px",
  125. minHeight:"400px",
  126. contents: [{
  127. id: "tab1",
  128. label: "",
  129. title: "图片上传",
  130. expand: true,
  131. width: "420px",
  132. height: "",
  133. padding: 0,
  134. elements: [
  135. {
  136. type: "html",
  137. style: "width:100%;",
  138. html: '<div><input type="file" id="uploadImg" placeholder="请选择上传的图片" style="margin:10px 0px;"/></div>',
  139. onLoad: function(a) {
  140. CKEDITOR.document.getById("uploadImg").on('change', function() {
  141. var fileType=this.$.files[0].type;
  142. var fileTypeBoolean=(fileType=="image/jpeg"||fileType=="image/jpg"||fileType=="image/png"||fileType=="image/bmp");
  143. if(this.$.files.length==1&&fileTypeBoolean){
  144. var imgBtn = document.getElementById('uploadImgBtn');
  145. imgBtn.style.backgroundColor = "dodgerblue";
  146. }else{
  147. console.log(false);
  148. }
  149. });
  150. }
  151. },
  152. {
  153. type: "html",
  154. style: "width:100%;",
  155. 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>',
  156. onLoad: function(a) {
  157. CKEDITOR.document.getById( this.domId ).on( 'click', function() {
  158. var fileType=CKEDITOR.document.getById("uploadImg").$.files[0].type;
  159. var filesLength=CKEDITOR.document.getById("uploadImg").$.files.length;
  160. var fileTypeBoolean=(fileType=="image/jpeg"||fileType=="image/jpg"||fileType=="image/png"||fileType=="image/bmp");
  161. var dialog = this.getDialog();
  162. if(filesLength==1&&fileTypeBoolean){
  163. dialog._.editor.upload(CKEDITOR.document.getById("uploadImg").$.files[0]);
  164. var imgBtn = document.getElementById('uploadImgBtn');
  165. imgBtn.style.backgroundColor = "#d6d6d8";
  166. dialog.hide();
  167. }
  168. },this);
  169. }
  170. },
  171. ]
  172. }],
  173. onOk: function() {
  174. },
  175. onShow: function () {
  176. }
  177. }
  178. })
  179. }
  180. });
  181. })();