@wordpress/patterns 1.12.2 → 1.12.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/build/components/create-pattern-modal.js +6 -79
  2. package/build/components/create-pattern-modal.js.map +1 -1
  3. package/build/components/partial-syncing-controls.js +3 -3
  4. package/build/components/partial-syncing-controls.js.map +1 -1
  5. package/build/components/reset-overrides-control.js +6 -1
  6. package/build/components/reset-overrides-control.js.map +1 -1
  7. package/build/constants.js +4 -23
  8. package/build/constants.js.map +1 -1
  9. package/build/private-apis.js +2 -0
  10. package/build/private-apis.js.map +1 -1
  11. package/build/private-hooks.js +100 -0
  12. package/build/private-hooks.js.map +1 -0
  13. package/build-module/components/create-pattern-modal.js +7 -79
  14. package/build-module/components/create-pattern-modal.js.map +1 -1
  15. package/build-module/components/partial-syncing-controls.js +3 -3
  16. package/build-module/components/partial-syncing-controls.js.map +1 -1
  17. package/build-module/components/reset-overrides-control.js +6 -1
  18. package/build-module/components/reset-overrides-control.js.map +1 -1
  19. package/build-module/constants.js +4 -22
  20. package/build-module/constants.js.map +1 -1
  21. package/build-module/private-apis.js +2 -0
  22. package/build-module/private-apis.js.map +1 -1
  23. package/build-module/private-hooks.js +94 -0
  24. package/build-module/private-hooks.js.map +1 -0
  25. package/package.json +7 -7
  26. package/src/components/create-pattern-modal.js +5 -79
  27. package/src/components/partial-syncing-controls.js +3 -3
  28. package/src/components/reset-overrides-control.js +13 -1
  29. package/src/constants.js +4 -19
  30. package/src/private-apis.js +2 -0
  31. package/src/private-hooks.js +91 -0
@@ -0,0 +1,91 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useSelect, useDispatch } from '@wordpress/data';
5
+ import { store as coreStore } from '@wordpress/core-data';
6
+ import { useMemo } from '@wordpress/element';
7
+
8
+ /**
9
+ * Internal dependencies
10
+ */
11
+ import { CATEGORY_SLUG } from './components/category-selector';
12
+
13
+ /**
14
+ * Helper hook that creates a Map with the core and user patterns categories
15
+ * and removes any duplicates. It's used when we need to create new user
16
+ * categories when creating or importing patterns.
17
+ * This hook also provides a function to find or create a pattern category.
18
+ *
19
+ * @return {Object} The merged categories map and the callback function to find or create a category.
20
+ */
21
+ export function useAddPatternCategory() {
22
+ const { saveEntityRecord, invalidateResolution } = useDispatch( coreStore );
23
+ const { corePatternCategories, userPatternCategories } = useSelect(
24
+ ( select ) => {
25
+ const { getUserPatternCategories, getBlockPatternCategories } =
26
+ select( coreStore );
27
+
28
+ return {
29
+ corePatternCategories: getBlockPatternCategories(),
30
+ userPatternCategories: getUserPatternCategories(),
31
+ };
32
+ },
33
+ []
34
+ );
35
+ const categoryMap = useMemo( () => {
36
+ // Merge the user and core pattern categories and remove any duplicates.
37
+ const uniqueCategories = new Map();
38
+ userPatternCategories.forEach( ( category ) => {
39
+ uniqueCategories.set( category.label.toLowerCase(), {
40
+ label: category.label,
41
+ name: category.name,
42
+ id: category.id,
43
+ } );
44
+ } );
45
+
46
+ corePatternCategories.forEach( ( category ) => {
47
+ if (
48
+ ! uniqueCategories.has( category.label.toLowerCase() ) &&
49
+ // There are two core categories with `Post` label so explicitly remove the one with
50
+ // the `query` slug to avoid any confusion.
51
+ category.name !== 'query'
52
+ ) {
53
+ uniqueCategories.set( category.label.toLowerCase(), {
54
+ label: category.label,
55
+ name: category.name,
56
+ } );
57
+ }
58
+ } );
59
+ return uniqueCategories;
60
+ }, [ userPatternCategories, corePatternCategories ] );
61
+
62
+ async function findOrCreateTerm( term ) {
63
+ try {
64
+ const existingTerm = categoryMap.get( term.toLowerCase() );
65
+ if ( existingTerm?.id ) {
66
+ return existingTerm.id;
67
+ }
68
+ // If we have an existing core category we need to match the new user category to the
69
+ // correct slug rather than autogenerating it to prevent duplicates, eg. the core `Headers`
70
+ // category uses the singular `header` as the slug.
71
+ const termData = existingTerm
72
+ ? { name: existingTerm.label, slug: existingTerm.name }
73
+ : { name: term };
74
+ const newTerm = await saveEntityRecord(
75
+ 'taxonomy',
76
+ CATEGORY_SLUG,
77
+ termData,
78
+ { throwOnError: true }
79
+ );
80
+ invalidateResolution( 'getUserPatternCategories' );
81
+ return newTerm.id;
82
+ } catch ( error ) {
83
+ if ( error.code !== 'term_exists' ) {
84
+ throw error;
85
+ }
86
+ return error.data.term_id;
87
+ }
88
+ }
89
+
90
+ return { categoryMap, findOrCreateTerm };
91
+ }