-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export and import NMRIUM file as ZIP.
- close #950, Compress NMRIUM json data for download / upload. - create custom hook for export. - refactor export and remove export actions from reducer.
- Loading branch information
1 parent
c06e5ac
commit 7bf6f44
Showing
13 changed files
with
262 additions
and
137 deletions.
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
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,89 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { toJSON } from '../../data/SpectraManager'; | ||
import { useChartData } from '../context/ChartContext'; | ||
import { useDispatch } from '../context/DispatchContext'; | ||
import { useAlert } from '../elements/popup/Alert'; | ||
import { SET_LOADING_FLAG } from '../reducer/types/Types'; | ||
import { | ||
exportAsJSON, | ||
exportAsNMRE, | ||
exportAsPng, | ||
exportAsSVG, | ||
} from '../utility/Export'; | ||
import { toNmredata } from '../utility/toNmredata'; | ||
|
||
export default function useExport() { | ||
const dispatch = useDispatch(); | ||
const alert = useAlert(); | ||
const state = useChartData(); | ||
|
||
const saveToClipboardHandler = useCallback(() => { | ||
if (state.data.length > 0) { | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: true }); | ||
setTimeout(() => { | ||
// eslint-disable-next-line no-undef | ||
copyPNGToClipboard('nmrSVG'); | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: false }); | ||
alert.show('Spectrum copied to clipboard'); | ||
}, 0); | ||
} | ||
}, [alert, dispatch, state]); | ||
|
||
const saveAsJSONHandler = useCallback( | ||
(spaceIndent = 0, isCompressed = true) => { | ||
if (state.data.length > 0) { | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: true }); | ||
setTimeout(async () => { | ||
//exported file name by default will be the first spectrum name | ||
const fileName = state.data[0]?.display?.name; | ||
const exportedData = toJSON(state); | ||
await exportAsJSON(exportedData, fileName, spaceIndent, isCompressed); | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: false }); | ||
}, 0); | ||
} | ||
}, | ||
[dispatch, state], | ||
); | ||
|
||
const saveAsNMREHandler = useCallback(() => { | ||
if (state.data.length > 0) { | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: true }); | ||
setTimeout(() => { | ||
const fileName = state.data[0]?.display?.name; | ||
const exportedData = toNmredata(state); | ||
exportAsNMRE(exportedData, fileName); | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: false }); | ||
}, 0); | ||
} | ||
}, [dispatch, state]); | ||
|
||
const saveAsSVGHandler = useCallback(() => { | ||
if (state.data.length > 0) { | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: true }); | ||
setTimeout(() => { | ||
const fileName = state.data[0]?.display?.name; | ||
exportAsSVG(fileName, 'nmrSVG'); | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: true }); | ||
}, 0); | ||
} | ||
}, [dispatch, state.data]); | ||
|
||
const saveAsPNGHandler = useCallback(() => { | ||
if (state.data.length > 0) { | ||
dispatch({ type: SET_LOADING_FLAG, isLoading: true }); | ||
setTimeout(() => { | ||
const fileName = state.data[0]?.display?.name; | ||
exportAsPng(fileName, 'nmrSVG'); | ||
}, 0); | ||
} | ||
}, [dispatch, state.data]); | ||
|
||
return { | ||
saveToClipboardHandler, | ||
saveAsJSONHandler, | ||
saveAsNMREHandler, | ||
saveAsSVGHandler, | ||
saveAsPNGHandler, | ||
}; | ||
} |
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
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.