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