bootstrap.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. window.UMEDITOR_HOME_URL = Config.__CDN__ + "/assets/addons/umeditor/";
  2. require.config({
  3. paths: {
  4. 'umeditor': '../addons/umeditor/umeditor.min',
  5. 'umeditor.config': '../addons/umeditor/umeditor.config',
  6. 'umeditor.lang': '../addons/umeditor/lang/zh-cn/zh-cn',
  7. },
  8. shim: {
  9. 'umeditor': {
  10. deps: [
  11. 'umeditor.config',
  12. 'css!../addons/umeditor/themes/default/css/umeditor.min.css'
  13. ],
  14. exports: 'UM',
  15. },
  16. 'umeditor.lang': ['umeditor']
  17. }
  18. });
  19. require(['form', 'upload'], function (Form, Upload) {
  20. var getFileFromBase64, uploadFiles;
  21. uploadFiles = async function (files, callback) {
  22. var self = this;
  23. for (var i = 0; i < files.length; i++) {
  24. try {
  25. await new Promise(function (resolve) {
  26. var url, html, file;
  27. file = files[i];
  28. Upload.api.send(file, function (data) {
  29. url = Fast.api.cdnurl(data.url, true);
  30. if (typeof callback === 'function') {
  31. callback.call(this, url, data)
  32. } else {
  33. if (file.type.indexOf("image") !== -1) {
  34. self.execCommand('insertImage', {
  35. src: url,
  36. title: file.name || "",
  37. });
  38. } else {
  39. self.execCommand('link', {
  40. href: url,
  41. title: file.name || "",
  42. target: '_blank'
  43. });
  44. }
  45. }
  46. resolve();
  47. }, function () {
  48. resolve();
  49. });
  50. });
  51. } catch (e) {
  52. }
  53. }
  54. };
  55. getFileFromBase64 = function (data, url) {
  56. var arr = data.split(','), mime = arr[0].match(/:(.*?);/)[1],
  57. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  58. while (n--) {
  59. u8arr[n] = bstr.charCodeAt(n);
  60. }
  61. var filename, suffix;
  62. if (typeof url != 'undefined') {
  63. var urlArr = url.split('.');
  64. filename = url.substr(url.lastIndexOf('/') + 1);
  65. suffix = urlArr.pop();
  66. } else {
  67. filename = Math.random().toString(36).substring(5, 15);
  68. }
  69. if (!suffix) {
  70. suffix = data.substring("data:image/".length, data.indexOf(";base64"));
  71. }
  72. var exp = new RegExp("\\." + suffix + "$", "i");
  73. filename = exp.test(filename) ? filename : filename + "." + suffix;
  74. var file = new File([u8arr], filename, {type: mime});
  75. return file;
  76. };
  77. //监听上传文本框的事件
  78. $(document).on("edui.file.change", ".edui-image-file", function (e, up, me, input, callback) {
  79. uploadFiles.call(me.editor, this.files, function (url, data) {
  80. me.uploadComplete(JSON.stringify({url: url, state: "SUCCESS"}));
  81. });
  82. up.updateInput(input);
  83. me.toggleMask("上传中....");
  84. callback && callback();
  85. });
  86. var _bindevent = Form.events.bindevent;
  87. Form.events.bindevent = function (form) {
  88. _bindevent.apply(this, [form]);
  89. require(['umeditor', 'umeditor.lang'], function (UME, undefined) {
  90. //重写编辑器加载
  91. UME.plugins['autoupload'] = function () {
  92. var that = this;
  93. that.addListener('ready', function () {
  94. if (window.FormData && window.FileReader) {
  95. that.getOpt('pasteImageEnabled') && that.$body.on('paste', function (event) {
  96. var originalEvent;
  97. originalEvent = event.originalEvent;
  98. if (originalEvent.clipboardData && originalEvent.clipboardData.files.length > 0) {
  99. uploadFiles.call(that, originalEvent.clipboardData.files);
  100. return false;
  101. }
  102. });
  103. that.getOpt('dropFileEnabled') && that.$body.on('drop', function (event) {
  104. var originalEvent;
  105. originalEvent = event.originalEvent;
  106. if (originalEvent.dataTransfer && originalEvent.dataTransfer.files.length > 0) {
  107. uploadFiles.call(that, originalEvent.dataTransfer.files);
  108. return false;
  109. }
  110. });
  111. //取消拖放图片时出现的文字光标位置提示
  112. that.$body.on('dragover', function (e) {
  113. if (e.originalEvent.dataTransfer.types[0] == 'Files') {
  114. return false;
  115. }
  116. });
  117. }
  118. });
  119. };
  120. $.extend(window.UMEDITOR_CONFIG.whiteList, {
  121. div: ['style', 'class', 'id', 'data-tpl', 'data-source', 'data-id'],
  122. span: ['style', 'class', 'id', 'data-id']
  123. });
  124. $(Config.umeditor.classname || '.editor', form).each(function () {
  125. var id = $(this).attr("id");
  126. $(this).removeClass('form-control');
  127. var options = $(this).data("umeditor-options");
  128. UME.list[id] = UME.getEditor(id, $.extend(true, {}, {
  129. initialFrameWidth: '100%',
  130. zIndex: 90,
  131. autoHeightEnabled: true,
  132. initialFrameHeight: 300,
  133. xssFilterRules: false,
  134. outputXssFilter: false,
  135. inputXssFilter: false,
  136. autoFloatEnabled: false,
  137. pasteImageEnabled: true,
  138. dropFileEnabled: true,
  139. baiduMapKey: Config.umeditor.baidumapkey || '',
  140. baiduMapCenter: Config.umeditor.baidumapcenter || '',
  141. imageUrl: '',
  142. imagePath: '',
  143. imageUploadCallback: function (file, fn) {
  144. var me = this;
  145. Upload.api.send(file, function (data) {
  146. var url = data.url;
  147. fn && fn.call(me, url, data);
  148. });
  149. }
  150. }, options || {}));
  151. });
  152. });
  153. }
  154. });