fix: Add copy to clipboard option for capture (#5720)

* Add copy to clipboard option for capture

* PR comments

* Added toast feedback and updated labels

---------

Co-authored-by: Dan Rukas <dan.rukas@gmail.com>
This commit is contained in:
Bill WallaceandDan Rukas authored and GitHub committed 2026-02-18 19:03:17 -05:00
1 parent c85ca0e177
commit 53c67f3612
4 files changed
+73 -5

No files matched your search

@@ -118,16 +118,20 @@ const CornerstoneViewportDownloadForm = ({
const downloadViewport = renderingEngine.getViewport(VIEWPORT_ID);
try {
const properties = viewport.getProperties();
if (downloadViewport instanceof StackViewport) {
const imageId = viewport.getCurrentImageId();
const properties = viewport.getProperties();
await downloadViewport.setStack([imageId]);
downloadViewport.setProperties(properties);
} else if (downloadViewport instanceof BaseVolumeViewport) {
const volumeIds = viewport.getAllVolumeIds();
downloadViewport.setVolumes([{ volumeId: volumeIds[0] }]);
await downloadViewport.setVolumes([{ volumeId: volumeIds[0] }]);
}
downloadViewport.setProperties(properties);
const viewRef = viewport.getViewReference();
downloadViewport.setViewReference(viewRef);
downloadViewport.render();
if (segmentationRepresentations?.length) {
segmentationRepresentations.forEach(segRepresentation => {
@@ -230,6 +234,48 @@ const CornerstoneViewportDownloadForm = ({
downloadUrl(canvas.toDataURL(`image/${fileType}`, 1.0), { filename });
};
const handleCopyToClipboard = async () => {
const divForDownloadViewport = document.querySelector(
`div[data-viewport-uid="${VIEWPORT_ID}"]`
);
if (!divForDownloadViewport) {
console.debug('No viewport found for copy');
return;
}
try {
const canvas = await html2canvas(divForDownloadViewport as HTMLElement);
// Clipboard API only supports PNG format in most browsers
const blob = await new Promise<Blob>((resolve, reject) => {
canvas.toBlob(
blob => {
if (blob) {
resolve(blob);
} else {
reject(new Error('Failed to create blob from canvas'));
}
},
'image/png',
1.0
);
});
// Copy to clipboard using the Clipboard API
await navigator.clipboard.write([
new ClipboardItem({
'image/png': blob,
}),
]);
console.log('Image copied to clipboard successfully');
} catch (error) {
console.error('Failed to copy image to clipboard:', error);
throw error;
}
};
const ViewportDownloadFormNew = customizationService.getCustomization(
'ohif.captureViewportModal'
);
@@ -247,6 +293,7 @@ const CornerstoneViewportDownloadForm = ({
onEnableViewport={handleEnableViewport}
onDisableViewport={handleDisableViewport}
onDownload={handleDownload}
onCopyToClipboard={handleCopyToClipboard}
warningState={warningState}
/>
);