@d-matrix/utils 1.9.2 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/file.d.ts +6 -6
- package/dist/file.js +55 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/operator.d.ts +6 -0
- package/dist/operator.js +6 -0
- package/package.json +1 -1
- package/readme.md +25 -1
package/dist/file.d.ts
CHANGED
|
@@ -38,12 +38,12 @@ export declare function getFilenameFromContentDispositionHeader(header: {
|
|
|
38
38
|
}): string;
|
|
39
39
|
declare const HyperLinkTargets: readonly ["_self", "_blank", "_parent", "_top"];
|
|
40
40
|
export declare type HyperLinkTarget = (typeof HyperLinkTargets)[number];
|
|
41
|
-
/**
|
|
42
|
-
* 文件下载
|
|
43
|
-
* @param source 文件地址或blob对象
|
|
44
|
-
* @param fileName 文件名
|
|
45
|
-
* @param isPreview 是否
|
|
46
|
-
*/
|
|
47
41
|
export declare function download(source: Blob, fileName?: string): void;
|
|
48
42
|
export declare function download(source: string, fileName?: string, target?: HyperLinkTarget): void;
|
|
43
|
+
/**
|
|
44
|
+
* 通过创建iframe进行文件下载
|
|
45
|
+
* @param source
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
export declare function downloadFileByIframe(source: string): boolean;
|
|
49
49
|
export {};
|
package/dist/file.js
CHANGED
|
@@ -79,9 +79,32 @@ export function getFilenameFromContentDispositionHeader(header) {
|
|
|
79
79
|
return decodeURIComponent(filename);
|
|
80
80
|
}
|
|
81
81
|
const HyperLinkTargets = ['_self', '_blank', '_parent', '_top'];
|
|
82
|
+
/**
|
|
83
|
+
* 文件下载
|
|
84
|
+
* @param source 文件地址或blob对象
|
|
85
|
+
* @param fileName 文件名
|
|
86
|
+
* @param isPreview 是否
|
|
87
|
+
*/
|
|
88
|
+
// `a.click()` doesn't work for all browsers (#465)
|
|
89
|
+
function click(node) {
|
|
90
|
+
try {
|
|
91
|
+
node.dispatchEvent(new MouseEvent('click'));
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
var evt = document.createEvent('MouseEvents');
|
|
95
|
+
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
|
|
96
|
+
node.dispatchEvent(evt);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
82
99
|
export function download(source, fileName = '', target) {
|
|
83
100
|
const link = document.createElement('a');
|
|
84
101
|
if (typeof source === 'string') {
|
|
102
|
+
if (!source.length) {
|
|
103
|
+
if (process.env.NODE_ENV === 'development') {
|
|
104
|
+
console.error('下载失败,原因:source为空字符串');
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
85
108
|
if (typeof target === 'string' && HyperLinkTargets.includes(target)) {
|
|
86
109
|
link.target = target;
|
|
87
110
|
}
|
|
@@ -90,12 +113,40 @@ export function download(source, fileName = '', target) {
|
|
|
90
113
|
}
|
|
91
114
|
link.href = source;
|
|
92
115
|
}
|
|
116
|
+
let objectURL;
|
|
93
117
|
if (source instanceof Blob) {
|
|
94
|
-
const
|
|
95
|
-
link.href =
|
|
118
|
+
const objectURL = window.URL.createObjectURL(source);
|
|
119
|
+
link.href = objectURL;
|
|
96
120
|
link.download = fileName;
|
|
97
121
|
}
|
|
98
122
|
document.body.appendChild(link);
|
|
99
|
-
|
|
100
|
-
|
|
123
|
+
click(link);
|
|
124
|
+
setTimeout(() => {
|
|
125
|
+
if (typeof objectURL === 'string') {
|
|
126
|
+
window.URL.revokeObjectURL(objectURL);
|
|
127
|
+
}
|
|
128
|
+
document.body.removeChild(link);
|
|
129
|
+
}, 4e4); // 40S
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 通过创建iframe进行文件下载
|
|
133
|
+
* @param source
|
|
134
|
+
* @returns
|
|
135
|
+
*/
|
|
136
|
+
export function downloadFileByIframe(source) {
|
|
137
|
+
const httpsPath = source.replace(/http:\/\//, "https://");
|
|
138
|
+
const iframes = document.getElementsByTagName("iframe");
|
|
139
|
+
if (iframes.length === 0 ||
|
|
140
|
+
(iframes.length > 0 && iframes[0].className === "fill" && iframes[iframes.length - 1].className === "fill")) {
|
|
141
|
+
const element = document.createElement("iframe");
|
|
142
|
+
element.style.display = "none";
|
|
143
|
+
element.src = httpsPath;
|
|
144
|
+
document.body.appendChild(element);
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
if (iframes.length > 0 && iframes[iframes.length - 1].className !== "fill") {
|
|
148
|
+
iframes[0].src = httpsPath;
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
return false;
|
|
101
152
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/operator.js
ADDED
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -16,6 +16,7 @@ A dozen of utils for Front-End Development
|
|
|
16
16
|
- [file](#file)
|
|
17
17
|
- [support](#support)
|
|
18
18
|
- [timer](#timer)
|
|
19
|
+
- [operator](#operator)
|
|
19
20
|
|
|
20
21
|
### clipboard
|
|
21
22
|
|
|
@@ -240,9 +241,13 @@ const filename = file.getFilenameFromContentDispositionHeader(header);
|
|
|
240
241
|
文件下载,`source`是文件地址或`blob`对象。
|
|
241
242
|
|
|
242
243
|
```ts
|
|
243
|
-
type HyperLinkTarget = "_self" | "
|
|
244
|
+
type HyperLinkTarget = "_self" | "_blank" | "_parent" | "_top"
|
|
244
245
|
```
|
|
245
246
|
|
|
247
|
+
- `downloadFileByIframe(source: string): boolean`
|
|
248
|
+
|
|
249
|
+
通过创建`iframe`进行文件下载
|
|
250
|
+
|
|
246
251
|
## support
|
|
247
252
|
|
|
248
253
|
- `isBrowserEnv(): boolean`
|
|
@@ -268,6 +273,25 @@ await sleep(3000); // 暂停3秒
|
|
|
268
273
|
console.log('continue'); // 继续执行
|
|
269
274
|
```
|
|
270
275
|
|
|
276
|
+
## operator
|
|
277
|
+
|
|
278
|
+
- `trueTypeOf = (obj: unknown): string`
|
|
279
|
+
|
|
280
|
+
检查数据类型
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
trueTypeOf([]); // array
|
|
284
|
+
trueTypeOf({}); // object
|
|
285
|
+
trueTypeOf(''); // string
|
|
286
|
+
trueTypeOf(new Date()); // date
|
|
287
|
+
trueTypeOf(1); // number
|
|
288
|
+
trueTypeOf(function () {}); // function
|
|
289
|
+
trueTypeOf(/test/i); // regexp
|
|
290
|
+
trueTypeOf(true); // boolean
|
|
291
|
+
trueTypeOf(null); // null
|
|
292
|
+
trueTypeOf(undefined); // undefined
|
|
293
|
+
```
|
|
294
|
+
|
|
271
295
|
## 测试
|
|
272
296
|
|
|
273
297
|
运行全部组件测试
|