diff --git a/package.json b/package.json index 4f86ab7..ec9623f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/plugin.php b/plugin.php index aa81c8f..2cf8d50 100644 --- a/plugin.php +++ b/plugin.php @@ -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'; diff --git a/src/blocks/map/edit.js b/src/blocks/map/edit.js index 0191e5d..20c3870 100644 --- a/src/blocks/map/edit.js +++ b/src/blocks/map/edit.js @@ -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, @@ -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 @@ -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; @@ -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, } ); 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', @@ -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 ] ); diff --git a/src/blocks/map/edit.test.js b/src/blocks/map/edit.test.js new file mode 100644 index 0000000..902ff31 --- /dev/null +++ b/src/blocks/map/edit.test.js @@ -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( + + ); + } ); + + 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( + + ); + } ); + + 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() ); + } ); +} );