@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131032.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.
- package/build/components/media-editor/index.cjs +560 -0
- package/build/components/media-editor/index.cjs.map +7 -0
- package/build/components/media-editor-canvas/index.cjs +14 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +34 -27
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +162 -0
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +42 -430
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-toolbar/index.cjs +143 -75
- package/build/components/media-editor-toolbar/index.cjs.map +3 -3
- package/build/components/media-form/index.cjs +3 -0
- package/build/components/media-form/index.cjs.map +2 -2
- package/build/components/rotation-ruler/index.cjs +197 -0
- package/build/components/rotation-ruler/index.cjs.map +7 -0
- package/build/components/rotation-ruler/use-ruler-drag.cjs +154 -0
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +7 -0
- package/build/hooks/use-crop-gesture-handlers.cjs +43 -0
- package/build/hooks/use-crop-gesture-handlers.cjs.map +7 -0
- package/build/image-editor/core/constants.cjs +9 -0
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs +7 -13
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +43 -7
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/state.cjs +5 -1
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs.map +1 -1
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/core/viewport-state.cjs +50 -0
- package/build/image-editor/core/viewport-state.cjs.map +7 -0
- package/build/image-editor/react/components/cropper.cjs +192 -72
- package/build/image-editor/react/components/cropper.cjs.map +3 -3
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +5 -2
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +16 -4
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/components/viewport-provider.cjs +51 -0
- package/build/image-editor/react/components/viewport-provider.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +53 -6
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-state.cjs +127 -8
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +26 -5
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-viewport-state.cjs +51 -0
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +7 -0
- package/build/image-editor/react/visually-hidden-style.cjs +41 -0
- package/build/image-editor/react/visually-hidden-style.cjs.map +7 -0
- package/build/private-apis.cjs +2 -0
- package/build/private-apis.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +545 -0
- package/build-module/components/media-editor/index.mjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +14 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +35 -28
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +144 -0
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +46 -455
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-toolbar/index.mjs +137 -77
- package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +3 -0
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +166 -0
- package/build-module/components/rotation-ruler/index.mjs.map +7 -0
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +126 -0
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +7 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs +17 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +7 -0
- package/build-module/image-editor/core/constants.mjs +6 -0
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs +6 -12
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +50 -8
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs +5 -1
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs.map +1 -1
- package/build-module/image-editor/core/viewport-state.mjs +24 -0
- package/build-module/image-editor/core/viewport-state.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper.mjs +195 -73
- package/build-module/image-editor/react/components/cropper.mjs.map +3 -3
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +5 -2
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +26 -5
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/components/viewport-provider.mjs +27 -0
- package/build-module/image-editor/react/components/viewport-provider.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +59 -7
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +134 -9
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +26 -5
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs +29 -0
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +7 -0
- package/build-module/image-editor/react/visually-hidden-style.mjs +16 -0
- package/build-module/image-editor/react/visually-hidden-style.mjs.map +7 -0
- package/build-module/private-apis.mjs +2 -0
- package/build-module/private-apis.mjs.map +2 -2
- package/build-style/style-rtl.css +254 -29
- package/build-style/style.css +254 -29
- package/build-types/components/media-editor/index.d.ts +34 -0
- package/build-types/components/media-editor/index.d.ts.map +1 -0
- package/build-types/components/media-editor-canvas/index.d.ts +10 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +6 -0
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts +2 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +1 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-editor-toolbar/index.d.ts +4 -1
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/index.d.ts +46 -0
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/stories/index.story.d.ts +14 -0
- package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts +61 -0
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts +42 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -0
- package/build-types/image-editor/core/constants.d.ts +9 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +11 -7
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +14 -2
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts +1 -1
- package/build-types/image-editor/core/types.d.ts +25 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/core/viewport-state.d.ts +7 -0
- package/build-types/image-editor/core/viewport-state.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +2 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +3 -13
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/components/viewport-provider.d.ts +9 -0
- package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +29 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -2
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +19 -0
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +1 -0
- package/build-types/image-editor/react/visually-hidden-style.d.ts +6 -0
- package/build-types/image-editor/react/visually-hidden-style.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/private-apis.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +3 -3
- package/build-types/store/actions.d.ts.map +1 -1
- package/package.json +16 -15
- package/src/components/media-editor/index.tsx +709 -0
- package/src/components/media-editor/style.scss +89 -0
- package/src/components/media-editor-canvas/index.tsx +24 -0
- package/src/components/media-editor-crop-panel/index.tsx +39 -27
- package/src/components/media-editor-crop-panel/test/index.tsx +109 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +177 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +53 -0
- package/src/components/media-editor-modal/index.tsx +49 -589
- package/src/components/media-editor-modal/style.scss +13 -112
- package/src/components/media-editor-toolbar/index.tsx +122 -63
- package/src/components/media-editor-toolbar/style.scss +39 -11
- package/src/components/media-form/index.tsx +5 -0
- package/src/components/rotation-ruler/README.md +37 -0
- package/src/components/rotation-ruler/index.tsx +266 -0
- package/src/components/rotation-ruler/stories/index.story.tsx +54 -0
- package/src/components/rotation-ruler/style.scss +147 -0
- package/src/components/rotation-ruler/test/index.tsx +109 -0
- package/src/components/rotation-ruler/use-ruler-drag.ts +230 -0
- package/src/hooks/use-crop-gesture-handlers.ts +54 -0
- package/src/image-editor/core/constants.ts +12 -0
- package/src/image-editor/core/containment.ts +16 -28
- package/src/image-editor/core/interaction-controller.ts +66 -10
- package/src/image-editor/core/state.ts +15 -8
- package/src/image-editor/core/test/camera.ts +15 -26
- package/src/image-editor/core/test/interaction-controller.ts +158 -7
- package/src/image-editor/core/test/state.ts +21 -1
- package/src/image-editor/core/transform-style.ts +1 -1
- package/src/image-editor/core/types.ts +16 -0
- package/src/image-editor/core/viewport-state.ts +33 -0
- package/src/image-editor/docs/architecture.md +1 -1
- package/src/image-editor/docs/recipes.md +2 -2
- package/src/image-editor/react/components/cropper.scss +28 -0
- package/src/image-editor/react/components/cropper.tsx +268 -72
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +12 -12
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +27 -8
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +29 -3
- package/src/image-editor/react/components/test/cropper.tsx +294 -5
- package/src/image-editor/react/components/viewport-provider.tsx +39 -0
- package/src/image-editor/react/hooks/test/use-aria-announcer.ts +81 -0
- package/src/image-editor/react/hooks/test/use-cropper-state.ts +286 -0
- package/src/image-editor/react/hooks/test/use-interaction.ts +1 -0
- package/src/image-editor/react/hooks/use-aria-announcer.ts +68 -7
- package/src/image-editor/react/hooks/use-cropper-state.ts +230 -11
- package/src/image-editor/react/hooks/use-interaction.ts +36 -6
- package/src/image-editor/react/hooks/use-viewport-state.ts +48 -0
- package/src/image-editor/react/visually-hidden-style.ts +15 -0
- package/src/private-apis.ts +2 -0
- package/src/style.scss +3 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import clsx from 'clsx';
|
|
5
|
+
import type { CSSProperties, KeyboardEvent } from 'react';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* WordPress dependencies
|
|
9
|
+
*/
|
|
10
|
+
import { useId, useMemo, useRef } from '@wordpress/element';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Internal dependencies
|
|
14
|
+
*/
|
|
15
|
+
import { useRulerDrag, clampValue } from './use-ruler-drag';
|
|
16
|
+
|
|
17
|
+
export interface RotationRulerProps {
|
|
18
|
+
/** Current value. */
|
|
19
|
+
value: number;
|
|
20
|
+
/** Called with the next value on each change. */
|
|
21
|
+
onChange: ( value: number ) => void;
|
|
22
|
+
/** Lower bound of the range. Default: -45. */
|
|
23
|
+
min?: number;
|
|
24
|
+
/** Upper bound of the range. Default: 45. */
|
|
25
|
+
max?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Value step for drag and keyboard arrows. Default: 1. Drag values
|
|
28
|
+
* are quantized to multiples of `step`. Holding Shift during drag
|
|
29
|
+
* or with arrow keys halves the step for precision adjustment.
|
|
30
|
+
*/
|
|
31
|
+
step?: number;
|
|
32
|
+
/** Accessible label for the slider input. */
|
|
33
|
+
label: string;
|
|
34
|
+
/**
|
|
35
|
+
* Unit suffix shown in the active label and `aria-valuetext`.
|
|
36
|
+
* Default: `°`.
|
|
37
|
+
*/
|
|
38
|
+
unit?: string;
|
|
39
|
+
/** CSS pixels of pointer travel per `step`. Default: 6. */
|
|
40
|
+
pixelsPerStep?: number;
|
|
41
|
+
/** Optional extra class for the wrapper. */
|
|
42
|
+
className?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Optional id for the underlying input; auto-generated when
|
|
45
|
+
* omitted.
|
|
46
|
+
*/
|
|
47
|
+
id?: string;
|
|
48
|
+
/** When true, ignores all input. */
|
|
49
|
+
disabled?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const STRIP_HEIGHT = 32;
|
|
53
|
+
const LABEL_BASELINE_Y = 11;
|
|
54
|
+
const MAJOR_INTERVAL = 15;
|
|
55
|
+
const TICK_HEIGHT_MINOR = 4;
|
|
56
|
+
const TICK_HEIGHT_MID = 8;
|
|
57
|
+
const TICK_HEIGHT_MAJOR = 14;
|
|
58
|
+
|
|
59
|
+
type TickKind = 'minor' | 'mid' | 'major';
|
|
60
|
+
interface Tick {
|
|
61
|
+
value: number;
|
|
62
|
+
kind: TickKind;
|
|
63
|
+
height: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Build the tick list for the ruler strip.
|
|
68
|
+
*
|
|
69
|
+
* @param min Lower bound of the ruler in value units.
|
|
70
|
+
* @param max Upper bound of the ruler in value units.
|
|
71
|
+
*/
|
|
72
|
+
function useTicks( min: number, max: number ): Tick[] {
|
|
73
|
+
return useMemo( () => {
|
|
74
|
+
const out: Tick[] = [];
|
|
75
|
+
for ( let v = Math.ceil( min ); v <= Math.floor( max ); v += 1 ) {
|
|
76
|
+
let kind: TickKind = 'minor';
|
|
77
|
+
let height = TICK_HEIGHT_MINOR;
|
|
78
|
+
if ( v % 15 === 0 ) {
|
|
79
|
+
kind = 'major';
|
|
80
|
+
height = TICK_HEIGHT_MAJOR;
|
|
81
|
+
} else if ( v % 5 === 0 ) {
|
|
82
|
+
kind = 'mid';
|
|
83
|
+
height = TICK_HEIGHT_MID;
|
|
84
|
+
}
|
|
85
|
+
out.push( { value: v, kind, height } );
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}, [ min, max ] );
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Format a numeric ruler value for display, trimming a trailing `.0`.
|
|
93
|
+
*
|
|
94
|
+
* @param value Value to format.
|
|
95
|
+
*/
|
|
96
|
+
function formatValue( value: number ): string {
|
|
97
|
+
const rounded = Math.round( value * 10 ) / 10;
|
|
98
|
+
return Number.isInteger( rounded )
|
|
99
|
+
? rounded.toFixed( 0 )
|
|
100
|
+
: rounded.toFixed( 1 );
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Horizontal "scrub-the-ruler" slider for fine-grained numeric input
|
|
105
|
+
* such as rotation in degrees.
|
|
106
|
+
*
|
|
107
|
+
* Renders a visually hidden `<input type="range">` for assistive
|
|
108
|
+
* technologies and keyboard input alongside a decorative tick strip
|
|
109
|
+
* that the user can drag with a pointer.
|
|
110
|
+
*
|
|
111
|
+
* @param props Component props. See `RotationRulerProps`.
|
|
112
|
+
*/
|
|
113
|
+
export default function RotationRuler( props: RotationRulerProps ) {
|
|
114
|
+
const {
|
|
115
|
+
value,
|
|
116
|
+
onChange,
|
|
117
|
+
min = -45,
|
|
118
|
+
max = 45,
|
|
119
|
+
step = 1,
|
|
120
|
+
label,
|
|
121
|
+
unit = '°',
|
|
122
|
+
pixelsPerStep = 6,
|
|
123
|
+
className,
|
|
124
|
+
id,
|
|
125
|
+
disabled = false,
|
|
126
|
+
} = props;
|
|
127
|
+
const inputRef = useRef< HTMLInputElement >( null );
|
|
128
|
+
const generatedId = useId();
|
|
129
|
+
const inputId = id ?? generatedId;
|
|
130
|
+
|
|
131
|
+
const dragHandlers = useRulerDrag( {
|
|
132
|
+
value,
|
|
133
|
+
onChange,
|
|
134
|
+
min,
|
|
135
|
+
max,
|
|
136
|
+
step,
|
|
137
|
+
pixelsPerStep,
|
|
138
|
+
disabled,
|
|
139
|
+
onPointerDownStart: () => inputRef.current?.focus(),
|
|
140
|
+
} );
|
|
141
|
+
|
|
142
|
+
const handleKeyDown = ( event: KeyboardEvent< HTMLInputElement > ) => {
|
|
143
|
+
// WAI-ARIA slider keyboard pattern: Up/Right increment,
|
|
144
|
+
// Down/Left decrement. Home / End / PageUp / PageDown fall
|
|
145
|
+
// through to native input.
|
|
146
|
+
const isIncrement =
|
|
147
|
+
event.key === 'ArrowRight' || event.key === 'ArrowUp';
|
|
148
|
+
const isDecrement =
|
|
149
|
+
event.key === 'ArrowLeft' || event.key === 'ArrowDown';
|
|
150
|
+
if ( ! isIncrement && ! isDecrement ) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Always prevent default so the native input's own arrow-key
|
|
154
|
+
// stepping doesn't double-fire `onChange` after our custom emit.
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
if ( disabled ) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const direction = isIncrement ? 1 : -1;
|
|
160
|
+
// Shift halves the step for precision adjustment
|
|
161
|
+
// (e.g. 0.5° rotation when step is 1°). Same modifier works
|
|
162
|
+
// on drag — see `useRulerDrag`.
|
|
163
|
+
const magnitude = event.shiftKey ? step / 2 : step;
|
|
164
|
+
const next = clampValue( value + direction * magnitude, min, max );
|
|
165
|
+
if ( next !== value ) {
|
|
166
|
+
onChange( next );
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const display = `${ formatValue( value ) }${ unit }`;
|
|
171
|
+
const ticks = useTicks( min, max );
|
|
172
|
+
// Visual scale is locked to the gesture: 1 step of pointer travel
|
|
173
|
+
// (`pixelsPerStep`) maps to `step` value units, so 1 value unit
|
|
174
|
+
// renders at `pixelsPerStep / step` CSS pixels. This keeps the
|
|
175
|
+
// ticks moving in lock-step with the pointer no matter what the
|
|
176
|
+
// caller chooses for `pixelsPerStep` and `step`.
|
|
177
|
+
const pxPerUnit = pixelsPerStep / step;
|
|
178
|
+
// `left: 50%` puts the SVG's left edge at the strip's centerline, so
|
|
179
|
+
// without an additional `-50%` self-offset all ticks would render to
|
|
180
|
+
// the right of the pointer. Combine the centering offset with the
|
|
181
|
+
// value-driven offset so value=0 lands the SVG's center on the
|
|
182
|
+
// pointer.
|
|
183
|
+
const stripStyle: CSSProperties = useMemo( () => {
|
|
184
|
+
const offset = -value * pxPerUnit;
|
|
185
|
+
return {
|
|
186
|
+
transform: `translateX(calc(-50% + ${ offset }px))`,
|
|
187
|
+
};
|
|
188
|
+
}, [ value, pxPerUnit ] );
|
|
189
|
+
|
|
190
|
+
// Closest major (every 15°) to current value. Its normal label is
|
|
191
|
+
// suppressed and replaced by the active label, which sits exactly
|
|
192
|
+
// under the pointer and shows the precise current value when off-
|
|
193
|
+
// major (e.g. "-7°") or the major itself when on-major (e.g. "0°").
|
|
194
|
+
const closestMajor = Math.round( value / MAJOR_INTERVAL ) * MAJOR_INTERVAL;
|
|
195
|
+
const onMajor = Math.abs( value - closestMajor ) < 0.01;
|
|
196
|
+
const activeText = onMajor ? `${ closestMajor }${ unit }` : display;
|
|
197
|
+
const majorTicks = ticks.filter( ( tick ) => tick.kind === 'major' );
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<div
|
|
201
|
+
className={ clsx( 'rotation-ruler', className ) }
|
|
202
|
+
role="presentation"
|
|
203
|
+
data-disabled={ disabled || undefined }
|
|
204
|
+
{ ...dragHandlers }
|
|
205
|
+
>
|
|
206
|
+
<input
|
|
207
|
+
ref={ inputRef }
|
|
208
|
+
id={ inputId }
|
|
209
|
+
type="range"
|
|
210
|
+
className="rotation-ruler__input"
|
|
211
|
+
min={ min }
|
|
212
|
+
max={ max }
|
|
213
|
+
step="any"
|
|
214
|
+
value={ value }
|
|
215
|
+
disabled={ disabled }
|
|
216
|
+
aria-label={ label }
|
|
217
|
+
aria-valuetext={ display }
|
|
218
|
+
onChange={ ( event ) =>
|
|
219
|
+
onChange(
|
|
220
|
+
clampValue( event.target.valueAsNumber, min, max )
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
onKeyDown={ handleKeyDown }
|
|
224
|
+
/>
|
|
225
|
+
<div className="rotation-ruler__strip" aria-hidden="true">
|
|
226
|
+
<svg
|
|
227
|
+
className="rotation-ruler__ticks"
|
|
228
|
+
style={ stripStyle }
|
|
229
|
+
width={ ( max - min ) * pxPerUnit }
|
|
230
|
+
height={ STRIP_HEIGHT }
|
|
231
|
+
viewBox={ `${ min * pxPerUnit } 0 ${
|
|
232
|
+
( max - min ) * pxPerUnit
|
|
233
|
+
} ${ STRIP_HEIGHT }` }
|
|
234
|
+
preserveAspectRatio="xMidYMid meet"
|
|
235
|
+
>
|
|
236
|
+
{ ticks.map( ( tick ) => (
|
|
237
|
+
<line
|
|
238
|
+
key={ tick.value }
|
|
239
|
+
x1={ tick.value * pxPerUnit }
|
|
240
|
+
x2={ tick.value * pxPerUnit }
|
|
241
|
+
y1={ STRIP_HEIGHT - tick.height }
|
|
242
|
+
y2={ STRIP_HEIGHT }
|
|
243
|
+
className={ `rotation-ruler__tick rotation-ruler__tick--${ tick.kind }` }
|
|
244
|
+
/>
|
|
245
|
+
) ) }
|
|
246
|
+
{ majorTicks.map( ( tick ) => (
|
|
247
|
+
<text
|
|
248
|
+
key={ tick.value }
|
|
249
|
+
x={ tick.value * pxPerUnit }
|
|
250
|
+
y={ LABEL_BASELINE_Y }
|
|
251
|
+
textAnchor="middle"
|
|
252
|
+
className="rotation-ruler__label"
|
|
253
|
+
>
|
|
254
|
+
{ tick.value }
|
|
255
|
+
{ unit }
|
|
256
|
+
</text>
|
|
257
|
+
) ) }
|
|
258
|
+
</svg>
|
|
259
|
+
</div>
|
|
260
|
+
<div className="rotation-ruler__active-label" aria-hidden="true">
|
|
261
|
+
{ activeText }
|
|
262
|
+
</div>
|
|
263
|
+
<div className="rotation-ruler__pointer" aria-hidden="true" />
|
|
264
|
+
</div>
|
|
265
|
+
);
|
|
266
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* WordPress dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { useState } from '@wordpress/element';
|
|
10
|
+
import { __ } from '@wordpress/i18n';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Internal dependencies
|
|
14
|
+
*/
|
|
15
|
+
import RotationRuler from '../index';
|
|
16
|
+
import '../style.scss';
|
|
17
|
+
|
|
18
|
+
const meta: Meta< typeof RotationRuler > = {
|
|
19
|
+
title: 'MediaEditor/RotationRuler',
|
|
20
|
+
component: RotationRuler,
|
|
21
|
+
args: {
|
|
22
|
+
label: __( 'Fine rotation' ),
|
|
23
|
+
min: -45,
|
|
24
|
+
max: 45,
|
|
25
|
+
step: 1,
|
|
26
|
+
pixelsPerStep: 6,
|
|
27
|
+
disabled: false,
|
|
28
|
+
},
|
|
29
|
+
argTypes: {
|
|
30
|
+
value: { control: false },
|
|
31
|
+
onChange: { control: false },
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
export default meta;
|
|
35
|
+
|
|
36
|
+
type Story = StoryObj< typeof RotationRuler >;
|
|
37
|
+
|
|
38
|
+
export const Default: Story = {
|
|
39
|
+
render: ( args ) => {
|
|
40
|
+
const [ value, setValue ] = useState( 0 );
|
|
41
|
+
return (
|
|
42
|
+
<div style={ { width: 360, color: '#1e1e1e' } }>
|
|
43
|
+
<RotationRuler
|
|
44
|
+
{ ...args }
|
|
45
|
+
value={ value }
|
|
46
|
+
onChange={ setValue }
|
|
47
|
+
/>
|
|
48
|
+
<p style={ { marginTop: 16 } }>
|
|
49
|
+
{ __( 'Current value:' ) } { value }°
|
|
50
|
+
</p>
|
|
51
|
+
</div>
|
|
52
|
+
);
|
|
53
|
+
},
|
|
54
|
+
};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
@use "@wordpress/base-styles/colors" as *;
|
|
2
|
+
@use "@wordpress/base-styles/variables" as *;
|
|
3
|
+
|
|
4
|
+
// Default to WordPress core's admin theme blue when the host hasn't
|
|
5
|
+
// set `--wp-admin-theme-color` (e.g. inside Storybook). Hosts that do
|
|
6
|
+
// set it (the media modal, the editor) will override this with their
|
|
7
|
+
// theme.
|
|
8
|
+
$rotation-ruler-accent: var(--wp-admin-theme-color, #3858e9);
|
|
9
|
+
|
|
10
|
+
// Vertical layout (top → bottom):
|
|
11
|
+
// 0–14: label row (translates with strip; active label sits centered)
|
|
12
|
+
// 14–32: tick lines (anchored to strip bottom)
|
|
13
|
+
// 23–32: pointer triangle (overlapping the bottom of major ticks)
|
|
14
|
+
.rotation-ruler {
|
|
15
|
+
position: relative;
|
|
16
|
+
min-width: 180px;
|
|
17
|
+
height: 32px;
|
|
18
|
+
padding: 0 $grid-unit-10;
|
|
19
|
+
box-sizing: border-box;
|
|
20
|
+
color: $gray-900;
|
|
21
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
|
|
22
|
+
user-select: none;
|
|
23
|
+
touch-action: none;
|
|
24
|
+
cursor: ew-resize;
|
|
25
|
+
// Always declare the container so the inline-size query below can
|
|
26
|
+
// match. Cheap; only affects layout if a child uses cqw/cqh.
|
|
27
|
+
container-type: inline-size;
|
|
28
|
+
|
|
29
|
+
&[data-disabled] {
|
|
30
|
+
opacity: 0.5;
|
|
31
|
+
pointer-events: none;
|
|
32
|
+
cursor: default;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
&:has(.rotation-ruler__input:focus-visible) {
|
|
36
|
+
outline: 2px solid $rotation-ruler-accent;
|
|
37
|
+
outline-offset: 2px;
|
|
38
|
+
border-radius: 4px;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Visually hidden but kept in layout so it's focusable.
|
|
43
|
+
.rotation-ruler__input {
|
|
44
|
+
position: absolute;
|
|
45
|
+
width: 1px;
|
|
46
|
+
height: 1px;
|
|
47
|
+
margin: -1px;
|
|
48
|
+
padding: 0;
|
|
49
|
+
border: 0;
|
|
50
|
+
clip: rect(0 0 0 0);
|
|
51
|
+
overflow: hidden;
|
|
52
|
+
white-space: nowrap;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Upward-pointing triangle anchored to the bottom of the ruler. Marks
|
|
56
|
+
// the centered value cleanly without the vertical stem of a
|
|
57
|
+
// traditional pointer.
|
|
58
|
+
.rotation-ruler__pointer {
|
|
59
|
+
position: absolute;
|
|
60
|
+
bottom: 0;
|
|
61
|
+
left: 50%;
|
|
62
|
+
width: 0;
|
|
63
|
+
height: 0;
|
|
64
|
+
border-left: 6px solid transparent;
|
|
65
|
+
border-right: 6px solid transparent;
|
|
66
|
+
border-bottom: 9px solid $rotation-ruler-accent;
|
|
67
|
+
transform: translateX(-50%);
|
|
68
|
+
pointer-events: none;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.rotation-ruler__strip {
|
|
72
|
+
position: absolute;
|
|
73
|
+
inset: 0;
|
|
74
|
+
overflow: hidden;
|
|
75
|
+
// Fade ticks/labels toward each edge so the strip looks like a
|
|
76
|
+
// continuous ruler running off-screen rather than a strip with hard
|
|
77
|
+
// cut-offs.
|
|
78
|
+
mask-image: linear-gradient(to right, transparent 0, #000 8px, #000 calc(100% - 8px), transparent 100%);
|
|
79
|
+
-webkit-mask-image: linear-gradient(to right, transparent 0, #000 8px, #000 calc(100% - 8px), transparent 100%);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.rotation-ruler__ticks {
|
|
83
|
+
position: absolute;
|
|
84
|
+
left: 50%;
|
|
85
|
+
top: 0;
|
|
86
|
+
display: block;
|
|
87
|
+
// Allow text glyphs at the SVG's left/right edges to render outside
|
|
88
|
+
// the SVG box; the strip's `overflow: hidden` still clips at its
|
|
89
|
+
// own edges, which is what we want.
|
|
90
|
+
overflow: visible;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.rotation-ruler__tick {
|
|
94
|
+
stroke: currentColor;
|
|
95
|
+
stroke-width: 1;
|
|
96
|
+
|
|
97
|
+
&--minor {
|
|
98
|
+
opacity: 0.35;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
&--mid {
|
|
102
|
+
opacity: 0.6;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
&--major {
|
|
106
|
+
opacity: 1;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Major-tick labels live inside the SVG and translate with the strip.
|
|
111
|
+
.rotation-ruler__label {
|
|
112
|
+
fill: currentColor;
|
|
113
|
+
font-size: 10px;
|
|
114
|
+
font-weight: 400;
|
|
115
|
+
opacity: 0.45;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Active label is a fixed-center HTML element so it stays steady at the
|
|
119
|
+
// pointer while the SVG behind it transitions smoothly. A white-fading-
|
|
120
|
+
// to-transparent background occludes the static labels directly behind.
|
|
121
|
+
.rotation-ruler__active-label {
|
|
122
|
+
position: absolute;
|
|
123
|
+
top: 0;
|
|
124
|
+
left: 50%;
|
|
125
|
+
height: 14px;
|
|
126
|
+
transform: translateX(-50%);
|
|
127
|
+
padding: 0 12px;
|
|
128
|
+
font-size: 12px;
|
|
129
|
+
font-weight: 600;
|
|
130
|
+
line-height: 14px;
|
|
131
|
+
color: $gray-900;
|
|
132
|
+
background: linear-gradient(to right, transparent 0, $white 12px, $white calc(100% - 12px), transparent 100%);
|
|
133
|
+
pointer-events: none;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Below ~220px container width, fade out 1° ticks; mid/major remain.
|
|
137
|
+
@container (max-width: 220px) {
|
|
138
|
+
.rotation-ruler__tick--minor {
|
|
139
|
+
opacity: 0;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@media (prefers-reduced-motion: reduce) {
|
|
144
|
+
.rotation-ruler__ticks {
|
|
145
|
+
transition: none;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { render, screen } from '@testing-library/react';
|
|
5
|
+
import userEvent from '@testing-library/user-event';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Internal dependencies
|
|
9
|
+
*/
|
|
10
|
+
import { pxToValueDelta, clampValue, quantize } from '../use-ruler-drag';
|
|
11
|
+
import RotationRuler from '../index';
|
|
12
|
+
|
|
13
|
+
describe( 'rotation-ruler math', () => {
|
|
14
|
+
describe( 'pxToValueDelta', () => {
|
|
15
|
+
it( 'converts pointer delta to a value delta using pixelsPerStep × step', () => {
|
|
16
|
+
// 60px / 6 px-per-step × 1° step = 10 steps.
|
|
17
|
+
// Negative because dragging the ruler right exposes smaller values.
|
|
18
|
+
expect( pxToValueDelta( 60, 6, 1 ) ).toBeCloseTo( -10 );
|
|
19
|
+
expect( pxToValueDelta( -60, 6, 1 ) ).toBeCloseTo( 10 );
|
|
20
|
+
} );
|
|
21
|
+
|
|
22
|
+
it( 'scales with a custom step', () => {
|
|
23
|
+
// 60px / 6 px-per-step × 0.5° step = 5°.
|
|
24
|
+
expect( pxToValueDelta( -60, 6, 0.5 ) ).toBeCloseTo( 5 );
|
|
25
|
+
} );
|
|
26
|
+
} );
|
|
27
|
+
|
|
28
|
+
describe( 'clampValue', () => {
|
|
29
|
+
it( 'clamps to [min, max]', () => {
|
|
30
|
+
expect( clampValue( 100, -45, 45 ) ).toBe( 45 );
|
|
31
|
+
expect( clampValue( -100, -45, 45 ) ).toBe( -45 );
|
|
32
|
+
expect( clampValue( 12.3, -45, 45 ) ).toBe( 12.3 );
|
|
33
|
+
} );
|
|
34
|
+
} );
|
|
35
|
+
|
|
36
|
+
describe( 'quantize', () => {
|
|
37
|
+
it( 'rounds to the nearest multiple of step', () => {
|
|
38
|
+
expect( quantize( 0.4, 1 ) ).toBe( 0 );
|
|
39
|
+
expect( quantize( 0.6, 1 ) ).toBe( 1 );
|
|
40
|
+
expect( quantize( -0.6, 1 ) ).toBe( -1 );
|
|
41
|
+
expect( quantize( 12.3, 5 ) ).toBe( 10 );
|
|
42
|
+
expect( quantize( 12.5, 5 ) ).toBe( 15 );
|
|
43
|
+
} );
|
|
44
|
+
} );
|
|
45
|
+
} );
|
|
46
|
+
|
|
47
|
+
describe( 'RotationRuler', () => {
|
|
48
|
+
it( 'renders the value on the underlying input and in aria-valuetext', () => {
|
|
49
|
+
render(
|
|
50
|
+
<RotationRuler
|
|
51
|
+
value={ -14 }
|
|
52
|
+
onChange={ () => {} }
|
|
53
|
+
label="Fine rotation"
|
|
54
|
+
/>
|
|
55
|
+
);
|
|
56
|
+
const input = screen.getByRole( 'slider', { name: 'Fine rotation' } );
|
|
57
|
+
expect( ( input as HTMLInputElement ).valueAsNumber ).toBe( -14 );
|
|
58
|
+
expect( input ).toHaveAttribute( 'aria-valuetext', '-14°' );
|
|
59
|
+
} );
|
|
60
|
+
|
|
61
|
+
it( 'fires onChange with value + step on ArrowRight', async () => {
|
|
62
|
+
const user = userEvent.setup();
|
|
63
|
+
const onChange = jest.fn();
|
|
64
|
+
render(
|
|
65
|
+
<RotationRuler
|
|
66
|
+
value={ 0 }
|
|
67
|
+
onChange={ onChange }
|
|
68
|
+
label="Fine rotation"
|
|
69
|
+
/>
|
|
70
|
+
);
|
|
71
|
+
const input = screen.getByRole( 'slider', { name: 'Fine rotation' } );
|
|
72
|
+
input.focus();
|
|
73
|
+
await user.keyboard( '{ArrowRight}' );
|
|
74
|
+
expect( onChange ).toHaveBeenCalledWith( 1 );
|
|
75
|
+
expect( onChange ).toHaveBeenCalledTimes( 1 );
|
|
76
|
+
} );
|
|
77
|
+
|
|
78
|
+
it( 'fires onChange with value + step / 2 on Shift+ArrowRight', async () => {
|
|
79
|
+
const user = userEvent.setup();
|
|
80
|
+
const onChange = jest.fn();
|
|
81
|
+
render(
|
|
82
|
+
<RotationRuler
|
|
83
|
+
value={ 0 }
|
|
84
|
+
onChange={ onChange }
|
|
85
|
+
label="Fine rotation"
|
|
86
|
+
/>
|
|
87
|
+
);
|
|
88
|
+
screen.getByRole( 'slider', { name: 'Fine rotation' } ).focus();
|
|
89
|
+
await user.keyboard( '{Shift>}{ArrowRight}{/Shift}' );
|
|
90
|
+
expect( onChange ).toHaveBeenCalledWith( 0.5 );
|
|
91
|
+
expect( onChange ).toHaveBeenCalledTimes( 1 );
|
|
92
|
+
} );
|
|
93
|
+
|
|
94
|
+
it( 'does not fire onChange when disabled', async () => {
|
|
95
|
+
const user = userEvent.setup();
|
|
96
|
+
const onChange = jest.fn();
|
|
97
|
+
render(
|
|
98
|
+
<RotationRuler
|
|
99
|
+
value={ 0 }
|
|
100
|
+
onChange={ onChange }
|
|
101
|
+
label="Fine rotation"
|
|
102
|
+
disabled
|
|
103
|
+
/>
|
|
104
|
+
);
|
|
105
|
+
screen.getByRole( 'slider', { name: 'Fine rotation' } ).focus();
|
|
106
|
+
await user.keyboard( '{ArrowRight}' );
|
|
107
|
+
expect( onChange ).not.toHaveBeenCalled();
|
|
108
|
+
} );
|
|
109
|
+
} );
|