PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.0
GenerateBlocks v2.0.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / hooks / useStyleIndicator.js
generateblocks / src / hooks Last commit date
index.js 1 year ago useAuthors.js 3 years ago useBlockStyles.js 1 year ago useDebounceState.js 4 years ago useDeviceAttributes.js 2 years ago useDeviceType.js 1 year ago useInnerBlocksCount.js 4 years ago useQueryReducer.js 1 year ago useRecordsReducer.js 3 years ago useSelectedBlockElements.js 2 years ago useStyleIndicator.js 2 years ago useTaxonomies.js 1 year ago useTaxonomyRecords.js 3 years ago
useStyleIndicator.js
35 lines
1 import { useReducer, useMemo, useRef, useEffect } from '@wordpress/element';
2 import { applyFilters } from '@wordpress/hooks';
3
4 function reducer( state, action ) {
5 return {
6 ...state,
7 [ action.type ]: action.value,
8 };
9 }
10
11 export function useStyleIndicator( computedStyles, panelControls, content = '', deviceAttributes = {} ) {
12 const [ controlGlobalStyle, dispatchControlGlobalStyle ] = useReducer( reducer, panelControls );
13
14 const styleSources = applyFilters(
15 'generateblocks.editor.panel.computedStyleSources',
16 {},
17 computedStyles,
18 Object.keys( controlGlobalStyle ),
19 deviceAttributes,
20 );
21 const hasGlobalStyle = useMemo( () => {
22 return Object.values( controlGlobalStyle ).some( ( control ) => control === true );
23 }, [ controlGlobalStyle ] );
24
25 const prevContentLength = useRef( 0 );
26 const currentContentLength = content ? content.length : 0;
27 const contentWasUpdated = prevContentLength.current !== currentContentLength;
28
29 useEffect( () => {
30 prevContentLength.current = currentContentLength;
31 }, [ content ] );
32
33 return { dispatchControlGlobalStyle, styleSources, hasGlobalStyle, contentWasUpdated };
34 }
35