|
|
import { select } from '@wordpress/data'; |
|
|
import { isEqual, some } from 'lodash'; |
|
|
import tracksRecordEvent from './tracking/track-record-event'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getEditorType = () => { |
|
|
if ( document.querySelector( '#editor .edit-post-layout' ) ) { |
|
|
return 'post'; |
|
|
} |
|
|
|
|
|
if ( document.querySelector( '#site-editor' ) ) { |
|
|
return 'site'; |
|
|
} |
|
|
|
|
|
if ( document.querySelector( '#widgets-editor' ) ) { |
|
|
return 'widgets'; |
|
|
} |
|
|
|
|
|
if ( document.querySelector( '#customize-controls .customize-widgets__sidebar-section.open' ) ) { |
|
|
return 'customize-widgets'; |
|
|
} |
|
|
|
|
|
return undefined; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const buildPropsFromContextBlock = ( block ) => { |
|
|
let context = block?.name; |
|
|
|
|
|
if ( block?.name === 'core/template-part' ) { |
|
|
const templatePartId = `${ block.attributes.theme }//${ block.attributes.slug }`; |
|
|
|
|
|
const entity = select( 'core' ).getEntityRecord( |
|
|
'postType', |
|
|
'wp_template_part', |
|
|
templatePartId |
|
|
); |
|
|
|
|
|
if ( entity?.area && entity?.area !== 'uncategorized' ) { |
|
|
context = `${ context }/${ entity.area }`; |
|
|
} |
|
|
|
|
|
return { |
|
|
entity_context: context, |
|
|
template_part_id: templatePartId, |
|
|
}; |
|
|
} |
|
|
|
|
|
return { |
|
|
entity_context: context, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getBlockEventContextProperties = ( rootClientId ) => { |
|
|
const { getBlockParentsByBlockName, getBlock } = select( 'core/block-editor' ); |
|
|
|
|
|
|
|
|
if ( typeof getBlockParentsByBlockName !== 'function' ) { |
|
|
return {}; |
|
|
} |
|
|
|
|
|
const editorType = getEditorType(); |
|
|
const defaultReturn = editorType === 'site' ? { entity_context: 'template' } : {}; |
|
|
|
|
|
|
|
|
if ( ! rootClientId ) { |
|
|
return defaultReturn; |
|
|
} |
|
|
|
|
|
|
|
|
const contexts = [ 'core/template-part', 'core/post-content', 'core/block', 'core/query' ]; |
|
|
|
|
|
|
|
|
const rootBlock = getBlock( rootClientId ); |
|
|
if ( contexts.some( ( context ) => context === rootBlock?.name ) ) { |
|
|
return buildPropsFromContextBlock( rootBlock ); |
|
|
} |
|
|
|
|
|
|
|
|
const matchingParentIds = getBlockParentsByBlockName( rootClientId, contexts, true ); |
|
|
if ( matchingParentIds.length ) { |
|
|
return buildPropsFromContextBlock( getBlock( matchingParentIds[ 0 ] ) ); |
|
|
} |
|
|
|
|
|
return defaultReturn; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const compareObjects = ( newObject, oldObject, keyMap = [] ) => { |
|
|
if ( isEqual( newObject, oldObject ) ) { |
|
|
return []; |
|
|
} |
|
|
|
|
|
const changedItems = []; |
|
|
for ( const key of Object.keys( newObject ) ) { |
|
|
|
|
|
|
|
|
if ( Array.isArray( newObject ) ) { |
|
|
if ( ! some( oldObject, ( item ) => isEqual( item, newObject[ key ] ) ) ) { |
|
|
changedItems.push( { keyMap: [ ...keyMap ], value: newObject[ key ] || 'reset' } ); |
|
|
} |
|
|
} else if ( ! isEqual( newObject[ key ], oldObject?.[ key ] ) ) { |
|
|
if ( typeof newObject[ key ] === 'object' && newObject[ key ] !== null ) { |
|
|
changedItems.push( |
|
|
...compareObjects( newObject[ key ], oldObject?.[ key ], [ ...keyMap, key ] ) |
|
|
); |
|
|
} else { |
|
|
changedItems.push( { keyMap: [ ...keyMap, key ], value: newObject[ key ] || 'reset' } ); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return changedItems; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const findUpdates = ( newContent, oldContent ) => { |
|
|
const newItems = compareObjects( newContent, oldContent ); |
|
|
|
|
|
const removedItems = compareObjects( oldContent, newContent ).filter( |
|
|
( update ) => ! some( newItems, ( { keyMap } ) => isEqual( update.keyMap, keyMap ) ) |
|
|
); |
|
|
removedItems.forEach( ( item ) => { |
|
|
if ( item.value?.color ) { |
|
|
|
|
|
item.value.color = 'reset'; |
|
|
} else if ( typeof item.value === 'object' && item.value !== null ) { |
|
|
|
|
|
|
|
|
|
|
|
item.value.is_reset = true; |
|
|
} else { |
|
|
item.value = 'reset'; |
|
|
} |
|
|
} ); |
|
|
|
|
|
return [ ...newItems, ...removedItems ]; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const buildGlobalStylesEventProps = ( keyMap, value ) => { |
|
|
let blockName; |
|
|
let elementType; |
|
|
let changeType; |
|
|
let propertyChanged; |
|
|
let fieldValue = value; |
|
|
let paletteSlug; |
|
|
|
|
|
if ( keyMap[ 1 ] === 'blocks' ) { |
|
|
blockName = keyMap[ 2 ]; |
|
|
if ( keyMap[ 3 ] === 'elements' ) { |
|
|
elementType = keyMap[ 4 ]; |
|
|
changeType = keyMap[ 5 ]; |
|
|
propertyChanged = keyMap[ 6 ]; |
|
|
} else { |
|
|
changeType = keyMap[ 3 ]; |
|
|
propertyChanged = keyMap[ 4 ]; |
|
|
} |
|
|
} else if ( keyMap[ 1 ] === 'elements' ) { |
|
|
elementType = keyMap[ 2 ]; |
|
|
changeType = keyMap[ 3 ]; |
|
|
propertyChanged = keyMap[ 4 ]; |
|
|
} else { |
|
|
changeType = keyMap[ 1 ]; |
|
|
propertyChanged = keyMap[ 2 ]; |
|
|
} |
|
|
|
|
|
if ( propertyChanged === 'palette' ) { |
|
|
fieldValue = value.color || 'reset'; |
|
|
paletteSlug = value.slug; |
|
|
} |
|
|
|
|
|
return { |
|
|
block_type: blockName, |
|
|
element_type: elementType, |
|
|
section: changeType, |
|
|
field: propertyChanged, |
|
|
field_value: |
|
|
typeof fieldValue === 'object' && fieldValue !== null |
|
|
? JSON.stringify( fieldValue ) |
|
|
: fieldValue, |
|
|
palette_slug: paletteSlug, |
|
|
}; |
|
|
}; |
|
|
|
|
|
let lastCalled; |
|
|
let originalGSObject; |
|
|
let functionTimeoutId; |
|
|
const debounceTimer = 500; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trackEventsWithTimer = ( updated, eventName ) => { |
|
|
functionTimeoutId = setTimeout( |
|
|
() => |
|
|
findUpdates( updated, originalGSObject )?.forEach( ( { keyMap, value } ) => { |
|
|
tracksRecordEvent( eventName, buildGlobalStylesEventProps( keyMap, value ) ); |
|
|
} ), |
|
|
debounceTimer |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const buildGlobalStylesContentEvents = ( updated, original, eventName ) => { |
|
|
|
|
|
const hasntBeenCalled = ! lastCalled; |
|
|
const timeCalled = new Date().getTime(); |
|
|
const recentlyCalled = timeCalled - lastCalled < debounceTimer; |
|
|
lastCalled = timeCalled; |
|
|
|
|
|
if ( hasntBeenCalled || ! recentlyCalled ) { |
|
|
|
|
|
originalGSObject = original; |
|
|
trackEventsWithTimer( updated, eventName ); |
|
|
} else { |
|
|
|
|
|
clearTimeout( functionTimeoutId ); |
|
|
trackEventsWithTimer( updated, eventName ); |
|
|
} |
|
|
}; |
|
|
|
|
|
export const getFlattenedBlockNames = ( block ) => { |
|
|
const blockNames = []; |
|
|
|
|
|
const queue = Array.isArray( block ) ? [ ...block ] : [ block ]; |
|
|
while ( queue.length > 0 ) { |
|
|
const currentBlock = queue.shift(); |
|
|
|
|
|
blockNames.push( currentBlock.name ); |
|
|
|
|
|
for ( const innerBlock of currentBlock.innerBlocks ) { |
|
|
queue.unshift( innerBlock ); |
|
|
} |
|
|
} |
|
|
|
|
|
return blockNames; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const findSavingSource = () => { |
|
|
const savePanel = document.querySelector( '.entities-saved-states__panel' ); |
|
|
|
|
|
|
|
|
return savePanel ? 'multi-entity-save-panel' : 'unknown'; |
|
|
}; |
|
|
|