Skip to content

Commit

Permalink
Merge pull request #22 from GarinZ/v1.0.9
Browse files Browse the repository at this point in the history
V1.0.9
  • Loading branch information
GarinZ authored Apr 8, 2023
2 parents 5c2d9be + fe4d718 commit 4e0bf2e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "link-map",
"displayName": "Link Map",
"version": "1.0.8",
"version": "1.0.9",
"browserslist": "Chrome >= 96",
"description": "Vertical Tabs Sidebar, But In Tree Structure",
"author": "Garin",
Expand Down
102 changes: 43 additions & 59 deletions src/tree/features/settings/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { sendMessage } from '@garinz/webext-bridge';
import { Button, message, Modal, Select } from 'antd';
import { Button, message, Modal, Select, Upload } from 'antd';
import type { RcFile } from 'antd/es/upload/interface';
import log from 'loglevel';
import { useContext, useState } from 'react';
import browser from 'webextension-polyfill';
Expand Down Expand Up @@ -44,61 +45,30 @@ const Settings = () => {
setIsModalOpen(false);
};

const handleImport = async () => {
try {
if (!window.showOpenFilePicker || !window.showSaveFilePicker) {
message.error('Your browser does not support this feature.');
return;
}
const [fileHandle] = await window.showOpenFilePicker({
types: [
{
description: 'JSON files',
accept: { 'application/json': ['.json'] },
},
],
});
const file = await fileHandle.getFile();
const fileContent = await file.text();
const jsonData: ExportJsonData = JSON.parse(fileContent);
// TODO: 这里需要做一些数据校验
log.debug('import-data', jsonData);
await sendMessage('import-data', jsonData);
hideModal();
} catch {
// do nothing
}
const handleLinkMapImport = async (file: RcFile) => {
const fileContent = await file.text();
const jsonData: ExportJsonData = JSON.parse(fileContent);
// TODO: 这里需要做一些数据校验
log.debug('import-data', jsonData);
await sendMessage('import-data', jsonData);
hideModal();
return '';
};

const handleTabOutlinerImport = async () => {
try {
if (!window.showOpenFilePicker || !window.showSaveFilePicker) {
message.error('Your browser does not support this feature.');
return;
}
const [fileHandle] = await window.showOpenFilePicker({
types: [
{
description: 'JSON files',
accept: { 'application/json': ['.tree'] },
},
],
});
const file = await fileHandle.getFile();
const fileContent = await file.text();
const jsonData = JSON.parse(fileContent);
if (
!Array.isArray(jsonData) ||
!jsonData[jsonData.length - 1].type ||
jsonData[jsonData.length - 1].type !== 11111
) {
message.error('Invalid Tab Outliner data.');
}
await sendMessage('import-tabOutliner-data', jsonData);
hideModal();
} catch {
// do nothing
const handleTabOutlinerImport = async (file: RcFile) => {
const fileContent = await file.text();
const jsonData = JSON.parse(fileContent);
if (
!Array.isArray(jsonData) ||
!jsonData[jsonData.length - 1].type ||
jsonData[jsonData.length - 1].type !== 11111
) {
message.error('Invalid Tab Outliner data.');
return '';
}
await sendMessage('import-tabOutliner-data', jsonData);
hideModal();
return '';
};

const handleThemeChange = async (value: ThemeType) => {
Expand Down Expand Up @@ -156,14 +126,28 @@ const Settings = () => {
</Button>
</div>
<div className="settings-item">
<Button className="setting-item-btn" onClick={handleImport}>
{browser.i18n.getMessage('importLinkMapData')}
</Button>
<Upload
name={'file'}
action={handleLinkMapImport}
fileList={[]}
accept={'.json'}
>
<Button className="setting-item-btn">
{browser.i18n.getMessage('importLinkMapData')}
</Button>
</Upload>
</div>
<div className="settings-item">
<Button className="setting-item-btn" onClick={handleTabOutlinerImport}>
{browser.i18n.getMessage('importTabOutlinerData')}
</Button>
<Upload
name={'file'}
action={handleTabOutlinerImport}
fileList={[]}
accept={'.json, .tree'}
>
<Button className="setting-item-btn">
{browser.i18n.getMessage('importTabOutlinerData')}
</Button>
</Upload>
</div>
</div>
<div className={'setting-section'}>
Expand Down

0 comments on commit 4e0bf2e

Please sign in to comment.