mirror of
https://github.com/stackblitz/bolt.new
synced 2025-03-12 06:51:11 +00:00
Second commit for screen cap feature
This commit is contained in:
parent
8b7e18e627
commit
7fdab0ad6a
@ -27,7 +27,7 @@ export const Preview = memo(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { baseUrl } = activePreview;
|
const { baseUrl } = activePreview;
|
||||||
|
|
||||||
setUrl(baseUrl);
|
setUrl(baseUrl);
|
||||||
setIframeUrl(baseUrl);
|
setIframeUrl(baseUrl);
|
||||||
}, [activePreview, iframeUrl]);
|
}, [activePreview, iframeUrl]);
|
||||||
@ -80,19 +80,24 @@ export const Preview = memo(() => {
|
|||||||
)}
|
)}
|
||||||
<div className="bg-bolt-elements-background-depth-2 p-2 flex items-center gap-1.5">
|
<div className="bg-bolt-elements-background-depth-2 p-2 flex items-center gap-1.5">
|
||||||
<IconButton icon="i-ph:arrow-clockwise" onClick={reloadPreview} />
|
<IconButton icon="i-ph:arrow-clockwise" onClick={reloadPreview} />
|
||||||
<IconButton icon="i-ph:selection" onClick={() => setIsSelectionMode(!isSelectionMode)} className={isSelectionMode ? 'bg-bolt-elements-background-depth-3' : ''} />
|
<IconButton
|
||||||
|
icon="i-ph:selection"
|
||||||
|
onClick={() => setIsSelectionMode(!isSelectionMode)}
|
||||||
|
className={isSelectionMode ? 'bg-bolt-elements-background-depth-3' : ''}
|
||||||
|
/>
|
||||||
<div
|
<div
|
||||||
className="flex items-center gap-1 flex-grow bg-bolt-elements-preview-addressBar-background border border-bolt-elements-borderColor text-bolt-elements-preview-addressBar-text rounded-full px-3 py-1 text-sm hover:bg-bolt-elements-preview-addressBar-backgroundHover hover:focus-within:bg-bolt-elements-preview-addressBar-backgroundActive focus-within:bg-bolt-elements-preview-addressBar-backgroundActive
|
className="flex items-center gap-1 flex-grow bg-bolt-elements-preview-addressBar-background border border-bolt-elements-borderColor text-bolt-elements-preview-addressBar-text rounded-full px-3 py-1 text-sm hover:bg-bolt-elements-preview-addressBar-backgroundHover hover:focus-within:bg-bolt-elements-preview-addressBar-backgroundActive focus-within:bg-bolt-elements-preview-addressBar-backgroundActive
|
||||||
focus-within-border-bolt-elements-borderColorActive focus-within:text-bolt-elements-preview-addressBar-textActive"
|
focus-within-border-bolt-elements-borderColorActive focus-within:text-bolt-elements-preview-addressBar-textActive"
|
||||||
>
|
>
|
||||||
<input title='URL'
|
<input
|
||||||
|
title="URL"
|
||||||
ref={inputRef}
|
ref={inputRef}
|
||||||
className="w-full bg-transparent outline-none"
|
className="w-full bg-transparent outline-none"
|
||||||
type="text"
|
type="text"
|
||||||
value={url}
|
value={url}
|
||||||
onChange={(event) => {
|
onChange={(event) => {
|
||||||
setUrl(event.target.value);
|
setUrl(event.target.value);
|
||||||
}}
|
}}
|
||||||
onKeyDown={(event) => {
|
onKeyDown={(event) => {
|
||||||
if (event.key === 'Enter' && validateUrl(url)) {
|
if (event.key === 'Enter' && validateUrl(url)) {
|
||||||
setIframeUrl(url);
|
setIframeUrl(url);
|
||||||
@ -119,7 +124,11 @@ export const Preview = memo(() => {
|
|||||||
{activePreview ? (
|
{activePreview ? (
|
||||||
<>
|
<>
|
||||||
<iframe ref={iframeRef} title="preview" className="border-none w-full h-full bg-white" src={iframeUrl} />
|
<iframe ref={iframeRef} title="preview" className="border-none w-full h-full bg-white" src={iframeUrl} />
|
||||||
<ScreenshotSelector isSelectionMode={isSelectionMode} setIsSelectionMode={setIsSelectionMode} containerRef={iframeRef} />
|
<ScreenshotSelector
|
||||||
|
isSelectionMode={isSelectionMode}
|
||||||
|
setIsSelectionMode={setIsSelectionMode}
|
||||||
|
containerRef={iframeRef}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex w-full h-full justify-center items-center bg-white">No preview available</div>
|
<div className="flex w-full h-full justify-center items-center bg-white">No preview available</div>
|
||||||
|
@ -7,197 +7,194 @@ interface ScreenshotSelectorProps {
|
|||||||
containerRef: React.RefObject<HTMLElement>;
|
containerRef: React.RefObject<HTMLElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, containerRef }: ScreenshotSelectorProps) => {
|
export const ScreenshotSelector = memo(
|
||||||
const [isCapturing, setIsCapturing] = useState(false);
|
({ isSelectionMode, setIsSelectionMode, containerRef }: ScreenshotSelectorProps) => {
|
||||||
const [selectionStart, setSelectionStart] = useState<{ x: number; y: number } | null>(null);
|
const [isCapturing, setIsCapturing] = useState(false);
|
||||||
const [selectionEnd, setSelectionEnd] = useState<{ x: number; y: number } | null>(null);
|
const [selectionStart, setSelectionStart] = useState<{ x: number; y: number } | null>(null);
|
||||||
|
const [selectionEnd, setSelectionEnd] = useState<{ x: number; y: number } | null>(null);
|
||||||
|
|
||||||
const handleCopySelection = useCallback(async () => {
|
const handleCopySelection = useCallback(async () => {
|
||||||
if (!isSelectionMode || !selectionStart || !selectionEnd || !containerRef.current) return;
|
if (!isSelectionMode || !selectionStart || !selectionEnd || !containerRef.current) return;
|
||||||
|
|
||||||
setIsCapturing(true);
|
|
||||||
try {
|
|
||||||
const width = Math.abs(selectionEnd.x - selectionStart.x);
|
|
||||||
const height = Math.abs(selectionEnd.y - selectionStart.y);
|
|
||||||
|
|
||||||
// Create a video element to capture the screen
|
|
||||||
const video = document.createElement('video');
|
|
||||||
video.style.opacity = '0';
|
|
||||||
document.body.appendChild(video);
|
|
||||||
|
|
||||||
|
setIsCapturing(true);
|
||||||
try {
|
try {
|
||||||
// Capture the entire screen
|
const width = Math.abs(selectionEnd.x - selectionStart.x);
|
||||||
const stream = await navigator.mediaDevices.getDisplayMedia({
|
const height = Math.abs(selectionEnd.y - selectionStart.y);
|
||||||
audio: false,
|
|
||||||
video: {
|
// Create a video element to capture the screen
|
||||||
displaySurface: "window"
|
const video = document.createElement('video');
|
||||||
|
video.style.opacity = '0';
|
||||||
|
document.body.appendChild(video);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Capture the entire screen
|
||||||
|
const stream = await navigator.mediaDevices.getDisplayMedia({
|
||||||
|
audio: false,
|
||||||
|
video: {
|
||||||
|
displaySurface: 'window',
|
||||||
|
},
|
||||||
|
} as MediaStreamConstraints);
|
||||||
|
|
||||||
|
// Set up video with the stream
|
||||||
|
video.srcObject = stream;
|
||||||
|
await video.play();
|
||||||
|
|
||||||
|
// Wait for video to be ready
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||||
|
|
||||||
|
// Create temporary canvas for full screenshot
|
||||||
|
const tempCanvas = document.createElement('canvas');
|
||||||
|
tempCanvas.width = video.videoWidth;
|
||||||
|
tempCanvas.height = video.videoHeight;
|
||||||
|
const tempCtx = tempCanvas.getContext('2d');
|
||||||
|
|
||||||
|
if (!tempCtx) {
|
||||||
|
throw new Error('Failed to get temporary canvas context');
|
||||||
}
|
}
|
||||||
} as MediaStreamConstraints);
|
|
||||||
|
|
||||||
// Set up video with the stream
|
// Draw the full video frame
|
||||||
video.srcObject = stream;
|
tempCtx.drawImage(video, 0, 0);
|
||||||
await video.play();
|
|
||||||
|
|
||||||
// Wait for video to be ready
|
// Get the container's position in the page
|
||||||
await new Promise(resolve => setTimeout(resolve, 300));
|
const containerRect = containerRef.current.getBoundingClientRect();
|
||||||
|
|
||||||
// Create temporary canvas for full screenshot
|
// Calculate scale factor between video and screen
|
||||||
const tempCanvas = document.createElement('canvas');
|
const scaleX = video.videoWidth / window.innerWidth;
|
||||||
tempCanvas.width = video.videoWidth;
|
const scaleY = video.videoHeight / window.innerHeight;
|
||||||
tempCanvas.height = video.videoHeight;
|
|
||||||
const tempCtx = tempCanvas.getContext('2d');
|
|
||||||
|
|
||||||
if (!tempCtx) {
|
// Calculate the scaled coordinates
|
||||||
throw new Error('Failed to get temporary canvas context');
|
const scaledX = (containerRect.left + Math.min(selectionStart.x, selectionEnd.x)) * scaleX;
|
||||||
}
|
const scaledY = (containerRect.top + Math.min(selectionStart.y, selectionEnd.y)) * scaleY;
|
||||||
|
const scaledWidth = width * scaleX;
|
||||||
|
const scaledHeight = height * scaleY;
|
||||||
|
|
||||||
// Draw the full video frame
|
// Create final canvas for the cropped area
|
||||||
tempCtx.drawImage(video, 0, 0);
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = width;
|
||||||
|
canvas.height = height;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
// Get the container's position in the page
|
if (!ctx) {
|
||||||
const containerRect = containerRef.current.getBoundingClientRect();
|
throw new Error('Failed to get canvas context');
|
||||||
|
}
|
||||||
// Calculate scale factor between video and screen
|
|
||||||
const scaleX = video.videoWidth / window.innerWidth;
|
|
||||||
const scaleY = video.videoHeight / window.innerHeight;
|
|
||||||
|
|
||||||
// Calculate the scaled coordinates
|
// Draw the cropped area
|
||||||
const scaledX = (containerRect.left + Math.min(selectionStart.x, selectionEnd.x)) * scaleX;
|
ctx.drawImage(tempCanvas, scaledX, scaledY, scaledWidth, scaledHeight, 0, 0, width, height);
|
||||||
const scaledY = (containerRect.top + Math.min(selectionStart.y, selectionEnd.y)) * scaleY;
|
|
||||||
const scaledWidth = width * scaleX;
|
|
||||||
const scaledHeight = height * scaleY;
|
|
||||||
|
|
||||||
// Create final canvas for the cropped area
|
// Convert to blob
|
||||||
const canvas = document.createElement('canvas');
|
const blob = await new Promise<Blob>((resolve, reject) => {
|
||||||
canvas.width = width;
|
canvas.toBlob((blob) => {
|
||||||
canvas.height = height;
|
if (blob) resolve(blob);
|
||||||
const ctx = canvas.getContext('2d');
|
else reject(new Error('Failed to create blob'));
|
||||||
|
}, 'image/png');
|
||||||
|
});
|
||||||
|
|
||||||
if (!ctx) {
|
// Create a FileReader to convert blob to base64
|
||||||
throw new Error('Failed to get canvas context');
|
const reader = new FileReader();
|
||||||
}
|
reader.onload = (e) => {
|
||||||
|
const base64Image = e.target?.result as string;
|
||||||
|
|
||||||
// Draw the cropped area
|
// Find the textarea element
|
||||||
ctx.drawImage(
|
const textarea = document.querySelector('textarea');
|
||||||
tempCanvas,
|
if (textarea) {
|
||||||
scaledX,
|
// Get the setters from the BaseChat component
|
||||||
scaledY,
|
const setUploadedFiles = (window as any).__BOLT_SET_UPLOADED_FILES__;
|
||||||
scaledWidth,
|
const setImageDataList = (window as any).__BOLT_SET_IMAGE_DATA_LIST__;
|
||||||
scaledHeight,
|
const uploadedFiles = (window as any).__BOLT_UPLOADED_FILES__ || [];
|
||||||
0,
|
const imageDataList = (window as any).__BOLT_IMAGE_DATA_LIST__ || [];
|
||||||
0,
|
|
||||||
width,
|
|
||||||
height
|
|
||||||
);
|
|
||||||
|
|
||||||
// Convert to blob
|
if (setUploadedFiles && setImageDataList) {
|
||||||
const blob = await new Promise<Blob>((resolve, reject) => {
|
// Update the files and image data
|
||||||
canvas.toBlob((blob) => {
|
const file = new File([blob], 'screenshot.png', { type: 'image/png' });
|
||||||
if (blob) resolve(blob);
|
setUploadedFiles([...uploadedFiles, file]);
|
||||||
else reject(new Error('Failed to create blob'));
|
setImageDataList([...imageDataList, base64Image]);
|
||||||
}, 'image/png');
|
toast.success('Screenshot captured and added to chat');
|
||||||
});
|
} else {
|
||||||
|
toast.error('Could not add screenshot to chat');
|
||||||
// Create a FileReader to convert blob to base64
|
}
|
||||||
const reader = new FileReader();
|
|
||||||
reader.onload = (e) => {
|
|
||||||
const base64Image = e.target?.result as string;
|
|
||||||
|
|
||||||
// Find the textarea element
|
|
||||||
const textarea = document.querySelector('textarea');
|
|
||||||
if (textarea) {
|
|
||||||
// Get the setters from the BaseChat component
|
|
||||||
const setUploadedFiles = (window as any).__BOLT_SET_UPLOADED_FILES__;
|
|
||||||
const setImageDataList = (window as any).__BOLT_SET_IMAGE_DATA_LIST__;
|
|
||||||
const uploadedFiles = (window as any).__BOLT_UPLOADED_FILES__ || [];
|
|
||||||
const imageDataList = (window as any).__BOLT_IMAGE_DATA_LIST__ || [];
|
|
||||||
|
|
||||||
if (setUploadedFiles && setImageDataList) {
|
|
||||||
// Update the files and image data
|
|
||||||
const file = new File([blob], 'screenshot.png', { type: 'image/png' });
|
|
||||||
setUploadedFiles([...uploadedFiles, file]);
|
|
||||||
setImageDataList([...imageDataList, base64Image]);
|
|
||||||
toast.success('Screenshot captured and added to chat');
|
|
||||||
} else {
|
|
||||||
toast.error('Could not add screenshot to chat');
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
reader.readAsDataURL(blob);
|
||||||
reader.readAsDataURL(blob);
|
|
||||||
|
|
||||||
// Stop all tracks
|
// Stop all tracks
|
||||||
stream.getTracks().forEach(track => track.stop());
|
stream.getTracks().forEach((track) => track.stop());
|
||||||
|
} finally {
|
||||||
} finally {
|
// Clean up video element
|
||||||
// Clean up video element
|
document.body.removeChild(video);
|
||||||
document.body.removeChild(video);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to capture screenshot:', error);
|
|
||||||
toast.error('Failed to capture screenshot');
|
|
||||||
} finally {
|
|
||||||
setIsCapturing(false);
|
|
||||||
setSelectionStart(null);
|
|
||||||
setSelectionEnd(null);
|
|
||||||
setIsSelectionMode(false);
|
|
||||||
}
|
|
||||||
}, [isSelectionMode, selectionStart, selectionEnd, containerRef, setIsSelectionMode]);
|
|
||||||
|
|
||||||
const handleSelectionStart = useCallback((e: React.MouseEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
if (!isSelectionMode) return;
|
|
||||||
const rect = e.currentTarget.getBoundingClientRect();
|
|
||||||
const x = e.clientX - rect.left;
|
|
||||||
const y = e.clientY - rect.top;
|
|
||||||
setSelectionStart({ x, y });
|
|
||||||
setSelectionEnd({ x, y });
|
|
||||||
}, [isSelectionMode]);
|
|
||||||
|
|
||||||
const handleSelectionMove = useCallback((e: React.MouseEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
if (!isSelectionMode || !selectionStart) return;
|
|
||||||
const rect = e.currentTarget.getBoundingClientRect();
|
|
||||||
const x = e.clientX - rect.left;
|
|
||||||
const y = e.clientY - rect.top;
|
|
||||||
setSelectionEnd({ x, y });
|
|
||||||
}, [isSelectionMode, selectionStart]);
|
|
||||||
|
|
||||||
if (!isSelectionMode) return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className="absolute inset-0 cursor-crosshair"
|
|
||||||
onMouseDown={handleSelectionStart}
|
|
||||||
onMouseMove={handleSelectionMove}
|
|
||||||
onMouseUp={handleCopySelection}
|
|
||||||
onMouseLeave={() => {
|
|
||||||
if (selectionStart) {
|
|
||||||
setSelectionStart(null);
|
|
||||||
}
|
}
|
||||||
}}
|
} catch (error) {
|
||||||
style={{
|
console.error('Failed to capture screenshot:', error);
|
||||||
backgroundColor: isCapturing ? 'transparent' : 'rgba(0, 0, 0, 0.1)',
|
toast.error('Failed to capture screenshot');
|
||||||
userSelect: 'none',
|
} finally {
|
||||||
WebkitUserSelect: 'none',
|
setIsCapturing(false);
|
||||||
pointerEvents: 'all',
|
setSelectionStart(null);
|
||||||
opacity: isCapturing ? 0 : 1,
|
setSelectionEnd(null);
|
||||||
zIndex: 50,
|
setIsSelectionMode(false);
|
||||||
transition: 'opacity 0.1s ease-in-out',
|
}
|
||||||
}}
|
}, [isSelectionMode, selectionStart, selectionEnd, containerRef, setIsSelectionMode]);
|
||||||
>
|
|
||||||
{selectionStart && selectionEnd && !isCapturing && (
|
const handleSelectionStart = useCallback(
|
||||||
<div
|
(e: React.MouseEvent) => {
|
||||||
className="absolute border-2 border-blue-500 bg-blue-200 bg-opacity-20"
|
e.preventDefault();
|
||||||
style={{
|
e.stopPropagation();
|
||||||
left: Math.min(selectionStart.x, selectionEnd.x),
|
if (!isSelectionMode) return;
|
||||||
top: Math.min(selectionStart.y, selectionEnd.y),
|
const rect = e.currentTarget.getBoundingClientRect();
|
||||||
width: Math.abs(selectionEnd.x - selectionStart.x),
|
const x = e.clientX - rect.left;
|
||||||
height: Math.abs(selectionEnd.y - selectionStart.y),
|
const y = e.clientY - rect.top;
|
||||||
}}
|
setSelectionStart({ x, y });
|
||||||
/>
|
setSelectionEnd({ x, y });
|
||||||
)}
|
},
|
||||||
</div>
|
[isSelectionMode],
|
||||||
);
|
);
|
||||||
});
|
|
||||||
|
const handleSelectionMove = useCallback(
|
||||||
|
(e: React.MouseEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
if (!isSelectionMode || !selectionStart) return;
|
||||||
|
const rect = e.currentTarget.getBoundingClientRect();
|
||||||
|
const x = e.clientX - rect.left;
|
||||||
|
const y = e.clientY - rect.top;
|
||||||
|
setSelectionEnd({ x, y });
|
||||||
|
},
|
||||||
|
[isSelectionMode, selectionStart],
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isSelectionMode) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="absolute inset-0 cursor-crosshair"
|
||||||
|
onMouseDown={handleSelectionStart}
|
||||||
|
onMouseMove={handleSelectionMove}
|
||||||
|
onMouseUp={handleCopySelection}
|
||||||
|
onMouseLeave={() => {
|
||||||
|
if (selectionStart) {
|
||||||
|
setSelectionStart(null);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
style={{
|
||||||
|
backgroundColor: isCapturing ? 'transparent' : 'rgba(0, 0, 0, 0.1)',
|
||||||
|
userSelect: 'none',
|
||||||
|
WebkitUserSelect: 'none',
|
||||||
|
pointerEvents: 'all',
|
||||||
|
opacity: isCapturing ? 0 : 1,
|
||||||
|
zIndex: 50,
|
||||||
|
transition: 'opacity 0.1s ease-in-out',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{selectionStart && selectionEnd && !isCapturing && (
|
||||||
|
<div
|
||||||
|
className="absolute border-2 border-blue-500 bg-blue-200 bg-opacity-20"
|
||||||
|
style={{
|
||||||
|
left: Math.min(selectionStart.x, selectionEnd.x),
|
||||||
|
top: Math.min(selectionStart.y, selectionEnd.y),
|
||||||
|
width: Math.abs(selectionEnd.x - selectionStart.x),
|
||||||
|
height: Math.abs(selectionEnd.y - selectionStart.y),
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user