image-dialog.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. if (exports)
  15. exports.fn.imageDialog = function() {
  16. var _this = this;
  17. var cm = this.cm;
  18. var lang = this.lang;
  19. var editor = this.editor;
  20. var settings = this.settings;
  21. var cursor = cm.getCursor();
  22. var selection = cm.getSelection();
  23. var imageLang = lang.dialog.image;
  24. var classPrefix = this.classPrefix;
  25. var iframeName = classPrefix + "image-iframe";
  26. var dialogName = classPrefix + pluginName,
  27. dialog;
  28. cm.focus();
  29. var loading = function(show) {
  30. var _loading = dialog.find("." + classPrefix + "dialog-mask");
  31. _loading[(show) ? "show" : "hide"]();
  32. };
  33. if (editor.find("." + dialogName).length < 1) {
  34. var guid = (new Date).getTime();
  35. var action = settings.imageUploadURL + (settings.imageUploadURL.indexOf("?") >= 0 ? "&" : "?") + "guid=" + guid;
  36. if (settings.crossDomainUpload) {
  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. alert(imageLang.imageURLEmpty);
  77. return false;
  78. }
  79. var altAttr = (alt !== "") ? " \"" + alt + "\"" : "";
  80. if (link === "" || link === "http://") {
  81. cm.replaceSelection("![" + alt + "](" + url + altAttr + ")");
  82. } else {
  83. cm.replaceSelection("[![" + alt + "](" + url + altAttr + ")](" + link + altAttr + ")");
  84. }
  85. if (alt === "") {
  86. cm.setCursor(cursor.line, cursor.ch + 2);
  87. }
  88. this.hide().lockScreen(false).hideMask();
  89. return false;
  90. }],
  91. cancel: [lang.buttons.cancel, function() {
  92. this.hide().lockScreen(false).hideMask();
  93. return false;
  94. }]
  95. }
  96. });
  97. dialog.attr("id", classPrefix + "image-dialog-" + guid);
  98. if (!settings.imageUpload) {
  99. return;
  100. }
  101. var fileInput = dialog.find("[name=\"" + classPrefix + "image-file\"]");
  102. fileInput.bind("change", function() {
  103. var fileName = fileInput.val();
  104. var isImage = new RegExp("(\\.(" + settings.imageFormats.join("|") + "))$"); // /(\.(webp|jpg|jpeg|gif|bmp|png))$/
  105. if (fileName === "") {
  106. alert(imageLang.uploadFileEmpty);
  107. return false;
  108. }
  109. if (!isImage.test(fileName)) {
  110. alert(imageLang.formatNotAllowed + settings.imageFormats.join(", "));
  111. return false;
  112. }
  113. loading(true);
  114. var Qiniu_upload = function(files, length, i) {
  115. if (length > i) {
  116. $.ajax({
  117. type: 'POST',
  118. url: '/common/Upload/getPublicUploadToken',
  119. dataType: 'json',
  120. contentType: false,
  121. processData: false
  122. }).then(function(data) {
  123. var formdata = new FormData()
  124. formdata.append('file', files[i])
  125. formdata.append('key', data.fileName)
  126. formdata.append('token', data.uptoken)
  127. $.ajax({
  128. type: 'POST',
  129. url: window.location.protocol === 'https:'?'https://up.qbox.me/':'http://up.qiniu.com/',
  130. data: formdata,
  131. dataType: 'json',
  132. contentType: false,
  133. processData: false
  134. }).then(function(json) {
  135. var oldurl = $('[data-url]').val()
  136. if (oldurl === '') {
  137. $('[data-url]').val('http://data.eolinker.com/' + json.key)
  138. } else {
  139. oldurl = oldurl + '$$http://data.eolinker.com/' + json.key
  140. $('[data-url]').val(oldurl)
  141. }
  142. i++
  143. Qiniu_upload(files, length, i)
  144. }, function(err) {
  145. console.log(err)
  146. })
  147. }, function(err) {
  148. console.log(err)
  149. })
  150. } else {
  151. $('[name="editormd-image-file"]').val('')
  152. loading(false)
  153. }
  154. }
  155. var submitHandler = function() {
  156. var files = $('[name="editormd-image-file"]')[0].files
  157. if (files.length > 0) {
  158. Qiniu_upload(files, files.length, 0)
  159. }
  160. return false
  161. }
  162. dialog.find("[type=\"submit\"]").bind("click", submitHandler).trigger("click");
  163. });
  164. }
  165. dialog = editor.find("." + dialogName);
  166. dialog.find("[type=\"text\"]").val("");
  167. dialog.find("[type=\"file\"]").val("");
  168. dialog.find("[data-link]").val("http://");
  169. this.dialogShowMask(dialog);
  170. this.dialogLockScreen();
  171. dialog.show();
  172. };
  173. };
  174. // CommonJS/Node.js
  175. if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
  176. module.exports = factory;
  177. } else if (typeof define === "function") // AMD/CMD/Sea.js
  178. {
  179. if (define.amd) { // for Require.js
  180. define(["editormd"], function(editormd) {
  181. factory(editormd);
  182. });
  183. } else { // for Sea.js
  184. define(function(require) {
  185. var editormd = require("./../../editormd");
  186. factory(editormd);
  187. });
  188. }
  189. } else {
  190. factory(window.editormd);
  191. }
  192. })();