2025-09-21 12:03:29 +01:00
|
|
|
import { Artifact } from '@/components/create-artifact';
|
|
|
|
|
import { CopyIcon, RedoIcon, UndoIcon } from '@/components/icons';
|
|
|
|
|
import { ImageEditor } from '@/components/image-editor';
|
|
|
|
|
import { toast } from 'sonner';
|
2025-01-27 14:19:47 +05:30
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
export const imageArtifact = new Artifact({
|
2025-09-21 12:03:29 +01:00
|
|
|
kind: 'image',
|
|
|
|
|
description: 'Useful for image generation',
|
2025-02-13 08:25:57 -08:00
|
|
|
onStreamPart: ({ streamPart, setArtifact }) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
if (streamPart.type === 'data-imageDelta') {
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact((draftArtifact) => ({
|
|
|
|
|
...draftArtifact,
|
2025-07-03 02:26:34 -07:00
|
|
|
content: streamPart.data,
|
2025-01-27 14:19:47 +05:30
|
|
|
isVisible: true,
|
2025-09-21 12:03:29 +01:00
|
|
|
status: 'streaming',
|
2025-01-27 14:19:47 +05:30
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
content: ImageEditor,
|
|
|
|
|
actions: [
|
|
|
|
|
{
|
|
|
|
|
icon: <UndoIcon size={18} />,
|
2025-09-21 12:03:29 +01:00
|
|
|
description: 'View Previous version',
|
2025-01-27 14:19:47 +05:30
|
|
|
onClick: ({ handleVersionChange }) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
handleVersionChange('prev');
|
2025-01-27 14:19:47 +05:30
|
|
|
},
|
2025-01-27 19:23:16 +05:30
|
|
|
isDisabled: ({ currentVersionIndex }) => {
|
|
|
|
|
if (currentVersionIndex === 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
2025-01-27 14:19:47 +05:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <RedoIcon size={18} />,
|
2025-09-21 12:03:29 +01:00
|
|
|
description: 'View Next version',
|
2025-01-27 14:19:47 +05:30
|
|
|
onClick: ({ handleVersionChange }) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
handleVersionChange('next');
|
2025-01-27 14:19:47 +05:30
|
|
|
},
|
2025-01-27 19:23:16 +05:30
|
|
|
isDisabled: ({ isCurrentVersion }) => {
|
|
|
|
|
if (isCurrentVersion) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
2025-01-27 14:19:47 +05:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <CopyIcon size={18} />,
|
2025-09-21 12:03:29 +01:00
|
|
|
description: 'Copy image to clipboard',
|
2025-01-27 14:19:47 +05:30
|
|
|
onClick: ({ content }) => {
|
|
|
|
|
const img = new Image();
|
|
|
|
|
img.src = `data:image/png;base64,${content}`;
|
|
|
|
|
|
|
|
|
|
img.onload = () => {
|
2025-09-21 12:03:29 +01:00
|
|
|
const canvas = document.createElement('canvas');
|
2025-01-27 14:19:47 +05:30
|
|
|
canvas.width = img.width;
|
|
|
|
|
canvas.height = img.height;
|
2025-09-21 12:03:29 +01:00
|
|
|
const ctx = canvas.getContext('2d');
|
2025-01-27 14:19:47 +05:30
|
|
|
ctx?.drawImage(img, 0, 0);
|
|
|
|
|
canvas.toBlob((blob) => {
|
|
|
|
|
if (blob) {
|
|
|
|
|
navigator.clipboard.write([
|
2025-09-21 12:03:29 +01:00
|
|
|
new ClipboardItem({ 'image/png': blob }),
|
2025-01-27 14:19:47 +05:30
|
|
|
]);
|
|
|
|
|
}
|
2025-09-21 12:03:29 +01:00
|
|
|
}, 'image/png');
|
2025-01-27 14:19:47 +05:30
|
|
|
};
|
2025-01-27 19:23:16 +05:30
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
toast.success('Copied image to clipboard!');
|
2025-01-27 14:19:47 +05:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
toolbar: [],
|
|
|
|
|
});
|