Skip to content

Commit

Permalink
added format to the dynamic content popup
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Sep 11, 2024
1 parent 4dbfbfd commit 5acc421
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/components/base-control2/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
get, set, cloneDeep,
} from 'lodash'

export const useControlHandlers = ( _attribute, responsive = false, hover = false, valueCallback = null, changeCallback = null ) => {
export const useControlHandlers = ( _attribute = '', responsive = false, hover = false, valueCallback = null, changeCallback = null ) => {
// If there's a '.' it means the attribute value is an object, and we want
// to change a single property inside that object.
const willUpdateObjectProperty = _attribute.includes( '.' )
Expand Down
13 changes: 10 additions & 3 deletions src/components/dynamic-content-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { QueryLoopContext } from '~stackable/higher-order/with-query-loop-contex
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n'
import { __, sprintf } from '@wordpress/i18n'
import { useBlockEditContext } from '@wordpress/block-editor'
import {
Button,
Expand Down Expand Up @@ -120,12 +120,17 @@ export const useDynamicContentControlProps = props => {
const isPressed = isPopoverOpen || activeAttributes.length
const activeAttribute = first( activeAttributes ) || ''

const onChange = ( newValue, editorQueryString, frontendQueryString ) => {
const onChange = ( newValue, editorQueryString, frontendQueryString, format = '' ) => {
// If `isFormatType` is true, the onChange function will generate a `stackable/dynamic-content` format type.
const willChangeValue = props.isFormatType
let willChangeValue = props.isFormatType
? `<span data-stk-dynamic="${ frontendQueryString }" contenteditable="false" class="stk-dynamic-content">${ newValue }</span>`
: `!#stk_dynamic/${ frontendQueryString }!#`

// If `format` is set, then we will use it to format the value.
if ( format ) {
willChangeValue = sprintf( format, willChangeValue )
}

props.onChange( willChangeValue )
setDebouncedValue( willChangeValue )

Expand Down Expand Up @@ -465,6 +470,8 @@ export const DynamicContentButton = memo( props => {
activeAttribute={ props.activeAttribute }
type={ props.type }
blockDetails={ block }
value={ props.value }
hasFormat={ !! props.value?.includes( 'class="stk-dynamic-content"' ) }
/>
) }

Expand Down

0 comments on commit 5acc421

Please sign in to comment.