Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"lint:js": "wp-scripts lint-js",
"lint:pkg-json": "wp-scripts lint-pkg-json",
"packages-update": "wp-scripts packages-update",
"test:unit": "wp-scripts test-unit-js",
"plugin-zip": "wp-scripts plugin-zip",
"env:cli": "npx @wordpress/env run cli",
"env:db": "npx @wordpress/env run cli -- mysql -uroot -ppassword wordpress --host=mysql",
Expand Down
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
die;
}

const PLUGIN_VERSION = '0.1.0';
const PLUGIN_VERSION = '0.1.1';
const PLUGIN_PATH = __DIR__;

require_once __DIR__ . '/inc/namespace.php';
Expand Down
51 changes: 48 additions & 3 deletions src/blocks/map/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const aspectRatioOptions = [
* @param {Function} props.updateMarkers Callback to update map pins.
* @return {React.ReactNode} Container node for Map.
*/
const MapPreview = ( {
export const MapPreview = ( {
mapStyle,
projection = 'equirectangular',
latitude = 0,
Expand All @@ -98,6 +98,14 @@ const MapPreview = ( {
} ) => {
const containerRef = useRef( null );

// The init effect must not re-run on view changes, so it reads the latest
// view from a ref.
const viewRef = useRef();
viewRef.current = {
center: [ longitude || 0, latitude || 0 ],
zoom: zoom ?? 1,
};

// Parse the slideBlocks into a stable JSON string, which we can use
// to rerender the map as needed without unnecessarily causing it to
// reinit (with a distracting visual flash) every time text is changed
Expand Down Expand Up @@ -146,6 +154,7 @@ const MapPreview = ( {
// realm, so mapbox accepts it, while the node renders in the canvas.
const mapContainer = document.createElement( 'div' );
mapContainer.style.minHeight = '250px';
mapContainer.style.height = '100%';
containerRef.current.appendChild( mapContainer );

mapboxgl.accessToken = hmMap.apiKey;
Expand All @@ -155,19 +164,55 @@ const MapPreview = ( {
// Fast repeat clicks while selecting or placing pins should not
// zoom the canvas; zoom via the nav control instead.
doubleClickZoom: false,
dragPan: false,
minZoom: 0,
projection,
renderWorldCopies: false,
scrollZoom: false,
style: mapStyle || 'mapbox://styles/mapbox/light-v11',
zoom: zoom || 1,
zoom: zoom ?? 1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

} );

map.addControl( fullScreenControl );

// Mapbox's built-in drag pan mixes pointer coordinates from the parent
// window and the editor iframe, so the map jumps by the iframe's
// offset. Pan from deltas read in one document instead.
const canvas = map.getCanvasContainer();
canvas.addEventListener( 'pointerdown', ( downEvent ) => {
if ( downEvent.button !== 0 ) {
return;
}

const doc = canvas.ownerDocument;
let lastX = downEvent.clientX;
let lastY = downEvent.clientY;

const onPointerMove = ( moveEvent ) => {
map.panBy(
[ lastX - moveEvent.clientX, lastY - moveEvent.clientY ],
{ animate: false }
);
lastX = moveEvent.clientX;
lastY = moveEvent.clientY;
};

const onPointerUp = () => {
doc.removeEventListener( 'pointermove', onPointerMove );
doc.removeEventListener( 'pointerup', onPointerUp );
};

doc.addEventListener( 'pointermove', onPointerMove );
doc.addEventListener( 'pointerup', onPointerUp );
} );

const slideMarkers = JSON.parse( serializedFeatures );

map.on( 'load', () => {
// The container may have changed size since creation (iframed canvas).
map.resize();
map.jumpTo( viewRef.current );

// For clustering we need to add a data source.
map.addSource( 'markers', {
type: 'geojson',
Expand Down Expand Up @@ -255,7 +300,7 @@ const MapPreview = ( {
map.setCenter( [ longitude || 0, latitude || 0 ] );
map.setMinZoom( minZoom );
map.setMaxZoom( maxZoom );
map.setZoom( zoom || 1 );
map.setZoom( zoom ?? 1 );
}
}, [ projection, latitude, longitude, zoom, minZoom, maxZoom ] );

Expand Down
157 changes: 157 additions & 0 deletions src/blocks/map/edit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { act } from 'react';
import { createRoot } from 'react-dom/client';

import { MapPreview } from './edit';

// The editor externalises @wordpress/* packages, so they are not installed
// under Jest; only withColors is called when edit.js loads.
jest.mock( '@wordpress/blocks', () => ( {} ), { virtual: true } );
jest.mock( '@wordpress/components', () => ( {} ), { virtual: true } );
jest.mock( '@wordpress/data', () => ( {} ), { virtual: true } );
jest.mock(
'@wordpress/block-editor',
() => ( { withColors: () => ( component ) => component } ),
{ virtual: true }
);

const mockLog = [];
const mockMaps = [];

jest.mock( 'mapbox-gl', () => {
const calls = ( name ) =>
function ( ...args ) {
this.calls[ name ].push( args );
mockLog.push( name );
};
const methods = [
'setZoom',
'setCenter',
'setProjection',
'setMinZoom',
'setMaxZoom',
'resize',
'jumpTo',
'addControl',
'removeControl',
'remove',
'addSource',
'addLayer',
'isSourceLoaded',
'panBy',
];
class Map {
constructor( options ) {
this.options = options;
this.handlers = {};
this.calls = Object.fromEntries(
methods.map( ( m ) => [ m, [] ] )
);
this.canvasContainer = global.document.body.appendChild(
global.document.createElement( 'div' )
);
mockMaps.push( this );
}

getCanvasContainer() {
return this.canvasContainer;
}

on( event, handler ) {
this.handlers[ event ] = handler;
}
}
methods.forEach( ( name ) => {
Map.prototype[ name ] = calls( name );
} );
return { __esModule: true, default: { Map, NavigationControl: class {} } };
} );

global.IS_REACT_ACT_ENVIRONMENT = true;
global.hmMap = { apiKey: 'test' };

describe( 'MapPreview', () => {
it( 'keeps a zoom of 0 and resizes before jumping to the saved view on load', () => {
const container = document.createElement( 'div' );
const root = createRoot( container );

act( () => {
root.render(
<MapPreview
zoom={ 0 }
latitude={ 10 }
longitude={ 20 }
slideBlocks={ [] }
updateMarkers={ jest.fn() }
mapStyle=""
/>
);
} );

const map = mockMaps[ mockMaps.length - 1 ];
expect( map.options.zoom ).toBe( 0 );
expect( map.calls.setZoom.at( -1 ) ).toEqual( [ 0 ] );

mockLog.length = 0;
act( () => {
map.handlers.load();
} );

expect( map.calls.resize ).toHaveLength( 1 );
expect( map.calls.jumpTo ).toEqual( [
[ { center: [ 20, 10 ], zoom: 0 } ],
] );
expect( mockLog.indexOf( 'resize' ) ).toBeLessThan(
mockLog.indexOf( 'jumpTo' )
);

act( () => root.unmount() );
} );

it( 'pans the map from same-document pointer deltas instead of mapbox dragPan', () => {
const container = document.createElement( 'div' );
const root = createRoot( container );

act( () => {
root.render(
<MapPreview
zoom={ 1 }
latitude={ 10 }
longitude={ 20 }
slideBlocks={ [] }
updateMarkers={ jest.fn() }
mapStyle=""
/>
);
} );

const map = mockMaps[ mockMaps.length - 1 ];
expect( map.options.dragPan ).toBe( false );

const fire = ( target, type, x, y, button = 0 ) =>
target.dispatchEvent(
new window.MouseEvent( type, {
bubbles: true,
button,
clientX: x,
clientY: y,
} )
);

fire( map.canvasContainer, 'pointerdown', 100, 100, 2 );
fire( document, 'pointermove', 150, 150 );
expect( map.calls.panBy ).toHaveLength( 0 );

fire( map.canvasContainer, 'pointerdown', 100, 100 );
fire( document, 'pointermove', 300, 100 );
fire( document, 'pointermove', 300, 150 );
fire( document, 'pointerup', 300, 150 );
fire( document, 'pointermove', 400, 400 );

expect( map.calls.panBy ).toEqual( [
[ [ -200, 0 ], { animate: false } ],
[ [ 0, -50 ], { animate: false } ],
] );

act( () => root.unmount() );
} );
} );
Loading