Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map: Add style variations #512

Merged
merged 1 commit into from
Nov 20, 2023
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
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 @@ -3,12 +3,13 @@
/**
* WordPress dependencies
*/
import { createRoot } from '@wordpress/element';

Check warning on line 6 in mu-plugins/blocks/google-map/src/front.js

View workflow job for this annotation

GitHub Actions / Lint everything

createRoot not found in '@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 @@
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
Loading