Skip to content

fix(@uppy/image-editor): destroy cropper and clean up state on file re-open and removal#6223

Open
alexh-humanitix wants to merge 1 commit intotransloadit:mainfrom
alexh-humanitix:fix/image-editor-cleanup-on-reopen
Open

fix(@uppy/image-editor): destroy cropper and clean up state on file re-open and removal#6223
alexh-humanitix wants to merge 1 commit intotransloadit:mainfrom
alexh-humanitix:fix/image-editor-cleanup-on-reopen

Conversation

@alexh-humanitix
Copy link
Copy Markdown

Closes #6210

Problem

When using the ImageEditor with the Dashboard, the following sequence causes the editor to break on the second file:

  1. Upload any image
  2. Open the image editor, save or cancel
  3. Open the image editor again

Expected: The image editor works normally in subsequent edits.

Actual: The image preview and actions show, but the cropper doesn't initialize properly, so no edits can be made.

Root Cause

Two cleanup gaps in ImageEditor:

  1. start() doesn't destroy the previous cropper. It revokes the old objectUrl but leaves the Cropper instance alive. When the Editor component mounts for the new file and calls initCropper(imgElement), it hits the early return if (this.cropper) return and never creates a new cropper — the stale instance retains the old file's crop dimensions and references a destroyed DOM element.

  2. No file-removed listener. When a file is removed from the Dashboard, the ImageEditor has no handler to clean up its state (currentImage, cropper, objectUrl, event listeners), so everything persists into the next editing session.

Fix

  • Call this.destroyCropper() at the top of start() to fully tear down any previous cropper before starting a new editing session
  • Add a handleFileRemoved listener (registered in install(), deregistered in uninstall()) that calls stop() when the currently-edited file is removed from Uppy

Changes

packages/@uppy/image-editor/src/ImageEditor.tsx — 10 insertions, 1 deletion

Test Plan

  1. Open Dashboard with ImageEditor (autoOpen="imageEditor")
  2. Drag an image — editor opens, crop/save/cancel
  3. Open the image editor again — verify it works normally with a fresh cropper
  4. Remove the file from the Dashboard, drag a new image — verify the editor opens cleanly
  5. Repeat multiple times to confirm no state leaks
  6. Verify that closing the editor via cancel still works correctly
  7. Verify that editing the same file multiple times (without removing) still works

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 13, 2026

🦋 Changeset detected

Latest commit: fe290e3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@uppy/image-editor Patch
@uppy/components Patch
uppy Patch
@uppy/react Patch
@uppy/svelte Patch
@uppy/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

…e-open and removal

When opening the image editor for a second file (e.g. drag file → save in
editor → clear → drag new file), the old Cropper instance was not destroyed.
This caused `initCropper()` to bail early (`if (this.cropper) return`),
leaving the new image with a stale cropper that retained the previous file's
crop dimensions and referenced a destroyed DOM element.

Additionally, when a file was removed from the Dashboard, the ImageEditor
plugin had no handler to clean up its internal state (currentImage, cropper,
objectUrl), so everything persisted into the next editing session.

Changes:
- Call `this.destroyCropper()` at the top of `start()` to tear down any
  previous cropper before initializing a new editing session
- Add a `file-removed` event listener (registered in `install()`, cleaned
  up in `uninstall()`) that calls `stop()` when the currently-edited file
  is removed from Uppy
@alexh-humanitix alexh-humanitix force-pushed the fix/image-editor-cleanup-on-reopen branch from 818d5d1 to fe290e3 Compare March 13, 2026 02:23
Copy link
Copy Markdown
Collaborator

@qxprakash qxprakash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm !

@qxprakash qxprakash requested a review from mifi March 16, 2026 12:50
@mifi
Copy link
Copy Markdown
Contributor

mifi commented Mar 24, 2026

shouldn't cropper reset happen inside resetEditorState, so that we are sure that it happens in all cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Image editor not cleaning up properly after cancel/save

3 participants