From e7d762b76e2e2484c9f0eb3435fa15ab9227cf35 Mon Sep 17 00:00:00 2001 From: Sander van Dragt Date: Mon, 19 Oct 2026 18:08:18 +0100 Subject: [PATCH 1/3] Keep the saved zoom and view in the editor map A saved zoom of 0 was replaced by 1, and in the iframed WordPress 7 editor the map could open at a different zoom because it was never resized after its first load. Use `?? 1` for the zoom fallback, resize and jump to the saved view on the first load, and let the map container follow its parent's height. Add a test:unit script and a Jest test for MapPreview with mapbox-gl mocked. Refs #3 Co-Authored-By: Claude --- package.json | 1 + src/blocks/map/edit.js | 19 +++++-- src/blocks/map/edit.test.js | 101 ++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 src/blocks/map/edit.test.js 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/src/blocks/map/edit.js b/src/blocks/map/edit.js index 0191e5d..1ea784e 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; @@ -160,7 +169,7 @@ const MapPreview = ( { renderWorldCopies: false, scrollZoom: false, style: mapStyle || 'mapbox://styles/mapbox/light-v11', - zoom: zoom || 1, + zoom: zoom ?? 1, } ); map.addControl( fullScreenControl ); @@ -168,6 +177,10 @@ const MapPreview = ( { 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 +268,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..1903c8b --- /dev/null +++ b/src/blocks/map/edit.test.js @@ -0,0 +1,101 @@ +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', + ]; + class Map { + constructor( options ) { + this.options = options; + this.handlers = {}; + this.calls = Object.fromEntries( + methods.map( ( m ) => [ m, [] ] ) + ); + mockMaps.push( this ); + } + + 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() ); + } ); +} ); From b284796446707c6f5588f18c5e7d063f1c8b260f Mon Sep 17 00:00:00 2001 From: Sander van Dragt Date: Mon, 19 Oct 2026 18:13:00 +0100 Subject: [PATCH 2/3] Pan the editor map from pointer events in the iframe document In the WordPress 7 editor the canvas is an iframe, while the bundled mapbox-gl runs in the parent window. Mapbox's drag pan then mixed pointer coordinates from both documents, so dragging the map background jumped by the iframe's offset. Turn off dragPan in the editor and pan by the distance between pointer events read from the canvas's own document. Fixes #3 Co-Authored-By: Claude --- src/blocks/map/edit.js | 32 +++++++++++++++++++++ src/blocks/map/edit.test.js | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/src/blocks/map/edit.js b/src/blocks/map/edit.js index 1ea784e..20c3870 100644 --- a/src/blocks/map/edit.js +++ b/src/blocks/map/edit.js @@ -164,6 +164,7 @@ export 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, @@ -174,6 +175,37 @@ export const MapPreview = ( { 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', () => { diff --git a/src/blocks/map/edit.test.js b/src/blocks/map/edit.test.js index 1903c8b..902ff31 100644 --- a/src/blocks/map/edit.test.js +++ b/src/blocks/map/edit.test.js @@ -37,6 +37,7 @@ jest.mock( 'mapbox-gl', () => { 'addSource', 'addLayer', 'isSourceLoaded', + 'panBy', ]; class Map { constructor( options ) { @@ -45,9 +46,16 @@ jest.mock( 'mapbox-gl', () => { 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; } @@ -98,4 +106,52 @@ describe( 'MapPreview', () => { 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() ); + } ); } ); From e0e4e142bedf38c64cfbfbdf4e679615fb8e688c Mon Sep 17 00:00:00 2001 From: Sander van Dragt Date: Mon, 19 Oct 2026 18:39:17 +0100 Subject: [PATCH 3/3] Match PLUGIN_VERSION to the plugin header version Co-Authored-By: Claude --- plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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';