image-dialog.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. "<input type=\"file\" name=\"" + classPrefix + "image-file\" accept=\"image/*\" />" +
  45. "<input type=\"submit\" value=\"" + imageLang.uploadButton + "\" />" +
  46. "</div>" : "";
  47. })() +
  48. "<br/>" +
  49. "<label>" + imageLang.alt + "</label>" +
  50. "<input type=\"text\" value=\"" + selection + "\" data-alt />" +
  51. "<br/>" +
  52. "<label>" + imageLang.link + "</label>" +
  53. "<input type=\"text\" value=\"http://\" data-link />" +
  54. "<br/>" +
  55. ( (settings.imageUpload) ? "</form>" : "</div>");
  56. //var imageFooterHTML = "<button class=\"" + classPrefix + "btn " + classPrefix + "image-manager-btn\" style=\"float:left;\">" + imageLang.managerButton + "</button>";
  57. dialog = this.createDialog({
  58. title : imageLang.title,
  59. width : (settings.imageUpload) ? 465 : 380,
  60. height : 254,
  61. name : dialogName,
  62. content : dialogContent,
  63. mask : settings.dialogShowMask,
  64. drag : settings.dialogDraggable,
  65. lockScreen : settings.dialogLockScreen,
  66. maskStyle : {
  67. opacity : settings.dialogMaskOpacity,
  68. backgroundColor : settings.dialogMaskBgColor
  69. },
  70. buttons : {
  71. enter : [lang.buttons.enter, function() {
  72. var url = this.find("[data-url]").val();
  73. var alt = this.find("[data-alt]").val();
  74. var link = this.find("[data-link]").val();
  75. if (url === "")
  76. {
  77. alert(imageLang.imageURLEmpty);
  78. return false;
  79. }
  80. var altAttr = (alt !== "") ? " \"" + alt + "\"" : "";
  81. if (link === "" || link === "http://")
  82. {
  83. cm.replaceSelection("![" + alt + "](" + url + altAttr + ")");
  84. }
  85. else
  86. {
  87. cm.replaceSelection("[![" + alt + "](" + url + altAttr + ")](" + link + altAttr + ")");
  88. }
  89. if (alt === "") {
  90. cm.setCursor(cursor.line, cursor.ch + 2);
  91. }
  92. this.hide().lockScreen(false).hideMask();
  93. //删除对话框
  94. this.remove();
  95. return false;
  96. }],
  97. cancel : [lang.buttons.cancel, function() {
  98. this.hide().lockScreen(false).hideMask();
  99. //删除对话框
  100. this.remove();
  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. var fileName = fileInput.val();
  112. var isImage = new RegExp("(\\.(" + settings.imageFormats.join("|") + "))$", "i"); // /(\.(webp|jpg|jpeg|gif|bmp|png))$/
  113. if (fileName === "")
  114. {
  115. alert(imageLang.uploadFileEmpty);
  116. return false;
  117. }
  118. if (!isImage.test(fileName))
  119. {
  120. alert(imageLang.formatNotAllowed + settings.imageFormats.join(", "));
  121. return false;
  122. }
  123. loading(true);
  124. var submitHandler = function() {
  125. var uploadIframe = document.getElementById(iframeName);
  126. uploadIframe.onload = function() {
  127. loading(false);
  128. var body = (uploadIframe.contentWindow ? uploadIframe.contentWindow : uploadIframe.contentDocument).document.body;
  129. var json = (body.innerText) ? body.innerText : ( (body.textContent) ? body.textContent : null);
  130. json = (typeof JSON.parse !== "undefined") ? JSON.parse(json) : eval("(" + json + ")");
  131. if(!settings.crossDomainUpload)
  132. {
  133. if (json.success === 1)
  134. {
  135. dialog.find("[data-url]").val(json.url);
  136. }
  137. else
  138. {
  139. alert(json.message);
  140. }
  141. }
  142. return false;
  143. };
  144. };
  145. dialog.find("[type=\"submit\"]").bind("click", submitHandler).trigger("click");
  146. });
  147. }
  148. dialog = editor.find("." + dialogName);
  149. dialog.find("[type=\"text\"]").val("");
  150. dialog.find("[type=\"file\"]").val("");
  151. dialog.find("[data-link]").val("http://");
  152. this.dialogShowMask(dialog);
  153. this.dialogLockScreen();
  154. dialog.show();
  155. };
  156. };
  157. // CommonJS/Node.js
  158. if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
  159. {
  160. module.exports = factory;
  161. }
  162. else if (typeof define === "function") // AMD/CMD/Sea.js
  163. {
  164. if (define.amd) { // for Require.js
  165. define(["editormd"], function(editormd) {
  166. factory(editormd);
  167. });
  168. } else { // for Sea.js
  169. define(function(require) {
  170. var editormd = require("./../../editormd");
  171. factory(editormd);
  172. });
  173. }
  174. }
  175. else
  176. {
  177. factory(window.editormd);
  178. }
  179. })();