mirror of
https://github.com/stackblitz/bolt.new
synced 2025-03-12 14:58:30 +00:00
Second commit for screen cap feature
This commit is contained in:
parent
8b7e18e627
commit
7fdab0ad6a
@ -80,12 +80,17 @@ 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"
|
||||||
@ -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,7 +7,8 @@ interface ScreenshotSelectorProps {
|
|||||||
containerRef: React.RefObject<HTMLElement>;
|
containerRef: React.RefObject<HTMLElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, containerRef }: ScreenshotSelectorProps) => {
|
export const ScreenshotSelector = memo(
|
||||||
|
({ isSelectionMode, setIsSelectionMode, containerRef }: ScreenshotSelectorProps) => {
|
||||||
const [isCapturing, setIsCapturing] = useState(false);
|
const [isCapturing, setIsCapturing] = useState(false);
|
||||||
const [selectionStart, setSelectionStart] = 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 [selectionEnd, setSelectionEnd] = useState<{ x: number; y: number } | null>(null);
|
||||||
@ -30,8 +31,8 @@ export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, c
|
|||||||
const stream = await navigator.mediaDevices.getDisplayMedia({
|
const stream = await navigator.mediaDevices.getDisplayMedia({
|
||||||
audio: false,
|
audio: false,
|
||||||
video: {
|
video: {
|
||||||
displaySurface: "window"
|
displaySurface: 'window',
|
||||||
}
|
},
|
||||||
} as MediaStreamConstraints);
|
} as MediaStreamConstraints);
|
||||||
|
|
||||||
// Set up video with the stream
|
// Set up video with the stream
|
||||||
@ -39,7 +40,7 @@ export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, c
|
|||||||
await video.play();
|
await video.play();
|
||||||
|
|
||||||
// Wait for video to be ready
|
// Wait for video to be ready
|
||||||
await new Promise(resolve => setTimeout(resolve, 300));
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||||
|
|
||||||
// Create temporary canvas for full screenshot
|
// Create temporary canvas for full screenshot
|
||||||
const tempCanvas = document.createElement('canvas');
|
const tempCanvas = document.createElement('canvas');
|
||||||
@ -78,17 +79,7 @@ export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, c
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draw the cropped area
|
// Draw the cropped area
|
||||||
ctx.drawImage(
|
ctx.drawImage(tempCanvas, scaledX, scaledY, scaledWidth, scaledHeight, 0, 0, width, height);
|
||||||
tempCanvas,
|
|
||||||
scaledX,
|
|
||||||
scaledY,
|
|
||||||
scaledWidth,
|
|
||||||
scaledHeight,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
width,
|
|
||||||
height
|
|
||||||
);
|
|
||||||
|
|
||||||
// Convert to blob
|
// Convert to blob
|
||||||
const blob = await new Promise<Blob>((resolve, reject) => {
|
const blob = await new Promise<Blob>((resolve, reject) => {
|
||||||
@ -126,8 +117,7 @@ export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, c
|
|||||||
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);
|
||||||
@ -143,7 +133,8 @@ export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, c
|
|||||||
}
|
}
|
||||||
}, [isSelectionMode, selectionStart, selectionEnd, containerRef, setIsSelectionMode]);
|
}, [isSelectionMode, selectionStart, selectionEnd, containerRef, setIsSelectionMode]);
|
||||||
|
|
||||||
const handleSelectionStart = useCallback((e: React.MouseEvent) => {
|
const handleSelectionStart = useCallback(
|
||||||
|
(e: React.MouseEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
if (!isSelectionMode) return;
|
if (!isSelectionMode) return;
|
||||||
@ -152,9 +143,12 @@ export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, c
|
|||||||
const y = e.clientY - rect.top;
|
const y = e.clientY - rect.top;
|
||||||
setSelectionStart({ x, y });
|
setSelectionStart({ x, y });
|
||||||
setSelectionEnd({ x, y });
|
setSelectionEnd({ x, y });
|
||||||
}, [isSelectionMode]);
|
},
|
||||||
|
[isSelectionMode],
|
||||||
|
);
|
||||||
|
|
||||||
const handleSelectionMove = useCallback((e: React.MouseEvent) => {
|
const handleSelectionMove = useCallback(
|
||||||
|
(e: React.MouseEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
if (!isSelectionMode || !selectionStart) return;
|
if (!isSelectionMode || !selectionStart) return;
|
||||||
@ -162,7 +156,9 @@ export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, c
|
|||||||
const x = e.clientX - rect.left;
|
const x = e.clientX - rect.left;
|
||||||
const y = e.clientY - rect.top;
|
const y = e.clientY - rect.top;
|
||||||
setSelectionEnd({ x, y });
|
setSelectionEnd({ x, y });
|
||||||
}, [isSelectionMode, selectionStart]);
|
},
|
||||||
|
[isSelectionMode, selectionStart],
|
||||||
|
);
|
||||||
|
|
||||||
if (!isSelectionMode) return null;
|
if (!isSelectionMode) return null;
|
||||||
|
|
||||||
@ -200,4 +196,5 @@ export const ScreenshotSelector = memo(({ isSelectionMode, setIsSelectionMode, c
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user