@wordpress/media-editor 0.13.1-next.v.202607070741.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/build/image-editor/react/components/cropper.cjs +1 -11
  3. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  4. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +8 -8
  5. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  6. package/build/image-editor/react/hooks/use-aria-announcer.cjs +75 -32
  7. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  8. package/build-module/image-editor/react/components/cropper.mjs +1 -11
  9. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  10. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +8 -8
  11. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  12. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +76 -38
  13. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  14. package/build-style/style-rtl.css +5 -5
  15. package/build-style/style.css +5 -5
  16. package/build-types/components/media-editor/use-crop-options.d.ts +1 -1
  17. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  18. package/build-types/components/media-editor/use-save-media-editor.d.ts +1 -1
  19. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  20. package/build-types/components/media-editor-canvas/index.d.ts +1 -1
  21. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  22. package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
  23. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  24. package/build-types/components/media-editor-fine-rotation/index.d.ts +1 -1
  25. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
  26. package/build-types/components/media-editor-image-controls/index.d.ts +1 -1
  27. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  28. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +1 -1
  29. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
  30. package/build-types/components/media-editor-modal/index.d.ts +1 -1
  31. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  32. package/build-types/components/media-editor-provider/index.d.ts +1 -1
  33. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  34. package/build-types/components/media-editor-toolbar/index.d.ts +24 -0
  35. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
  36. package/build-types/components/media-editor-transform-controls/index.d.ts +21 -0
  37. package/build-types/components/media-editor-transform-controls/index.d.ts.map +1 -0
  38. package/build-types/components/media-form/index.d.ts +1 -1
  39. package/build-types/components/media-form/index.d.ts.map +1 -1
  40. package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
  41. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
  42. package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
  43. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  44. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  45. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +1 -1
  46. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -1
  47. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +1 -1
  48. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  49. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
  50. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
  51. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
  52. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  53. package/build-types/image-editor/react/components/viewport-provider.d.ts +1 -1
  54. package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -1
  55. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +5 -4
  56. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  57. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +108 -0
  58. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
  59. package/build-types/state/media-editor-state-provider.d.ts +1 -1
  60. package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
  61. package/build-types/store/actions.d.ts +1 -1
  62. package/build-types/store/actions.d.ts.map +1 -1
  63. package/package.json +22 -21
  64. package/src/components/media-editor-image-controls/style.scss +1 -1
  65. package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +1 -1
  66. package/src/components/media-preview/style.scss +1 -1
  67. package/src/components/rotation-ruler/style.scss +2 -2
  68. package/src/image-editor/react/components/cropper.tsx +3 -12
  69. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +8 -8
  70. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +17 -15
  71. package/src/image-editor/react/components/test/cropper.tsx +17 -17
  72. package/src/image-editor/react/hooks/test/use-aria-announcer.ts +413 -5
  73. package/src/image-editor/react/hooks/use-aria-announcer.ts +120 -47
  74. package/tsconfig.json +1 -0
  75. package/tsconfig.tsbuildinfo +1 -1
@@ -1,23 +1,30 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import {
5
- useState,
6
- useEffect,
7
- useLayoutEffect,
8
- useRef,
9
- } from '@wordpress/element';
4
+ import { speak } from '@wordpress/a11y';
5
+ import { useEffect, useLayoutEffect, useRef } from '@wordpress/element';
10
6
  import { __, sprintf } from '@wordpress/i18n';
11
7
 
12
8
  /**
13
9
  * Internal dependencies
14
10
  */
15
11
  import type { CropperState } from '../../core/types';
12
+ import { getSourceRegion } from '../../core/source-region';
16
13
 
17
14
  /** Debounce delay for ARIA live announcements (ms). */
18
15
  const ARIA_DEBOUNCE_MS = 300;
19
16
 
20
- function buildFlipAnnouncement( state: CropperState ): string {
17
+ function getFlipAnnouncement(
18
+ state: CropperState,
19
+ previousState: CropperState | null
20
+ ): string | undefined {
21
+ if (
22
+ ! previousState ||
23
+ ( previousState.flip.horizontal === state.flip.horizontal &&
24
+ previousState.flip.vertical === state.flip.vertical )
25
+ ) {
26
+ return undefined;
27
+ }
21
28
  const { horizontal, vertical } = state.flip;
22
29
  if ( horizontal && vertical ) {
23
30
  return __( 'Flipped horizontally and vertically' );
@@ -31,64 +38,132 @@ function buildFlipAnnouncement( state: CropperState ): string {
31
38
  return __( 'Flip removed' );
32
39
  }
33
40
 
34
- // Build a human-readable announcement string from cropper state.
35
- function buildAnnouncement(
41
+ function getRotationAnnouncement(
36
42
  state: CropperState,
37
43
  previousState: CropperState | null
38
- ): string {
44
+ ): string | undefined {
39
45
  if (
40
46
  previousState &&
41
- ( previousState.flip.horizontal !== state.flip.horizontal ||
42
- previousState.flip.vertical !== state.flip.vertical )
47
+ Math.round( previousState.rotation ) === Math.round( state.rotation )
43
48
  ) {
44
- return buildFlipAnnouncement( state );
49
+ return undefined;
45
50
  }
46
-
47
- const parts: string[] = [];
48
- parts.push(
49
- sprintf(
50
- /* translators: %d: zoom level as a percentage. */
51
- __( 'Zoom %d%%' ),
52
- Math.round( state.zoom * 100 )
53
- )
51
+ // Announce the visual rotation — what the user perceives on screen. A
52
+ // single-axis flip mirrors the image, reversing the entire on-screen
53
+ // rotation relative to the stored field (the same S·R·S inversion the
54
+ // reducer applies), so negate the whole angle in that case.
55
+ const singleFlip = state.flip.horizontal !== state.flip.vertical;
56
+ const visualDir = singleFlip ? -1 : 1;
57
+ // Normalize to (-180, 180] so the sign indicates direction.
58
+ let visualRotation = ( Math.round( state.rotation ) * visualDir ) % 360;
59
+ if ( visualRotation > 180 ) {
60
+ visualRotation -= 360;
61
+ }
62
+ if ( visualRotation <= -180 ) {
63
+ visualRotation += 360;
64
+ }
65
+ if ( visualRotation === 0 ) {
66
+ return previousState ? __( 'Rotation 0 degrees' ) : undefined;
67
+ }
68
+ if ( visualRotation > 0 ) {
69
+ return sprintf(
70
+ /* translators: %d: rotation angle in degrees. */
71
+ __( 'Rotated %d degrees clockwise' ),
72
+ visualRotation
73
+ );
74
+ }
75
+ return sprintf(
76
+ /* translators: %d: rotation angle in degrees. */
77
+ __( 'Rotated %d degrees counterclockwise' ),
78
+ Math.abs( visualRotation )
54
79
  );
55
- if ( state.rotation !== 0 ) {
56
- parts.push(
57
- sprintf(
58
- /* translators: %d: rotation angle in degrees. */
59
- __( 'Rotation %d degrees' ),
60
- Math.round( state.rotation )
61
- )
80
+ }
81
+
82
+ function getCropAnnouncement(
83
+ state: CropperState,
84
+ previousState: CropperState | null
85
+ ): string | undefined {
86
+ if ( ! state.image ) {
87
+ return undefined;
88
+ }
89
+ const imageSize = {
90
+ width: state.image.naturalWidth,
91
+ height: state.image.naturalHeight,
92
+ };
93
+ const region = getSourceRegion( state, imageSize );
94
+ // Announce only when the crop's pixel dimensions change. Measuring both the
95
+ // previous and current crop rects under the current rotation and zoom keeps
96
+ // the comparison in the same pixel units we announce, and leaves a zoom-only
97
+ // change — which doesn't move the crop rect — silent.
98
+ if ( previousState?.image ) {
99
+ const previousRegion = getSourceRegion(
100
+ { ...state, cropRect: previousState.cropRect },
101
+ imageSize
62
102
  );
103
+ if (
104
+ Math.round( previousRegion.width ) === Math.round( region.width ) &&
105
+ Math.round( previousRegion.height ) === Math.round( region.height )
106
+ ) {
107
+ return undefined;
108
+ }
63
109
  }
64
- const cropW = Math.round( state.cropRect.width * 100 );
65
- const cropH = Math.round( state.cropRect.height * 100 );
66
- parts.push(
67
- sprintf(
68
- /* translators: 1: crop width as a percentage, 2: crop height as a percentage. */
69
- __( 'Crop %1$d%% by %2$d%%' ),
70
- cropW,
71
- cropH
72
- )
110
+ return sprintf(
111
+ /* translators: 1: crop width in pixels, 2: crop height in pixels. */
112
+ __( 'Crop %1$d by %2$d pixels' ),
113
+ Math.round( region.width ),
114
+ Math.round( region.height )
73
115
  );
74
- if ( state.flip.horizontal || state.flip.vertical ) {
75
- parts.push( buildFlipAnnouncement( state ) );
116
+ }
117
+
118
+ function getZoomAnnouncement(
119
+ state: CropperState,
120
+ previousState: CropperState | null
121
+ ): string | undefined {
122
+ if (
123
+ previousState &&
124
+ Math.round( previousState.zoom * 100 ) ===
125
+ Math.round( state.zoom * 100 )
126
+ ) {
127
+ return undefined;
76
128
  }
129
+ return sprintf(
130
+ /* translators: %d: zoom level as a percentage. */
131
+ __( 'Zoom %d%%' ),
132
+ Math.round( state.zoom * 100 )
133
+ );
134
+ }
135
+
136
+ function buildAnnouncement(
137
+ state: CropperState,
138
+ previousState: CropperState | null
139
+ ): string {
140
+ // Flip changes are announced alone.
141
+ const flip = getFlipAnnouncement( state, previousState );
142
+ if ( flip ) {
143
+ return flip;
144
+ }
145
+
146
+ const parts = [
147
+ getRotationAnnouncement( state, previousState ),
148
+ getZoomAnnouncement( state, previousState ),
149
+ getCropAnnouncement( state, previousState ),
150
+ ].filter( ( part ): part is string => part !== undefined );
151
+
77
152
  return parts.join( ', ' );
78
153
  }
79
154
 
80
155
  /**
81
156
  * Debounce and dedupe ARIA-live announcements for screen readers as the
82
- * cropper state changes. Returns the current announcement message to render
83
- * inside an `aria-live="polite"` region.
157
+ * cropper state changes. Calls `speak()` from `@wordpress/a11y` to use the
158
+ * centralized, persistent live region instead of a local one that may be
159
+ * dynamically mounted.
84
160
  *
85
- * Debouncing avoids flooding the live region during drag/pointermove bursts;
161
+ * Debouncing avoids flooding announcements during drag/pointermove bursts;
86
162
  * deduping avoids re-announcing the same state.
87
163
  *
88
164
  * @param state The current cropper state to announce.
89
165
  */
90
- export function useAriaAnnouncer( state: CropperState ): string {
91
- const [ ariaMessage, setAriaMessage ] = useState( '' );
166
+ export function useAriaAnnouncer( state: CropperState ): void {
92
167
  const timerRef = useRef< ReturnType< typeof setTimeout > >();
93
168
  const prevMessageRef = useRef( '' );
94
169
  const prevStateRef = useRef< CropperState | null >( null );
@@ -105,7 +180,7 @@ export function useAriaAnnouncer( state: CropperState ): string {
105
180
  prevStateRef.current = current;
106
181
  if ( msg !== prevMessageRef.current ) {
107
182
  prevMessageRef.current = msg;
108
- setAriaMessage( msg );
183
+ speak( msg );
109
184
  }
110
185
  }, ARIA_DEBOUNCE_MS );
111
186
 
@@ -120,6 +195,4 @@ export function useAriaAnnouncer( state: CropperState ): string {
120
195
  state.flip.horizontal,
121
196
  state.flip.vertical,
122
197
  ] );
123
-
124
- return ariaMessage;
125
198
  }
package/tsconfig.json CHANGED
@@ -6,6 +6,7 @@
6
6
  "checkJs": false
7
7
  },
8
8
  "references": [
9
+ { "path": "../a11y" },
9
10
  { "path": "../api-fetch" },
10
11
  { "path": "../components" },
11
12
  { "path": "../compose" },