Repository navigation
Persist rich text image settings after authors apply edits - #2898
Conversation
🦋 Changeset detectedLatest commit: 3130949 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
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 |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
This bug fix makes image attribute edits apply live in the admin panel and keeps captions and tooltip titles as separate values. That is the right problem and the right place to fix it: ImageNodeView already opens an ImageDetailPanel whose onUpdate calls TipTap's updateAttributes, so wiring every control directly to onUpdate is a clean way to persist edits as authors type. The frontend Image.astro renderer correctly picks up the new title field, and the converter types have been updated.
I checked:
- The admin and inline ProseMirror ⇄ Portable Text converters.
- The admin
ImageExtensionschema (it already declarescaption, so the new field round-trips safely). - The frontend
Image.astrorender output. - The new and updated tests.
- The changeset.
There is one real regression that contradicts the PR description: title-only legacy Portable Text image nodes lose their captions when loaded into the editor. The PM→PT half of the code has a fallback that reads title as caption only when the caption attribute is absent. But the PT→PM converters always add a caption attribute (as an empty string), so the fallback is never reached for legacy data. The new legacy tests pass because they construct raw ProseMirror JSON, not real Portable Text input, so they bypass the problematic code path.
Other than that, the changeset is clear and the changes are localized. There is also a small import-convention slip in the new test file.
Overlapping PRsThis PR modifies files that are also changed by other open PRs:
This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
There was a problem hiding this comment.
This re-review checks the fixes since the previous COMMENTED review. The approach remains sound: wiring every image detail control directly to onUpdate is the right way to make image edits persist as authors type, and the converter-side separation of caption and title is the right place to keep the two values independent.
What I verified:
- Legacy title-only fallback is fixed. In the current code, every PT→PM converter (admin
PortableTextEditor, coreportable-text-to-prosemirror, andInlinePortableTextEditor) now setscaptiontotitleonly when the block does not already carry acaptionfield. The PM→PT side preserves an explicitly emptycaptionwhentitleis present. The new tests exercise real Portable Text input as well as raw PM JSON, so the prior regression concern is addressed. - Live edits propagate correctly.
ImageDetailPanelnow callsonUpdateon every text, dimension, and alignment change; the Save/Cancel flow is removed consistently in both inline and overlay variants. - Frontend rendering picks up
title.Image.astronow passestitlethrough to both<AstroImage>and the fallback<img>, and the render test pins the output. - Changeset is clear, present-tense, names the affected behavior, and describes both the persistence fix and the caption/title separation. It meets the .changeset/README.md guidance for a patch entry.
One real bug remains: the dimension handlers can propagate NaN into the live ProseMirror node because parseInt can return NaN for intermediate non-numeric input, and the existing truthiness guard treats NaN as a valid number. The aspect-ratio branch then computes Math.round(NaN / aspectRatio), producing NaN for both dimensions, which serializes to the node and renders as "NaNpx" widths/heights in the editor and on the rendered page. The fix is a small Number.isNaN guard before calling onUpdate.
Overall the PR is close; once the NaN path is guarded, it should be good to merge.
| const handleWidthChange = (value: string) => { | ||
| const newWidth = value ? parseInt(value, 10) : undefined; | ||
| const newHeight = | ||
| lockAspectRatio && aspectRatio && newWidth | ||
| ? Math.round(newWidth / aspectRatio) | ||
| : displayHeight; | ||
| setDisplayWidth(newWidth); | ||
| if (lockAspectRatio && aspectRatio && newWidth) { | ||
| setDisplayHeight(Math.round(newWidth / aspectRatio)); | ||
| } | ||
| setDisplayHeight(newHeight); | ||
| onUpdate({ displayWidth: newWidth, displayHeight: newHeight }); | ||
| }; | ||
|
|
||
| const handleHeightChange = (value: string) => { | ||
| const newHeight = value ? parseInt(value, 10) : undefined; | ||
| const newWidth = | ||
| lockAspectRatio && aspectRatio && newHeight | ||
| ? Math.round(newHeight * aspectRatio) | ||
| : displayWidth; | ||
| setDisplayHeight(newHeight); | ||
| if (lockAspectRatio && aspectRatio && newHeight) { | ||
| setDisplayWidth(Math.round(newHeight * aspectRatio)); | ||
| } | ||
| setDisplayWidth(newWidth); | ||
| onUpdate({ displayWidth: newWidth, displayHeight: newHeight }); | ||
| }; |
There was a problem hiding this comment.
[needs fixing] The handleWidthChange and handleHeightChange handlers now call onUpdate live, but they do not guard against parseInt returning NaN. For an invalid intermediate value such as "e" or " " from a number input, value ? parseInt(value, 10) : undefined assigns NaN to newWidth/newHeight (because NaN is truthy). That NaN is then written into the ProseMirror node via updateAttributes(...) and can surface as "NaNpx" in the editor/published image styles.
| const handleWidthChange = (value: string) => { | |
| const newWidth = value ? parseInt(value, 10) : undefined; | |
| const newHeight = | |
| lockAspectRatio && aspectRatio && newWidth | |
| ? Math.round(newWidth / aspectRatio) | |
| : displayHeight; | |
| setDisplayWidth(newWidth); | |
| if (lockAspectRatio && aspectRatio && newWidth) { | |
| setDisplayHeight(Math.round(newWidth / aspectRatio)); | |
| } | |
| setDisplayHeight(newHeight); | |
| onUpdate({ displayWidth: newWidth, displayHeight: newHeight }); | |
| }; | |
| const handleHeightChange = (value: string) => { | |
| const newHeight = value ? parseInt(value, 10) : undefined; | |
| const newWidth = | |
| lockAspectRatio && aspectRatio && newHeight | |
| ? Math.round(newHeight * aspectRatio) | |
| : displayWidth; | |
| setDisplayHeight(newHeight); | |
| if (lockAspectRatio && aspectRatio && newHeight) { | |
| setDisplayWidth(Math.round(newHeight * aspectRatio)); | |
| } | |
| setDisplayWidth(newWidth); | |
| onUpdate({ displayWidth: newWidth, displayHeight: newHeight }); | |
| }; | |
| const handleWidthChange = (value: string) => { | |
| const parsed = value ? parseInt(value, 10) : undefined; | |
| if (Number.isNaN(parsed)) return; | |
| const newWidth = parsed; | |
| const newHeight = | |
| lockAspectRatio && aspectRatio && newWidth | |
| ? Math.round(newWidth / aspectRatio) | |
| : displayHeight; | |
| setDisplayWidth(newWidth); | |
| setDisplayHeight(newHeight); | |
| onUpdate({ displayWidth: newWidth, displayHeight: newHeight }); | |
| }; | |
| const handleHeightChange = (value: string) => { | |
| const parsed = value ? parseInt(value, 10) : undefined; | |
| if (Number.isNaN(parsed)) return; | |
| const newHeight = parsed; | |
| const newWidth = | |
| lockAspectRatio && aspectRatio && newHeight | |
| ? Math.round(newHeight * aspectRatio) | |
| : displayWidth; | |
| setDisplayHeight(newHeight); | |
| setDisplayWidth(newWidth); | |
| onUpdate({ displayWidth: newWidth, displayHeight: newHeight }); | |
| }; |
|
This PR has been inactive for 14 days. It will be closed automatically in 7 days if there is no further activity. If you're still working on this, please push an update or leave a comment. |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
docs | 81b114c | Sep 26 2026, 03:58 PM |
There was a problem hiding this comment.
The approach of this PR is sound: separate caption and title at the converter seams and wire image settings through onUpdate so they survive a save/reload cycle. The prior review's open items are fixed: PT→PM converters now only fall back to title when caption is absent, the PM→PT side preserves an explicit empty caption, non-finite dimensions are sanitized in both the panel and the renderers, and Image.astro now forwards title to the rendered image.
One real bug remains. The ImageDetailPanel still batches edits behind an Apply/Save button, which contradicts the PR title's claim that settings persist "as authors edit them" — edits only persist after Apply. More importantly, handleSave collapses an empty Caption field to caption: undefined, which removes the ProseMirror caption attribute. Both the admin convertPMNode and the core prosemirror-to-portable-text converters then interpret an absent caption as a legacy title-only block and restore the tooltip text as the caption, undoing the PR's explicit goal that "an explicitly cleared caption stays cleared." Fix handleSave to send the empty string as an explicit cleared value and add a panel-level regression test that clears the Caption field, clicks Apply, and asserts the resulting Portable Text keeps caption: "" while preserving title.
Once that is fixed and the PR title/description are aligned with the Apply/Save behavior, this should be good to merge.
| @@ -246,8 +256,8 @@ export function ImageDetailPanel({ | |||
| alt: alt || undefined, | |||
| caption: caption || undefined, | |||
There was a problem hiding this comment.
[needs fixing] handleSave collapses an empty caption to undefined, which removes the caption attribute from the ProseMirror node. Both the admin convertPMNode and the core prosemirror-to-portable-text converters treat a missing caption as a legacy title-only block and fall back to title, so a caption the author intentionally clears is restored from the tooltip text on the next save/load. This contradicts the PR's stated goal that "an explicitly cleared caption stays cleared."
Send an empty string as an explicit cleared value instead:
| caption: caption || undefined, | |
| caption, |
Add a regression test that clears the Caption field in the panel, clicks Apply, and asserts the resulting Portable Text keeps caption: "" while preserving title.
There was a problem hiding this comment.
The approach remains sound: this PR separates caption and title at the converter seams and wires image settings through the detail panel’s onUpdate so they survive a save/reopen cycle. All of the prior review’s blockers are fixed:
ImageDetailPanel.handleSavenow emits an empty string for an explicitly cleared caption instead of collapsing it toundefined, and a regression test verifies the cleared-caption/title case.- Width/height change handlers guard against non-finite intermediate values, and
ImageNodeViewandImage.astrosanitize dimensions before rendering. - Admin and core PT→PM converters only fall back to
titlefor caption whencaptionis absent, and malformed/title-only legacy blocks are handled consistently. Image.astronow forwardstitleto the rendered<img>.
One minor consistency item remains. The inline visual editor (InlinePortableTextEditor) now converts displayWidth/displayHeight to and from Portable Text, but the inline TipTap image schema was only extended with caption; it does not declare displayWidth or displayHeight. TipTap will ignore those attributes on load, so an image with custom display sizes will silently lose them during an inline-edit round trip. Add the missing attributes so the converter changes are actually preserved by ProseMirror.
The changeset names both affected packages and describes the user-facing fix, so it reads fine as a CHANGELOG entry.
There was a problem hiding this comment.
The approach is the right one for the reported bug: this PR separates caption and title at the converter seams, wires the image detail panel’s onUpdate through handleSave, and guards dimensions against non-finite values. All of the prior review’s blockers are addressed:
ImageDetailPanel.handleSavenow emitscaption: ""for an explicitly cleared caption, and a regression test verifies the cleared-caption/title case.- Width/height change handlers guard against non-finite intermediate values, and
ImageNodeViewandImage.astrosanitize dimensions before rendering. - Admin and core PT→PM converters only fall back to
titlefor caption whencaptionis absent, and malformed/title-only legacy blocks are handled consistently. Image.astronow forwardstitleto the rendered<img>.InlinePortableTextEditornow extends its inline image schema withdisplayWidth,displayHeight, andcaption.
The user-facing changeset names both affected packages and describes the fix, so it reads fine as a CHANGELOG entry.
One round-trip issue remains in the inline visual editor. The admin and core converters use truthy string extraction (attrStr/collapsed conditionals) for caption and title, so empty values are omitted from Portable Text. The inline editor’s convertPMNode uses attrStrOpt, which preserves empty strings. Because convertPTBlock initializes missing captions/titles as "" in the ProseMirror node, an inline edit of any nearby text causes the save payload to include pointless title: "" (and sometimes caption: "") blocks, diverging from admin/core output and leaking empty title="" attributes to published <img> elements. A small helper that drops empty strings before deciding what to serialize fixes it.
There was a problem hiding this comment.
This is the right fix for the reported regression: it separates caption and title at every converter seam, persists custom image dimensions through the inline editor, and guards dimensions against non-finite values. All of the prior review’s blockers are addressed in this revision.
What I checked:
ImageDetailPanel.handleSavenow emitscaption: ""so an explicitly cleared caption stays cleared, and the regression tests cover the cleared-caption + title case.- Dimension change handlers use
imageDimension()to reject non-finite / non-positive input, and bothImageNodeViewandImage.astrosanitize dimensions before rendering. - The admin
PortableTextEditorand core converters only fall back totitlefor caption whencaptionis absent; legacy title-only blocks are handled consistently, including the malformed no-asset branch. Image.astronow forwardstitleto the rendered<img>andAstroImageelements.InlinePortableTextEditorextends the inline image schema withdisplayWidth,displayHeight, andcaption, and its PM→PT converter uses truthy string extraction so empty caption/title values are dropped instead of serializing""back into stored Portable Text.- Tests added across admin, core converters, inline editor, and render paths cover round-tripping, legacy fallback, cleared captions, and non-finite dimensions.
- The changeset lists both affected packages and describes the observable fix for readers.
I did not find any remaining logic bugs, security issues, AGENTS.md convention violations, or follow-up blockers. The PR is clean.
There was a problem hiding this comment.
This is the right fix for the image settings regression, and this revision resolves every blocker from the previous review.
As a re-review, I concentrated on verifying the changes requested in the prior round:
- Explicitly cleared captions stay cleared.
ImageDetailPanel.handleSavenow emitscaptionas-is (including""), and the regression test covers the cleared-caption + retained-title case. - Non-finite dimensions are rejected everywhere. The new
imageDimension()/attrDimension()helpers guard the panel,ImageNodeView,Image.astro, and both core converters; the dimension change handlers short-circuit on invalidparseIntresults. - Title and caption round-trip independently across every seam. The admin
PortableTextEditor, coreprosemirror-to-portable-textandportable-text-to-prosemirrorconverters, and the inlineInlinePortableTextEditorall use the sameObject.hasOwn(...)fallback logic so legacy title-only blocks keep their visible caption while explicitly stored captions do not backfill fromtitle. - Inline editor schema now carries
displayWidth,displayHeight, andcaption. This closes the gap where custom sizes were lost after any inline edit, and the unchanged-save test verifies dimensions survive a subsequent edit. - Malformed / asset-less title-only blocks get the legacy caption fallback in both admin and core converters, with regression coverage.
- Rendered
<img>andAstroImageelements forwardtitle, and both English and RTL render paths filter non-finite dimensions. - Changeset lists both affected packages, uses the correct patch bump, and describes the observable author-facing fix.
I also spot-checked the new and updated tests, the import extension in image-metadata.test.ts, and the LQIP / link normalization comments; none violate the comment-discipline or convention requirements. No remaining logic bugs, security issues, AGENTS.md violations, or unaddressed prior findings.
The PR is clean.
What does this PR do?
Image settings now persist after authors click Apply, so saving and reopening the post no longer drops caption, alt text, tooltip, size, or alignment changes.
It also keeps visible captions and tooltip titles as separate values across the admin and inline editors. Existing title-only image nodes still load their title as a caption, while an explicitly cleared caption stays cleared.
Related issue: Not linked.
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
Screenshots / test output
After Apply, save, and reload in English, the image settings retain the caption, tooltip, dimensions, and alignment:
The same settings render correctly in Arabic RTL:
The cleared-caption regression was also verified after a full save and reload: the caption remains empty while the tooltip title remains present.
Verified with:
pnpm formatpnpm buildpnpm lint:quickpnpm --silent lint:json | jq '.diagnostics | length'→0pnpm typecheck