Skip to content

feat(image): add upload progress, retry, and safe dialog lifecycle - #997

Merged
petyosi merged 6 commits into
mdx-editor:mainfrom
huyanxius:fix/268-image-upload-lifecycle
Nov 2, 2026
Merged

petyosi merged 6 commits into
mdx-editor:mainfrom
huyanxius:fix/268-image-upload-lifecycle

Conversation

@huyanxius

@huyanxius huyanxius commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes #268.

Implementation

  • Show accessible indeterminate upload progress, prevent duplicate dialog submissions and retain file/form values for retry.
  • Share request/document/editor validity guards between dialog saves and public insertImage$ uploads. Imperative uploads capture their target at invocation; dialogs capture theirs when opened. Focus/editor changes do not cancel an otherwise valid upload or redirect its result.
  • Ignore results after Cancel/Escape, replaced dialog sessions, document replacement, read-only transitions or unmount/removal. A setMarkdown resync leaves an idle URL dialog and its fields intact.
  • Preserve error diagnostics through console.error, including custom dialogs, without exposing raw errors in the default UI.
  • Retain unique label IDs and root/table/directive insertion and editing. The already-landed Firefox fixture has no feature diff after integrating main.

The callback remains File => Promise<string>; invalidation does not abort its network request. Paste/drop behavior is unchanged. Custom dialogs can read imageUploadState$ (idle, uploading, error).

Verification

Head: 1972c6dd5189634b425e0947f4777366a9899d16. Published tree 14d21dc33f24ff3026b545475fae4ca1f64517b3 matches the tested local tree exactly.

  • Local full Vitest: 296 passed, 1 existing skipped, 1 existing todo.
  • Focused lifecycle/table-image/linked-image suites: 56 + 15 + 2 passed.
  • Full lint, typecheck, library/declaration build and 24-package Lexical lockstep check: passed.
  • Independent read-only review found no correctness blocker. Tests cover target changes before submission/during upload, public invocation, concurrent uploads, custom-dialog diagnostics, stale completion, cancellation, read-only toggles, replacement/unmount and idle resync with/without autocomplete.
  • Local native browser execution is unavailable because cloud sandbox setup fails before startup. Browser target-switch coverage is integration-level; the browser fixtures exercise progress/cancellation/replacement/idle resync.

Exact-head CI run 40539763308: Test and Build Docs have passed; Build JS and Browser compatibility are still running. No browser/package/cross-version pass is claimed yet.

Maintainer re-review remains pending; successful tests do not constitute acceptance or merge.

@huyanxius
huyanxius marked this pull request as ready for review October 16, 2026 16:06

@petyosi petyosi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this. The lifecycle guards fix real bugs:

  • A double submit uploads twice.
  • A late upload still inserts after Cancel, Escape or a switch to read-only.
  • The image lands in the wrong table cell.
  • The image is inserted into a document that setMarkdown has replaced.
  • A failed upload is an unhandled rejection.

The label id fix is good too. A few things before it can go in:

1. insertImage$ keeps the unguarded path. The public signal still uses .then(handler) and reads activeEditor$ when the upload resolves, so the same stale-insert bug remains for imperative callers. Please send it through the same session/isCurrent guard.

2. Closing the dialog on every setMarkdown. The new link closes an open dialog on any non-whitespace markdown change, even when no upload is running. A parent that re-syncs non-normalized markdown on every change would close the dialog while the user types a URL. Please limit this to invalidating a pending upload, or explain why the dialog must close.

3. Dropping the upload when the active editor changes. This conflicts with 72b0140, where paste and drop uploads finish in the editor they started in. The dialog should behave the same way: capture the target editor when the dialog opens and insert there.

Smaller things

  • Upload errors no longer reach the console. Please keep a console.error, so failures can still be diagnosed when a custom dialog ignores imageUploadState$.
  • Please drop the table-image-dialog.spec.ts hunk. That fixture is on main now.

The progress element and retry text are fine in the default dialog, because imageUploadState$ lets custom dialogs render their own.

Integrate current main without rewriting branch history. Guard imperative insertion, capture dialog targets at open, preserve idle URL dialogs on document resync, and retain upload diagnostics.

Copy link
Copy Markdown
Contributor Author

Updated this in 1972c6dd5189634b425e0947f4777366a9899d16 following your review.

  • Public insertImage$ uploads and dialog saves now share the request/document/editor validity guard. Imperative uploads capture the target at invocation; dialogs capture it when opened, so changing focus before submission or during upload does not redirect the result.
  • Document replacement invalidates pending uploads without closing an idle dialog or clearing its fields. Cancel/replacement/read-only/unmount cases still reject stale results.
  • Kept console.error diagnostics, including custom-dialog failures, without leaking stale errors into a newer dialog. Retained progress/retry behavior and independent concurrent public uploads.
  • Integrated main; the Firefox fixture has no feature diff.

The focused integration suites passed 73 tests; the full suite passed 296 (1 existing skip, 1 todo), with lint/typecheck/build and independent review passing. Exact-head CI passed Test, Build JS and Build Docs, including packed React 18/19 consumers and cross-version replay. Browser validation remains incomplete: installation was cancelled before the browser test step, which was skipped. This supersedes the earlier pending CI status in the description. Target-switch coverage is from the integration tests, not a new browser pass.

@petyosi petyosi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the update. Sharing one guard between insertImage$ and the dialog, keeping idle dialogs open on setMarkdown, and capturing the target editor are the right direction. A few regressions came with it:

1. Imperative uploads are tied to the dialog. insertImage$ attaches its request to any open dialog (image/index.ts:294), and every dialog state change deletes those requests (:224-227). Repro: openNewImageDialog$, then insertImage$({ file }), then closeImageDialog$, then the upload resolves. Main inserts the image. This PR drops it silently, and image-upload-lifecycle.test.tsx:552 asserts the drop. An imperative upload should not depend on dialog state. Please pass null there and invert that test.

2. Synchronous { src } inserts now go through the upload guard. startRequest needs a connected root, so a plugin that publishes insertImage$({ src }) in postInit (editor exists, root not attached yet) now loses the image with no error. Main inserted it. The same guard also turns insertImage$ into a no-op in read-only editors, and saveImage$ into a no-op when no dialog is open (:325). Main handled both. Please apply the lifetime guard only to asynchronous file uploads and keep { src } inserts as they were.

3. A dialog whose target editor is gone breaks without feedback.

  • Open the dialog in a table cell, submit an upload, remove the table, then let the upload resolve. save only calls finish() (:297), so the state stays uploading. The fields and Save stay disabled, and only Cancel works.
  • Open an idle dialog in a table cell, enter a URL, call setMarkdown, then press Save. startRequest returns undefined (:325-327). The dialog stays open, nothing is inserted, and nothing is logged.

While the dialog session is current but its target no longer exists, please close the dialog or publish an error state. Never return silently.

Smaller things

  • A custom dialog that publishes saveImage$ and closes right away now loses the upload. That follows from "Cancel discards", but please document it for custom dialogs.
  • The setMarkdown$ filter (:238) repeats core's trim check and ignores viewMode$.
  • Dialog session identity depends on map(() => ({ type: 'new' })) producing a fresh object (:457). A comment would help, or replace it with a counter. One document-generation counter, bumped on setMarkdown, read-only and dispose, could also replace the requests set, dialogRequest and disposed, and would fix (1) by design.
  • The browser job was cancelled, so image-upload-lifecycle.spec.ts has never run. Please get it green.

…rgets

Separate asynchronous file lifetime guards from synchronous URL insertion.
Keep imperative file uploads independent of dialog sessions, close stale-target
dialogs, match source-mode document replacement semantics, and document custom
dialog cancellation. Add regressions for the reviewed public API contracts.

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. Addressed in d4105b8 and 794068c:

  • Imperative insertImage$({file}) now uses an independent request (null dialog session); closing a dialog no longer cancels it. The previous cancellation test was inverted.
  • Lifecycle guards apply to asynchronous file requests, not synchronous src insertion/save. Regression coverage includes postInit before root attachment, readOnly, and save without an open dialog.
  • A removed target editor closes the matching dialog session on upload settlement or idle URL save, rather than leaving it uploading; stale completions cannot close a newer session.
  • Custom-dialog documentation now explicitly states that closing immediately after publishing a file save discards that upload. setMarkdown$ matches core's rich-text/source-mode no-op comparison; the fresh dialog-object session identity is documented.
  • The first real browser run exposed an existing-image edit path capturing the wrong active editor when the table cell had not been selected. The settings toolbar now publishes its actual owner before opening the dialog; both strengthened unit cases failed before the fix and pass after it.

Validation for exact head 794068c: CI passes all four jobs: full lint/typecheck, 368 unit tests passed (1 skipped, 1 todo), JS/docs builds, and 168 browser tests across Chromium/Firefox/WebKit. Local focused suite: 80 passed; independent static review completed. No cancelled browser run is being counted as validation.

@petyosi
petyosi merged commit 7564707 into mdx-editor:main Nov 2, 2026
4 checks passed
@petyosi

petyosi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merged, thanks. All three points are fixed:

  • uploads started with insertImage$({ file }) no longer depend on the dialog;
  • { src } inserts are synchronous again;
  • a dialog whose target editor is gone now closes instead of getting stuck.

The setMarkdown$ filter now matches core in both view modes.

On top of it, 0d0cfba fixes one case the session tokens made easy to reach. If saveImage$ is published while no dialog is open, and the user opens a dialog before that upload settles, the settling upload closed the new dialog (on success) or set it to error (on failure). Now it does neither if a dialog has opened in the meantime. The commit adds tests for both outcomes and also removes the redundant inner 'file' in values check in the insertImage$ handler.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 4.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loading Wheel for Image Upload

2 participants