-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
121 lines (115 loc) · 2.9 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
/**
* WordPress dependencies
*/
import {
Button,
ButtonGroup,
SelectControl,
__experimentalNumberControl as NumberControl,
__experimentalHStack as HStack,
} from '@wordpress/components';
import deprecated from '@wordpress/deprecated';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import useDimensionHandler from './use-dimension-handler';
const IMAGE_SIZE_PRESETS = [ 25, 50, 75, 100 ];
const noop = () => {};
export default function ImageSizeControl( {
imageSizeHelp,
imageWidth,
imageHeight,
imageSizeOptions = [],
isResizable = true,
slug,
width,
height,
onChange,
onChangeImage = noop,
} ) {
deprecated( 'wp.blockEditor.__experimentalImageSizeControl', {
since: '6.3',
alternative:
'wp.blockEditor.privateApis.DimensionsTool and wp.blockEditor.privateApis.ResolutionTool',
} );
const { currentHeight, currentWidth, updateDimension, updateDimensions } =
useDimensionHandler( height, width, imageHeight, imageWidth, onChange );
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>
<HStack>
<ButtonGroup aria-label={ __( 'Image size presets' ) }>
{ IMAGE_SIZE_PRESETS.map( ( scale ) => {
const scaledWidth = Math.round(
imageWidth * ( scale / 100 )
);
const scaledHeight = Math.round(
imageHeight * ( scale / 100 )
);
const isCurrent =
currentWidth === scaledWidth &&
currentHeight === scaledHeight;
return (
<Button
key={ scale }
isSmall
variant={
isCurrent ? 'primary' : undefined
}
isPressed={ isCurrent }
onClick={ () =>
updateDimensions(
scaledHeight,
scaledWidth
)
}
>
{ scale }%
</Button>
);
} ) }
</ButtonGroup>
<Button isSmall onClick={ () => updateDimensions() }>
{ __( 'Reset' ) }
</Button>
</HStack>
</div>
) }
</>
);
}