-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
use-pattern-settings.js
51 lines (43 loc) · 1.31 KB
/
use-pattern-settings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* WordPress dependencies
*/
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import { filterOutDuplicatesByName } from './utils';
export default function usePatternSettings() {
const storedSettings = useSelect( ( select ) => {
const { getSettings } = unlock( select( editSiteStore ) );
return getSettings();
}, [] );
const settingsBlockPatterns =
storedSettings.__experimentalAdditionalBlockPatterns ?? // WP 6.0
storedSettings.__experimentalBlockPatterns; // WP 5.9
const restBlockPatterns = useSelect(
( select ) => select( coreStore ).getBlockPatterns(),
[]
);
const blockPatterns = useMemo(
() =>
[
...( settingsBlockPatterns || [] ),
...( restBlockPatterns || [] ),
].filter( filterOutDuplicatesByName ),
[ settingsBlockPatterns, restBlockPatterns ]
);
const settings = useMemo( () => {
const { __experimentalAdditionalBlockPatterns, ...restStoredSettings } =
storedSettings;
return {
...restStoredSettings,
__experimentalBlockPatterns: blockPatterns,
isPreviewMode: true,
};
}, [ storedSettings, blockPatterns ] );
return settings;
}