xxf_react 0.6.9 → 0.7.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.
@@ -0,0 +1,137 @@
1
+ /**
2
+ * MIME 类型工具类
3
+ *
4
+ * @description
5
+ * 提供 MIME 类型相关的常用操作,包括类型判断、扩展名解析、文件类型标签等
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { Mime } from 'xxf_react/utils'
10
+ *
11
+ * Mime.getExtension('image/png; charset=utf-8') // 'png'
12
+ * Mime.getType('photo.png') // 'image/png'
13
+ * Mime.isImage('image/png') // true
14
+ * Mime.isMedia('video/mp4') // true
15
+ * Mime.getInputAccept(['png', 'jpg']) // 'image/png,image/jpeg'
16
+ * Mime.getLabel('video/mp4') // '视频'
17
+ * ```
18
+ */
19
+ export declare class Mime {
20
+ /**
21
+ * 从 Content-Type 头中提取文件扩展名
22
+ *
23
+ * @description
24
+ * 处理带参数的 Content-Type(如 `text/html; charset=utf-8`),
25
+ * 自动去除 charset 等参数,仅保留 MIME 类型部分进行解析
26
+ *
27
+ * @param contentType - HTTP Content-Type 头的值
28
+ * @returns 对应的文件扩展名(不含点),如 'png'、'json';未知类型返回 null
29
+ *
30
+ * @example
31
+ * Mime.getExtension('image/png') // 'png'
32
+ * Mime.getExtension('application/json; charset=utf-8') // 'json'
33
+ * Mime.getExtension('text/html; charset=utf-8') // 'html'
34
+ * Mime.getExtension('unknown/type') // null
35
+ */
36
+ static getExtension(contentType: string): string | null;
37
+ /**
38
+ * 根据文件名或路径获取 MIME 类型
39
+ *
40
+ * @param filename - 文件名或文件路径
41
+ * @returns MIME 类型字符串,未知类型返回 null
42
+ *
43
+ * @example
44
+ * Mime.getType('photo.png') // 'image/png'
45
+ * Mime.getType('/path/to/file.json') // 'application/json'
46
+ * Mime.getType('document.pdf') // 'application/pdf'
47
+ */
48
+ static getType(filename: string): string | null;
49
+ /**
50
+ * 判断 MIME 类型是否为图片
51
+ *
52
+ * @param mimeType - MIME 类型字符串
53
+ * @returns 是否为图片类型
54
+ *
55
+ * @example
56
+ * Mime.isImage('image/png') // true
57
+ * Mime.isImage('image/jpeg') // true
58
+ * Mime.isImage('application/json') // false
59
+ */
60
+ static isImage(mimeType: string | null | undefined): boolean;
61
+ /**
62
+ * 判断 MIME 类型是否为视频
63
+ *
64
+ * @param mimeType - MIME 类型字符串
65
+ * @returns 是否为视频类型
66
+ *
67
+ * @example
68
+ * Mime.isVideo('video/mp4') // true
69
+ * Mime.isVideo('video/webm') // true
70
+ * Mime.isVideo('image/png') // false
71
+ */
72
+ static isVideo(mimeType: string | null | undefined): boolean;
73
+ /**
74
+ * 判断 MIME 类型是否为音频
75
+ *
76
+ * @param mimeType - MIME 类型字符串
77
+ * @returns 是否为音频类型
78
+ *
79
+ * @example
80
+ * Mime.isAudio('audio/mpeg') // true
81
+ * Mime.isAudio('audio/wav') // true
82
+ * Mime.isAudio('video/mp4') // false
83
+ */
84
+ static isAudio(mimeType: string | null | undefined): boolean;
85
+ /**
86
+ * 判断 MIME 类型是否为媒体文件(图片/视频/音频)
87
+ *
88
+ * @param mimeType - MIME 类型字符串
89
+ * @returns 是否为媒体类型
90
+ *
91
+ * @example
92
+ * Mime.isMedia('image/png') // true
93
+ * Mime.isMedia('video/mp4') // true
94
+ * Mime.isMedia('audio/mpeg') // true
95
+ * Mime.isMedia('text/plain') // false
96
+ */
97
+ static isMedia(mimeType: string | null | undefined): boolean;
98
+ /**
99
+ * 判断 MIME 类型是否为文本类型
100
+ *
101
+ * @param mimeType - MIME 类型字符串
102
+ * @returns 是否为文本类型(包括 text/* 和常见的文本格式如 JSON、XML)
103
+ *
104
+ * @example
105
+ * Mime.isText('text/plain') // true
106
+ * Mime.isText('text/html') // true
107
+ * Mime.isText('application/json') // true
108
+ * Mime.isText('application/xml') // true
109
+ * Mime.isText('image/png') // false
110
+ */
111
+ static isText(mimeType: string | null | undefined): boolean;
112
+ /**
113
+ * 根据文件扩展名生成用于 input[type=file] 的 accept 属性值
114
+ *
115
+ * @param extensions - 文件扩展名数组(不含点)
116
+ * @returns accept 属性字符串
117
+ *
118
+ * @example
119
+ * Mime.getInputAccept(['png', 'jpg', 'gif']) // 'image/png,image/jpeg,image/gif'
120
+ * Mime.getInputAccept(['pdf', 'doc']) // 'application/pdf,application/msword'
121
+ */
122
+ static getInputAccept(extensions: string[]): string;
123
+ /**
124
+ * 获取文件类型的友好显示名称
125
+ *
126
+ * @param mimeType - MIME 类型字符串
127
+ * @returns 友好的类型名称
128
+ *
129
+ * @example
130
+ * Mime.getLabel('image/png') // '图片'
131
+ * Mime.getLabel('video/mp4') // '视频'
132
+ * Mime.getLabel('application/pdf') // '文档'
133
+ * Mime.getLabel('unknown/type') // '文件'
134
+ */
135
+ static getLabel(mimeType: string | null | undefined): string;
136
+ }
137
+ //# sourceMappingURL=MIME.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MIME.d.ts","sourceRoot":"","sources":["../../src/utils/MIME.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,IAAI;IACb;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAKvD;;;;;;;;;;OAUG;IACH,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAI/C;;;;;;;;;;OAUG;IACH,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO;IAI5D;;;;;;;;;;OAUG;IACH,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO;IAI5D;;;;;;;;;;OAUG;IACH,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO;IAI5D;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO;IAI5D;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO;IAe3D;;;;;;;;;OASG;IACH,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,MAAM;IAOnD;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM;CAa/D"}
@@ -0,0 +1,192 @@
1
+ import mime from 'mime';
2
+ /**
3
+ * MIME 类型工具类
4
+ *
5
+ * @description
6
+ * 提供 MIME 类型相关的常用操作,包括类型判断、扩展名解析、文件类型标签等
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { Mime } from 'xxf_react/utils'
11
+ *
12
+ * Mime.getExtension('image/png; charset=utf-8') // 'png'
13
+ * Mime.getType('photo.png') // 'image/png'
14
+ * Mime.isImage('image/png') // true
15
+ * Mime.isMedia('video/mp4') // true
16
+ * Mime.getInputAccept(['png', 'jpg']) // 'image/png,image/jpeg'
17
+ * Mime.getLabel('video/mp4') // '视频'
18
+ * ```
19
+ */
20
+ export class Mime {
21
+ /**
22
+ * 从 Content-Type 头中提取文件扩展名
23
+ *
24
+ * @description
25
+ * 处理带参数的 Content-Type(如 `text/html; charset=utf-8`),
26
+ * 自动去除 charset 等参数,仅保留 MIME 类型部分进行解析
27
+ *
28
+ * @param contentType - HTTP Content-Type 头的值
29
+ * @returns 对应的文件扩展名(不含点),如 'png'、'json';未知类型返回 null
30
+ *
31
+ * @example
32
+ * Mime.getExtension('image/png') // 'png'
33
+ * Mime.getExtension('application/json; charset=utf-8') // 'json'
34
+ * Mime.getExtension('text/html; charset=utf-8') // 'html'
35
+ * Mime.getExtension('unknown/type') // null
36
+ */
37
+ static getExtension(contentType) {
38
+ const mimeType = contentType.split(';')[0].trim();
39
+ return mime.getExtension(mimeType);
40
+ }
41
+ /**
42
+ * 根据文件名或路径获取 MIME 类型
43
+ *
44
+ * @param filename - 文件名或文件路径
45
+ * @returns MIME 类型字符串,未知类型返回 null
46
+ *
47
+ * @example
48
+ * Mime.getType('photo.png') // 'image/png'
49
+ * Mime.getType('/path/to/file.json') // 'application/json'
50
+ * Mime.getType('document.pdf') // 'application/pdf'
51
+ */
52
+ static getType(filename) {
53
+ return mime.getType(filename);
54
+ }
55
+ /**
56
+ * 判断 MIME 类型是否为图片
57
+ *
58
+ * @param mimeType - MIME 类型字符串
59
+ * @returns 是否为图片类型
60
+ *
61
+ * @example
62
+ * Mime.isImage('image/png') // true
63
+ * Mime.isImage('image/jpeg') // true
64
+ * Mime.isImage('application/json') // false
65
+ */
66
+ static isImage(mimeType) {
67
+ return !!mimeType && mimeType.startsWith('image/');
68
+ }
69
+ /**
70
+ * 判断 MIME 类型是否为视频
71
+ *
72
+ * @param mimeType - MIME 类型字符串
73
+ * @returns 是否为视频类型
74
+ *
75
+ * @example
76
+ * Mime.isVideo('video/mp4') // true
77
+ * Mime.isVideo('video/webm') // true
78
+ * Mime.isVideo('image/png') // false
79
+ */
80
+ static isVideo(mimeType) {
81
+ return !!mimeType && mimeType.startsWith('video/');
82
+ }
83
+ /**
84
+ * 判断 MIME 类型是否为音频
85
+ *
86
+ * @param mimeType - MIME 类型字符串
87
+ * @returns 是否为音频类型
88
+ *
89
+ * @example
90
+ * Mime.isAudio('audio/mpeg') // true
91
+ * Mime.isAudio('audio/wav') // true
92
+ * Mime.isAudio('video/mp4') // false
93
+ */
94
+ static isAudio(mimeType) {
95
+ return !!mimeType && mimeType.startsWith('audio/');
96
+ }
97
+ /**
98
+ * 判断 MIME 类型是否为媒体文件(图片/视频/音频)
99
+ *
100
+ * @param mimeType - MIME 类型字符串
101
+ * @returns 是否为媒体类型
102
+ *
103
+ * @example
104
+ * Mime.isMedia('image/png') // true
105
+ * Mime.isMedia('video/mp4') // true
106
+ * Mime.isMedia('audio/mpeg') // true
107
+ * Mime.isMedia('text/plain') // false
108
+ */
109
+ static isMedia(mimeType) {
110
+ return Mime.isImage(mimeType) || Mime.isVideo(mimeType) || Mime.isAudio(mimeType);
111
+ }
112
+ /**
113
+ * 判断 MIME 类型是否为文本类型
114
+ *
115
+ * @param mimeType - MIME 类型字符串
116
+ * @returns 是否为文本类型(包括 text/* 和常见的文本格式如 JSON、XML)
117
+ *
118
+ * @example
119
+ * Mime.isText('text/plain') // true
120
+ * Mime.isText('text/html') // true
121
+ * Mime.isText('application/json') // true
122
+ * Mime.isText('application/xml') // true
123
+ * Mime.isText('image/png') // false
124
+ */
125
+ static isText(mimeType) {
126
+ if (!mimeType)
127
+ return false;
128
+ if (mimeType.startsWith('text/'))
129
+ return true;
130
+ // 常见的文本格式
131
+ const textMimes = [
132
+ 'application/json',
133
+ 'application/xml',
134
+ 'application/javascript',
135
+ 'application/typescript',
136
+ 'application/x-yaml',
137
+ 'application/x-www-form-urlencoded',
138
+ ];
139
+ return textMimes.some(t => mimeType.startsWith(t));
140
+ }
141
+ /**
142
+ * 根据文件扩展名生成用于 input[type=file] 的 accept 属性值
143
+ *
144
+ * @param extensions - 文件扩展名数组(不含点)
145
+ * @returns accept 属性字符串
146
+ *
147
+ * @example
148
+ * Mime.getInputAccept(['png', 'jpg', 'gif']) // 'image/png,image/jpeg,image/gif'
149
+ * Mime.getInputAccept(['pdf', 'doc']) // 'application/pdf,application/msword'
150
+ */
151
+ static getInputAccept(extensions) {
152
+ return extensions
153
+ .map(ext => mime.getType(ext))
154
+ .filter((type) => type !== null)
155
+ .join(',');
156
+ }
157
+ /**
158
+ * 获取文件类型的友好显示名称
159
+ *
160
+ * @param mimeType - MIME 类型字符串
161
+ * @returns 友好的类型名称
162
+ *
163
+ * @example
164
+ * Mime.getLabel('image/png') // '图片'
165
+ * Mime.getLabel('video/mp4') // '视频'
166
+ * Mime.getLabel('application/pdf') // '文档'
167
+ * Mime.getLabel('unknown/type') // '文件'
168
+ */
169
+ static getLabel(mimeType) {
170
+ if (!mimeType)
171
+ return '文件';
172
+ if (Mime.isImage(mimeType))
173
+ return '图片';
174
+ if (Mime.isVideo(mimeType))
175
+ return '视频';
176
+ if (Mime.isAudio(mimeType))
177
+ return '音频';
178
+ if (mimeType.includes('pdf'))
179
+ return '文档';
180
+ if (mimeType.includes('word') || mimeType.includes('document'))
181
+ return '文档';
182
+ if (mimeType.includes('sheet') || mimeType.includes('excel'))
183
+ return '表格';
184
+ if (mimeType.includes('presentation') || mimeType.includes('powerpoint'))
185
+ return '演示文稿';
186
+ if (mimeType.includes('zip') || mimeType.includes('rar') || mimeType.includes('tar'))
187
+ return '压缩包';
188
+ if (Mime.isText(mimeType))
189
+ return '文本';
190
+ return '文件';
191
+ }
192
+ }
@@ -1,4 +1,5 @@
1
1
  export * from './ScrollToCenter';
2
2
  export * from './Reload';
3
3
  export * from './url-placholder';
4
+ export * from './MIME';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,cAAc,kBAAkB,CAAA;AAChC,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,cAAc,kBAAkB,CAAA;AAChC,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,QAAQ,CAAA"}
@@ -2,3 +2,4 @@
2
2
  export * from './ScrollToCenter';
3
3
  export * from './Reload';
4
4
  export * from './url-placholder';
5
+ export * from './MIME';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xxf_react",
3
- "version": "0.6.9",
3
+ "version": "0.7.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -76,6 +76,7 @@
76
76
  "dayjs": "^1.11.19",
77
77
  "idb-keyval": "^6.2.2",
78
78
  "ky": "^1.14.3",
79
+ "mime": "^4.1.0",
79
80
  "react-async-hook": "^4.0.0",
80
81
  "react-resize-detector": "^12.3.0",
81
82
  "react-responsive": "^10.0.1",