utils.js 4.0 KB

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