LibreChat/client/src/utils/screenshotContext.jsx

45 lines
1.3 KiB
React
Raw Normal View History

2023-04-06 02:48:32 +08:00
import React, { createContext, useRef, useContext, useCallback } from 'react';
import html2canvas from 'html2canvas';
2023-04-06 02:48:32 +08:00
const ScreenshotContext = createContext({});
export const useScreenshot = () => {
const { ref } = useContext(ScreenshotContext);
const takeScreenShot = node => {
if (!node) {
throw new Error('You should provide correct html node.');
}
return html2canvas(node).then(canvas => {
const croppedCanvas = document.createElement('canvas');
const croppedCanvasContext = croppedCanvas.getContext('2d');
// init data
const cropPositionTop = 0;
const cropPositionLeft = 0;
const cropWidth = canvas.width;
const cropHeight = canvas.height;
croppedCanvas.width = cropWidth;
croppedCanvas.height = cropHeight;
croppedCanvasContext.drawImage(canvas, cropPositionLeft, cropPositionTop);
const base64Image = croppedCanvas.toDataURL('image/png', 1);
return base64Image;
});
};
2023-04-06 02:48:32 +08:00
const captureScreenshot = () => {
return takeScreenShot(ref.current);
2023-04-06 02:48:32 +08:00
};
return { screenshotTargetRef: ref, captureScreenshot };
};
export const ScreenshotProvider = ({ children }) => {
const ref = useRef(null);
return <ScreenshotContext.Provider value={{ ref }}>{children}</ScreenshotContext.Provider>;
};