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

fix (map): allow dynamic content in location field #3105

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
8 changes: 7 additions & 1 deletion src/block/map/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
PanelAdvancedSettings,
ResizerTooltip,
StyleControl,
getDynamicContent,
} from '~stackable/components'
import { useDeviceType } from '~stackable/hooks'
import {
Expand Down Expand Up @@ -104,6 +105,7 @@ const Edit = props => {

const { stackable_google_maps_api_key: apiKey } = settings
const userCanManageApiKey = useMemo( () => currentUserHasCapability( 'manage_options' ), [] )
const mapAddress = address.startsWith( '!#stk_dynamic/' ) ? getDynamicContent( address ) : address

// This just forces the tooltip to update.
const [ , setResizingHeight ] = useState( '' )
Expand Down Expand Up @@ -243,6 +245,9 @@ const Edit = props => {
// Try geocoding the address.
const [ useGeocoding, setUseGeocoding ] = useState( true )
const geocodeAddress = useCallback( debounce( address => {
if ( address.startsWith( '!#stk_dynamic/' ) ) {
address = getDynamicContent( address )
}
if ( useGeocoding ) {
geocoderRef.current.geocode( {
address,
Expand Down Expand Up @@ -291,6 +296,7 @@ const Edit = props => {
label={ __( 'Location', i18n ) }
attribute="address"
placeholder={ __( 'Enter an address or location', i18n ) }
isDynamic={ true }
/>
</>
) : (
Expand Down Expand Up @@ -537,7 +543,7 @@ const Edit = props => {
) : (
<iframe
title={ __( 'Embedded content from Google Map Platform.', i18n ) }
src={ `https://maps.google.com/maps?q=${ address || DEFAULT_ADDRESS }&t=&z=${ zoom || DEFAULT_ZOOM }&ie=UTF8&output=embed` }
src={ `https://maps.google.com/maps?q=${ mapAddress || DEFAULT_ADDRESS }&t=&z=${ zoom || DEFAULT_ZOOM }&ie=UTF8&output=embed` }
frameBorder="0"
/>
) }
Expand Down
16 changes: 16 additions & 0 deletions src/block/map/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@
}
}

.stk-control__location-control {
display: flex;

> :not(.stk--has-dynamic-content) {
.stk-dynamic-content-control__button {
margin-top: 23px;
}
}

.stk-control__reset-button {
position: static;
margin-left: 2px;
margin-right: 2px;
}
}

.stk-block-map__api-key-notice {
margin: 0 0 16px;
}
Expand Down
36 changes: 27 additions & 9 deletions src/block/map/location-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { i18n } from 'stackable'
import { DynamicContentControl, useDynamicContentControlProps } from '~stackable/components'

/**
* WordPress dependencies
Expand All @@ -20,6 +21,14 @@ import {
const LocationControl = props => {
const [ waitForGoogle, setWaitForGoogle ] = useState( 0 )

const dynamicContentProps = useDynamicContentControlProps( {
value: props.value,
onChange: value => {
props.onTextChange( value )
},
isFormatType: false,
} )

useEffect( () => {
if ( ! window?.google?.maps ) {
const interval = setInterval( () => {
Expand Down Expand Up @@ -54,21 +63,30 @@ const LocationControl = props => {
}, [ ref.current, waitForGoogle ] )

return (
<TextControl
label={ __( 'Location', i18n ) }
ref={ ref }
value={ props.value }
help={ __( 'Type in a pair of latitude longitude coordinates. You can also type in the name of the location if your API Key has Geocoding API and Places API enabled.', i18n ) }
onChange={ value => {
props.onTextChange( value )
} }
/>
<div className="stk-control__location-control">
<DynamicContentControl
enable={ true }
hasPanelModifiedIndicator={ true }
{ ...dynamicContentProps }
>
<TextControl
label={ __( 'Location', i18n ) }
ref={ ref }
value={ props.value }
help={ __( 'Type in a pair of latitude longitude coordinates. You can also type in the name of the location if your API Key has Geocoding API and Places API enabled.', i18n ) }
onChange={ value => {
props.onTextChange( value )
} }
/>
</DynamicContentControl>
</div>
)
}

LocationControl.defaultProps = {
onChange: null,
value: '',
hasPanelModifiedIndicator: true,
}

export default LocationControl
2 changes: 1 addition & 1 deletion src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export {
export { default as Div } from './div'
export { default as ControlIconToggle } from './control-icon-toggle'
export {
default as DynamicContentControl, useDynamicContent, getDynamicContent,
default as DynamicContentControl, useDynamicContent, getDynamicContent, useDynamicContentControlProps,
} from './dynamic-content-control'
export { default as Separator2 } from './separator2'
export { default as ColumnInnerBlocks } from './column-inner-blocks'
Expand Down
Loading