giime 0.0.30 → 0.1.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.
@@ -1,89 +1,50 @@
1
1
  'use strict';
2
2
 
3
3
  var vue = require('vue');
4
+ var FileSaver = require('file-saver');
4
5
  var index = require('../../../components/src/plugins/notification/index.js');
6
+ var uuid = require('uuid');
5
7
  var JSZip = require('jszip');
6
8
 
7
9
  const useDownload = () => {
8
- let xhrInstance;
9
- let downloading = false;
10
- const progress = vue.ref(0);
11
- const currentClass = `class${Date.now()}`;
12
- vue.onBeforeUnmount(() => {
13
- if (xhrInstance) {
14
- xhrInstance.abort();
15
- xhrInstance = null;
16
- }
17
- });
18
- const downloadBlob = (options) => {
19
- return new Promise((resolve, reject) => {
20
- xhrInstance = new XMLHttpRequest();
21
- xhrInstance.responseType = "blob";
22
- xhrInstance.open("get", options.url, true);
23
- xhrInstance.onprogress = (e) => {
24
- progress.value = Math.floor(e.loaded / e.total * 100);
25
- if (!options.isZip) {
26
- editElementContent(currentClass, `\u5F53\u524D\u4E0B\u8F7D\u8FDB\u5EA6 ${progress.value}%`);
27
- }
28
- if (progress.value == 100) {
29
- progress.value = 0;
30
- downloading = false;
31
- }
32
- };
33
- xhrInstance.onloadend = (e) => {
34
- const eventTarget = e.target;
35
- if ([200, 304].includes(eventTarget.status)) {
36
- const blob = eventTarget.response;
37
- if (!options.isZip) {
38
- createDownload(blob, options.fileName, options.fileType);
39
- index.GmNotification({
40
- type: "success",
41
- title: "\u6E29\u99A8\u63D0\u793A",
42
- message: "\u4E0B\u8F7D\u5B8C\u6210",
43
- duration: 3e3
44
- });
45
- }
46
- xhrInstance = null;
47
- resolve(blob);
48
- }
49
- };
50
- xhrInstance.onerror = function(e) {
51
- downloading = false;
52
- index.GmNotification({
53
- type: "error",
54
- title: "\u6E29\u99A8\u63D0\u793A",
55
- message: "\u4E0B\u8F7D\u5F02\u5E38\uFF0C\u8BF7\u91CD\u8BD5",
56
- duration: 1500
57
- });
58
- reject(e);
59
- };
60
- xhrInstance.send();
10
+ const getUrlFilename = (url) => {
11
+ const filename = url.split("/").pop()?.split("?")[0] || "file";
12
+ return filename;
13
+ };
14
+ const downloadByUrl = async (url, options) => {
15
+ const filename = options?.filename || getUrlFilename(url);
16
+ const curId = uuid.v4();
17
+ const modalInfo = index.GmNotification({
18
+ title: "\u4E0B\u8F7D\u4E2D",
19
+ message: vue.h("div", { className: curId }, [`\u5F53\u524D\u4E0B\u8F7D\u8FDB\u5EA6 0%`]),
20
+ duration: 0
21
+ });
22
+ await createDownload({
23
+ url,
24
+ filename,
25
+ uuid: curId
61
26
  });
27
+ modalInfo.close();
62
28
  };
63
- const downloadFile = async (options) => {
64
- try {
65
- if (downloading || !options.url || !options.fileName)
66
- return;
67
- downloading = true;
68
- const fileType = options.fileType || options.url.split(".").pop();
69
- const modalInfo = index.GmNotification({
70
- title: "\u4E0B\u8F7D\u4E2D",
71
- message: vue.h("div", { className: currentClass }, [`\u5F53\u524D\u4E0B\u8F7D\u8FDB\u5EA6 ${progress.value}%`]),
72
- duration: 0
73
- });
74
- const downblobOptions = {
75
- url: options.url,
76
- fileName: options.fileName,
77
- fileType,
78
- isZip: false
79
- };
80
- await downloadBlob(downblobOptions);
81
- modalInfo.close();
82
- } catch (error) {
83
- console.error(error);
84
- }
29
+ const downloadByRes = async (res, options) => {
30
+ const filename = options?.filename || res.headers["Content-Disposition"]?.split("filename=")[1] || "file";
31
+ const resBlob = await res.data;
32
+ const curId = uuid.v4();
33
+ const modalInfo = index.GmNotification({
34
+ title: "\u4E0B\u8F7D\u4E2D",
35
+ message: vue.h("div", { className: curId }, [`\u5F53\u524D\u4E0B\u8F7D\u8FDB\u5EA6 0%`]),
36
+ duration: 0
37
+ });
38
+ const blob = new Blob([resBlob]);
39
+ const fileUrl = window.URL.createObjectURL(blob);
40
+ await createDownload({
41
+ url: fileUrl,
42
+ filename,
43
+ uuid: curId
44
+ });
45
+ modalInfo.close();
85
46
  };
86
- const downloadZip = async (fileList, fileName) => {
47
+ const downloadToZip = async (fileList, filename) => {
87
48
  try {
88
49
  const currentContentClass = `message${Date.now()}`;
89
50
  const downloadCount = vue.ref(0);
@@ -96,28 +57,21 @@ const useDownload = () => {
96
57
  const zipInstance = new JSZip();
97
58
  for (let i = 0; i < len; i++) {
98
59
  const currentItem = fileList[i];
99
- const currentFileType = currentItem.fileType || currentItem.url.split(".").pop();
100
60
  const downblobOptions = {
101
61
  url: currentItem.url,
102
- fileName: currentItem.fileName,
103
- fileType: currentFileType,
62
+ filename: currentItem.filename ?? getUrlFilename(currentItem.url),
104
63
  isZip: true
105
64
  };
106
- downloadBlob(downblobOptions).then((res) => {
107
- zipInstance.file(`${currentItem.fileName}.${currentFileType}`, res);
65
+ createDownload(downblobOptions).then((res) => {
66
+ zipInstance.file(downblobOptions.filename, res);
108
67
  downloadCount.value++;
109
68
  editElementContent(currentContentClass, `\u603B\u4E0B\u8F7D\u6587\u4EF6\u6570\uFF1A${len}\uFF0C\u5DF2\u4E0B\u8F7D\u6587\u4EF6\u6570\uFF1A${downloadCount.value}`);
110
69
  if (downloadCount.value == len) {
111
- downloading = false;
112
- modalInfo.close();
113
- index.GmNotification({
114
- type: "success",
115
- title: "\u6E29\u99A8\u63D0\u793A",
116
- message: "\u4E0B\u8F7D\u5B8C\u6210",
117
- duration: 3e3
118
- });
70
+ editElementContent(currentContentClass, `\u6B63\u5728\u5408\u5E76...`);
119
71
  zipInstance.generateAsync({ type: "blob" }).then((res2) => {
120
- createDownload(res2, fileName, "zip");
72
+ modalInfo.close();
73
+ const fileUrl = window.URL.createObjectURL(res2);
74
+ downloadByUrl(fileUrl, { filename });
121
75
  }).catch((error) => {
122
76
  console.error(error);
123
77
  });
@@ -128,23 +82,43 @@ const useDownload = () => {
128
82
  console.error(error);
129
83
  }
130
84
  };
131
- return {
132
- downloadFile,
133
- downloadZip
85
+ const createDownload = (options) => {
86
+ return new Promise((resolve, reject) => {
87
+ const xhr = new XMLHttpRequest();
88
+ xhr.open("GET", options.url, true);
89
+ xhr.responseType = "blob";
90
+ xhr.onprogress = function(event) {
91
+ if (event.lengthComputable) {
92
+ const percentComplete = Math.floor(event.loaded / event.total * 100);
93
+ if (options.uuid) {
94
+ editElementContent(options.uuid, `\u5F53\u524D\u4E0B\u8F7D\u8FDB\u5EA6 ${percentComplete}%`);
95
+ }
96
+ }
97
+ };
98
+ xhr.onload = function(e) {
99
+ const eventTarget = e.target;
100
+ if ([200, 304].includes(eventTarget.status)) {
101
+ const blob = eventTarget.response;
102
+ if (!options.isZip) {
103
+ FileSaver.saveAs(blob, options.filename);
104
+ index.GmNotification({
105
+ type: "success",
106
+ title: "\u6E29\u99A8\u63D0\u793A",
107
+ message: "\u4E0B\u8F7D\u5B8C\u6210",
108
+ duration: 3e3
109
+ });
110
+ }
111
+ resolve(blob);
112
+ }
113
+ };
114
+ xhr.onerror = function(e) {
115
+ GmMessage.error("\u4E0B\u8F7D\u5F02\u5E38\uFF0C\u8BF7\u91CD\u8BD5");
116
+ reject(e);
117
+ };
118
+ xhr.send();
119
+ });
134
120
  };
135
- };
136
- const createDownload = (blob, fileName, fileType) => {
137
- if (!blob || !fileName || !fileType)
138
- return;
139
- const ele = document.createElement("a");
140
- const url = window.URL.createObjectURL(blob);
141
- ele.style.display = "none";
142
- ele.href = url;
143
- ele.download = `${fileName}.${fileType}`;
144
- document.body.appendChild(ele);
145
- ele.click();
146
- window.URL.revokeObjectURL(url);
147
- document.body.removeChild(ele);
121
+ return { downloadByUrl, downloadByRes, downloadToZip };
148
122
  };
149
123
  const editElementContent = (className, content) => {
150
124
  const currentEle = document.getElementsByClassName(className)[0];
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../../../../../packages/hooks/base/useDownload/index.ts"],"sourcesContent":["import { h, onBeforeUnmount, ref } from 'vue';\r\nimport GmNotification from '@giime/components/src/plugins/notification';\r\nimport JSZip from 'jszip';\r\n\r\n// 参数类型\r\nexport interface OptionsType {\r\n url: string;\r\n fileName: string;\r\n fileType?: string;\r\n}\r\n\r\ninterface DownloadOptionsType extends OptionsType {\r\n isZip: boolean;\r\n fileType: string;\r\n}\r\n\r\nexport const useDownload = () => {\r\n let xhrInstance: XMLHttpRequest | null;\r\n // 是否正在下载\r\n let downloading = false;\r\n // 单文件下载进度\r\n const progress = ref(0);\r\n // 动态生成唯一的 class\r\n const currentClass = `class${Date.now()}`;\r\n onBeforeUnmount(() => {\r\n if (xhrInstance) {\r\n xhrInstance.abort();\r\n xhrInstance = null;\r\n }\r\n });\r\n // 将 url 下载为 blob 文件\r\n const downloadBlob = (options: DownloadOptionsType) => {\r\n return new Promise<Blob>((resolve, reject) => {\r\n xhrInstance = new XMLHttpRequest();\r\n xhrInstance.responseType = 'blob';\r\n xhrInstance.open('get', options.url, true);\r\n xhrInstance.onprogress = e => {\r\n progress.value = Math.floor((e.loaded / e.total) * 100);\r\n if (!options.isZip) {\r\n editElementContent(currentClass, `当前下载进度 ${progress.value}%`);\r\n }\r\n if (progress.value == 100) {\r\n progress.value = 0;\r\n downloading = false;\r\n }\r\n };\r\n xhrInstance.onloadend = e => {\r\n const eventTarget = e.target as XMLHttpRequest;\r\n if ([200, 304].includes(eventTarget.status)) {\r\n const blob = eventTarget.response;\r\n if (!options.isZip) {\r\n createDownload(blob, options.fileName, options.fileType);\r\n GmNotification({\r\n type: 'success',\r\n title: '温馨提示',\r\n message: '下载完成',\r\n duration: 3000,\r\n });\r\n }\r\n xhrInstance = null;\r\n resolve(blob);\r\n }\r\n };\r\n xhrInstance.onerror = function (e) {\r\n downloading = false;\r\n GmNotification({\r\n type: 'error',\r\n title: '温馨提示',\r\n message: '下载异常,请重试',\r\n duration: 1500,\r\n });\r\n reject(e);\r\n };\r\n xhrInstance.send();\r\n });\r\n };\r\n // 下载单文件\r\n const downloadFile = async (options: OptionsType) => {\r\n try {\r\n if (downloading || !options.url || !options.fileName) return;\r\n downloading = true;\r\n const fileType = options.fileType || options.url.split('.').pop()!;\r\n const modalInfo = GmNotification({\r\n title: '下载中',\r\n message: h('div', { className: currentClass }, [`当前下载进度 ${progress.value}%`]),\r\n duration: 0,\r\n });\r\n const downblobOptions = {\r\n url: options.url,\r\n fileName: options.fileName,\r\n fileType,\r\n isZip: false,\r\n };\r\n await downloadBlob(downblobOptions);\r\n modalInfo.close();\r\n } catch (error) {\r\n console.error(error);\r\n }\r\n };\r\n // 下载多文件 转为zip\r\n const downloadZip = async (fileList: OptionsType[], fileName: string) => {\r\n try {\r\n const currentContentClass = `message${Date.now()}`;\r\n const downloadCount = ref(0);\r\n const len = fileList.length;\r\n const modalInfo = GmNotification({\r\n title: '下载中',\r\n message: h('div', [h('div', { className: currentContentClass }, [`总下载文件数:${len},已下载文件数:${downloadCount.value}`])]),\r\n duration: 0,\r\n });\r\n // h('div', { className: currentClass }, [`当前下载进度 ${progress.value}%`]),\r\n const zipInstance = new JSZip();\r\n for (let i = 0; i < len; i++) {\r\n const currentItem = fileList[i];\r\n const currentFileType = currentItem.fileType || currentItem.url.split('.').pop()!;\r\n // const fileBlob = await\r\n const downblobOptions = {\r\n url: currentItem.url,\r\n fileName: currentItem.fileName,\r\n fileType: currentFileType,\r\n isZip: true,\r\n };\r\n downloadBlob(downblobOptions).then(res => {\r\n zipInstance.file(`${currentItem.fileName}.${currentFileType}`, res);\r\n downloadCount.value++;\r\n editElementContent(currentContentClass, `总下载文件数:${len},已下载文件数:${downloadCount.value}`);\r\n if (downloadCount.value == len) {\r\n downloading = false;\r\n modalInfo.close();\r\n GmNotification({\r\n type: 'success',\r\n title: '温馨提示',\r\n message: '下载完成',\r\n duration: 3000,\r\n });\r\n zipInstance\r\n .generateAsync({ type: 'blob' })\r\n .then(res => {\r\n createDownload(res, fileName, 'zip');\r\n })\r\n .catch(error => {\r\n console.error(error);\r\n });\r\n }\r\n });\r\n }\r\n } catch (error) {\r\n console.error(error);\r\n }\r\n };\r\n\r\n return {\r\n downloadFile,\r\n downloadZip,\r\n };\r\n};\r\n// 创建下载链接 并下载\r\nconst createDownload = (blob: Blob, fileName: string, fileType: string) => {\r\n if (!blob || !fileName || !fileType) return;\r\n const ele = document.createElement('a');\r\n const url = window.URL.createObjectURL(blob);\r\n ele.style.display = 'none';\r\n ele.href = url;\r\n ele.download = `${fileName}.${fileType}`;\r\n document.body.appendChild(ele);\r\n ele.click();\r\n window.URL.revokeObjectURL(url);\r\n document.body.removeChild(ele);\r\n};\r\n\r\n// 动态更新 notify 中的message 信息\r\nconst editElementContent = (className: string, content: string) => {\r\n // eslint-disable-next-line unicorn/prefer-query-selector\r\n const currentEle = document.getElementsByClassName(className)[0];\r\n if (currentEle) {\r\n currentEle.innerHTML = content;\r\n }\r\n};\r\n"],"names":["ref","onBeforeUnmount","GmNotification","h","res"],"mappings":";;;;;;AAgBO,MAAM,cAAc,MAAM;AAC/B,EAAI,IAAA,WAAA,CAAA;AAEJ,EAAA,IAAI,WAAc,GAAA,KAAA,CAAA;AAElB,EAAM,MAAA,QAAA,GAAWA,QAAI,CAAC,CAAA,CAAA;AAEtB,EAAA,MAAM,YAAe,GAAA,CAAA,KAAA,EAAQ,IAAK,CAAA,GAAA,EAAK,CAAA,CAAA,CAAA;AACvC,EAAAC,mBAAA,CAAgB,MAAM;AACpB,IAAA,IAAI,WAAa,EAAA;AACf,MAAA,WAAA,CAAY,KAAM,EAAA,CAAA;AAClB,MAAc,WAAA,GAAA,IAAA,CAAA;AAAA,KAChB;AAAA,GACD,CAAA,CAAA;AAED,EAAM,MAAA,YAAA,GAAe,CAAC,OAAiC,KAAA;AACrD,IAAA,OAAO,IAAI,OAAA,CAAc,CAAC,OAAA,EAAS,MAAW,KAAA;AAC5C,MAAA,WAAA,GAAc,IAAI,cAAe,EAAA,CAAA;AACjC,MAAA,WAAA,CAAY,YAAe,GAAA,MAAA,CAAA;AAC3B,MAAA,WAAA,CAAY,IAAK,CAAA,KAAA,EAAO,OAAQ,CAAA,GAAA,EAAK,IAAI,CAAA,CAAA;AACzC,MAAA,WAAA,CAAY,aAAa,CAA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A,CAAA;AACtC,EAAA,MAAM,GAAM,GAAA,MAAA,CAAO,GAAI,CAAA,eAAA,CAAgB,IAAI,CAAA,CAAA;AAC3C,EAAA,GAAA,CAAI,MAAM,OAAU,GAAA,MAAA,CAAA;AACpB,EAAA,GAAA,CAAI,IAAO,GAAA,GAAA,CAAA;AACX,EAAA,GAAA,CAAI,QAAW,GAAA,CAAA,EAAG,QAAQ,CAAA,CAAA,EAAI,QAAQ,CAAA,CAAA,CAAA;AACtC,EAAS,QAAA,CAAA,IAAA,CAAK,YAAY,GAAG,CAAA,CAAA;AAC7B,EAAA,GAAA,CAAI,KAAM,EAAA,CAAA;AACV,EAAO,MAAA,CAAA,GAAA,CAAI,gBAAgB,GAAG,CAAA,CAAA;AAC9B,EAAS,QAAA,CAAA,IAAA,CAAK,YAAY,GAAG,CAAA,CAAA;AAC/B,CAAA,CAAA;AAGA,MAAM,kBAAA,GAAqB,CAAC,SAAA,EAAmB,OAAoB,KAAA;AAEjE,EAAA,MAAM,UAAa,GAAA,QAAA,CAAS,sBAAuB,CAAA,SAAS,EAAE,CAAC,CAAA,CAAA;AAC/D,EAAA,IAAI,UAAY,EAAA;AACd,IAAA,UAAA,CAAW,SAAY,GAAA,OAAA,CAAA;AAAA,GACzB;AACF,CAAA;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../../../../../../packages/hooks/base/useDownload/index.ts"],"sourcesContent":["import { h, ref } from 'vue';\r\nimport FileSaver from 'file-saver';\r\n\r\nimport GmNotification from '@giime/components/src/plugins/notification';\r\nimport { v4 as uuidv4 } from 'uuid';\r\nimport JSZip from 'jszip';\r\nimport type { AxiosResponse } from 'axios';\r\n\r\nexport interface DownloadOptions {\r\n filename?: string;\r\n}\r\nexport interface DateownloadZipItem {\r\n url: string;\r\n filename?: string;\r\n}\r\n\r\nexport const useDownload = () => {\r\n const getUrlFilename = (url: string) => {\r\n const filename = url.split('/').pop()?.split('?')[0] || 'file';\r\n return filename;\r\n };\r\n /**\r\n * 通过url下载\r\n * @param url\r\n * @param options\r\n */\r\n const downloadByUrl = async (url: string, options?: DownloadOptions) => {\r\n const filename = options?.filename || getUrlFilename(url);\r\n const curId = uuidv4();\r\n const modalInfo = GmNotification({\r\n title: '下载中',\r\n message: h('div', { className: curId }, [`当前下载进度 0%`]),\r\n duration: 0,\r\n });\r\n await createDownload({\r\n url,\r\n filename,\r\n uuid: curId,\r\n });\r\n modalInfo.close();\r\n };\r\n /**\r\n * 通过接口响应结果下载\r\n * @param res\r\n * @param options\r\n */\r\n const downloadByRes = async (res: AxiosResponse<any, any>, options?: DownloadOptions) => {\r\n const filename = options?.filename || res.headers['Content-Disposition']?.split('filename=')[1] || 'file';\r\n const resBlob = await res.data;\r\n const curId = uuidv4();\r\n const modalInfo = GmNotification({\r\n title: '下载中',\r\n message: h('div', { className: curId }, [`当前下载进度 0%`]),\r\n duration: 0,\r\n });\r\n // 设置type类型\r\n const blob = new Blob([resBlob]);\r\n const fileUrl = window.URL.createObjectURL(blob);\r\n await createDownload({\r\n url: fileUrl,\r\n filename,\r\n uuid: curId,\r\n });\r\n modalInfo.close();\r\n };\r\n /**\r\n * 下载多文件 转为zip\r\n * @param fileList\r\n * @param filename\r\n */\r\n const downloadToZip = async (fileList: DateownloadZipItem[], filename: string) => {\r\n try {\r\n const currentContentClass = `message${Date.now()}`;\r\n const downloadCount = ref(0);\r\n const len = fileList.length;\r\n const modalInfo = GmNotification({\r\n title: '下载中',\r\n message: h('div', [h('div', { className: currentContentClass }, [`总下载文件数:${len},已下载文件数:${downloadCount.value}`])]),\r\n duration: 0,\r\n });\r\n // h('div', { className: currentClass }, [`当前下载进度 ${progress.value}%`]),\r\n const zipInstance = new JSZip();\r\n for (let i = 0; i < len; i++) {\r\n const currentItem = fileList[i];\r\n // const fileBlob = await\r\n const downblobOptions = {\r\n url: currentItem.url,\r\n filename: currentItem.filename ?? getUrlFilename(currentItem.url),\r\n isZip: true,\r\n };\r\n createDownload(downblobOptions).then(res => {\r\n zipInstance.file(downblobOptions.filename, res);\r\n downloadCount.value++;\r\n editElementContent(currentContentClass, `总下载文件数:${len},已下载文件数:${downloadCount.value}`);\r\n if (downloadCount.value == len) {\r\n editElementContent(currentContentClass, `正在合并...`);\r\n\r\n zipInstance\r\n .generateAsync({ type: 'blob' })\r\n .then(res => {\r\n modalInfo.close();\r\n\r\n const fileUrl = window.URL.createObjectURL(res);\r\n // FileSaver.saveAs(res, filename);\r\n downloadByUrl(fileUrl, { filename });\r\n })\r\n .catch(error => {\r\n console.error(error);\r\n });\r\n }\r\n });\r\n }\r\n } catch (error) {\r\n console.error(error);\r\n }\r\n };\r\n const createDownload = (options: { url: string; filename: string; uuid?: string; isZip?: boolean }) => {\r\n return new Promise<Blob>((resolve, reject) => {\r\n // 创建一个新的 XMLHttpRequest 对象\r\n const xhr = new XMLHttpRequest();\r\n // 初始化一个GET请求\r\n xhr.open('GET', options.url, true);\r\n xhr.responseType = 'blob'; // 设置响应类型为blob,以便处理二进制文件\r\n\r\n // 监听 progress 事件\r\n xhr.onprogress = function (event) {\r\n if (event.lengthComputable) {\r\n const percentComplete = Math.floor((event.loaded / event.total) * 100);\r\n if (options.uuid) {\r\n editElementContent(options.uuid, `当前下载进度 ${percentComplete}%`);\r\n }\r\n }\r\n };\r\n\r\n // 监听 load 事件,表示下载完成\r\n xhr.onload = function (e) {\r\n const eventTarget = e.target as XMLHttpRequest;\r\n if ([200, 304].includes(eventTarget.status)) {\r\n const blob = eventTarget.response;\r\n // const url = window.URL.createObjectURL(blob);\r\n if (!options.isZip) {\r\n FileSaver.saveAs(blob, options.filename);\r\n GmNotification({\r\n type: 'success',\r\n title: '温馨提示',\r\n message: '下载完成',\r\n duration: 3000,\r\n });\r\n }\r\n\r\n resolve(blob);\r\n }\r\n };\r\n\r\n // 监听 error 事件\r\n xhr.onerror = function (e) {\r\n GmMessage.error('下载异常,请重试');\r\n reject(e);\r\n };\r\n\r\n // 发送请求\r\n xhr.send();\r\n });\r\n };\r\n return { downloadByUrl, downloadByRes, downloadToZip };\r\n};\r\n\r\n// 动态更新 notify 中的message 信息\r\nconst editElementContent = (className: string, content: string) => {\r\n // eslint-disable-next-line unicorn/prefer-query-selector\r\n const currentEle = document.getElementsByClassName(className)[0];\r\n if (currentEle) {\r\n currentEle.innerHTML = content;\r\n }\r\n};\r\n"],"names":["uuidv4","GmNotification","h","ref","res"],"mappings":";;;;;;;;AAgBO,MAAM,cAAc,MAAM;AAC/B,EAAM,MAAA,cAAA,GAAiB,CAAC,GAAgB,KAAA;AACtC,IAAM,MAAA,QAAA,GAAW,GAAI,CAAA,KAAA,CAAM,GAAG,CAAA,CAAE,GAAI,EAAA,EAAG,KAAM,CAAA,GAAG,CAAE,CAAA,CAAC,CAAK,IAAA,MAAA,CAAA;AACxD,IAAO,OAAA,QAAA,CAAA;AAAA,GACT,CAAA;AAMA,EAAM,MAAA,aAAA,GAAgB,OAAO,GAAA,EAAa,OAA8B,KAAA;AACtE,IAAA,MAAM,QAAW,GAAA,OAAA,EAAS,QAAY,IAAA,cAAA,CAAe,GAAG,CAAA,CAAA;AACxD,IAAA,MAAM,QAAQA,OAAO,EAAA,CAAA;AACrB,IAAA,MAAM,YAAYC,oBAAe,CAAA;AAAA,MAC/B,KAAO,EAAA,oBAAA;AAAA,MACP,OAAA,EAASC,MAAE,KAAO,EAAA,EAAE,WAAW,KAAM,EAAA,EAAG,CAAC,CAAA,uCAAA,CAAW,CAAC,CAAA;AAAA,MACrD,QAAU,EAAA,CAAA;AAAA,KACX,CAAA,CAAA;AACD,IAAA,MAAM,cAAe,CAAA;AAAA,MACnB,GAAA;AAAA,MACA,QAAA;AAAA,MACA,IAAM,EAAA,KAAA;AAAA,KACP,CAAA,CAAA;AACD,IAAA,SAAA,CAAU,KAAM,EAAA,CAAA;AAAA,GAClB,CAAA;AAMA,EAAM,MAAA,aAAA,GAAgB,OAAO,GAAA,EAA8B,OAA8B,KAAA;AACvF,IAAM,MAAA,QAAA,GAAW,OAAS,EAAA,QAAA,IAAY,GAAI,CAA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AAA,MAAM,OAAO,WAAY,CAAA,QAAA,CAAA;AAEzB,UAAI,IAAA,CAAC,QAAQ,KAAO,EAAA;AAClB,YAAU,SAAA,CAAA,MAAA,CAAO,IAAM,EAAA,OAAA,CAAQ,QAAQ,CAAA,CAAA;AACvC,YAAeH,oBAAA,CAAA;AAAA,cACb,IAAM,EAAA,SAAA;AAAA,cACN,KAAO,EAAA,0BAAA;AAAA,cACP,OAAS,EAAA,0BAAA;AAAA,cACT,QAAU,EAAA,GAAA;AAAA,aACX,CAAA,CAAA;AAAA,WACH;AAEA,UAAA,OAAA,CAAQ,IAAI,CAAA,CAAA;AAAA,SACd;AAAA,OACF,CAAA;AAGA,MAAI,GAAA,CAAA,OAAA,GAAU,SAAU,CAAG,EAAA;AACzB,QAAA,SAAA,CAAU,MAAM,kDAAU,CAAA,CAAA;AAC1B,QAAA,MAAA,CAAO,CAAC,CAAA,CAAA;AAAA,OACV,CAAA;AAGA,MAAA,GAAA,CAAI,IAAK,EAAA,CAAA;AAAA,KACV,CAAA,CAAA;AAAA,GACH,CAAA;AACA,EAAO,OAAA,EAAE,aAAe,EAAA,aAAA,EAAe,aAAc,EAAA,CAAA;AACvD,EAAA;AAGA,MAAM,kBAAA,GAAqB,CAAC,SAAA,EAAmB,OAAoB,KAAA;AAEjE,EAAA,MAAM,UAAa,GAAA,QAAA,CAAS,sBAAuB,CAAA,SAAS,EAAE,CAAC,CAAA,CAAA;AAC/D,EAAA,IAAI,UAAY,EAAA;AACd,IAAA,UAAA,CAAW,SAAY,GAAA,OAAA,CAAA;AAAA,GACzB;AACF,CAAA;;;;"}
package/lib/index.css CHANGED
@@ -1,7 +1,3 @@
1
- [data-v-512cf874] .gmSearchForm .el-form-item {
2
- margin-right: 8px;
3
- margin-bottom: 8px;
4
- }
5
1
  .gm-flex-center[data-v-d1c6dac0] {
6
2
  align-items: center;
7
3
  }
@@ -9,27 +5,31 @@
9
5
  .gm-flex-justify-between[data-v-d1c6dac0] {
10
6
  justify-content: space-between;
11
7
  }
12
- [data-v-9f72778f] .gmTableNoBorder .el-table__body .el-table__cell {
8
+ [data-v-512cf874] .gmSearchForm .el-form-item {
9
+ margin-right: 8px;
10
+ margin-bottom: 8px;
11
+ }
12
+ [data-v-e38c9d81] .gmTableNoBorder .el-table__body .el-table__cell {
13
13
  border-right: none;
14
14
  }
15
15
 
16
- [data-v-9f72778f] .gmTableNoBorder .el-table__inner-wrapper:before {
16
+ [data-v-e38c9d81] .gmTableNoBorder .el-table__inner-wrapper:before {
17
17
  height: 0;
18
18
  }
19
19
 
20
- [data-v-9f72778f] .gmTableNoBorder.el-table--border .el-table__inner-wrapper:after {
20
+ [data-v-e38c9d81] .gmTableNoBorder.el-table--border .el-table__inner-wrapper:after {
21
21
  height: 0;
22
22
  }
23
23
 
24
- [data-v-9f72778f] .gmTableNoBorder.el-table--border:after {
24
+ [data-v-e38c9d81] .gmTableNoBorder.el-table--border:after {
25
25
  height: 0;
26
26
  }
27
27
 
28
- [data-v-9f72778f] .gmTableNoBorder.el-table--border:before {
28
+ [data-v-e38c9d81] .gmTableNoBorder.el-table--border:before {
29
29
  height: 0;
30
30
  }
31
31
 
32
- [data-v-9f72778f] .gmTableNoBorder .el-table__border-left-patch {
32
+ [data-v-e38c9d81] .gmTableNoBorder .el-table__border-left-patch {
33
33
  height: 0;
34
34
  }
35
35
  /*
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "giime",
3
- "version": "0.0.30",
3
+ "version": "0.1.0",
4
4
  "description": "A Component Library for Vue 3",
5
5
  "keywords": [
6
6
  "giime",
@@ -63,6 +63,8 @@
63
63
  "clipboard": "^2.0.11",
64
64
  "uuid": "^9.0.1",
65
65
  "jszip": "^3.10.1",
66
+ "file-saver": "^2.0.5",
67
+ "@types/file-saver": "^2.0.7",
66
68
  "@types/lodash": "^4.17.0",
67
69
  "@types/lodash-es": "^4.17.12"
68
70
  },