utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. export function file2Obj(file) {
  4. return _objectSpread(_objectSpread({}, file), {}, {
  5. lastModified: file.lastModified,
  6. lastModifiedDate: file.lastModifiedDate,
  7. name: file.name,
  8. size: file.size,
  9. type: file.type,
  10. uid: file.uid,
  11. percent: 0,
  12. originFileObj: file
  13. });
  14. }
  15. /** Upload fileList. Replace file if exist or just push into it. */
  16. export function updateFileList(file, fileList) {
  17. var nextFileList = _toConsumableArray(fileList);
  18. var fileIndex = nextFileList.findIndex(function (_ref) {
  19. var uid = _ref.uid;
  20. return uid === file.uid;
  21. });
  22. if (fileIndex === -1) {
  23. nextFileList.push(file);
  24. } else {
  25. nextFileList[fileIndex] = file;
  26. }
  27. return nextFileList;
  28. }
  29. export function getFileItem(file, fileList) {
  30. var matchKey = file.uid !== undefined ? 'uid' : 'name';
  31. return fileList.filter(function (item) {
  32. return item[matchKey] === file[matchKey];
  33. })[0];
  34. }
  35. export function removeFileItem(file, fileList) {
  36. var matchKey = file.uid !== undefined ? 'uid' : 'name';
  37. var removed = fileList.filter(function (item) {
  38. return item[matchKey] !== file[matchKey];
  39. });
  40. if (removed.length === fileList.length) {
  41. return null;
  42. }
  43. return removed;
  44. }
  45. // ==================== Default Image Preview ====================
  46. var extname = function extname() {
  47. var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  48. var temp = url.split('/');
  49. var filename = temp[temp.length - 1];
  50. var filenameWithoutSuffix = filename.split(/#|\?/)[0];
  51. return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
  52. };
  53. var isImageFileType = function isImageFileType(type) {
  54. return type.indexOf('image/') === 0;
  55. };
  56. export var isImageUrl = function isImageUrl(file) {
  57. if (file.type && !file.thumbUrl) {
  58. return isImageFileType(file.type);
  59. }
  60. var url = file.thumbUrl || file.url || '';
  61. var extension = extname(url);
  62. if (/^data:image\//.test(url) || /(webp|svg|png|gif|jpg|jpeg|jfif|bmp|dpg|ico)$/i.test(extension)) {
  63. return true;
  64. }
  65. if (/^data:/.test(url)) {
  66. // other file types of base64
  67. return false;
  68. }
  69. if (extension) {
  70. // other file types which have extension
  71. return false;
  72. }
  73. return true;
  74. };
  75. var MEASURE_SIZE = 200;
  76. export function previewImage(file) {
  77. return new Promise(function (resolve) {
  78. if (!file.type || !isImageFileType(file.type)) {
  79. resolve('');
  80. return;
  81. }
  82. var canvas = document.createElement('canvas');
  83. canvas.width = MEASURE_SIZE;
  84. canvas.height = MEASURE_SIZE;
  85. canvas.style.cssText = "position: fixed; left: 0; top: 0; width: ".concat(MEASURE_SIZE, "px; height: ").concat(MEASURE_SIZE, "px; z-index: 9999; display: none;");
  86. document.body.appendChild(canvas);
  87. var ctx = canvas.getContext('2d');
  88. var img = new Image();
  89. img.onload = function () {
  90. var width = img.width,
  91. height = img.height;
  92. var drawWidth = MEASURE_SIZE;
  93. var drawHeight = MEASURE_SIZE;
  94. var offsetX = 0;
  95. var offsetY = 0;
  96. if (width > height) {
  97. drawHeight = height * (MEASURE_SIZE / width);
  98. offsetY = -(drawHeight - drawWidth) / 2;
  99. } else {
  100. drawWidth = width * (MEASURE_SIZE / height);
  101. offsetX = -(drawWidth - drawHeight) / 2;
  102. }
  103. ctx.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
  104. var dataURL = canvas.toDataURL();
  105. document.body.removeChild(canvas);
  106. resolve(dataURL);
  107. };
  108. img.src = window.URL.createObjectURL(file);
  109. });
  110. }