bootstrap.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. //监听上传文本框的事件
  21. $(document).on("edui.file.change", ".edui-image-file", function (e, up, me, input, callback) {
  22. for (var i = 0; i < this.files.length; i++) {
  23. Upload.api.send(this.files[i], function (data) {
  24. var url = data.url;
  25. me.uploadComplete(JSON.stringify({url: url, state: "SUCCESS"}));
  26. });
  27. }
  28. up.updateInput(input);
  29. me.toggleMask("Loading....");
  30. callback && callback();
  31. });
  32. var _bindevent = Form.events.bindevent;
  33. Form.events.bindevent = function (form) {
  34. _bindevent.apply(this, [form]);
  35. require(['umeditor', 'umeditor.lang'], function (UME, undefined) {
  36. //重写编辑器加载
  37. UME.plugins['autoupload'] = function () {
  38. var me = this;
  39. me.setOpt('pasteImageEnabled', true);
  40. me.setOpt('dropFileEnabled', true);
  41. var sendAndInsertImage = function (file, editor) {
  42. try {
  43. Upload.api.send(file, function (data) {
  44. var url = Fast.api.cdnurl(data.url, true);
  45. editor.execCommand('insertimage', {
  46. src: url,
  47. _src: url
  48. });
  49. });
  50. } catch (er) {
  51. }
  52. };
  53. function getPasteImage(e) {
  54. return e.clipboardData && e.clipboardData.items && e.clipboardData.items.length == 1 && /^image\//.test(e.clipboardData.items[0].type) ? e.clipboardData.items : null;
  55. }
  56. function getDropImage(e) {
  57. return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files : null;
  58. }
  59. me.addListener('ready', function () {
  60. if (window.FormData && window.FileReader) {
  61. var autoUploadHandler = function (e) {
  62. var hasImg = false,
  63. items;
  64. //获取粘贴板文件列表或者拖放文件列表
  65. items = e.type == 'paste' ? getPasteImage(e.originalEvent) : getDropImage(e.originalEvent);
  66. if (items) {
  67. var len = items.length,
  68. file;
  69. while (len--) {
  70. file = items[len];
  71. if (file.getAsFile)
  72. file = file.getAsFile();
  73. if (file && file.size > 0 && /image\/\w+/i.test(file.type)) {
  74. sendAndInsertImage(file, me);
  75. hasImg = true;
  76. }
  77. }
  78. if (hasImg)
  79. return false;
  80. }
  81. };
  82. me.getOpt('pasteImageEnabled') && me.$body.on('paste', autoUploadHandler);
  83. me.getOpt('dropFileEnabled') && me.$body.on('drop', autoUploadHandler);
  84. //取消拖放图片时出现的文字光标位置提示
  85. me.$body.on('dragover', function (e) {
  86. if (e.originalEvent.dataTransfer.types[0] == 'Files') {
  87. return false;
  88. }
  89. });
  90. }
  91. });
  92. };
  93. $.extend(window.UMEDITOR_CONFIG.whiteList, {
  94. div: ['style', 'class', 'id', 'data-tpl', 'data-source', 'data-id'],
  95. span: ['style', 'class', 'id', 'data-id']
  96. });
  97. $(Config.umeditor.classname || '.editor', form).each(function () {
  98. var id = $(this).attr("id");
  99. $(this).removeClass('form-control');
  100. var options = $(this).data("umeditor-options");
  101. UME.list[id] = UME.getEditor(id, $.extend(true, {}, {
  102. initialFrameWidth: '100%',
  103. zIndex: 90,
  104. autoHeightEnabled: true,
  105. initialFrameHeight: 300,
  106. xssFilterRules: false,
  107. outputXssFilter: false,
  108. inputXssFilter: false,
  109. autoFloatEnabled: false,
  110. imageUrl: '',
  111. imagePath: Config.upload.cdnurl,
  112. imageUploadCallback: function (file, fn) {
  113. var me = this;
  114. Upload.api.send(file, function (data) {
  115. var url = data.url;
  116. fn && fn.call(me, url, data);
  117. });
  118. }
  119. }, options || {}));
  120. });
  121. });
  122. }
  123. });