Skip to content

Commit

Permalink
Map: Add block styles for WP20 and SotW 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Nov 20, 2023
1 parent 711ed97 commit 31b0a5b
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 19 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions mu-plugins/blocks/google-map/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ function render( $attributes, $content, $block ) {
$attributes['searchIcon'] = plugins_url( 'images/search.svg', __FILE__ );

$attributes['markerIcon'] = array(
'markerUrl' => plugins_url( 'images/map-marker-red.svg', __FILE__ ),
'imagesDirUrl' => plugins_url( 'images', __FILE__ ),
'markerHeight' => 68,
'markerWidth' => 68,
'markerAnchorYOffset' => -5,
'clusterUrl' => plugins_url( 'images/cluster-background.svg', __FILE__ ),
'clusterWidth' => 38,
'clusterHeight' => 38,
);
Expand Down
6 changes: 5 additions & 1 deletion mu-plugins/blocks/google-map/src/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getValidMarkers } from '../utilities/google-maps-api';
/**
*
* @param {Object} props
* @param {string} props.blockStyle
* @param {boolean} props.showMap
* @param {boolean} props.showList
* @param {boolean} props.showSearch
Expand All @@ -33,6 +34,7 @@ import { getValidMarkers } from '../utilities/google-maps-api';
* @return {JSX.Element}
*/
export default function Main( {
blockStyle,
showMap,
showList,
showSearch,
Expand Down Expand Up @@ -109,7 +111,9 @@ export default function Main( {
<Search searchQuery={ searchQuery } onQueryChange={ onQueryChange } iconURL={ searchIcon } />
) }

{ showMap && <Map apiKey={ apiKey } markers={ visibleMarkers } icon={ markerIcon } /> }
{ showMap && (
<Map apiKey={ apiKey } markers={ visibleMarkers } icon={ markerIcon } blockStyle={ blockStyle } />
) }

{ showList && visibleMarkers.length > 0 && <List markers={ visibleMarkers } /> }

Expand Down
20 changes: 15 additions & 5 deletions mu-plugins/blocks/google-map/src/components/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import {
* @param {Object} props
* @param {string} props.apiKey
* @param {Array} props.markers
* @param {string} props.blockStyle
* @param {Object} props.icon
*
* @return {JSX.Element}
*/
export default function Map( { apiKey, markers: rawMarkers, icon } ) {
export default function Map( { apiKey, markers: rawMarkers, icon, blockStyle } ) {
const [ loaded, setLoaded ] = useState( false );
const clusterer = useRef( null );
const googleMap = useRef( null );
Expand All @@ -45,7 +46,7 @@ export default function Map( { apiKey, markers: rawMarkers, icon } ) {
zoomControl: true,
mapTypeControl: false,
streetViewControl: false,
styles: mapStyles,
styles: mapStyles[ blockStyle ],
};

/**
Expand All @@ -65,13 +66,21 @@ export default function Map( { apiKey, markers: rawMarkers, icon } ) {
} );

combinedMarkers = combineDuplicateLocations( rawMarkers );
combinedMarkers = assignMarkerReferences( map, maps, infoWindow.current, combinedMarkers, icon );
combinedMarkers = assignMarkerReferences(
map,
maps,
infoWindow.current,
combinedMarkers,
icon,
blockStyle
);

clusterer.current = clusterMarkers(
map,
maps,
combinedMarkers.map( ( marker ) => marker.markerRef ),
icon
icon,
blockStyle
);

panToCenter(
Expand Down Expand Up @@ -99,7 +108,8 @@ export default function Map( { apiKey, markers: rawMarkers, icon } ) {
googleMapsApi.current,
infoWindow.current,
combinedMarkers,
icon
icon,
blockStyle
);

const markerObjects = combinedMarkers.map( ( marker ) => marker.markerRef );
Expand Down
8 changes: 7 additions & 1 deletion mu-plugins/blocks/google-map/src/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createRoot } from '@wordpress/element';
* Internal dependencies
*/
import Main from './components/main';
import { getBlockStyle } from './utilities/map-styles';

const init = () => {
const containers = document.querySelectorAll( '.wp-block-wporg-google-map' );
Expand All @@ -22,7 +23,12 @@ const init = () => {
for ( const container of containers ) {
root = createRoot( container );

root.render( <Main { ...wporgGoogleMap[ container.dataset.mapId ] } /> );
root.render(
<Main
blockStyle={ getBlockStyle( container.className ) }
{ ...wporgGoogleMap[ container.dataset.mapId ] }
/>
);
}
};

Expand Down
18 changes: 16 additions & 2 deletions mu-plugins/blocks/google-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';
import { registerBlockStyle, registerBlockType } from '@wordpress/blocks';
import { useBlockProps } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import metadata from './block.json';
import Main from './components/main';
import { getBlockStyle } from './utilities/map-styles';

function Edit( { attributes } ) {
const { id, className } = attributes;

return (
<div { ...useBlockProps() }>
<Main { ...wporgGoogleMap[ attributes.id ] } />
<Main blockStyle={ getBlockStyle( className ) } { ...wporgGoogleMap[ id ] } />
</div>
);
}

registerBlockType( metadata.name, {
edit: Edit,
} );

registerBlockStyle( metadata.name, {
name: 'wp20',
label: 'WP20',
isDefault: true,
} );

registerBlockStyle( metadata.name, {
name: 'sotw-2023',
label: 'State of the Word 2023',
} );
10 changes: 6 additions & 4 deletions mu-plugins/blocks/google-map/src/utilities/google-maps-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ export function combineDuplicateLocations( rawMarkers ) {
* @param {google.maps.InfoWindow} infoWindow
* @param {Array} wpEvents
* @param {Object} rawIcon
* @param {string} blockStyle
*/
export function assignMarkerReferences( map, maps, infoWindow, wpEvents, rawIcon ) {
export function assignMarkerReferences( map, maps, infoWindow, wpEvents, rawIcon, blockStyle ) {
const icon = {
url: rawIcon.markerUrl,
url: rawIcon.imagesDirUrl + `/map-marker-${ blockStyle }.svg`,
size: new maps.Size( rawIcon.markerHeight, rawIcon.markerWidth ),
anchor: new maps.Point( 34, rawIcon.markerWidth / 2 ),
scaledSize: new maps.Size( rawIcon.markerHeight / 2, rawIcon.markerWidth / 2 ),
Expand Down Expand Up @@ -141,12 +142,13 @@ function openInfoWindow( infoWindow, map, markerObject, rawMarker ) {
* @param {google.maps} maps
* @param {google.maps.Marker[]} markers
* @param {Object} rawIcon
* @param {string} blockStyle
*
* @return {MarkerClusterer}
*/
export function clusterMarkers( map, maps, markers, rawIcon ) {
export function clusterMarkers( map, maps, markers, rawIcon, blockStyle ) {
const clusterIcon = {
url: rawIcon.clusterUrl,
url: rawIcon.imagesDirUrl + `/cluster-background-${ blockStyle }.svg`,
size: new maps.Size( rawIcon.clusterHeight, rawIcon.clusterWidth ),
anchor: new maps.Point( rawIcon.clusterHeight, rawIcon.clusterWidth ),
scaledSize: new maps.Size( rawIcon.clusterHeight, rawIcon.clusterWidth ),
Expand Down
Loading

0 comments on commit 31b0a5b

Please sign in to comment.