-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
149 lines (138 loc) · 3.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* WordPress dependencies
*/
import {
SelectControl,
__experimentalNumberControl as NumberControl,
__experimentalHStack as HStack,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import useDimensionHandler from './use-dimension-handler';
const IMAGE_SIZE_PRESETS = [ 25, 50, 75, 100 ];
const noop = () => {};
/**
* Get scaled width and height for the given scale.
*
* @param {number} scale The scale to get the scaled width and height for.
* @param {number} imageWidth The image width.
* @param {number} imageHeight The image height.
*
* @return {Object} The scaled width and height.
*/
function getScaledWidthAndHeight( scale, imageWidth, imageHeight ) {
const scaledWidth = Math.round( imageWidth * ( scale / 100 ) );
const scaledHeight = Math.round( imageHeight * ( scale / 100 ) );
return {
scaledWidth,
scaledHeight,
};
}
export default function ImageSizeControl( {
imageSizeHelp,
imageWidth,
imageHeight,
imageSizeOptions = [],
isResizable = true,
slug,
width,
height,
onChange,
onChangeImage = noop,
} ) {
const { currentHeight, currentWidth, updateDimension, updateDimensions } =
useDimensionHandler( height, width, imageHeight, imageWidth, onChange );
/**
* Updates the dimensions for the given scale.
* Handler for toggle group control change.
*
* @param {number} scale The scale to update the dimensions for.
*/
const handleUpdateDimensions = ( scale ) => {
if ( undefined === scale ) {
updateDimensions();
return;
}
const { scaledWidth, scaledHeight } = getScaledWidthAndHeight(
scale,
imageWidth,
imageHeight
);
updateDimensions( scaledHeight, scaledWidth );
};
/**
* Add the stored image preset value to toggle group control.
*/
const selectedValue = IMAGE_SIZE_PRESETS.find( ( scale ) => {
const { scaledWidth, scaledHeight } = getScaledWidthAndHeight(
scale,
imageWidth,
imageHeight
);
return currentWidth === scaledWidth && currentHeight === scaledHeight;
} );
return (
<>
{ imageSizeOptions && imageSizeOptions.length > 0 && (
<SelectControl
__nextHasNoMarginBottom
label={ __( 'Resolution' ) }
value={ slug }
options={ imageSizeOptions }
onChange={ onChangeImage }
help={ imageSizeHelp }
size="__unstable-large"
/>
) }
{ isResizable && (
<div className="block-editor-image-size-control">
<HStack align="baseline" spacing="3">
<NumberControl
className="block-editor-image-size-control__width"
label={ __( 'Width' ) }
value={ currentWidth }
min={ 1 }
onChange={ ( value ) =>
updateDimension( 'width', value )
}
size="__unstable-large"
/>
<NumberControl
className="block-editor-image-size-control__height"
label={ __( 'Height' ) }
value={ currentHeight }
min={ 1 }
onChange={ ( value ) =>
updateDimension( 'height', value )
}
size="__unstable-large"
/>
</HStack>
<ToggleGroupControl
label={ __( 'Image size presets' ) }
hideLabelFromVision
onChange={ handleUpdateDimensions }
value={ selectedValue }
isBlock
__next40pxDefaultSize
__nextHasNoMarginBottom
>
{ IMAGE_SIZE_PRESETS.map( ( scale ) => {
return (
<ToggleGroupControlOption
key={ scale }
value={ scale }
label={ `${ scale }%` }
/>
);
} ) }
</ToggleGroupControl>
</div>
) }
</>
);
}