Skip to content

[WC-3435]: Image Cropper Design and feats#2280

Open
rahmanunver wants to merge 20 commits into
mainfrom
feat/image-cropper-polish-rotate-bw
Open

[WC-3435]: Image Cropper Design and feats#2280
rahmanunver wants to merge 20 commits into
mainfrom
feat/image-cropper-polish-rotate-bw

Conversation

@rahmanunver

@rahmanunver rahmanunver commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Pull request type

New feature (non-breaking change which adds functionality)


Description

Initial development of the Image Cropper widget. This is a pre-release widget; the items below describe its current capabilities, not changes to a shipped version.

Cropping

  • Interactive crop with configurable shape (rectangle / circle) and aspect ratio (free or custom)
  • Zoom via inline slider and optional mouse-wheel zoom
  • Output configuration: format, quality, and size (original or viewport)

Toolbar

  • Single-row toolbar: flip-left, flip-right, grayscale, zoom slider, reset
  • Flip controls (90° rotation under the hood) rendered as SVG icon buttons
  • Grayscale toggle
  • Reset to the original image
  • Always-on native tooltips on every toolbar button

Editor experience (Studio Pro)

  • Structure-mode and design-mode previews
  • Design-mode preview renders the real crop area against the configured image
  • Studio Pro toolbox category and icons

Layout

  • Widget sizes to its image and renders block-level, so sibling widgets stack below it
  • Editor preview fills the available width

Behavior

  • Grayscale stays reversible after a flip; grayscale is baked into the saved file only when the toggle is on
  • Crop auto-commit is gated on a genuine user drag, so editing one cropper instance does not commit others in a list
  • Crop alignment preserved across flips via a live blob preview
  • Image source guarded through a safeImageUri allowlist

Testing

  • Unit/integration specs for flip, grayscale, zoom, crop commit, grayscale reversibility, multi-instance commit isolation, and editor previews (103 tests)

What should be covered while testing?

  • Crop, zoom, flip, grayscale, and reset on a bound image attribute
  • Grayscale ON -> flip -> grayscale OFF restores color; grayscale ON -> flip -> save with no further crop persists grayscale
  • Two croppers in a list: editing one and saving commits only that instance

@rahmanunver rahmanunver requested a review from a team as a code owner June 19, 2026 07:49
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@rahmanunver rahmanunver force-pushed the feat/image-cropper-polish-rotate-bw branch from 60f8a9b to 2741159 Compare June 25, 2026 08:15
rahmanunver and others added 15 commits June 29, 2026 15:29
… fix crop alignment

Replace CSS-transform rotation with a pixel re-bake so the crop selection
maps to the visible image, and add a blob-URL live preview so the rotation
is visible before the deferred Mendix commit (Save). Reset now restores the
true first-load original via an internal-change gate in useOriginalImage.
…esign mode

Simplify structure mode to a title row plus a single status/config row
([No attribute selected] vs config summary), dropping the icon and large
image render. Rewrite design mode into a three-state preview driven by the
bound image: static images render the real CropArea (non-interactive) with a
config caption, while dynamic/unbound images show a placeholder glyph with
[No image selected yet].

Extract shared describeConfig/aspectLabel into utils, add the cropper
placeholder asset, declare the png module for TypeScript, and cover both
editor surfaces with new specs.
…t fixes

Terminology:
- rename user-facing "Rotate" to "Flip" (toolbar, tooltips, enableFlip property)
- rename "Black and white"/"B&W" toggle to "Grayscale"

Toolbar:
- single-row layout: flip-left, flip-right, grayscale, zoom slider, reset
- flip buttons use SVG icons; square, icon-only styling
- always-on native tooltips on every toolbar button

Layout:
- size widget to its image and render block-level so sibling widgets stack below
- editor design-mode preview fills available width
- remove redundant "Image cropper" label above the design-mode preview

Behavior:
- keep a color working image on flip so grayscale stays reversible; bake
  grayscale only into the committed file when the toggle is on
- gate crop auto-commit on a genuine user drag so editing one cropper does not
  commit sibling instances in a list

Tests: rename specs for flip/grayscale; add grayscale-reversibility and
multi-instance commit regression specs.
@r0b1n r0b1n force-pushed the feat/image-cropper-polish-rotate-bw branch from 2741159 to 1472687 Compare June 29, 2026 15:05
@github-actions

This comment has been minimized.

Remove keepSelection and add MIN_CROP_PX min width/height so dragging on
empty canvas starts a fresh selection instead of nudging the old one.

A bare click (mousedown+mouseup, no drag) yields a ~0-size crop from
react-image-crop that minWidth/minHeight does not catch, which would wipe
the box. Guard it with isStrayCrop: crops below MIN_DRAW_FRACTION of the
displayed image are ignored so the existing selection survives.
Reset previously cleared the selection, leaving no box. Restore the
original bytes and re-seed the 80%-centered default via the extracted
buildInitialCrop (now in utils/initialCrop). Two paths: when restoring
changed the uri, CropArea's onLoad re-seeds against the correct
dimensions; when nothing was edited the uri is unchanged and onLoad
won't refire, so the container re-seeds directly from imageRef.

Also mirror handleFlip and drive the live preview with the original
bytes, so a stale rotated/flipped blob stops rendering after Reset.
Zoom now anchors on a fixed point (the crop-box center, captured when the
zoom value changes and frozen while the box moves/draws), so scaling keeps
that point on screen instead of drifting. The container owns the anchor and
passes it to both CropArea's transformOrigin and the export math.

Centralize the source-rect mapping into computeSourceRect, shared by
cropImage and PreviewPane, so exported pixels match the on-screen framing.
This also fixes a pre-existing off-center bug: the old pixelCrop.x / z math
assumed a top-left origin while the CSS used center, giving the wrong region
for any off-center crop. The read window is clamped into the image to guard
zoom-out (z < 1) from reading off-canvas.
@github-actions

This comment has been minimized.

The buttons rotate the image by ±90° (via rotateImage), so "flip" was the
wrong term. Rename it everywhere: the enableFlip XML property key becomes
enableRotation, the rotate-left/rotate-right icon assets, CropToolbar props
and aria-labels/titles, the container's handleRotate handler, and all
affected tests, comments, and generated typings.

Also polish the toolbar: render the "Zoom" label at regular weight (Atlas
styles labels bold), and rename the Reset button caption to "Enable reset".
The showResetButton property key is unchanged to avoid breaking existing
app bindings.
@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

AI Code Review

🔶 Changes requested — one or more medium-severity items must be addressed


What was reviewed

File Change
src/ImageCropper.xml New widget manifest
typings/ImageCropperProps.d.ts Generated container/preview prop types
src/ImageCropper.tsx Widget entry point
src/components/ImageCropperContainer.tsx Main container — wires all hooks, handles crop/rotate/reset
src/components/CropArea.tsx Renders react-image-crop + zoom + safeImageUri guard
src/components/CropToolbar.tsx Toolbar (rotate, grayscale, zoom slider, reset)
src/components/PreviewPane.tsx Canvas-based live crop preview pane
src/hooks/useAutoApplyCrop.ts Debounced auto-commit hook
src/hooks/useImageCropperState.ts State container hook
src/hooks/useOriginalImage.ts Captures original bytes for Reset
src/hooks/usePreviewSrc.ts Blob URL bridge for deferred Mendix commits
src/utils/cropImage.ts Canvas crop export
src/utils/rotateImage.ts Canvas rotation export
src/utils/safeImageUri.ts URI allowlist guard
src/utils/cropGuard.ts Stray-click filter
src/utils/initialCrop.ts Default 80%-centered crop box
src/utils/describeConfig.ts Studio Pro caption formatter
src/ImageCropper.editorConfig.ts Studio Pro structure/design-mode preview
src/ImageCropper.editorPreview.tsx Design-mode preview component
src/__tests__/*.spec.tsx Container/integration tests
src/components/__tests__/*.spec.tsx Component unit tests
src/hooks/__tests__/*.spec.ts Hook unit tests
src/utils/__tests__/*.spec.ts Utility unit tests
src/ui/ImageCropper.scss Widget SCSS
jest.config.js Jest config (extends base)
CHANGELOG.md Changelog — "Initial release" entry present

Skipped (out of scope): dist/, pnpm-lock.yaml, PNG/SVG assets, openspec/


Findings

🔶 Medium — 3 unit tests failing in CI

File: src/__tests__/ImageCropper.editor.spec.tsx lines 55, 60, 72
Problem: The CI "Run code quality check" job exits with code 1. Three tests in ImageCropper.editor.spec.tsx fail because the actual structure-preview output doesn't match what the spec expects.

Test 1 expects the text node "Image cropper" but the component returns "[Configure Image Cropper]".
Test 2 expects "[No attribute selected]" but receives "[Configure Image Cropper]".
Test 3 expects "Circle · 1:1 · JPEG · Viewport" but receives "[Circle · 1:1 · JPEG · Viewport] Image Cropper" — the whole string is wrapped with a widget-title prefix instead of being a standalone body text.

The mismatch is between editorConfig.ts's getPreview implementation (which wraps the caption inside "[…] Image Cropper") and the collectText helper in the spec that expects the caption and title to be separate Text nodes. Either the structure-preview tree layout doesn't match the assertions, or the caption strings changed during development and the tests were never updated.

Fix: Bring the spec into sync with the actual getPreview output. Based on what CI reports, the simplest fix is:

// test 1 — title is embedded in the body string, not a separate node
expect(texts.some(t => t.includes("Image Cropper"))).toBe(true);

// test 2 — placeholder is different
expect(texts).toContain("[Configure Image Cropper]");

// test 3 — full caption string, not just the config part
expect(texts).toContain("Circle · 1:1 · JPEG · Viewport");  // this one only needs the full string to be present as a sub-item — check whether collectText splits it

Alternatively, align the spec assertions to "[Configure Image Cropper]" and "[Circle · 1:1 · JPEG · Viewport] Image Cropper" if those are the intended strings. Do not merge until tests are green.


🔶 Medium — Lint warning (import order) in CI

File: src/components/CropArea.tsx line 6
Problem: CI lint step reports:

warning  `../utils/cropGuard` import should occur before import of `../utils/cropImage`  import/order

While this is a warning not an error, it contributes to the failed CI job and will accumulate technical debt. The auto-format hook does not fix import ordering.

Fix: Move the cropGuard import above the cropImage import:

import { isStrayCrop, MIN_CROP_PX } from "../utils/cropGuard";
import { CENTER_ANCHOR, type ZoomAnchor } from "../utils/cropImage";

⚠️ Low — editorPreview.tsx: createRef used inside a function component

File: src/ImageCropper.editorPreview.tsx line 22
Note: createRef is used inside StaticCropPreview instead of useRef. createRef creates a new ref object on every render — for a functional component useRef is the correct API. In the editor context this is unlikely to cause visible problems (the preview re-renders infrequently), but it's a latent misuse of the ref API.

// Replace
const imageRef = createRef<HTMLImageElement>();
// With
const imageRef = useRef<HTMLImageElement>(null);

⚠️ Low — usePreviewSrc: state mutation during render

File: src/hooks/usePreviewSrc.ts lines 38–44
Note: The prevUri comparison and the revoke()/setPreviewSrc(undefined) calls happen unconditionally during render (not inside a useEffect or useMemo). Calling setPreviewSrc during render is allowed by React as a synchronous bail-out, but mixing revoke() (a side-effect on a Ref) with the state update makes the pattern fragile and will trigger warnings in React Strict Mode's double-invoke behaviour. Consider wrapping this in a useEffect that reacts to committedUri changes instead.


Positives

  • safeImageUri correctly allowlists only https?:, blob:, and data:image/ URIs with a tested unit spec — good defense-in-depth against XSS via Mendix image attributes.
  • onCropAction.canExecute is properly checked before execute() — Mendix API contract honoured.
  • props.image.status and props.image.readOnly are checked in both applyCrop and handleRotate before any setValue call — correct guard against read-only and loading states.
  • The useAutoApplyCrop / userDraggedRef pattern cleanly separates layout-driven onCropComplete from genuine user drags, preventing unintended auto-commits in list views — the multi-instance test suite confirms this.
  • useOriginalImage's async fetch sets a cancelled flag and returns a cleanup function — correct async-effect guard against state updates after unmount.
  • computeSourceRect is shared between cropImage and PreviewPane, ensuring the preview and the exported file always frame the same pixels.
  • Jest config correctly extends @mendix/pluggable-widgets-tools/test-config/jest.config, and setupFilesAfterEnv is properly extended rather than replaced.
  • 115 tests across 18 suites, with real scenario coverage (grayscale reversibility, multi-instance isolation, reset, zoom debounce).

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.

2 participants