image-dialog.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*!
  2. * Image (upload) dialog plugin for Editor.md
  3. *
  4. * @file image-dialog.js
  5. * @author pandao
  6. * @version 1.3.4
  7. * @updateTime 2015-06-09
  8. * {@link https://github.com/pandao/editor.md}
  9. * @license MIT
  10. */
  11. (function() {
  12. var factory = function (exports) {
  13. var pluginName = "image-dialog";
  14. exports.fn.imageDialog = function() {
  15. var _this = this;
  16. var cm = this.cm;
  17. var lang = this.lang;
  18. var editor = this.editor;
  19. var settings = this.settings;
  20. var cursor = cm.getCursor();
  21. var selection = cm.getSelection();
  22. var imageLang = lang.dialog.image;
  23. var classPrefix = this.classPrefix;
  24. var iframeName = classPrefix + "image-iframe";
  25. var dialogName = classPrefix + pluginName, dialog;
  26. cm.focus();
  27. var loading = function(show) {
  28. var _loading = dialog.find("." + classPrefix + "dialog-mask");
  29. _loading[(show) ? "show" : "hide"]();
  30. };
  31. if (editor.find("." + dialogName).length < 1)
  32. {
  33. var guid = (new Date).getTime();
  34. var action = settings.imageUploadURL + (settings.imageUploadURL.indexOf("?") >= 0 ? "&" : "?") + "guid=" + guid;
  35. if (settings.crossDomainUpload)
  36. {
  37. action += "&callback=" + settings.uploadCallbackURL + "&dialog_id=editormd-image-dialog-" + guid;
  38. }
  39. var dialogContent = ((settings.imageUpload) ? "<form action=\"" + action + "\" target=\"" + iframeName + "\" method=\"post\" enctype=\"multipart/form-data\" class=\"" + classPrefix + "form\">" : "<div class=\"" + classPrefix + "form\">") +
  40. ((settings.imageUpload) ? "<iframe name=\"" + iframeName + "\" id=\"" + iframeName + "\" guid=\"" + guid + "\"></iframe>" : "") +
  41. "<label>" + imageLang.url + "</label>" +
  42. "<input type=\"text\" data-url />" + (function() {
  43. return (settings.imageUpload) ? "<div class=\"" + classPrefix + "file-input\">" +
  44. // 3xxx �������multiple=\"multiple\"
  45. "<input type=\"file\" name=\"" + classPrefix + "image-file\" accept=\"image/jpeg,image/png,image/gif,image/jpg\" multiple=\"multiple\" />" +
  46. "<input type=\"submit\" value=\"" + imageLang.uploadButton + "\" />" +
  47. "</div>" : "";
  48. })() +
  49. "<br/>" +
  50. "<label>" + imageLang.alt + "</label>" +
  51. "<input type=\"text\" value=\"" + selection + "\" data-alt />" +
  52. "<br/>" +
  53. "<label>" + imageLang.link + "</label>" +
  54. "<input type=\"text\" value=\"http://\" data-link />" +
  55. "<br/>" +
  56. ((settings.imageUpload) ? "</form>" : "</div>");
  57. //var imageFooterHTML = "<button class=\"" + classPrefix + "btn " + classPrefix + "image-manager-btn\" style=\"float:left;\">" + imageLang.managerButton + "</button>";
  58. dialog = this.createDialog({
  59. title: imageLang.title,
  60. width: (settings.imageUpload) ? 465 : 380,
  61. height: 254,
  62. name: dialogName,
  63. content: dialogContent,
  64. mask: settings.dialogShowMask,
  65. drag: settings.dialogDraggable,
  66. lockScreen: settings.dialogLockScreen,
  67. maskStyle: {
  68. opacity: settings.dialogMaskOpacity,
  69. backgroundColor: settings.dialogMaskBgColor
  70. },
  71. // ���ォ��ͼƬ��ַ���������ĵ���
  72. buttons: {
  73. enter: [lang.buttons.enter, function() {
  74. var url = this.find("[data-url]").val();
  75. var alt = this.find("[data-alt]").val();
  76. var link = this.find("[data-link]").val();
  77. if (url === "") {
  78. alert(imageLang.imageURLEmpty);
  79. return false;
  80. }
  81. // ��������ѭ��
  82. let arr = url.split(";");
  83. var altAttr = (alt !== "") ? " \"" + alt + "\"" : "";
  84. for (let i = 0; i < arr.length; i++) {
  85. if (link === "" || link === "http://") {
  86. // cm.replaceSelection("![" + alt + "](" + url + altAttr + ")");
  87. cm.replaceSelection("![" + alt + "](" + arr[i] + altAttr + ")");
  88. } else {
  89. // cm.replaceSelection("[![" + alt + "](" + url + altAttr + ")](" + link + altAttr + ")");
  90. cm.replaceSelection("[![" + alt + "](" + arr[i] + altAttr + ")](" + link + altAttr + ")");
  91. }
  92. }
  93. if (alt === "") {
  94. cm.setCursor(cursor.line, cursor.ch + 2);
  95. }
  96. this.hide().lockScreen(false).hideMask();
  97. return false;
  98. }],
  99. cancel: [lang.buttons.cancel, function() {
  100. this.hide().lockScreen(false).hideMask();
  101. return false;
  102. }]
  103. }
  104. });
  105. dialog.attr("id", classPrefix + "image-dialog-" + guid);
  106. if (!settings.imageUpload) {
  107. return;
  108. }
  109. var fileInput = dialog.find("[name=\"" + classPrefix + "image-file\"]");
  110. fileInput.bind("change", function() {
  111. // 3xxx 20240602
  112. // let formData = new FormData();
  113. // ��ȡ�ı���dom
  114. // var doc = document.getElementById('doc');
  115. // ��ȡ�ϴ��ؼ�dom
  116. // var upload = document.getElementById('upload');
  117. // let files = upload.files;
  118. //�����ļ���Ϣappend��formData�洢
  119. // for (let i = 0; i < files.length; i++) {
  120. // let file = files[i]
  121. // formData.append('files', file)
  122. // }
  123. // ��ȡ�ļ���
  124. // var fileName = upload.files[0].name;
  125. // ��ȡ�ļ�·��
  126. // var filePath = upload.value;
  127. // doc.value = fileName;
  128. // 3xxx
  129. console.log(fileInput);
  130. console.log(fileInput[0].files);
  131. let files = fileInput[0].files;
  132. for (let i = 0; i < files.length; i++) {
  133. var fileName = files[i].name;
  134. // var fileName = fileInput.val();
  135. var isImage = new RegExp("(\\.(" + settings.imageFormats.join("|") + "))$"); // /(\.(webp|jpg|jpeg|gif|bmp|png))$/
  136. if (fileName === "") {
  137. alert(imageLang.uploadFileEmpty);
  138. return false;
  139. }
  140. if (!isImage.test(fileName)) {
  141. alert(imageLang.formatNotAllowed + settings.imageFormats.join(", "));
  142. return false;
  143. }
  144. loading(true);
  145. var submitHandler = function() {
  146. var uploadIframe = document.getElementById(iframeName);
  147. uploadIframe.onload = function() {
  148. loading(false);
  149. var body = (uploadIframe.contentWindow ? uploadIframe.contentWindow : uploadIframe.contentDocument).document.body;
  150. var json = (body.innerText) ? body.innerText : ((body.textContent) ? body.textContent : null);
  151. json = (typeof JSON.parse !== "undefined") ? JSON.parse(json) : eval("(" + json + ")");
  152. var url="";
  153. if (json.success === 1) {
  154. url=json.url;
  155. } else {
  156. alert(json.message);
  157. }
  158. dialog.find("[data-url]").val(url)
  159. return false;
  160. };
  161. };
  162. dialog.find("[type=\"submit\"]").bind("click", submitHandler).trigger("click");
  163. }
  164. });
  165. }
  166. dialog = editor.find("." + dialogName);
  167. dialog.find("[type=\"text\"]").val("");
  168. dialog.find("[type=\"file\"]").val("");
  169. dialog.find("[data-link]").val("http://");
  170. this.dialogShowMask(dialog);
  171. this.dialogLockScreen();
  172. dialog.show();
  173. };
  174. };
  175. // CommonJS/Node.js
  176. if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
  177. {
  178. module.exports = factory;
  179. }
  180. else if (typeof define === "function") // AMD/CMD/Sea.js
  181. {
  182. if (define.amd) { // for Require.js
  183. define(["editormd"], function(editormd) {
  184. factory(editormd);
  185. });
  186. } else { // for Sea.js
  187. define(function(require) {
  188. var editormd = require("./../../editormd");
  189. factory(editormd);
  190. });
  191. }
  192. }
  193. else
  194. {
  195. factory(window.editormd);
  196. }
  197. })();