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