Skip to content

Commit

Permalink
feat: export ranges as TSV file
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Jul 19, 2024
1 parent 9c59558 commit cea8135
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"multiplet-analysis": "^2.1.2",
"nmr-correlation": "^2.3.3",
"nmr-load-save": "^0.34.2",
"nmr-processing": "^12.7.1",
"nmr-processing": "^12.8.0",
"nmredata": "^0.9.11",
"numeral": "^2.0.6",
"openchemlib": "^8.14.0",
Expand Down
71 changes: 65 additions & 6 deletions src/component/panels/RangesPanel/RangesHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ import {
SvgNmrPeaks,
SvgNmrPeaksTopLabels,
} from 'cheminfo-font';
import { saveAs } from 'file-saver';
import lodashGet from 'lodash/get';
import { RangesViewState } from 'nmr-load-save';
import { rangesToACS } from 'nmr-processing';
import { FaFileExport, FaUnlink, FaSitemap, FaChartBar } from 'react-icons/fa';
import { rangesToACS, rangesToTSV } from 'nmr-processing';
import {
FaFileExport,
FaUnlink,
FaSitemap,
FaChartBar,
FaCopy,
FaDownload,
} from 'react-icons/fa';
import { ImLink } from 'react-icons/im';
import { LuSubtitles } from 'react-icons/lu';

Expand All @@ -15,6 +23,10 @@ import { useClipboard } from '../../../utils/clipboard/clipboardHooks';
import { useAssignmentData } from '../../assignment/AssignmentsContext';
import { useDispatch } from '../../context/DispatchContext';
import { useToaster } from '../../context/ToasterContext';
import {
ToolbarPopoverItem,
ToolbarPopoverMenuItem,
} from '../../elements/ToolbarPopoverItem';
import { useModal } from '../../elements/popup/Modal';
import { useActiveSpectrumRangesViewState } from '../../hooks/useActiveSpectrumRangesViewState';
import { usePanelPreferences } from '../../hooks/usePanelPreferences';
Expand All @@ -24,6 +36,29 @@ import { booleanToString } from '../../utility/booleanToString';
import { FilterType } from '../../utility/filterType';
import DefaultPanelHeader from '../header/DefaultPanelHeader';

type ExportRangesType = 'publicationString' | 'rangesToTSV';
interface ExportData {
id: ExportRangesType;
}
type ExportItem = ToolbarPopoverMenuItem<ExportData>;

const EXPORT_MENU: ExportItem[] = [
{
icon: <FaCopy />,
text: 'Preview publication string',
data: {
id: 'publicationString',
},
},
{
icon: <FaDownload />,
text: 'Export ranges as TSV',
data: {
id: 'rangesToTSV',
},
},
] as const;

function RangesHeader({
ranges,
info,
Expand Down Expand Up @@ -162,6 +197,25 @@ function RangesHeader({
dispatch({ type: 'TOGGLE_RANGES_PEAKS_DISPLAYING_MODE' });
}

function handleRangesToTSV() {
const tsv = rangesToTSV(ranges.values);
const blob = new Blob([tsv], { type: 'text/plain' });
saveAs(blob, `${info.name}.tsv`);
}

function exportHandler(data?: ExportData) {
switch (data?.id) {
case 'publicationString':
saveAsHTMLHandler();
break;
case 'rangesToTSV':
handleRangesToTSV();
break;
default:
break;
}
}

const hasRanges = Array.isArray(ranges?.values) && ranges.values.length > 0;
const total = ranges?.values?.length || 0;
return (
Expand All @@ -178,10 +232,15 @@ function RangesHeader({
onSettingClick={onSettingClick}
leftButtons={[
{
disabled: !hasRanges,
icon: <FaFileExport />,
tooltip: 'Preview publication string',
onClick: saveAsHTMLHandler,
component: (
<ToolbarPopoverItem<ExportData>
disabled={!hasRanges}
icon={<FaFileExport />}
tooltip="Export as"
options={EXPORT_MENU}
onClick={exportHandler}
/>
),
},
{
component: (
Expand Down

0 comments on commit cea8135

Please sign in to comment.