[PR #661] [MERGED] feat(avatar-crop): full-image mode + persona crop UI #4718

Closed
opened 2026-10-22 04:10:05 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/661
Author: @Romuromylus
Created: 5/10/2026
Status: ✅ Merged
Merged: 5/11/2026
Merged by: @Romuromylus

Base: main ← Head: feat/avatar-crop-full-image


📝 Commits (10+)

  • 4c132f0 feat(avatar-crop): support full-image mode (zoom < 1, pan-anywhere)
  • 7633c25 feat(persona): add avatarCrop column end-to-end (schema, storage, routes, hooks, importer)
  • ce78aac feat(persona): surface avatar crop widget in PersonaEditor + PersonasPanel
  • f36426b feat(avatar-crop): replace zoom+pan widget with square-region crop selector
  • 93ff857 fix(persona-panel): clip oversized cropped with inner overflow-hidden wrapper
  • 3485e6d fix(cropper): force remount on src change so cached images still init the overlay
  • 2c5ac21 fix(cropper): defensively run handleImgLoad for cached images
  • fb075fb docs(changelog): note avatar crop tool rework under [Unreleased] Added
  • 1769e84 fix(avatar-crop): address CodeRabbit review on PR #661
  • 8dc39bb Merge branch 'main' into feat/avatar-crop-full-image

📊 Changes

15 files changed (+744 additions, -159 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/client/src/components/characters/CharacterEditor.tsx (+19 -129)
📝 packages/client/src/components/game/GameNarration.tsx (+4 -4)
📝 packages/client/src/components/game/GameSurface.tsx (+2 -2)
📝 packages/client/src/components/panels/PersonasPanel.tsx (+79 -13)
📝 packages/client/src/components/personas/PersonaEditor.tsx (+100 -5)
➕ packages/client/src/components/ui/AvatarCropWidget.tsx (+424 -0)
📝 packages/client/src/hooks/use-characters.ts (+2 -0)
📝 packages/client/src/lib/utils.ts (+66 -6)
📝 packages/server/src/db/migrate.ts (+6 -0)
📝 packages/server/src/db/schema/characters.ts (+2 -0)
📝 packages/server/src/routes/characters.routes.ts (+1 -0)
📝 packages/server/src/services/import/marinara.importer.ts (+8 -0)
📝 packages/server/src/services/storage/characters.storage.ts (+5 -0)
📝 packages/shared/src/types/persona.ts (+25 -0)

📄 Description

Linked issue

Closes #658

Why this change

The avatar crop tool was a slider + drag-to-pan over a circular preview — a soft visual nudge, not a real crop selector. Users with tall portraits couldn't reach face-centered crops without fighting the controls, and Personas had no crop UI at all. This PR replaces the inline widget with a proper square-region cropper, brings the same widget to Personas, and makes both crops non-destructive so the original avatar file (used by the Roleplay glued side panel) is never overwritten.

What changed

Cropper UX (the headline)

  • New AvatarCropWidget shows the source image at full size with a draggable square overlay: 4 corner handles + interior pan, dark mask outside the selection, live circle preview next to it.
  • Square-locked in source pixels (because the avatar circle is square; freeform rectangle would just get distorted by the circle clip). The data shape itself is generic enough for freeform later if you ever want it.
  • Both Characters and Personas use the same widget. Persona avatars previously had no crop UI at all.

Data model

  • AvatarCrop is now { srcX, srcY, srcWidth, srcHeight } normalized to source image dimensions. The editor enforces a square crop in source pixels (srcWidth * sourceW === srcHeight * sourceH).
  • LegacyAvatarCrop (the prior { zoom, offsetX, offsetY, fullImage } shape) is preserved as a render-only path. Existing crops keep rendering unchanged via the historical CSS transform until the user re-edits them with the new cropper, at which point the widget writes the current shape. No automatic migration — backward compatible by construction.
  • getAvatarCropStyle discriminates on shape: legacy crops keep returning the transform; current crops return absolute-position styles that map the crop rectangle onto the container's full area without distortion at any source aspect ratio.

Server / persistence

  • personas.avatar_crop TEXT column added (CREATE TABLE + idempotent ADD COLUMN migration), wired through createPersona / updatePersona / duplicatePersona. POST /personas accepts it; PATCH already forwards arbitrary fields. Marinara importer round-trips it (handles object or stringified shapes). PersonaAvatarCrop type lives in packages/shared/src/types/persona.ts.
  • Character crops continue to live under extensions.avatarCrop via the existing storage path.

UI integration

  • CharacterEditor MetadataTab and PersonaEditor Description tab both host the new AvatarCropWidget.
  • PersonasPanel (main row + group member list) renders the crop on persona avatar tiles via a small parsePersonaAvatarCrop helper that defensively validates either shape.
  • PersonasPanel main row's avatar <button> got an inner relative overflow-hidden rounded-xl wrapper for the <img> so an oversized cropped image is clipped to the rounded tile — needed because the button itself can't have overflow:hidden (would clip the active-indicator star and camera-hover overlay).
  • PersonasPanel group-member tile got relative overflow-hidden added for the same reason.

Cropper init for cached images

  • The avatar URL is almost always cached (the persona/character panel just rendered a thumbnail of the same URL), and browsers fire the load event so quickly that React's onLoad listener can miss it for the new src. Two complementary defenses:
    • key={src} on the <img> forces a remount when the source changes.
    • A deps-less useEffect checks img.complete after every render and runs handleImgLoad if imgRect is still null. The null-guard makes it a no-op once initialized.
  • Verified on the deployed instance — switching between personas reliably initializes the cropper for each one (was previously broken: the second persona's overlay was missing).

Out of scope (deliberate deferral)

  • Plumbing AvatarCrop through ChatArea.tsx into chat-side render sites — chat bubbles, QuickPersonaSwitcher, QuickSwitcherMobile, ChatSetupWizard / GameSetupWizard, and ChatSettingsDrawer's persona chooser. Those keep rendering personas the way they did before. The follow-up will add userPersonaInfo.avatarCrop, sweep position: relative + overflow-hidden across those parents (same pattern as the persona panel fix here), and verify in chat.
  • Character avatars in chat already receive crop data; if a character is re-edited via the new editor, those chat-side render sites may need the same relative overflow-hidden treatment. None reported breaking yet but worth verifying during review.

Docs

  • CHANGELOG.md — entry under [Unreleased] / Added summarizing the user-facing change.

Validation

  • `pnpm check` passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed `CONTRIBUTING.md`

Manual verification notes

  • Manually verify an existing character with no `avatarCrop` extension renders identically in chat sidebars, chat messages, the character library, the bot browser, and the character editor header — no visual shift (legacy render path is preserved).
  • Manually verify an existing character / persona with a legacy `{ zoom, offsetX, offsetY, fullImage }` crop renders identically (legacy render path still active until re-edit).
  • Manually verify the new cropper on a tall portrait (e.g. 692×1024 Pathfinder/RT-style):
    • Drag the square overlay to a face-centered crop
    • Drag a corner — square resizes symmetrically (square-locked)
    • Live preview circle matches the chosen region
    • Header avatar in the editor mirrors the chosen region
  • Manually verify Save persists the crop (DB stores `{srcX, srcY, srcWidth, srcHeight}` JSON for personas; `extensions.avatarCrop` for characters) across reload.
  • Manually verify switching between personas in the editor reliably initializes the cropper for each one (no missing overlay on the second/third).
  • Manually verify a persona avatar with a saved crop renders the chosen framing in the Personas panel list and in any persona group's member list — no overflow into adjacent rows.
  • Manually verify the persona import/export round-trip: export a persona with a crop set, re-import, confirm the crop survives.
  • Manually verify the DB migration runs cleanly on an upgraded install (existing `personas` table with no `avatar_crop` column yet).

Docs and release impact

  • No docs changes needed (other than the CHANGELOG entry already in this PR)
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

To be added — the editor flow: persona row → editor opens → cropper widget with image canvas + draggable square overlay + live preview circle → save → tile reflects the new crop in the persona panel.

Summary by CodeRabbit

Release Notes

New Features

  • Redesigned avatar crop editor with an improved square-region selector featuring corner resize handles and interior panning for precise avatar framing control
  • Avatar cropping functionality now available for Personas, enabling the same editing capabilities as character avatars
  • Original avatar files remain unmodified; the full portrait is displayed in the side panel for visual reference during cropping

Review Change Stack


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/661 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/10/2026 **Status:** ✅ Merged **Merged:** 5/11/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `main` ← **Head:** `feat/avatar-crop-full-image` --- ### 📝 Commits (10+) - [`4c132f0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4c132f0849130cfad0cae9c005b21512bf58af26) feat(avatar-crop): support full-image mode (zoom < 1, pan-anywhere) - [`7633c25`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7633c25b9a0080066cf92bd81ab56a464e4b4c9f) feat(persona): add avatarCrop column end-to-end (schema, storage, routes, hooks, importer) - [`ce78aac`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ce78aaca5ac7a761f02c5f960de6d4d7af284ed7) feat(persona): surface avatar crop widget in PersonaEditor + PersonasPanel - [`f36426b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f36426b7eda090c881b1b4cbaea822134d333d81) feat(avatar-crop): replace zoom+pan widget with square-region crop selector - [`93ff857`](https://github.com/Pasta-Devs/Marinara-Engine/commit/93ff85762602c7fe63073a10c944a221ea11678a) fix(persona-panel): clip oversized cropped <img> with inner overflow-hidden wrapper - [`3485e6d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3485e6d25cf731da58ff5cd41eca51fc92b1117d) fix(cropper): force <img> remount on src change so cached images still init the overlay - [`2c5ac21`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2c5ac21ea7fd74a4d268f8cbefcb29938fbcbe82) fix(cropper): defensively run handleImgLoad for cached images - [`fb075fb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/fb075fb104963f3edf179c5166686cfff262b670) docs(changelog): note avatar crop tool rework under [Unreleased] Added - [`1769e84`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1769e842caf9db910739c87f5e0b0a5348c08ac0) fix(avatar-crop): address CodeRabbit review on PR #661 - [`8dc39bb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8dc39bbc741d2917325fdca14a5ea6c16c85ed89) Merge branch 'main' into feat/avatar-crop-full-image ### 📊 Changes **15 files changed** (+744 additions, -159 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/client/src/components/characters/CharacterEditor.tsx` (+19 -129) 📝 `packages/client/src/components/game/GameNarration.tsx` (+4 -4) 📝 `packages/client/src/components/game/GameSurface.tsx` (+2 -2) 📝 `packages/client/src/components/panels/PersonasPanel.tsx` (+79 -13) 📝 `packages/client/src/components/personas/PersonaEditor.tsx` (+100 -5) ➕ `packages/client/src/components/ui/AvatarCropWidget.tsx` (+424 -0) 📝 `packages/client/src/hooks/use-characters.ts` (+2 -0) 📝 `packages/client/src/lib/utils.ts` (+66 -6) 📝 `packages/server/src/db/migrate.ts` (+6 -0) 📝 `packages/server/src/db/schema/characters.ts` (+2 -0) 📝 `packages/server/src/routes/characters.routes.ts` (+1 -0) 📝 `packages/server/src/services/import/marinara.importer.ts` (+8 -0) 📝 `packages/server/src/services/storage/characters.storage.ts` (+5 -0) 📝 `packages/shared/src/types/persona.ts` (+25 -0) </details> ### 📄 Description ## Linked issue Closes #658 ## Why this change The avatar crop tool was a slider + drag-to-pan over a circular preview — a soft visual nudge, not a real crop selector. Users with tall portraits couldn't reach face-centered crops without fighting the controls, and Personas had no crop UI at all. This PR replaces the inline widget with a proper square-region cropper, brings the same widget to Personas, and makes both crops non-destructive so the original avatar file (used by the Roleplay glued side panel) is never overwritten. ## What changed ### Cropper UX (the headline) - New `AvatarCropWidget` shows the source image at full size with a draggable square overlay: 4 corner handles + interior pan, dark mask outside the selection, live circle preview next to it. - Square-locked in source pixels (because the avatar circle is square; freeform rectangle would just get distorted by the circle clip). The data shape itself is generic enough for freeform later if you ever want it. - Both Characters and Personas use the same widget. Persona avatars previously had no crop UI at all. ### Data model - `AvatarCrop` is now `{ srcX, srcY, srcWidth, srcHeight }` normalized to source image dimensions. The editor enforces a square crop in source pixels (`srcWidth * sourceW === srcHeight * sourceH`). - `LegacyAvatarCrop` (the prior `{ zoom, offsetX, offsetY, fullImage }` shape) is preserved as a render-only path. Existing crops keep rendering unchanged via the historical CSS transform until the user re-edits them with the new cropper, at which point the widget writes the current shape. **No automatic migration** — backward compatible by construction. - `getAvatarCropStyle` discriminates on shape: legacy crops keep returning the transform; current crops return absolute-position styles that map the crop rectangle onto the container's full area without distortion at any source aspect ratio. ### Server / persistence - `personas.avatar_crop` TEXT column added (CREATE TABLE + idempotent ADD COLUMN migration), wired through `createPersona` / `updatePersona` / `duplicatePersona`. POST /personas accepts it; PATCH already forwards arbitrary fields. Marinara importer round-trips it (handles object or stringified shapes). `PersonaAvatarCrop` type lives in `packages/shared/src/types/persona.ts`. - Character crops continue to live under `extensions.avatarCrop` via the existing storage path. ### UI integration - `CharacterEditor` MetadataTab and `PersonaEditor` Description tab both host the new `AvatarCropWidget`. - `PersonasPanel` (main row + group member list) renders the crop on persona avatar tiles via a small `parsePersonaAvatarCrop` helper that defensively validates either shape. - `PersonasPanel` main row's avatar `<button>` got an inner `relative overflow-hidden rounded-xl` wrapper for the `<img>` so an oversized cropped image is clipped to the rounded tile — needed because the button itself can't have `overflow:hidden` (would clip the active-indicator star and camera-hover overlay). - `PersonasPanel` group-member tile got `relative overflow-hidden` added for the same reason. ### Cropper init for cached images - The avatar URL is almost always cached (the persona/character panel just rendered a thumbnail of the same URL), and browsers fire the `load` event so quickly that React's `onLoad` listener can miss it for the new src. Two complementary defenses: - `key={src}` on the `<img>` forces a remount when the source changes. - A deps-less `useEffect` checks `img.complete` after every render and runs `handleImgLoad` if `imgRect` is still null. The null-guard makes it a no-op once initialized. - Verified on the deployed instance — switching between personas reliably initializes the cropper for each one (was previously broken: the second persona's overlay was missing). ### Out of scope (deliberate deferral) - Plumbing `AvatarCrop` through `ChatArea.tsx` into chat-side render sites — chat bubbles, `QuickPersonaSwitcher`, `QuickSwitcherMobile`, `ChatSetupWizard` / `GameSetupWizard`, and `ChatSettingsDrawer`'s persona chooser. Those keep rendering personas the way they did before. The follow-up will add `userPersonaInfo.avatarCrop`, sweep `position: relative` + `overflow-hidden` across those parents (same pattern as the persona panel fix here), and verify in chat. - Character avatars in chat already receive crop data; if a character is re-edited via the new editor, those chat-side render sites may need the same `relative overflow-hidden` treatment. None reported breaking yet but worth verifying during review. ### Docs - `CHANGELOG.md` — entry under `[Unreleased] / Added` summarizing the user-facing change. ## Validation - [x] \`pnpm check\` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed \`CONTRIBUTING.md\` ### Manual verification notes - Manually verify an existing character with no \`avatarCrop\` extension renders identically in chat sidebars, chat messages, the character library, the bot browser, and the character editor header — no visual shift (legacy render path is preserved). - Manually verify an existing character / persona with a legacy \`{ zoom, offsetX, offsetY, fullImage }\` crop renders identically (legacy render path still active until re-edit). - Manually verify the new cropper on a tall portrait (e.g. 692×1024 Pathfinder/RT-style): - Drag the square overlay to a face-centered crop - Drag a corner — square resizes symmetrically (square-locked) - Live preview circle matches the chosen region - Header avatar in the editor mirrors the chosen region - Manually verify Save persists the crop (DB stores \`{srcX, srcY, srcWidth, srcHeight}\` JSON for personas; \`extensions.avatarCrop\` for characters) across reload. - Manually verify switching between personas in the editor reliably initializes the cropper for each one (no missing overlay on the second/third). - Manually verify a persona avatar with a saved crop renders the chosen framing in the Personas panel list and in any persona group's member list — no overflow into adjacent rows. - Manually verify the persona import/export round-trip: export a persona with a crop set, re-import, confirm the crop survives. - Manually verify the DB migration runs cleanly on an upgraded install (existing \`personas\` table with no \`avatar_crop\` column yet). ## Docs and release impact - [x] No docs changes needed (other than the CHANGELOG entry already in this PR) - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) To be added — the editor flow: persona row → editor opens → cropper widget with image canvas + draggable square overlay + live preview circle → save → tile reflects the new crop in the persona panel. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes **New Features** - Redesigned avatar crop editor with an improved square-region selector featuring corner resize handles and interior panning for precise avatar framing control - Avatar cropping functionality now available for Personas, enabling the same editing capabilities as character avatars - Original avatar files remain unmodified; the full portrait is displayed in the side panel for visual reference during cropping [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/Pasta-Devs/Marinara-Engine/pull/661) <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:10:05 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4718
No description provided.