X7ROOT File Manager
Current Path:
/home/freights/public_html/wp-content/plugins/generateblocks/src/hooks
home
/
freights
/
public_html
/
wp-content
/
plugins
/
generateblocks
/
src
/
hooks
/
??
..
??
4zxl4en7
(9.03 KB)
??
dg8glb28
(9.03 KB)
??
index.js
(530 B)
??
mlvvn743
(9.03 KB)
??
portugues.lng
(9.03 KB)
??
useAuthors.js
(1.89 KB)
??
useBlockStyles.js
(1.33 KB)
??
useDebounceState.js
(324 B)
??
useDeviceAttributes.js
(2.52 KB)
??
useDeviceType.js
(2.26 KB)
??
useInnerBlocksCount.js
(238 B)
??
useQueryReducer.js
(1.03 KB)
??
useRecordsReducer.js
(1.44 KB)
??
useSelectedBlockElements.js
(1.57 KB)
??
useStyleIndicator.js
(1.1 KB)
??
useTaxonomies.js
(659 B)
??
useTaxonomyRecords.js
(2.05 KB)
Editing: useDeviceAttributes.js
import { useDeviceType } from './index'; import { useMemo } from '@wordpress/element'; import isObject from 'lodash/isObject'; /** * Given an object of attributes will split by device. * * @param {Object} attributes The block attributes. * * @return {{tablet: {}, desktop: {}, mobile: {}}} The device attributes. */ export function splitAttributes( attributes ) { return Object .entries( attributes ) .reduce( ( deviceKeys, [ key, value ] ) => { if ( key.includes( 'Tablet' ) ) { deviceKeys.tablet[ key.replace( 'Tablet', '' ) ] = value; } else if ( key.includes( 'Mobile' ) ) { deviceKeys.mobile[ key.replace( 'Mobile', '' ) ] = value; } if ( isObject( value ) ) { const valueDeviceKeys = splitAttributes( value ); deviceKeys.desktop[ key ] = valueDeviceKeys.desktop; deviceKeys.tablet[ key ] = valueDeviceKeys.tablet; deviceKeys.mobile[ key ] = valueDeviceKeys.mobile; } else { deviceKeys.desktop[ key ] = value; } return deviceKeys; }, { desktop: {}, tablet: {}, mobile: {} } ); } /** * Given an object of attributes it will add the device suffix. * * @param {Object} attrs The device attributes. * @param {string} device The device name, capitalized. * * @return {Object} The block attributes. */ export function addDeviceToAttributes( attrs, device = 'Tablet' ) { return Object.entries( attrs ).reduce( ( result, [ key, value ] ) => { if ( isObject( value ) ) { result[ key ] = addDeviceToAttributes( value, device ); } else { result[ key + device ] = value; } return result; }, {} ); } /** * Hook to handle device attributes and setAttributes. * * @param {Object} attributes The block attributes. * @param {Function} setAttributes The block setAttributes function. * * @return {Array} The device attributes and set function. */ export default function useDeviceAttributes( attributes, setAttributes ) { const [ device ] = useDeviceType(); const deviceName = 'Desktop' !== device ? device : ''; const deviceAttributes = useMemo( () => ( splitAttributes( attributes ) ), [ JSON.stringify( attributes ) ] ); const setDeviceAttributes = useMemo( () => ( attrs = {}, objName = '' ) => { if ( objName ) { setAttributes( { [ objName ]: addDeviceToAttributes( attrs, deviceName ), } ); return; } setAttributes( addDeviceToAttributes( attrs, deviceName ) ); }, [ deviceName, setAttributes, JSON.stringify( attributes ) ] ); const activeDevice = device.toLowerCase(); return [ deviceAttributes[ activeDevice ], setDeviceAttributes ]; }
Upload File
Create Folder