Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: choose between exporting with or without annotations #137

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
140 changes: 140 additions & 0 deletions src/components/modal/ExportClipboardModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import {
Checkbox,
Dialog,
DialogBody,
DialogFooter,
Position,
OverlayToaster,
} from '@blueprintjs/core';
import styled from '@emotion/styled';
import { memo, useCallback, useMemo, useState } from 'react';
import { Button } from 'react-science/ui';

import useLog from '../../hooks/useLog';
import useModal from '../../hooks/useModal';
import useOriginalFilteredROIs from '../../hooks/useOriginalFilteredROIs';
import { saveToClipboard } from '../../utils/export';
import { useMergeToImage } from '../tool/ExportTool';

import StyledModalBody from './utils/StyledModalBody';

const ExportStyle = styled.div`
display: flex;
flex-direction: column;
width: 500px;

.header {
font-size: 1.25rem;
}

.container {
padding: 20px;
}
`;

const SaveButtonInner = styled.span`
display: flex;
align-items: center;
`;

const AppToaster = OverlayToaster.create({
position: Position.TOP,
});

type ExportClipboardModalProps = {
previewImageIdentifier: string;
};

function ExportClipboardModal({
previewImageIdentifier,
}: ExportClipboardModalProps) {
const { isOpen, close } = useModal('exportClipboard');
const { logger } = useLog();

const ROIs = useOriginalFilteredROIs(previewImageIdentifier);
const hasAnnotations = ROIs.length > 0;

const defaultFormState = useMemo(
() => ({
annotations: true,
}),
[],
);

const [formState, setFormState] = useState(defaultFormState);
const resetForm = useCallback(
() => setFormState(defaultFormState),
[setFormState, defaultFormState],
);

const mergeToImage = useMergeToImage(formState.annotations);

const copyToClipboard = useCallback(() => {
return mergeToImage().then(({ toSave }) => saveToClipboard(toSave));
}, [mergeToImage]);

const save = useCallback(() => {
copyToClipboard()
.catch((error) => {
logger.error(`Failed to copy to clipboard: ${error}`);
AppToaster.show({
message: 'Failed to copy to clipboard',
intent: 'danger',
timeout: 1500,
});
})
.finally(() => {
resetForm();
close();
});
AppToaster.show({
message: 'Copied successfully !',
intent: 'success',
timeout: 1500,
});
}, [close, copyToClipboard, logger, resetForm]);

if (!hasAnnotations) {
save();
return null;
}
Comment on lines +97 to +100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion was not just to remove the effect (it was correct to use an effect in that case), but to move this logic up to the component that handles the export options, which will prevent having to use an effect because then it can be called in an event callback.


return (
<Dialog
title="Copy to clipboard"
isOpen={isOpen}
onClose={close}
style={{ width: 'fit-content' }}
>
<ExportStyle>
<DialogBody>
<StyledModalBody>
<div>
<Checkbox
label="Include annotations"
alignIndicator="right"
checked={formState.annotations}
onChange={(e) =>
setFormState({
...formState,
annotations: e.target.checked,
})
}
/>
</div>
</StyledModalBody>
</DialogBody>
<DialogFooter
minimal
actions={
<Button intent="primary" onClick={save}>
<SaveButtonInner>Copy</SaveButtonInner>
</Button>
}
/>
</ExportStyle>
</Dialog>
);
}

export default memo(ExportClipboardModal);
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const SaveButtonInner = styled.span`
align-items: center;
`;

function ExportModal() {
const { isOpen, close } = useModal('export');
function ExportPixeliumModal() {
const { isOpen, close } = useModal('exportPixelium');
const data = useData();
const preferences = usePreferences();
const view = useView();
Expand All @@ -48,9 +48,9 @@ function ExportModal() {
const defaultFormState = useMemo(
() => ({
name: '',
view: false,
preferences: false,
data: false,
view: true,
preferences: true,
data: true,
}),
[],
);
Expand Down Expand Up @@ -158,4 +158,4 @@ function ExportModal() {
);
}

export default memo(ExportModal);
export default memo(ExportPixeliumModal);
150 changes: 150 additions & 0 deletions src/components/modal/ExportPngModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import {
Checkbox,
InputGroup,
FormGroup,
Dialog,
DialogBody,
DialogFooter,
} from '@blueprintjs/core';
import styled from '@emotion/styled';
import { memo, useCallback, useMemo, useState } from 'react';
import { Button } from 'react-science/ui';

import useCurrentTab from '../../hooks/useCurrentTab';
import useData from '../../hooks/useData';
import useLog from '../../hooks/useLog';
import useModal from '../../hooks/useModal';
import useOriginalFilteredROIs from '../../hooks/useOriginalFilteredROIs';
import { saveAsPng } from '../../utils/export';
import { useMergeToImage } from '../tool/ExportTool';

import StyledModalBody from './utils/StyledModalBody';

const ExportStyle = styled.div`
display: flex;
flex-direction: column;
width: 500px;

.header {
font-size: 1.25rem;
}

.container {
padding: 20px;
}
`;

const SaveButtonInner = styled.span`
display: flex;
align-items: center;
`;

type ExportPngModalProps = {
previewImageIdentifier: string;
};

function ExportPngModal({ previewImageIdentifier }: ExportPngModalProps) {
const { isOpen, close } = useModal('exportPng');
const currentTab = useCurrentTab();
const data = useData();

const ROIs = useOriginalFilteredROIs(previewImageIdentifier);
const hasAnnotations = ROIs.length > 0;

const { logger } = useLog();

const title = useMemo(() => {
let fullTitle;
if (currentTab) {
fullTitle = data.images[currentTab]?.metadata.name || currentTab;
} else {
fullTitle = 'unnamed';
}
return fullTitle.split('.')[0];
}, [currentTab, data]);

const defaultFormState = useMemo(
() => ({
name: '',
annotations: true,
}),
[],
);

const [formState, setFormState] = useState(defaultFormState);
const resetForm = useCallback(
() => setFormState(defaultFormState),
[setFormState, defaultFormState],
);

const mergeToImage = useMergeToImage(formState.annotations);

const exportPNG = useCallback(async () => {
return mergeToImage().then(({ toSave }) =>
saveAsPng(toSave, formState.name || title),
);
}, [formState.name, mergeToImage, title]);

const save = useCallback(() => {
exportPNG()
.catch((error) => logger.error(`Failed to generate PNG: ${error}`))
.finally(() => {
resetForm();
close();
});
}, [close, exportPNG, logger, resetForm]);

return (
<Dialog
title="Export PNG file"
isOpen={isOpen}
onClose={close}
style={{ width: 'fit-content' }}
>
<ExportStyle>
<DialogBody>
<StyledModalBody>
<div>
<FormGroup label="Name">
<InputGroup
type="text"
placeholder={title}
value={formState.name}
onChange={(e) =>
setFormState({
...formState,
name: e.target.value,
})
}
/>
</FormGroup>
{hasAnnotations ? (
<Checkbox
label="Include annotations"
alignIndicator="right"
checked={formState.annotations}
onChange={(e) =>
setFormState({
...formState,
annotations: e.target.checked,
})
}
/>
) : null}
</div>
</StyledModalBody>
</DialogBody>
<DialogFooter
minimal
actions={
<Button intent="primary" onClick={save}>
<SaveButtonInner>Save</SaveButtonInner>
</Button>
}
/>
</ExportStyle>
</Dialog>
);
}

export default memo(ExportPngModal);
12 changes: 10 additions & 2 deletions src/components/modal/ModalContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import useCurrentTab from '../../hooks/useCurrentTab';
import useView from '../../hooks/useView';

import AboutModal from './AboutModal';
import ExportModal from './ExportModal';
import ExportClipboardModal from './ExportClipboardModal';
import ExportPixeliumModal from './ExportPixeliumModal';
import ExportPngModal from './ExportPngModal';
import ExtractROIModal from './ExtractROIModal';
import LogModal from './LogModal';
import BlurModal from './preview/filters/BlurModal';
Expand Down Expand Up @@ -74,7 +76,13 @@ function ModalContainer() {
{view.modals.close && (
<CloseModal previewImageIdentifier={currentTab} />
)}
{view.modals.export && <ExportModal />}
{view.modals.exportPng && (
<ExportPngModal previewImageIdentifier={currentTab} />
)}
{view.modals.exportClipboard && (
<ExportClipboardModal previewImageIdentifier={currentTab} />
)}
{view.modals.exportPixelium && <ExportPixeliumModal />}
{view.modals.resize && (
<ResizeModal previewImageIdentifier={currentTab} />
)}
Expand Down
Loading
Loading