-
Notifications
You must be signed in to change notification settings - Fork 0
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
moonayyur
wants to merge
8
commits into
main
Choose a base branch
from
119-choose-between-exporting-with-or-without-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cf5a892
feat: add modals for all export types
moonayyur 9db4529
Merge remote-tracking branch 'origin/main' into 119-choose-between-ex…
moonayyur b53b920
feat: display modal only when annotations exist
moonayyur f71a4ea
fix: fix error when the image is binary
moonayyur e7ba45f
fix: use image name given by the user for png export
moonayyur f2958d9
feat: use image title when saving as png
moonayyur f0513b0
refactor: check the existance of annotations using rois
moonayyur 5865214
feat: add toast notification when copying to clipboard
moonayyur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.