image-dialog.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. return false;
  94. }],
  95. cancel : [lang.buttons.cancel, function() {
  96. this.hide().lockScreen(false).hideMask();
  97. return false;
  98. }]
  99. }
  100. });
  101. dialog.attr("id", classPrefix + "image-dialog-" + guid);
  102. if (!settings.imageUpload) {
  103. return ;
  104. }
  105. var fileInput = dialog.find("[name=\"" + classPrefix + "image-file\"]");
  106. fileInput.bind("change", function() {
  107. var fileName = fileInput.val();
  108. var isImage = new RegExp("(\\.(" + settings.imageFormats.join("|") + "))$"); // /(\.(webp|jpg|jpeg|gif|bmp|png))$/
  109. if (fileName === "")
  110. {
  111. alert(imageLang.uploadFileEmpty);
  112. return false;
  113. }
  114. if (!isImage.test(fileName))
  115. {
  116. alert(imageLang.formatNotAllowed + settings.imageFormats.join(", "));
  117. return false;
  118. }
  119. loading(true);
  120. var submitHandler = function() {
  121. var uploadIframe = document.getElementById(iframeName);
  122. uploadIframe.onload = function() {
  123. loading(false);
  124. var body = (uploadIframe.contentWindow ? uploadIframe.contentWindow : uploadIframe.contentDocument).document.body;
  125. var json = (body.innerText) ? body.innerText : ( (body.textContent) ? body.textContent : null);
  126. json = (typeof JSON.parse !== "undefined") ? JSON.parse(json) : eval("(" + json + ")");
  127. if (json.success === 1)
  128. {
  129. dialog.find("[data-url]").val(json.url);
  130. }
  131. else
  132. {
  133. alert(json.message);
  134. }
  135. return false;
  136. };
  137. };
  138. dialog.find("[type=\"submit\"]").bind("click", submitHandler).trigger("click");
  139. });
  140. }
  141. dialog = editor.find("." + dialogName);
  142. dialog.find("[type=\"text\"]").val("");
  143. dialog.find("[type=\"file\"]").val("");
  144. dialog.find("[data-link]").val("http://");
  145. this.dialogShowMask(dialog);
  146. this.dialogLockScreen();
  147. dialog.show();
  148. };
  149. };
  150. // CommonJS/Node.js
  151. if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
  152. {
  153. module.exports = factory;
  154. }
  155. else if (typeof define === "function") // AMD/CMD/Sea.js
  156. {
  157. if (define.amd) { // for Require.js
  158. define(["editormd"], function(editormd) {
  159. factory(editormd);
  160. });
  161. } else { // for Sea.js
  162. define(function(require) {
  163. var editormd = require("./../../editormd");
  164. factory(editormd);
  165. });
  166. }
  167. }
  168. else
  169. {
  170. factory(window.editormd);
  171. }
  172. })();