Browse Source

裁剪时使用原图

feature/071
yun 7 days ago
parent
commit
f314de62fe
  1. 22
      src/components/__tests__/GenerateImageNode.test.tsx
  2. 28
      src/components/__tests__/ImageInputNode.test.tsx
  3. 4
      src/components/nodes/GenerateImageNode.tsx
  4. 4
      src/components/nodes/ImageInputNode.tsx

22
src/components/__tests__/GenerateImageNode.test.tsx

@ -379,6 +379,28 @@ describe("GenerateImageNode", () => {
expect(mockDownloadMedia).toHaveBeenCalledWith("https://cdn.example.com/full.png", "image");
});
it("should use the full output image while cropping and return to preview afterward", () => {
render(
<TestWrapper>
<GenerateImageNode {...createNodeProps({
outputImage: "https://cdn.example.com/full.png",
previewImg: "https://cdn.example.com/full_thumb.webp",
})} selected />
</TestWrapper>
);
const img = screen.getByAltText("Generated");
expect(img).toHaveAttribute("src", "https://cdn.example.com/full_thumb.webp");
fireEvent.click(screen.getByRole("button", { name: /Crop|裁剪/ }));
expect(img).toHaveAttribute("src", "https://cdn.example.com/full.png");
fireEvent.click(screen.getByRole("button", { name: "关闭编辑工具" }));
expect(img).toHaveAttribute("src", "https://cdn.example.com/full_thumb.webp");
});
it("should not resize the canvas node while rendering an output image", () => {
render(
<TestWrapper>

28
src/components/__tests__/ImageInputNode.test.tsx

@ -308,6 +308,34 @@ describe("ImageInputNode", () => {
expect(img).toHaveClass("object-cover");
});
it("should use the full image while cropping and return to preview afterward", () => {
mockNodes = [{ id: propsWithImage.id, selected: true }];
render(
<TestWrapper>
<ImageInputNode
{...propsWithImage}
selected
data={{
...propsWithImage.data,
image: "https://example.com/full.png",
previewImage: "https://example.com/full_thumb.webp",
}}
/>
</TestWrapper>
);
const img = screen.getByAltText("test-image.png");
expect(img).toHaveAttribute("src", "https://example.com/full_thumb.webp");
fireEvent.click(screen.getByRole("button", { name: "Crop" }));
expect(img).toHaveAttribute("src", "https://example.com/full.png");
fireEvent.click(screen.getByRole("button", { name: "关闭编辑工具" }));
expect(img).toHaveAttribute("src", "https://example.com/full_thumb.webp");
});
it("should show a load failure state when the image cannot be rendered", () => {
render(
<TestWrapper>

4
src/components/nodes/GenerateImageNode.tsx

@ -375,7 +375,7 @@ export function GenerateImageNodeView({ id, data, selected }: GenerateImageNodeV
}, [nodeData.status, displayError, t]);
const selectedNodeCount = useSelectedNodeCount();
const editableImage = displayImage;
const editableImage = nodeData.outputImage || displayImage;
const downloadImage = nodeData.outputImage || editableImage;
const overlayImageDimensions = cropImageDimensions || asImageDimensions(nodeData.dimensions);
const showSelectedActions = Boolean(selected && selectedNodeCount === 1 && editableImage);
@ -576,7 +576,7 @@ export function GenerateImageNodeView({ id, data, selected }: GenerateImageNodeV
onLoad={(event) => {
setImageRetryNonce(0);
const image = event.currentTarget;
if (image.naturalWidth > 0 && image.naturalHeight > 0) {
if (renderedImageSrc === nodeData.outputImage && image.naturalWidth > 0 && image.naturalHeight > 0) {
const dimensions = { width: image.naturalWidth, height: image.naturalHeight };
setCropImageDimensions(dimensions);
updateMediaNodeData(id, { dimensions }, dimensions);

4
src/components/nodes/ImageInputNode.tsx

@ -124,7 +124,7 @@ export function ImageInputNodeView({ id, data, selected, renderInputHandle = fal
const [imageLoadFailed, setImageLoadFailed] = useState(false);
const isUploading = nodeData.uploadStatus === "loading";
const displayImage = displayFallbackImage || preferredDisplayImage;
const imageSrc = displayImage;
const imageSrc = isCropping ? nodeData.image || displayImage : displayImage;
useEffect(() => {
setDisplayFallbackImage(null);
@ -576,7 +576,7 @@ export function ImageInputNodeView({ id, data, selected, renderInputHandle = fal
onLoad={(event) => {
setImageLoadFailed(false);
const image = event.currentTarget;
if (image.naturalWidth > 0 && image.naturalHeight > 0) {
if (imageSrc === nodeData.image && image.naturalWidth > 0 && image.naturalHeight > 0) {
const dimensions = { width: image.naturalWidth, height: image.naturalHeight };
if (nodeData.dimensions?.width !== dimensions.width || nodeData.dimensions?.height !== dimensions.height) {
updateMediaNodeData(id, { dimensions }, dimensions);

Loading…
Cancel
Save