Skip to content

Commit 625d599

Browse files
authored
Merge pull request #83 from 10up/feature/make-map-resizable-using-resize-handle
Feature/make map resizable using resize handle
2 parents b23bf03 + 2fb65c3 commit 625d599

4 files changed

Lines changed: 77 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
All notable changes to this project will be documented in this file, per [the Keep a Changelog standard](http://keepachangelog.com/), and will adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

55
## [Unreleased] - TBD
6-
6+
### Added
7+
- Resize Handle to modify the height of the Map inline
78
## [1.0.1] - 2020-08-11
89
### Added
910
- Internationalization support via loading translations for the block (props [@dinhtungdu](https://github.com/dinhtungdu), [@helen](https://github.com/helen) via [#69](https://github.com/10up/maps-block-apple/pull/69))

includes/rest-routes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ function add_endpoints() {
2424
MAPS_BLOCK_APPLE_VERSION_REST_NAMESPACE,
2525
'/GetJWT',
2626
[
27-
'methods' => 'GET',
28-
'callback' => __NAMESPACE__ . '\get_jwt',
27+
'methods' => 'GET',
28+
'callback' => __NAMESPACE__ . '\get_jwt',
29+
'permission_callback' => '__return_true',
2930
]
3031
);
3132
}

src/components/ResizableMap.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ResizableBox } from '@wordpress/components';
2+
import { useState } from '@wordpress/element';
3+
4+
const RESIZABLE_BOX_ENABLE_OPTION = {
5+
top: false,
6+
right: false,
7+
bottom: true,
8+
left: false,
9+
topRight: false,
10+
bottomRight: false,
11+
bottomLeft: false,
12+
topLeft: false,
13+
};
14+
15+
const MAP_MIN_HEIGHT = 100;
16+
17+
export function ResizableMap( {
18+
onResizeStart,
19+
onResize,
20+
onResizeStop,
21+
...props
22+
} ) {
23+
const [ isResizing, setIsResizing ] = useState( false );
24+
25+
return (
26+
<ResizableBox
27+
style={{
28+
position: 'absolute',
29+
top: 0,
30+
left: 0,
31+
right: 0,
32+
bottom: 0,
33+
}}
34+
className={ `apple-maps-block__resize-container ${isResizing ? 'is-resizing' : ''}` }
35+
enable={ RESIZABLE_BOX_ENABLE_OPTION }
36+
onResizeStart={ ( _event, _direction, elt ) => {
37+
onResizeStart( elt.clientHeight );
38+
onResize( elt.clientHeight );
39+
} }
40+
onResize={ ( _event, _direction, elt ) => {
41+
onResize( elt.clientHeight );
42+
if ( ! isResizing ) {
43+
setIsResizing( true );
44+
}
45+
} }
46+
onResizeStop={ ( _event, _direction, elt ) => {
47+
onResizeStop( elt.clientHeight );
48+
setIsResizing( false );
49+
} }
50+
minHeight={ MAP_MIN_HEIGHT }
51+
{ ...props }
52+
/>
53+
);
54+
}

src/edit.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {
77
import { __ } from '@wordpress/i18n';
88
import { useEffect, useRef, useState } from '@wordpress/element';
99
import { BlockControls } from '@wordpress/block-editor';
10+
import { dispatch } from '@wordpress/data';
1011

1112
import { AppleMapEdit } from './components/AppleMap';
1213
import EditAuthForm from './components/EditAuthForm';
1314
import InspectorSettings from './inspector-settings';
1415
import IsAdmin from './helper';
1516
import BlockIcon from './block-icon';
1617
import { debounce } from 'lodash';
18+
import { ResizableMap } from './components/ResizableMap'
1719

1820
export default function MapsBlockAppleEdit( props ) {
1921
const {
@@ -36,6 +38,7 @@ export default function MapsBlockAppleEdit( props ) {
3638
},
3739
setAttributes,
3840
clientId,
41+
isSelected,
3942
} = props;
4043

4144
const [ authenticated, setAuthenticated ] = useState( false );
@@ -44,6 +47,8 @@ export default function MapsBlockAppleEdit( props ) {
4447
const mapElement = useRef();
4548
const map = useRef();
4649

50+
const { toggleSelection } = dispatch( 'core/block-editor' );
51+
4752
useEffect( () => {
4853
if ( mapkit.authenticated ) {
4954
setIsLoading( false );
@@ -215,6 +220,19 @@ export default function MapsBlockAppleEdit( props ) {
215220
authenticated={ authenticated }
216221
map={ map }
217222
/>
223+
<ResizableMap
224+
onResizeStart={ () => {
225+
toggleSelection( false );
226+
} }
227+
onResize={ (newHeight) => {
228+
setAttributes( { height: newHeight } )
229+
} }
230+
onResizeStop={ ( newHeight ) => {
231+
toggleSelection( true );
232+
setAttributes( { height: newHeight } );
233+
} }
234+
showHandle={ isSelected }
235+
/>
218236
<div
219237
ref={ mapElement }
220238
className={ className }

0 commit comments

Comments
 (0)