Skip to content

Commit 730d34f

Browse files
committed
feat: support export csv in expand menu
1 parent 425cd78 commit 730d34f

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

pages/content/lib/index.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { Injector } from '@wendellhu/redi';
22
import type { IMessage, PushStorageMessage, SetStorageMessage } from '@univer-clipsheet-core/shared';
33
import { ClipsheetMessageTypeEnum, IframeViewTypeEnum, listenPingSignal, PingSignalKeyEnum, promisifyMessage, requestDataSource, sendSetIframeViewMessage } from '@univer-clipsheet-core/shared';
44
import { ClientController, ClientViewService, CoverService, DetectTablesService, ElementInspectService, IframeViewController, RemountObserver, ScraperClientService, TableScrapingShadowComponent } from '@univer-clipsheet-core/ui';
5-
import type { IGetTableRecordsParams, IPreviewSheetStorageValue, ITableRecordsResponse } from '@univer-clipsheet-core/table';
6-
import { PreviewSheetFromEnum, TableDataSourceKeyEnum, TableStorageKeyEnum } from '@univer-clipsheet-core/table';
5+
import type { IGetTableRecordsParams, IInitialSheet, IPreviewSheetStorageValue, ITableRecord, ITableRecordsResponse } from '@univer-clipsheet-core/table';
6+
import { getCellValue, PreviewSheetFromEnum, TableDataSourceKeyEnum, TableStorageKeyEnum } from '@univer-clipsheet-core/table';
7+
import { saveAs } from 'file-saver';
8+
import Papa from 'papaparse';
79

810
// You can use startAjaxIntercept to intercept AJAX requests
911
// startAjaxIntercept(chrome.runtime.getURL('ajax-interceptor/index.iife.js'), (res) => {
@@ -103,3 +105,32 @@ listenPingSignal(PingSignalKeyEnum.ScraperFormShowed, () => {
103105
});
104106
});
105107

108+
chrome.runtime.onMessage.addListener(async (req, _sender, _sendResponse) => {
109+
if (req.type === 'EXPORT_CSV') {
110+
const tableRecord = req.payload as ITableRecord;
111+
const key = TableStorageKeyEnum.TableSheetsPrefix + tableRecord.id;
112+
chrome.runtime.sendMessage({
113+
type: ClipsheetMessageTypeEnum.GetStorage,
114+
payload: key,
115+
});
116+
const res = await promisifyMessage<PushStorageMessage>((msg) => msg.type === ClipsheetMessageTypeEnum.PushStorage && msg.payload.key === key);
117+
118+
if (!Array.isArray(res.payload.value)) {
119+
return;
120+
}
121+
const sheet = res.payload.value[0] as IInitialSheet;
122+
if (!sheet) {
123+
return;
124+
}
125+
const rows = sheet.rows;
126+
127+
const rowValues = rows.map((row) => row.cells.map(getCellValue));
128+
const matrix = [sheet.columnName].concat(rowValues);
129+
const csv = Papa.unparse(matrix);
130+
131+
const blob = new Blob([`\uFEFF${csv}`], { type: 'text/csv;charset=utf-8' });
132+
133+
saveAs(blob, `${tableRecord?.title}.csv`);
134+
}
135+
});
136+

pages/content/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"type-check": "tsc --noEmit"
1616
},
1717
"dependencies": {
18-
1918
"@univer-clipsheet-core/ajax-intercept": "workspace:*",
2019
"@univer-clipsheet-core/scraper": "workspace:*",
2120
"@univer-clipsheet-core/shared": "workspace:*",
@@ -24,6 +23,8 @@
2423
"@univer-clipsheet/shared": "workspace:*",
2524
"@wendellhu/redi": "0.16.1",
2625
"debounce": "2.1.0",
26+
"file-saver": "2.0.5",
27+
"papaparse": "5.4.1",
2728
"rc-checkbox": "3.3.0",
2829
"rc-select": "14.15.2",
2930
"rc-switch": "4.1.0",

pages/popup/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"type-check": "tsc --noEmit"
1616
},
1717
"dependencies": {
18-
1918
"@heroicons/react": "2.1.3",
2019
"@univer-clipsheet-core/locale": "workspace:*",
2120
"@univer-clipsheet-core/shared": "workspace:*",

pages/popup/src/MyPopup.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import '@src/index.css';
22
import { isZhCN, t } from '@univer-clipsheet-core/locale';
33
import type { SetStorageMessage } from '@univer-clipsheet-core/shared';
4-
import { ClipsheetMessageTypeEnum, closePopup, IframeViewTypeEnum, sendSetIframeViewMessage } from '@univer-clipsheet-core/shared';
4+
import { ClipsheetMessageTypeEnum, closePopup, getActiveTabId, IframeViewTypeEnum, sendSetIframeViewMessage } from '@univer-clipsheet-core/shared';
55
import { deleteTaskRecord, TableStorageKeyEnum, triggerRecordTypes } from '@univer-clipsheet-core/table';
66
import type { DropdownMenuItem } from '@univer-clipsheet-core/ui';
77
import { DropdownMenu, getWorkflowColumnsByTable, openWorkflowDialog, Popup, PopupViewService, Tooltip } from '@univer-clipsheet-core/ui';
@@ -20,6 +20,7 @@ import {
2020
enum MoreMenuKey {
2121
Schedule = 'schedule',
2222
Delete = 'delete',
23+
ExportCSV = 'export_csv',
2324
}
2425

2526
enum ContactMenuKey {
@@ -35,7 +36,7 @@ function navigateToGithub() {
3536

3637
export function MyPopup() {
3738
const service = useMemo(() => {
38-
const popupViewService = new PopupViewService<string>();
39+
const popupViewService = new PopupViewService();
3940

4041
popupViewService.onTableMoreMenuClick(async (key, record) => {
4142
if (key === MoreMenuKey.Delete) {
@@ -48,6 +49,13 @@ export function MyPopup() {
4849
columns,
4950
});
5051
}
52+
if (key === MoreMenuKey.ExportCSV) {
53+
const activeTabId = await getActiveTabId();
54+
activeTabId && chrome.tabs.sendMessage(activeTabId, {
55+
type: 'EXPORT_CSV',
56+
payload: record,
57+
});
58+
}
5159
});
5260

5361
popupViewService.onTableRecordClick((record) => {
@@ -80,6 +88,10 @@ export function MyPopup() {
8088
text: t('ScheduleDataUpdate'),
8189
key: MoreMenuKey.Schedule,
8290
},
91+
{
92+
text: t('ExportAsWith', { text: 'CSV' }),
93+
key: MoreMenuKey.ExportCSV,
94+
},
8395
{
8496
text: <span className="text-[#F05252]">{t('Delete')}</span>,
8597
key: MoreMenuKey.Delete,

pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)