fix: add cancel for generate text

This commit is contained in:
jialin
2024-12-02 17:50:21 +08:00
parent 804cbb8c56
commit a7456d1182
3 changed files with 77 additions and 50 deletions
+52 -30
View File
@@ -44,15 +44,52 @@ const SingleImage: React.FC<SingleImageProps> = (props) => {
width: width, width: width,
height: height height: height
}); });
const [originSize, setOriginSize] = React.useState({
width: width,
height: height
});
const thumImgWrapStyle = React.useMemo(() => { const thumImgWrapStyle = React.useMemo(() => {
return loading ? { width: '100%', height: '100%' } : {}; return loading ? { width: '100%', height: '100%' } : {};
}, [loading, imgSize]); }, [loading, imgSize]);
const handleResize = React.useCallback(
async (size: { width: number; height: number }) => {
if (!autoSize || !dataUrl) return;
const { width: containerWidth, height: containerHeight } = size;
const { width: originalWidth, height: originalHeight } = props;
console.log('size:', size, originalWidth, originalHeight);
if (!originalWidth || !originalHeight) return;
const imageAspectRatio = originalWidth / originalHeight;
const containerAspectRatio = containerWidth / containerHeight;
let newWidth, newHeight;
if (containerAspectRatio > imageAspectRatio) {
newHeight = containerHeight;
newWidth = containerHeight * imageAspectRatio;
} else {
newWidth = containerWidth;
newHeight = containerWidth / imageAspectRatio;
}
setImgSize({
width: newWidth,
height: newHeight
});
},
[autoSize, dataUrl, props]
);
const handleOnLoad = React.useCallback(async () => { const handleOnLoad = React.useCallback(async () => {
if (!autoBgColor) { if (!autoBgColor) {
return; return;
} }
const img = imgWrapper.current?.querySelector('img'); const img = imgWrapper.current?.querySelector('img');
if (!img) { if (!img) {
return; return;
@@ -76,37 +113,22 @@ const SingleImage: React.FC<SingleImageProps> = (props) => {
backgroundImage: `linear-gradient(135deg, ${startColor}, ${stopColor})` backgroundImage: `linear-gradient(135deg, ${startColor}, ${stopColor})`
}); });
}); });
}, []); }, [autoBgColor]);
const handleResize = React.useCallback( const getImgSize = (
(size: { width: number; height: number }) => { url: string
if (!autoSize) return; ): Promise<{ width: number; height: number }> => {
return new Promise((resolve) => {
const { width: containerWidth, height: containerHeight } = size; const img = new Image();
const { width: originalWidth, height: originalHeight } = props; img.onload = () => {
resolve({ width: img.width, height: img.height });
if (!originalWidth || !originalHeight) return; };
img.onerror = () => {
const imageAspectRatio = originalWidth / originalHeight; resolve({ width: 0, height: 0 });
const containerAspectRatio = containerWidth / containerHeight; };
img.src = url;
let newWidth, newHeight; });
};
if (containerAspectRatio > imageAspectRatio) {
newHeight = containerHeight;
newWidth = containerHeight * imageAspectRatio;
} else {
newWidth = containerWidth;
newHeight = containerWidth / imageAspectRatio;
}
setImgSize({
width: newWidth,
height: newHeight
});
},
[autoSize, props]
);
return ( return (
<ResizeObserver onResize={handleResize}> <ResizeObserver onResize={handleResize}>
@@ -133,28 +133,18 @@ const GroundImages: React.FC<MessageProps> = forwardRef((props, ref) => {
const setImageSize = useCallback(() => { const setImageSize = useCallback(() => {
let size: Record<string, string | number> = { let size: Record<string, string | number> = {
with: 256,
height: 256,
span: 12 span: 12
}; };
if (parameters.n === 1) { if (parameters.n === 1) {
size.width = '100%';
size.height = '100%';
size.span = 24; size.span = 24;
} }
if (parameters.n === 2) { if (parameters.n === 2) {
size.width = '50%';
size.height = 256;
size.span = 12; size.span = 12;
} }
if (parameters.n === 3) { if (parameters.n === 3) {
size.width = '33%';
size.height = 256;
size.span = 12; size.span = 12;
} }
if (parameters.n === 4) { if (parameters.n === 4) {
size.width = '25%';
size.height = 256;
size.span = 12; size.span = 12;
} }
return size; return size;
@@ -194,6 +184,7 @@ const GroundImages: React.FC<MessageProps> = forwardRef((props, ref) => {
setMessageId(); setMessageId();
setTokenResult(null); setTokenResult(null);
setCurrentPrompt(current?.content || ''); setCurrentPrompt(current?.content || '');
const imgSize = _.split(finalParameters.size, 'x');
let newImageList = Array(parameters.n) let newImageList = Array(parameters.n)
.fill({}) .fill({})
@@ -202,8 +193,8 @@ const GroundImages: React.FC<MessageProps> = forwardRef((props, ref) => {
dataUrl: 'data:image/png;base64,', dataUrl: 'data:image/png;base64,',
...size, ...size,
progress: 0, progress: 0,
height: '100%', height: imgSize[1],
width: '100%', width: imgSize[0],
loading: true, loading: true,
uid: index uid: index
}; };
@@ -245,7 +236,6 @@ const GroundImages: React.FC<MessageProps> = forwardRef((props, ref) => {
} }
const { reader, decoder } = result; const { reader, decoder } = result;
const imgSize = _.split(finalParameters.size, 'x');
await readStreamData(reader, decoder, (chunk: any) => { await readStreamData(reader, decoder, (chunk: any) => {
if (chunk?.error) { if (chunk?.error) {
+22 -7
View File
@@ -319,13 +319,28 @@ const GroundLeft: React.FC<MessageProps> = forwardRef((props, ref) => {
id: 'playground.audio.button.generate' id: 'playground.audio.button.generate'
})} })}
> >
<Button {loading ? (
disabled={!audioData} <Button
type="primary" disabled={!audioData}
shape="circle" type="primary"
onClick={handleOnGenerate} shape="circle"
icon={<SendOutlined></SendOutlined>} onClick={handleStopConversation}
></Button> icon={
<IconFont
type="icon-stop1"
className="font-size-14"
></IconFont>
}
></Button>
) : (
<Button
disabled={!audioData}
type="primary"
shape="circle"
onClick={handleOnGenerate}
icon={<SendOutlined></SendOutlined>}
></Button>
)}
</Tooltip> </Tooltip>
</div> </div>
</div> </div>