UNPKG

2.69 kBJavaScriptView Raw
1export function T() {
2 return true;
3} // Fix IE file.status problem
4// via coping a new Object
5
6export function fileToObject(file) {
7 return {
8 lastModified: file.lastModified,
9 lastModifiedDate: file.lastModifiedDate,
10 name: file.filename || file.name,
11 size: file.size,
12 type: file.type,
13 uid: file.uid,
14 response: file.response,
15 error: file.error,
16 percent: 0,
17 originFileObj: file
18 };
19}
20/**
21 * 生成Progress percent: 0.1 -> 0.98
22 * - for ie
23 */
24
25export function genPercentAdd() {
26 var k = 0.1;
27 var i = 0.01;
28 var end = 0.98;
29 return function (s) {
30 var start = s;
31
32 if (start >= end) {
33 return start;
34 }
35
36 start += k;
37 k -= i;
38
39 if (k < 0.001) {
40 k = 0.001;
41 }
42
43 return start * 100;
44 };
45}
46export function getFileItem(file, fileList) {
47 var matchKey = file.uid !== undefined ? 'uid' : 'name';
48 return fileList.filter(function (item) {
49 return item[matchKey] === file[matchKey];
50 })[0];
51}
52export function removeFileItem(file, fileList) {
53 var matchKey = file.uid !== undefined ? 'uid' : 'name';
54 var removed = fileList.filter(function (item) {
55 return item[matchKey] !== file[matchKey];
56 });
57
58 if (removed.length === fileList.length) {
59 return null;
60 }
61
62 return removed;
63}
64
65var isImageFileType = function isImageFileType(type) {
66 return type.indexOf('image/') === 0;
67};
68
69var MEASURE_SIZE = 200;
70export function previewImage(file) {
71 return new Promise(function (resolve) {
72 if (!file.type || !isImageFileType(file.type)) {
73 resolve('');
74 return;
75 }
76
77 var canvas = document.createElement('canvas');
78 canvas.width = MEASURE_SIZE;
79 canvas.height = MEASURE_SIZE;
80 canvas.style.cssText = "position: fixed; left: 0; top: 0; width: ".concat(MEASURE_SIZE, "px; height: ").concat(MEASURE_SIZE, "px; z-index: 9999; display: none;");
81 document.body.appendChild(canvas);
82 var ctx = canvas.getContext('2d');
83 var img = new Image();
84
85 img.onload = function () {
86 var width = img.width,
87 height = img.height;
88 var drawWidth = MEASURE_SIZE;
89 var drawHeight = MEASURE_SIZE;
90 var offsetX = 0;
91 var offsetY = 0;
92
93 if (width < height) {
94 drawHeight = height * (MEASURE_SIZE / width);
95 offsetY = -(drawHeight - drawWidth) / 2;
96 } else {
97 drawWidth = width * (MEASURE_SIZE / height);
98 offsetX = -(drawWidth - drawHeight) / 2;
99 }
100
101 ctx.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
102 var dataURL = canvas.toDataURL();
103 document.body.removeChild(canvas);
104 resolve(dataURL);
105 };
106
107 img.src = window.URL.createObjectURL(file);
108 });
109}
110//# sourceMappingURL=utils.js.map