@qy_better_lib/core 0.1.3 → 0.1.4

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.
@@ -1,12 +1,16 @@
1
1
  /**
2
2
  * Echarts图表字体、间距自适应
3
- * @param size
4
- * @param defalteWidth
5
- * @returns
3
+ * @param size 原始尺寸
4
+ * @param deflate_width 基准宽度,默认1920
5
+ * @returns 自适应后的尺寸
6
6
  */
7
- export declare function auto_size(size: number, defalteWidth?: number): number;
7
+ export declare function auto_size(size: number, deflate_width?: number): number;
8
8
  /**
9
9
  * Echarts线性渐变颜色
10
+ * @param type 渐变类型,'v'为垂直渐变,'h'为水平渐变
11
+ * @param start_color 起始颜色
12
+ * @param end_color 结束颜色
13
+ * @returns 渐变配置对象
10
14
  */
11
15
  export declare function get_chart_gradient_color(type: 'v' | 'h', start_color: string, end_color: string): {
12
16
  type: string;
@@ -19,3 +23,127 @@ export declare function get_chart_gradient_color(type: 'v' | 'h', start_color: s
19
23
  color: string;
20
24
  }[];
21
25
  };
26
+ /**
27
+ * Echarts图表配置项类型
28
+ */
29
+ export type EChartsOption = any;
30
+ /**
31
+ * 初始化Echarts图表
32
+ * @param container 容器元素或容器ID
33
+ * @param options 图表配置项
34
+ * @param renderer 渲染方式,可选 'canvas' 或 'svg'
35
+ * @returns Echarts实例
36
+ */
37
+ export declare function init_chart(container: HTMLElement | string, options?: EChartsOption, renderer?: 'canvas' | 'svg'): Promise<any>;
38
+ /**
39
+ * 销毁Echarts图表
40
+ * @param chart Echarts实例
41
+ */
42
+ export declare function destroy_chart(chart: any): void;
43
+ /**
44
+ * 更新Echarts图表配置
45
+ * @param chart Echarts实例
46
+ * @param options 图表配置项
47
+ */
48
+ export declare function update_chart(chart: any, options: EChartsOption): void;
49
+ /**
50
+ * 调整Echarts图表大小
51
+ * @param chart Echarts实例
52
+ */
53
+ export declare function resize_chart(chart: any): void;
54
+ /**
55
+ * 为Echarts图表添加响应式处理
56
+ * @param chart Echarts实例
57
+ * @param debounce_time 防抖时间,默认200ms
58
+ */
59
+ export declare function make_chart_responsive(chart: any, debounce_time?: number): (() => void) | undefined;
60
+ /**
61
+ * 生成Echarts颜色数组
62
+ * @param count 颜色数量
63
+ * @param base_colors 基础颜色数组,默认使用ECharts默认颜色
64
+ * @returns 颜色数组
65
+ */
66
+ export declare function generate_chart_colors(count: number, base_colors?: string[]): string[];
67
+ /**
68
+ * 格式化Echarts数据标签
69
+ * @param value 数值
70
+ * @param formatter 格式化函数或格式化模板
71
+ * @returns 格式化后的值
72
+ */
73
+ export declare function format_chart_label(value: any, formatter?: ((value: any) => string) | string): string;
74
+ /**
75
+ * 合并Echarts配置项
76
+ * @param defaultOption 默认配置项
77
+ * @param customOption 自定义配置项
78
+ * @returns 合并后的配置项
79
+ */
80
+ export declare function merge_chart_options(defaultOption: EChartsOption, customOption: EChartsOption): EChartsOption;
81
+ /**
82
+ * 为Echarts图表添加事件监听器
83
+ * @param chart Echarts实例
84
+ * @param eventName 事件名称
85
+ * @param handler 事件处理函数
86
+ */
87
+ export declare function add_chart_listener(chart: any, eventName: string, handler: (...args: any[]) => void): void;
88
+ /**
89
+ * 移除Echarts图表事件监听器
90
+ * @param chart Echarts实例
91
+ * @param eventName 事件名称
92
+ * @param handler 事件处理函数
93
+ */
94
+ export declare function remove_chart_listener(chart: any, eventName: string, handler: (...args: any[]) => void): void;
95
+ /**
96
+ * 导出Echarts图表为图片
97
+ * @param chart Echarts实例
98
+ * @param options 导出选项
99
+ * @returns 图片URL
100
+ */
101
+ export declare function export_chart_image(chart: any, options?: {
102
+ type?: string;
103
+ pixel_ratio?: number;
104
+ background_color?: string;
105
+ exclude_components?: string[];
106
+ file_name?: string;
107
+ }): any;
108
+ /**
109
+ * 检查Echarts是否已加载
110
+ * @returns 是否已加载
111
+ */
112
+ export declare function is_echarts_loaded(): boolean;
113
+ /**
114
+ * 批量调整Echarts图表大小
115
+ * @param charts 图表实例数组
116
+ */
117
+ export declare function resize_charts(charts: any[]): void;
118
+ /**
119
+ * 批量销毁Echarts图表
120
+ * @param charts 图表实例数组
121
+ */
122
+ export declare function destroy_charts(charts: any[]): void;
123
+ /**
124
+ * 数据排序函数
125
+ * @param data 原始数据数组
126
+ * @param sort_key 排序字段名
127
+ * @param order 排序顺序,默认'asc'
128
+ * @returns 排序后的数据数组
129
+ */
130
+ export declare function sort_chart_data(data: any[], sort_key: string, order?: 'asc' | 'desc'): any[];
131
+ /**
132
+ * 数据分组函数
133
+ * @param data 原始数据数组
134
+ * @param group_key 分组字段名
135
+ * @returns 分组后的数据对象
136
+ */
137
+ export declare function group_chart_data(data: any[], group_key: string): Record<string, any[]>;
138
+ /**
139
+ * 数据聚合函数
140
+ * @param data 原始数据数组
141
+ * @param group_key 分组字段名
142
+ * @param value_key 数值字段名
143
+ * @param aggregator 聚合函数,默认求和
144
+ * @returns 聚合后的数据数组
145
+ */
146
+ export declare function aggregate_chart_data(data: any[], group_key: string, value_key: string, aggregator?: (values: number[]) => number): {
147
+ [group_key]: string;
148
+ [value_key]: number;
149
+ }[];
@@ -1,31 +1,228 @@
1
- function r(t, o = 1920) {
2
- let e = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
3
- if (!e) return t;
4
- let n = e / o;
5
- return Number((t * n).toFixed(3));
1
+ function auto_size(size, deflate_width = 1920) {
2
+ let client_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
3
+ if (!client_width) return size;
4
+ let scale = client_width / deflate_width;
5
+ return Number((size * scale).toFixed(3));
6
6
  }
7
- function i(t, o, e) {
7
+ function get_chart_gradient_color(type, start_color, end_color) {
8
8
  return {
9
9
  type: "linear",
10
10
  x: 0,
11
11
  y: 0,
12
- x2: t === "v" ? 0 : 1,
13
- y2: t === "v" ? 1 : 0,
12
+ x2: type === "v" ? 0 : 1,
13
+ y2: type === "v" ? 1 : 0,
14
14
  colorStops: [
15
15
  {
16
16
  offset: 0,
17
- color: o
17
+ color: start_color
18
18
  // 0% 处的颜色
19
19
  },
20
20
  {
21
21
  offset: 1,
22
- color: e
22
+ color: end_color
23
23
  // 100% 处的颜色
24
24
  }
25
25
  ]
26
26
  };
27
27
  }
28
+ function init_chart(container, options, renderer = "canvas") {
29
+ const load_echarts = async () => {
30
+ try {
31
+ let echarts;
32
+ if (typeof window !== "undefined" && window.echarts) {
33
+ echarts = window.echarts;
34
+ } else {
35
+ const imported = await import("echarts");
36
+ echarts = imported.default || imported;
37
+ }
38
+ const chart_dom = typeof container === "string" ? document.getElementById(container) : container;
39
+ if (!chart_dom) {
40
+ console.error("ECharts DOM容器未找到");
41
+ return null;
42
+ }
43
+ const chart = echarts.init(chart_dom, void 0, {
44
+ renderer
45
+ });
46
+ if (options) {
47
+ chart.setOption(options);
48
+ }
49
+ return chart;
50
+ } catch (error) {
51
+ console.error("加载 ECharts 失败:", error);
52
+ return null;
53
+ }
54
+ };
55
+ return load_echarts();
56
+ }
57
+ function destroy_chart(chart) {
58
+ if (chart && typeof chart.dispose === "function") {
59
+ chart.dispose();
60
+ }
61
+ }
62
+ function update_chart(chart, options) {
63
+ if (chart && typeof chart.setOption === "function") {
64
+ chart.setOption(options);
65
+ }
66
+ }
67
+ function resize_chart(chart) {
68
+ if (chart && typeof chart.resize === "function") {
69
+ chart.resize();
70
+ }
71
+ }
72
+ function make_chart_responsive(chart, debounce_time = 200) {
73
+ if (!chart) return;
74
+ let resize_timer;
75
+ const resize_handler = () => {
76
+ clearTimeout(resize_timer);
77
+ resize_timer = setTimeout(() => {
78
+ resize_chart(chart);
79
+ }, debounce_time);
80
+ };
81
+ window.addEventListener("resize", resize_handler);
82
+ return () => {
83
+ window.removeEventListener("resize", resize_handler);
84
+ clearTimeout(resize_timer);
85
+ };
86
+ }
87
+ function generate_chart_colors(count, base_colors) {
88
+ const default_colors = [
89
+ "#5470c6",
90
+ "#91cc75",
91
+ "#fac858",
92
+ "#ee6666",
93
+ "#73c0de",
94
+ "#3ba272",
95
+ "#fc8452",
96
+ "#9a60b4",
97
+ "#ea7ccc",
98
+ "#67c23a"
99
+ ];
100
+ const colors = base_colors || default_colors;
101
+ const result = [];
102
+ for (let i = 0; i < count; i++) {
103
+ result.push(colors[i % colors.length]);
104
+ }
105
+ return result;
106
+ }
107
+ function format_chart_label(value, formatter) {
108
+ if (typeof formatter === "function") {
109
+ return formatter(value);
110
+ }
111
+ if (typeof formatter === "string") {
112
+ return formatter.replace("{value}", String(value));
113
+ }
114
+ return String(value);
115
+ }
116
+ function merge_chart_options(defaultOption, customOption) {
117
+ return {
118
+ ...defaultOption,
119
+ ...customOption,
120
+ // 特殊处理数组类型的配置
121
+ series: customOption.series || defaultOption.series,
122
+ xAxis: customOption.xAxis || defaultOption.xAxis,
123
+ yAxis: customOption.yAxis || defaultOption.yAxis,
124
+ legend: customOption.legend || defaultOption.legend,
125
+ tooltip: customOption.tooltip || defaultOption.tooltip,
126
+ title: customOption.title || defaultOption.title
127
+ };
128
+ }
129
+ function add_chart_listener(chart, eventName, handler) {
130
+ if (chart && typeof chart.on === "function") {
131
+ chart.on(eventName, handler);
132
+ }
133
+ }
134
+ function remove_chart_listener(chart, eventName, handler) {
135
+ if (chart && typeof chart.off === "function") {
136
+ chart.off(eventName, handler);
137
+ }
138
+ }
139
+ function export_chart_image(chart, options = {}) {
140
+ if (!chart || typeof chart.getDataURL !== "function") {
141
+ return null;
142
+ }
143
+ const {
144
+ type = "png",
145
+ pixel_ratio = 2,
146
+ background_color = "#fff",
147
+ exclude_components = [],
148
+ file_name = "echarts-image"
149
+ } = options;
150
+ const data_url = chart.getDataURL({
151
+ type,
152
+ pixelRatio: pixel_ratio,
153
+ backgroundColor: background_color,
154
+ excludeComponents: exclude_components
155
+ });
156
+ const link = document.createElement("a");
157
+ link.download = `${file_name}.${type}`;
158
+ link.href = data_url;
159
+ link.click();
160
+ return data_url;
161
+ }
162
+ function is_echarts_loaded() {
163
+ return typeof window !== "undefined" && typeof window.echarts !== "undefined";
164
+ }
165
+ function resize_charts(charts) {
166
+ charts.forEach((chart) => {
167
+ resize_chart(chart);
168
+ });
169
+ }
170
+ function destroy_charts(charts) {
171
+ charts.forEach((chart) => {
172
+ destroy_chart(chart);
173
+ });
174
+ }
175
+ function sort_chart_data(data, sort_key, order = "asc") {
176
+ return [...data].sort((a, b) => {
177
+ const a_value = a[sort_key];
178
+ const b_value = b[sort_key];
179
+ if (a_value < b_value) {
180
+ return order === "asc" ? -1 : 1;
181
+ }
182
+ if (a_value > b_value) {
183
+ return order === "asc" ? 1 : -1;
184
+ }
185
+ return 0;
186
+ });
187
+ }
188
+ function group_chart_data(data, group_key) {
189
+ return data.reduce((groups, item) => {
190
+ const key = item[group_key];
191
+ if (!groups[key]) {
192
+ groups[key] = [];
193
+ }
194
+ groups[key].push(item);
195
+ return groups;
196
+ }, {});
197
+ }
198
+ function aggregate_chart_data(data, group_key, value_key, aggregator = (values) => values.reduce((sum, val) => sum + val, 0)) {
199
+ const groups = group_chart_data(data, group_key);
200
+ return Object.entries(groups).map(([key, items]) => {
201
+ const values = items.map((item) => item[value_key]).filter((val) => typeof val === "number");
202
+ return {
203
+ [group_key]: key,
204
+ [value_key]: aggregator(values)
205
+ };
206
+ });
207
+ }
28
208
  export {
29
- r as auto_size,
30
- i as get_chart_gradient_color
209
+ add_chart_listener,
210
+ aggregate_chart_data,
211
+ auto_size,
212
+ destroy_chart,
213
+ destroy_charts,
214
+ export_chart_image,
215
+ format_chart_label,
216
+ generate_chart_colors,
217
+ get_chart_gradient_color,
218
+ group_chart_data,
219
+ init_chart,
220
+ is_echarts_loaded,
221
+ make_chart_responsive,
222
+ merge_chart_options,
223
+ remove_chart_listener,
224
+ resize_chart,
225
+ resize_charts,
226
+ sort_chart_data,
227
+ update_chart
31
228
  };
@@ -2,16 +2,92 @@
2
2
  * 下载文件
3
3
  * @param filePath 文件路径
4
4
  */
5
- export declare function downLoadFile({ path, name }: {
5
+ export declare function download_file({ path, name }: {
6
6
  path: string;
7
7
  name?: string;
8
8
  }): void;
9
9
  /**批量下载文件 */
10
- export declare function downLoadFiles(fileList: any[]): void;
10
+ export declare function download_files(file_list: any[]): void;
11
11
  /**
12
12
  * 将file转换成base64
13
13
  * @param file
14
14
  * @param callback
15
15
  * @returns
16
16
  */
17
- export declare function toBase64(file: Blob): Promise<string | undefined>;
17
+ export declare function to_base64(file: Blob): Promise<string | undefined>;
18
+ /**
19
+ * 读取文件内容
20
+ * @param file 文件对象
21
+ * @param encoding 编码方式,默认为 UTF-8
22
+ * @returns 文件内容
23
+ */
24
+ export declare function read_file(file: Blob, encoding?: string): Promise<string | undefined>;
25
+ /**
26
+ * 获取文件扩展名
27
+ * @param file_name 文件名
28
+ * @returns 文件扩展名
29
+ */
30
+ export declare function get_file_extension(file_name: string): string;
31
+ /**
32
+ * 获取不带扩展名的文件名
33
+ * @param file_name 文件名
34
+ * @returns 不带扩展名的文件名
35
+ */
36
+ export declare function get_file_name_without_extension(file_name: string): string;
37
+ /**
38
+ * 验证文件大小
39
+ * @param file 文件对象
40
+ * @param max_size 最大文件大小(字节)
41
+ * @returns 是否符合大小要求
42
+ */
43
+ export declare function validate_file_size(file: File, max_size: number): boolean;
44
+ /**
45
+ * 验证文件类型
46
+ * @param file 文件对象
47
+ * @param allowed_types 允许的文件类型数组
48
+ * @returns 是否符合类型要求
49
+ */
50
+ export declare function validate_file_type(file: File, allowed_types: string[]): boolean;
51
+ /**
52
+ * 从 Blob 创建文件
53
+ * @param blob Blob 对象
54
+ * @param file_name 文件名
55
+ * @param mime_type MIME 类型
56
+ * @returns 文件对象
57
+ */
58
+ export declare function create_file_from_blob(blob: Blob, file_name: string, mime_type: string): File;
59
+ /**
60
+ * 下载文本文件
61
+ * @param content 文件内容
62
+ * @param file_name 文件名
63
+ * @param mime_type MIME 类型,默认为 text/plain
64
+ */
65
+ export declare function download_text_file(content: string, file_name: string, mime_type?: string): void;
66
+ /**
67
+ * 压缩图片文件
68
+ * @param file 图片文件
69
+ * @param max_width 最大宽度
70
+ * @param max_height 最大高度
71
+ * @param quality 压缩质量,0-1之间
72
+ * @returns 压缩后的文件
73
+ */
74
+ export declare function compress_image(file: File, max_width?: number, max_height?: number, quality?: number): Promise<File | undefined>;
75
+ /**
76
+ * 获取文件信息
77
+ * @param file 文件对象
78
+ * @returns 文件信息对象
79
+ */
80
+ export declare function get_file_info(file: File): {
81
+ name: string;
82
+ size: number;
83
+ size_text: string;
84
+ type: string;
85
+ extension: string;
86
+ name_without_extension: string;
87
+ };
88
+ /**
89
+ * 生成唯一的文件名
90
+ * @param original_name 原始文件名
91
+ * @returns 唯一的文件名
92
+ */
93
+ export declare function generate_unique_file_name(original_name: string): string;
package/lib/utils/file.js CHANGED
@@ -1,21 +1,144 @@
1
- function a({ path: n, name: e }) {
2
- const r = document.createElement("a");
3
- r.href = n, r.target = "_blank", e && r.setAttribute("download", e), document.body.appendChild(r), r.click(), document.body.removeChild(r);
1
+ function download_file({ path, name }) {
2
+ const link = document.createElement("a");
3
+ link.href = path;
4
+ link.target = "_blank";
5
+ name && link.setAttribute("download", name);
6
+ document.body.appendChild(link);
7
+ link.click();
8
+ document.body.removeChild(link);
4
9
  }
5
- function d(n) {
6
- n.forEach((e) => {
7
- a({ path: e.url, name: e.name });
10
+ function download_files(file_list) {
11
+ file_list.forEach((file_item) => {
12
+ download_file({ path: file_item.url, name: file_item.name });
8
13
  });
9
14
  }
10
- async function c(n) {
11
- if (!n) return;
12
- const e = new FileReader();
13
- return e.readAsDataURL(n), new Promise((r, t) => {
14
- e.onload = (o) => r(o.target?.result), e.onerror = (o) => t(void 0);
15
+ async function to_base64(file) {
16
+ if (!file) return void 0;
17
+ const reader = new FileReader();
18
+ reader.readAsDataURL(file);
19
+ return new Promise((resolve, reject) => {
20
+ reader.onload = (event) => {
21
+ return resolve(event.target?.result);
22
+ };
23
+ reader.onerror = (error) => {
24
+ return reject(void 0);
25
+ };
15
26
  });
16
27
  }
28
+ async function read_file(file, encoding = "UTF-8") {
29
+ if (!file) return void 0;
30
+ const reader = new FileReader();
31
+ reader.readAsText(file, encoding);
32
+ return new Promise((resolve, reject) => {
33
+ reader.onload = (event) => {
34
+ return resolve(event.target?.result);
35
+ };
36
+ reader.onerror = () => {
37
+ return reject(void 0);
38
+ };
39
+ });
40
+ }
41
+ function get_file_extension(file_name) {
42
+ if (!file_name) return "";
43
+ const last_dot_index = file_name.lastIndexOf(".");
44
+ if (last_dot_index === -1) return "";
45
+ return file_name.substring(last_dot_index + 1).toLowerCase();
46
+ }
47
+ function get_file_name_without_extension(file_name) {
48
+ if (!file_name) return "";
49
+ const last_dot_index = file_name.lastIndexOf(".");
50
+ if (last_dot_index === -1) return file_name;
51
+ return file_name.substring(0, last_dot_index);
52
+ }
53
+ function validate_file_size(file, max_size) {
54
+ return file.size <= max_size;
55
+ }
56
+ function validate_file_type(file, allowed_types) {
57
+ const file_extension = get_file_extension(file.name);
58
+ return allowed_types.includes(file_extension);
59
+ }
60
+ function create_file_from_blob(blob, file_name, mime_type) {
61
+ return new File([blob], file_name, { type: mime_type });
62
+ }
63
+ function download_text_file(content, file_name, mime_type = "text/plain") {
64
+ const blob = new Blob([content], { type: mime_type });
65
+ const url = URL.createObjectURL(blob);
66
+ download_file({ path: url, name: file_name });
67
+ URL.revokeObjectURL(url);
68
+ }
69
+ async function compress_image(file, max_width = 800, max_height = 800, quality = 0.8) {
70
+ if (!file.type.startsWith("image/")) return void 0;
71
+ return new Promise((resolve) => {
72
+ const canvas = document.createElement("canvas");
73
+ const ctx = canvas.getContext("2d");
74
+ const img = new Image();
75
+ img.onload = () => {
76
+ let { width, height } = img;
77
+ if (width > max_width || height > max_height) {
78
+ const ratio = Math.min(max_width / width, max_height / height);
79
+ width *= ratio;
80
+ height *= ratio;
81
+ }
82
+ canvas.width = width;
83
+ canvas.height = height;
84
+ ctx?.drawImage(img, 0, 0, width, height);
85
+ canvas.toBlob((blob) => {
86
+ if (blob) {
87
+ const compressed_file = new File([blob], file.name, { type: file.type });
88
+ resolve(compressed_file);
89
+ } else {
90
+ resolve(void 0);
91
+ }
92
+ }, file.type, quality);
93
+ };
94
+ img.src = URL.createObjectURL(file);
95
+ });
96
+ }
97
+ function get_file_info(file) {
98
+ const extension = get_file_extension(file.name);
99
+ const name_without_extension = get_file_name_without_extension(file.name);
100
+ let size_text = "";
101
+ if (file.size < 1024) {
102
+ size_text = `${file.size} B`;
103
+ } else if (file.size < 1024 * 1024) {
104
+ size_text = `${(file.size / 1024).toFixed(2)} KB`;
105
+ } else if (file.size < 1024 * 1024 * 1024) {
106
+ size_text = `${(file.size / (1024 * 1024)).toFixed(2)} MB`;
107
+ } else {
108
+ size_text = `${(file.size / (1024 * 1024 * 1024)).toFixed(2)} GB`;
109
+ }
110
+ return {
111
+ name: file.name,
112
+ size: file.size,
113
+ size_text,
114
+ type: file.type,
115
+ extension,
116
+ name_without_extension
117
+ };
118
+ }
119
+ function generate_unique_file_name(original_name) {
120
+ const extension = get_file_extension(original_name);
121
+ const name_without_extension = get_file_name_without_extension(original_name);
122
+ const timestamp = Date.now();
123
+ const random_string = Math.random().toString(36).substring(2, 8);
124
+ if (extension) {
125
+ return `${name_without_extension}_${timestamp}_${random_string}.${extension}`;
126
+ } else {
127
+ return `${name_without_extension}_${timestamp}_${random_string}`;
128
+ }
129
+ }
17
130
  export {
18
- a as downLoadFile,
19
- d as downLoadFiles,
20
- c as toBase64
131
+ compress_image,
132
+ create_file_from_blob,
133
+ download_file,
134
+ download_files,
135
+ download_text_file,
136
+ generate_unique_file_name,
137
+ get_file_extension,
138
+ get_file_info,
139
+ get_file_name_without_extension,
140
+ read_file,
141
+ to_base64,
142
+ validate_file_size,
143
+ validate_file_type
21
144
  };
@@ -7,5 +7,6 @@ export * from './object';
7
7
  export * from './dom';
8
8
  export * from './file';
9
9
  export * from './color';
10
+ export * from './date';
10
11
  export * from './share';
11
12
  export * from './echarts';