bootstrap.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. require.config({
  2. paths: {
  3. 'nkeditor': '../addons/nkeditor/js/customplugin',
  4. 'nkeditor-core': '../addons/nkeditor/nkeditor.min',
  5. 'nkeditor-lang': '../addons/nkeditor/lang/zh-CN',
  6. },
  7. shim: {
  8. 'nkeditor': {
  9. deps: [
  10. 'nkeditor-core',
  11. 'nkeditor-lang'
  12. ]
  13. },
  14. 'nkeditor-core': {
  15. deps: [
  16. 'css!../addons/nkeditor/themes/black/editor.min.css',
  17. 'css!../addons/nkeditor/css/common.css'
  18. ],
  19. exports: 'window.KindEditor'
  20. },
  21. 'nkeditor-lang': {
  22. deps: [
  23. 'nkeditor-core'
  24. ]
  25. }
  26. }
  27. });
  28. require(['form'], function (Form) {
  29. var _bindevent = Form.events.bindevent;
  30. Form.events.bindevent = function (form) {
  31. _bindevent.apply(this, [form]);
  32. if ($(".editor", form).size() > 0) {
  33. require(['nkeditor', 'upload'], function (Nkeditor, Upload) {
  34. var getImageFromClipboard, getImageFromDrop, getFileFromBase64;
  35. getImageFromClipboard = function (data) {
  36. var i, item;
  37. i = 0;
  38. while (i < data.clipboardData.items.length) {
  39. item = data.clipboardData.items[i];
  40. if (item.type.indexOf("image") !== -1) {
  41. return item.getAsFile() || false;
  42. }
  43. i++;
  44. }
  45. return false;
  46. };
  47. getImageFromDrop = function (data) {
  48. var i, item, images;
  49. i = 0;
  50. images = [];
  51. while (i < data.dataTransfer.files.length) {
  52. item = data.dataTransfer.files[i];
  53. if (item.type.indexOf("image") !== -1) {
  54. images.push(item);
  55. }
  56. i++;
  57. }
  58. return images;
  59. };
  60. getFileFromBase64 = function (data, url) {
  61. var arr = data.split(','), mime = arr[0].match(/:(.*?);/)[1],
  62. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  63. while (n--) {
  64. u8arr[n] = bstr.charCodeAt(n);
  65. }
  66. var filename, suffix;
  67. if (typeof url != 'undefined') {
  68. var urlArr = url.split('.');
  69. filename = url.substr(url.lastIndexOf('/') + 1);
  70. suffix = urlArr.pop();
  71. } else {
  72. filename = Math.random().toString(36).substring(5, 15);
  73. }
  74. if (!suffix) {
  75. suffix = data.substring("data:image/".length, data.indexOf(";base64"));
  76. }
  77. var exp = new RegExp("\\." + suffix + "$", "i");
  78. filename = exp.test(filename) ? filename : filename + "." + suffix;
  79. var file = new File([u8arr], filename, {type: mime});
  80. return file;
  81. };
  82. var getImageFromUrl = function (url, callback, outputFormat) {
  83. var canvas = document.createElement('CANVAS'),
  84. ctx = canvas.getContext('2d'),
  85. img = new Image;
  86. img.crossOrigin = 'Anonymous';
  87. img.onload = function () {
  88. var urlArr = url.split('.');
  89. var suffix = urlArr.pop();
  90. suffix = suffix.match(/^(jpg|png|gif|bmp|jpeg)$/i) ? suffix : 'png';
  91. try {
  92. canvas.height = img.height;
  93. canvas.width = img.width;
  94. ctx.drawImage(img, 0, 0);
  95. var dataURL = canvas.toDataURL(outputFormat || 'image/' + suffix);
  96. var file = getFileFromBase64(dataURL, url);
  97. } catch (e) {
  98. callback.call(this, null);
  99. }
  100. callback.call(this, file);
  101. canvas = null;
  102. };
  103. img.onerror = function (e) {
  104. callback.call(this, null);
  105. };
  106. img.src = Fast.api.fixurl("/addons/nkeditor/index/download") + "?url=" + encodeURIComponent(url);
  107. };
  108. //上传Word图片
  109. Nkeditor.uploadwordimage = function (index, image) {
  110. var that = this;
  111. (function () {
  112. var file = getFileFromBase64(image);
  113. var placeholder = new RegExp("##" + index + "##", "g");
  114. Upload.api.send(file, function (data) {
  115. that.html(that.html().replace(placeholder, Fast.api.cdnurl(data.url)));
  116. }, function (data) {
  117. that.html(that.html().replace(placeholder, ""));
  118. });
  119. }(index, image));
  120. };
  121. Nkeditor.lang({
  122. remoteimage: '下载远程图片'
  123. });
  124. //远程下载图片
  125. Nkeditor.plugin('remoteimage', function (K) {
  126. var editor = this, name = 'remoteimage';
  127. editor.plugin.remoteimage = {
  128. download: function (e) {
  129. var that = this;
  130. var html = that.html();
  131. var staging = {}, orgined = {}, index = 0, images = 0, completed = 0, failured = 0;
  132. var checkrestore = function () {
  133. if (completed + failured >= images) {
  134. $.each(staging, function (i, j) {
  135. that.html(that.html().replace("<code>" + i + "</code>", j));
  136. });
  137. }
  138. };
  139. html.replace(/<code>([\s\S]*?)<\/code>/g, function (code) {
  140. staging[index] = code;
  141. return "<code>" + index + "</code>";
  142. }
  143. );
  144. html = html.replace(/<img([\s\S]*?)\ssrc\s*=\s*('|")((http(s?):)([\s\S]*?))('|")([\s\S]*?)[\/]?>/g, function () {
  145. images++;
  146. var url = arguments[3];
  147. var placeholder = '<img src="' + Fast.api.cdnurl("/assets/addons/nkeditor/img/downloading.png") + '" data-index="' + index + '" />';
  148. //如果是云存储的链接,则忽略
  149. if (Config.upload.cdnurl && url.indexOf(Config.upload.cdnurl) > -1) {
  150. completed++;
  151. return arguments[0];
  152. } else {
  153. orgined[index] = arguments[0];
  154. }
  155. //下载远程图片
  156. (function (index, url, placeholder) {
  157. getImageFromUrl(url, function (file) {
  158. if (!file) {
  159. failured++;
  160. that.html(that.html().replace(placeholder, orgined[index]));
  161. checkrestore();
  162. } else {
  163. Upload.api.send(file, function (data) {
  164. completed++;
  165. that.html(that.html().replace(placeholder, '<img src="' + Fast.api.cdnurl(data.url) + '" />'));
  166. checkrestore();
  167. }, function (data) {
  168. failured++;
  169. that.html(that.html().replace(placeholder, orgined[index]));
  170. checkrestore();
  171. });
  172. }
  173. });
  174. })(index, url, placeholder);
  175. index++;
  176. return placeholder;
  177. });
  178. if (index > 0) {
  179. that.html(html);
  180. } else {
  181. Toastr.info("没有需要下载的远程图片");
  182. }
  183. }
  184. };
  185. // 点击图标时执行
  186. editor.clickToolbar(name, editor.plugin.remoteimage.download);
  187. });
  188. $(".editor", form).each(function () {
  189. var that = this;
  190. Nkeditor.create(that, {
  191. width: '100%',
  192. filterMode: false,
  193. wellFormatMode: false,
  194. allowMediaUpload: true, //是否允许媒体上传
  195. allowFileManager: true,
  196. allowImageUpload: true,
  197. fontSizeTable: ['9px', '10px', '12px', '14px', '16px', '18px', '21px', '24px', '32px'],
  198. wordImageServer: typeof Config.nkeditor != 'undefined' && Config.nkeditor.wordimageserver ? "127.0.0.1:10101" : "", //word图片替换服务器的IP和端口
  199. cssPath: Fast.api.cdnurl('/assets/addons/nkeditor/plugins/code/prism.css'),
  200. cssData: "body {font-size: 13px}",
  201. fillDescAfterUploadImage: false, //是否在上传后继续添加描述信息
  202. themeType: typeof Config.nkeditor != 'undefined' ? Config.nkeditor.theme : 'black', //编辑器皮肤,这个值从后台获取
  203. fileManagerJson: Fast.api.fixurl("/addons/nkeditor/index/attachment/module/" + Config.modulename),
  204. items: [
  205. 'source', 'undo', 'redo', 'preview', 'print', 'template', 'code', 'quote', 'cut', 'copy', 'paste',
  206. 'plainpaste', 'wordpaste', 'justifyleft', 'justifycenter', 'justifyright',
  207. 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
  208. 'superscript', 'clearhtml', 'quickformat', 'selectall',
  209. 'formatblock', 'fontname', 'fontsize', 'forecolor', 'hilitecolor', 'bold',
  210. 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', 'image', 'multiimage', 'graft',
  211. 'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
  212. 'anchor', 'link', 'unlink', 'remoteimage', 'about', 'fullscreen'
  213. ],
  214. afterCreate: function () {
  215. var self = this;
  216. //Ctrl+回车提交
  217. Nkeditor.ctrl(document, 13, function () {
  218. self.sync();
  219. $(that).closest("form").submit();
  220. });
  221. Nkeditor.ctrl(self.edit.doc, 13, function () {
  222. self.sync();
  223. $(that).closest("form").submit();
  224. });
  225. //粘贴上传
  226. $("body", self.edit.doc).bind('paste', function (event) {
  227. var image, pasteEvent;
  228. pasteEvent = event.originalEvent;
  229. if (pasteEvent.clipboardData && pasteEvent.clipboardData.items) {
  230. image = getImageFromClipboard(pasteEvent);
  231. if (image) {
  232. event.preventDefault();
  233. Upload.api.send(image, function (data) {
  234. self.exec("insertimage", Fast.api.cdnurl(data.url));
  235. });
  236. }
  237. }
  238. });
  239. //挺拽上传
  240. $("body", self.edit.doc).bind('drop', function (event) {
  241. var image, pasteEvent;
  242. pasteEvent = event.originalEvent;
  243. if (pasteEvent.dataTransfer && pasteEvent.dataTransfer.files) {
  244. images = getImageFromDrop(pasteEvent);
  245. if (images.length > 0) {
  246. event.preventDefault();
  247. $.each(images, function (i, image) {
  248. Upload.api.send(image, function (data) {
  249. self.exec("insertimage", Fast.api.cdnurl(data.url));
  250. });
  251. });
  252. }
  253. }
  254. });
  255. },
  256. //FastAdmin自定义处理
  257. beforeUpload: function (callback, file) {
  258. var file = file ? file : $("input.ke-upload-file", this.form).prop('files')[0];
  259. Upload.api.send(file, function (data) {
  260. var data = {code: '000', data: {url: Fast.api.cdnurl(data.url)}, title: '', width: '', height: '', border: '', align: ''};
  261. callback(data);
  262. });
  263. },
  264. //错误处理 handler
  265. errorMsgHandler: function (message, type) {
  266. try {
  267. console.log(message, type);
  268. } catch (Error) {
  269. alert(message);
  270. }
  271. }
  272. });
  273. });
  274. });
  275. }
  276. }
  277. });