@wordpress/patterns 1.15.0 → 1.17.0

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 (77) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/api/index.js +22 -0
  3. package/build/api/index.js.map +1 -0
  4. package/build/components/allow-overrides-modal.js +114 -0
  5. package/build/components/allow-overrides-modal.js.map +1 -0
  6. package/build/components/category-selector.js.map +1 -1
  7. package/build/components/create-pattern-modal.js.map +1 -1
  8. package/build/components/duplicate-pattern-modal.js.map +1 -1
  9. package/build/components/index.js.map +1 -1
  10. package/build/components/overrides-panel.js +44 -0
  11. package/build/components/overrides-panel.js.map +1 -0
  12. package/build/components/pattern-convert-button.js.map +1 -1
  13. package/build/components/pattern-overrides-controls.js +107 -0
  14. package/build/components/pattern-overrides-controls.js.map +1 -0
  15. package/build/components/patterns-manage-button.js.map +1 -1
  16. package/build/components/rename-pattern-category-modal.js.map +1 -1
  17. package/build/components/rename-pattern-modal.js.map +1 -1
  18. package/build/components/reset-overrides-control.js.map +1 -1
  19. package/build/constants.js +2 -1
  20. package/build/constants.js.map +1 -1
  21. package/build/index.js.map +1 -1
  22. package/build/index.native.js.map +1 -1
  23. package/build/lock-unlock.js.map +1 -1
  24. package/build/private-apis.js +7 -3
  25. package/build/private-apis.js.map +1 -1
  26. package/build/private-hooks.js.map +1 -1
  27. package/build/store/actions.js.map +1 -1
  28. package/build/store/constants.js.map +1 -1
  29. package/build/store/index.js +1 -1
  30. package/build/store/index.js.map +1 -1
  31. package/build/store/reducer.js.map +1 -1
  32. package/build/store/selectors.js.map +1 -1
  33. package/build-module/api/index.js +16 -0
  34. package/build-module/api/index.js.map +1 -0
  35. package/build-module/components/allow-overrides-modal.js +106 -0
  36. package/build-module/components/allow-overrides-modal.js.map +1 -0
  37. package/build-module/components/category-selector.js.map +1 -1
  38. package/build-module/components/create-pattern-modal.js.map +1 -1
  39. package/build-module/components/duplicate-pattern-modal.js.map +1 -1
  40. package/build-module/components/index.js.map +1 -1
  41. package/build-module/components/overrides-panel.js +37 -0
  42. package/build-module/components/overrides-panel.js.map +1 -0
  43. package/build-module/components/pattern-convert-button.js.map +1 -1
  44. package/build-module/components/pattern-overrides-controls.js +100 -0
  45. package/build-module/components/pattern-overrides-controls.js.map +1 -0
  46. package/build-module/components/patterns-manage-button.js.map +1 -1
  47. package/build-module/components/rename-pattern-category-modal.js.map +1 -1
  48. package/build-module/components/rename-pattern-modal.js.map +1 -1
  49. package/build-module/components/reset-overrides-control.js.map +1 -1
  50. package/build-module/constants.js +1 -0
  51. package/build-module/constants.js.map +1 -1
  52. package/build-module/index.js.map +1 -1
  53. package/build-module/index.native.js.map +1 -1
  54. package/build-module/lock-unlock.js.map +1 -1
  55. package/build-module/private-apis.js +6 -2
  56. package/build-module/private-apis.js.map +1 -1
  57. package/build-module/private-hooks.js.map +1 -1
  58. package/build-module/store/actions.js.map +1 -1
  59. package/build-module/store/constants.js.map +1 -1
  60. package/build-module/store/index.js.map +1 -1
  61. package/build-module/store/reducer.js.map +1 -1
  62. package/build-module/store/selectors.js.map +1 -1
  63. package/build-style/style-rtl.css +6 -1
  64. package/build-style/style.css +6 -1
  65. package/package.json +16 -16
  66. package/src/api/index.js +24 -0
  67. package/src/components/allow-overrides-modal.js +149 -0
  68. package/src/components/overrides-panel.js +45 -0
  69. package/src/components/pattern-overrides-controls.js +145 -0
  70. package/src/components/style.scss +6 -0
  71. package/src/constants.js +2 -0
  72. package/src/private-apis.js +6 -2
  73. package/build/components/use-set-pattern-bindings.js +0 -103
  74. package/build/components/use-set-pattern-bindings.js.map +0 -1
  75. package/build-module/components/use-set-pattern-bindings.js +0 -96
  76. package/build-module/components/use-set-pattern-bindings.js.map +0 -1
  77. package/src/components/use-set-pattern-bindings.js +0 -120
@@ -1,103 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = useSetPatternBindings;
7
- var _compose = require("@wordpress/compose");
8
- var _blocks = require("@wordpress/blocks");
9
- var _element = require("@wordpress/element");
10
- var _data = require("@wordpress/data");
11
- var _constants = require("../constants");
12
- var _lockUnlock = require("../lock-unlock");
13
- /**
14
- * WordPress dependencies
15
- */
16
-
17
- /**
18
- * Internal dependencies
19
- */
20
-
21
- function removeBindings(bindings, syncedAttributes) {
22
- let updatedBindings = {};
23
- for (const attributeName of syncedAttributes) {
24
- // Omit any pattern override bindings from the `updatedBindings` object.
25
- if (bindings?.[attributeName]?.source !== 'core/pattern-overrides' && bindings?.[attributeName]?.source !== undefined) {
26
- updatedBindings[attributeName] = bindings[attributeName];
27
- }
28
- }
29
- if (!Object.keys(updatedBindings).length) {
30
- updatedBindings = undefined;
31
- }
32
- return updatedBindings;
33
- }
34
- function addBindings(bindings, syncedAttributes) {
35
- const updatedBindings = {
36
- ...bindings
37
- };
38
- for (const attributeName of syncedAttributes) {
39
- if (!bindings?.[attributeName]) {
40
- updatedBindings[attributeName] = {
41
- source: 'core/pattern-overrides'
42
- };
43
- }
44
- }
45
- return updatedBindings;
46
- }
47
- function useSetPatternBindings({
48
- name,
49
- attributes,
50
- setAttributes
51
- }, currentPostType) {
52
- var _attributes$metadata$, _usePrevious;
53
- const hasPatternOverridesSource = (0, _data.useSelect)(select => {
54
- const {
55
- getBlockBindingsSource
56
- } = (0, _lockUnlock.unlock)(select(_blocks.store));
57
-
58
- // For editing link to the site editor if the theme and user permissions support it.
59
- return !!getBlockBindingsSource('core/pattern-overrides');
60
- }, []);
61
- const metadataName = (_attributes$metadata$ = attributes?.metadata?.name) !== null && _attributes$metadata$ !== void 0 ? _attributes$metadata$ : '';
62
- const prevMetadataName = (_usePrevious = (0, _compose.usePrevious)(metadataName)) !== null && _usePrevious !== void 0 ? _usePrevious : '';
63
- const bindings = attributes?.metadata?.bindings;
64
- (0, _element.useEffect)(() => {
65
- // Bindings should only be created when editing a wp_block post type,
66
- // and also when there's a change to the user-given name for the block.
67
- // Also check that the pattern overrides source is registered.
68
- if (!hasPatternOverridesSource || currentPostType !== 'wp_block' || metadataName === prevMetadataName) {
69
- return;
70
- }
71
- const syncedAttributes = _constants.PARTIAL_SYNCING_SUPPORTED_BLOCKS[name];
72
- const attributeSources = syncedAttributes.map(attributeName => attributes.metadata?.bindings?.[attributeName]?.source);
73
- const isConnectedToOtherSources = attributeSources.every(source => source && source !== 'core/pattern-overrides');
74
-
75
- // Avoid overwriting other (e.g. meta) bindings.
76
- if (isConnectedToOtherSources) {
77
- return;
78
- }
79
-
80
- // The user-given name for the block was deleted, remove the bindings.
81
- if (!metadataName?.length && prevMetadataName?.length) {
82
- const updatedBindings = removeBindings(bindings, syncedAttributes);
83
- setAttributes({
84
- metadata: {
85
- ...attributes.metadata,
86
- bindings: updatedBindings
87
- }
88
- });
89
- }
90
-
91
- // The user-given name for the block was set, set the bindings.
92
- if (!prevMetadataName?.length && metadataName.length) {
93
- const updatedBindings = addBindings(bindings, syncedAttributes);
94
- setAttributes({
95
- metadata: {
96
- ...attributes.metadata,
97
- bindings: updatedBindings
98
- }
99
- });
100
- }
101
- }, [hasPatternOverridesSource, bindings, prevMetadataName, metadataName, currentPostType, name, attributes.metadata, setAttributes]);
102
- }
103
- //# sourceMappingURL=use-set-pattern-bindings.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_compose","require","_blocks","_element","_data","_constants","_lockUnlock","removeBindings","bindings","syncedAttributes","updatedBindings","attributeName","source","undefined","Object","keys","length","addBindings","useSetPatternBindings","name","attributes","setAttributes","currentPostType","_attributes$metadata$","_usePrevious","hasPatternOverridesSource","useSelect","select","getBlockBindingsSource","unlock","blocksStore","metadataName","metadata","prevMetadataName","usePrevious","useEffect","PARTIAL_SYNCING_SUPPORTED_BLOCKS","attributeSources","map","isConnectedToOtherSources","every"],"sources":["@wordpress/patterns/src/components/use-set-pattern-bindings.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { usePrevious } from '@wordpress/compose';\nimport { store as blocksStore } from '@wordpress/blocks';\nimport { useEffect } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';\n\nimport { unlock } from '../lock-unlock';\n\nfunction removeBindings( bindings, syncedAttributes ) {\n\tlet updatedBindings = {};\n\tfor ( const attributeName of syncedAttributes ) {\n\t\t// Omit any pattern override bindings from the `updatedBindings` object.\n\t\tif (\n\t\t\tbindings?.[ attributeName ]?.source !== 'core/pattern-overrides' &&\n\t\t\tbindings?.[ attributeName ]?.source !== undefined\n\t\t) {\n\t\t\tupdatedBindings[ attributeName ] = bindings[ attributeName ];\n\t\t}\n\t}\n\tif ( ! Object.keys( updatedBindings ).length ) {\n\t\tupdatedBindings = undefined;\n\t}\n\treturn updatedBindings;\n}\n\nfunction addBindings( bindings, syncedAttributes ) {\n\tconst updatedBindings = { ...bindings };\n\tfor ( const attributeName of syncedAttributes ) {\n\t\tif ( ! bindings?.[ attributeName ] ) {\n\t\t\tupdatedBindings[ attributeName ] = {\n\t\t\t\tsource: 'core/pattern-overrides',\n\t\t\t};\n\t\t}\n\t}\n\treturn updatedBindings;\n}\n\nexport default function useSetPatternBindings(\n\t{ name, attributes, setAttributes },\n\tcurrentPostType\n) {\n\tconst hasPatternOverridesSource = useSelect( ( select ) => {\n\t\tconst { getBlockBindingsSource } = unlock( select( blocksStore ) );\n\n\t\t// For editing link to the site editor if the theme and user permissions support it.\n\t\treturn !! getBlockBindingsSource( 'core/pattern-overrides' );\n\t}, [] );\n\n\tconst metadataName = attributes?.metadata?.name ?? '';\n\tconst prevMetadataName = usePrevious( metadataName ) ?? '';\n\tconst bindings = attributes?.metadata?.bindings;\n\n\tuseEffect( () => {\n\t\t// Bindings should only be created when editing a wp_block post type,\n\t\t// and also when there's a change to the user-given name for the block.\n\t\t// Also check that the pattern overrides source is registered.\n\t\tif (\n\t\t\t! hasPatternOverridesSource ||\n\t\t\tcurrentPostType !== 'wp_block' ||\n\t\t\tmetadataName === prevMetadataName\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];\n\t\tconst attributeSources = syncedAttributes.map(\n\t\t\t( attributeName ) =>\n\t\t\t\tattributes.metadata?.bindings?.[ attributeName ]?.source\n\t\t);\n\t\tconst isConnectedToOtherSources = attributeSources.every(\n\t\t\t( source ) => source && source !== 'core/pattern-overrides'\n\t\t);\n\n\t\t// Avoid overwriting other (e.g. meta) bindings.\n\t\tif ( isConnectedToOtherSources ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// The user-given name for the block was deleted, remove the bindings.\n\t\tif ( ! metadataName?.length && prevMetadataName?.length ) {\n\t\t\tconst updatedBindings = removeBindings(\n\t\t\t\tbindings,\n\t\t\t\tsyncedAttributes\n\t\t\t);\n\t\t\tsetAttributes( {\n\t\t\t\tmetadata: {\n\t\t\t\t\t...attributes.metadata,\n\t\t\t\t\tbindings: updatedBindings,\n\t\t\t\t},\n\t\t\t} );\n\t\t}\n\n\t\t// The user-given name for the block was set, set the bindings.\n\t\tif ( ! prevMetadataName?.length && metadataName.length ) {\n\t\t\tconst updatedBindings = addBindings( bindings, syncedAttributes );\n\t\t\tsetAttributes( {\n\t\t\t\tmetadata: {\n\t\t\t\t\t...attributes.metadata,\n\t\t\t\t\tbindings: updatedBindings,\n\t\t\t\t},\n\t\t\t} );\n\t\t}\n\t}, [\n\t\thasPatternOverridesSource,\n\t\tbindings,\n\t\tprevMetadataName,\n\t\tmetadataName,\n\t\tcurrentPostType,\n\t\tname,\n\t\tattributes.metadata,\n\t\tsetAttributes,\n\t] );\n}\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AAKA,IAAAI,UAAA,GAAAJ,OAAA;AAEA,IAAAK,WAAA,GAAAL,OAAA;AAbA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UAAU,CAACY,QAAQ;UACtBxB,QAAQ,EAAEE;QACX;MACD,CAAE,CAAC;IACJ;EACD,CAAC,EAAE,CACFe,yBAAyB,EACzBjB,QAAQ,EACRyB,gBAAgB,EAChBF,YAAY,EACZT,eAAe,EACfH,IAAI,EACJC,UAAU,CAACY,QAAQ,EACnBX,aAAa,CACZ,CAAC;AACJ"}
@@ -1,96 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { usePrevious } from '@wordpress/compose';
5
- import { store as blocksStore } from '@wordpress/blocks';
6
- import { useEffect } from '@wordpress/element';
7
- import { useSelect } from '@wordpress/data';
8
-
9
- /**
10
- * Internal dependencies
11
- */
12
- import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';
13
- import { unlock } from '../lock-unlock';
14
- function removeBindings(bindings, syncedAttributes) {
15
- let updatedBindings = {};
16
- for (const attributeName of syncedAttributes) {
17
- // Omit any pattern override bindings from the `updatedBindings` object.
18
- if (bindings?.[attributeName]?.source !== 'core/pattern-overrides' && bindings?.[attributeName]?.source !== undefined) {
19
- updatedBindings[attributeName] = bindings[attributeName];
20
- }
21
- }
22
- if (!Object.keys(updatedBindings).length) {
23
- updatedBindings = undefined;
24
- }
25
- return updatedBindings;
26
- }
27
- function addBindings(bindings, syncedAttributes) {
28
- const updatedBindings = {
29
- ...bindings
30
- };
31
- for (const attributeName of syncedAttributes) {
32
- if (!bindings?.[attributeName]) {
33
- updatedBindings[attributeName] = {
34
- source: 'core/pattern-overrides'
35
- };
36
- }
37
- }
38
- return updatedBindings;
39
- }
40
- export default function useSetPatternBindings({
41
- name,
42
- attributes,
43
- setAttributes
44
- }, currentPostType) {
45
- var _attributes$metadata$, _usePrevious;
46
- const hasPatternOverridesSource = useSelect(select => {
47
- const {
48
- getBlockBindingsSource
49
- } = unlock(select(blocksStore));
50
-
51
- // For editing link to the site editor if the theme and user permissions support it.
52
- return !!getBlockBindingsSource('core/pattern-overrides');
53
- }, []);
54
- const metadataName = (_attributes$metadata$ = attributes?.metadata?.name) !== null && _attributes$metadata$ !== void 0 ? _attributes$metadata$ : '';
55
- const prevMetadataName = (_usePrevious = usePrevious(metadataName)) !== null && _usePrevious !== void 0 ? _usePrevious : '';
56
- const bindings = attributes?.metadata?.bindings;
57
- useEffect(() => {
58
- // Bindings should only be created when editing a wp_block post type,
59
- // and also when there's a change to the user-given name for the block.
60
- // Also check that the pattern overrides source is registered.
61
- if (!hasPatternOverridesSource || currentPostType !== 'wp_block' || metadataName === prevMetadataName) {
62
- return;
63
- }
64
- const syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[name];
65
- const attributeSources = syncedAttributes.map(attributeName => attributes.metadata?.bindings?.[attributeName]?.source);
66
- const isConnectedToOtherSources = attributeSources.every(source => source && source !== 'core/pattern-overrides');
67
-
68
- // Avoid overwriting other (e.g. meta) bindings.
69
- if (isConnectedToOtherSources) {
70
- return;
71
- }
72
-
73
- // The user-given name for the block was deleted, remove the bindings.
74
- if (!metadataName?.length && prevMetadataName?.length) {
75
- const updatedBindings = removeBindings(bindings, syncedAttributes);
76
- setAttributes({
77
- metadata: {
78
- ...attributes.metadata,
79
- bindings: updatedBindings
80
- }
81
- });
82
- }
83
-
84
- // The user-given name for the block was set, set the bindings.
85
- if (!prevMetadataName?.length && metadataName.length) {
86
- const updatedBindings = addBindings(bindings, syncedAttributes);
87
- setAttributes({
88
- metadata: {
89
- ...attributes.metadata,
90
- bindings: updatedBindings
91
- }
92
- });
93
- }
94
- }, [hasPatternOverridesSource, bindings, prevMetadataName, metadataName, currentPostType, name, attributes.metadata, setAttributes]);
95
- }
96
- //# sourceMappingURL=use-set-pattern-bindings.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["usePrevious","store","blocksStore","useEffect","useSelect","PARTIAL_SYNCING_SUPPORTED_BLOCKS","unlock","removeBindings","bindings","syncedAttributes","updatedBindings","attributeName","source","undefined","Object","keys","length","addBindings","useSetPatternBindings","name","attributes","setAttributes","currentPostType","_attributes$metadata$","_usePrevious","hasPatternOverridesSource","select","getBlockBindingsSource","metadataName","metadata","prevMetadataName","attributeSources","map","isConnectedToOtherSources","every"],"sources":["@wordpress/patterns/src/components/use-set-pattern-bindings.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { usePrevious } from '@wordpress/compose';\nimport { store as blocksStore } from '@wordpress/blocks';\nimport { useEffect } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';\n\nimport { unlock } from '../lock-unlock';\n\nfunction removeBindings( bindings, syncedAttributes ) {\n\tlet updatedBindings = {};\n\tfor ( const attributeName of syncedAttributes ) {\n\t\t// Omit any pattern override bindings from the `updatedBindings` object.\n\t\tif (\n\t\t\tbindings?.[ attributeName ]?.source !== 'core/pattern-overrides' &&\n\t\t\tbindings?.[ attributeName ]?.source !== undefined\n\t\t) {\n\t\t\tupdatedBindings[ attributeName ] = bindings[ attributeName ];\n\t\t}\n\t}\n\tif ( ! Object.keys( updatedBindings ).length ) {\n\t\tupdatedBindings = undefined;\n\t}\n\treturn updatedBindings;\n}\n\nfunction addBindings( bindings, syncedAttributes ) {\n\tconst updatedBindings = { ...bindings };\n\tfor ( const attributeName of syncedAttributes ) {\n\t\tif ( ! bindings?.[ attributeName ] ) {\n\t\t\tupdatedBindings[ attributeName ] = {\n\t\t\t\tsource: 'core/pattern-overrides',\n\t\t\t};\n\t\t}\n\t}\n\treturn updatedBindings;\n}\n\nexport default function useSetPatternBindings(\n\t{ name, attributes, setAttributes },\n\tcurrentPostType\n) {\n\tconst hasPatternOverridesSource = useSelect( ( select ) => {\n\t\tconst { getBlockBindingsSource } = unlock( select( blocksStore ) );\n\n\t\t// For editing link to the site editor if the theme and user permissions support it.\n\t\treturn !! getBlockBindingsSource( 'core/pattern-overrides' );\n\t}, [] );\n\n\tconst metadataName = attributes?.metadata?.name ?? '';\n\tconst prevMetadataName = usePrevious( metadataName ) ?? '';\n\tconst bindings = attributes?.metadata?.bindings;\n\n\tuseEffect( () => {\n\t\t// Bindings should only be created when editing a wp_block post type,\n\t\t// and also when there's a change to the user-given name for the block.\n\t\t// Also check that the pattern overrides source is registered.\n\t\tif (\n\t\t\t! hasPatternOverridesSource ||\n\t\t\tcurrentPostType !== 'wp_block' ||\n\t\t\tmetadataName === prevMetadataName\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];\n\t\tconst attributeSources = syncedAttributes.map(\n\t\t\t( attributeName ) =>\n\t\t\t\tattributes.metadata?.bindings?.[ attributeName ]?.source\n\t\t);\n\t\tconst isConnectedToOtherSources = attributeSources.every(\n\t\t\t( source ) => source && source !== 'core/pattern-overrides'\n\t\t);\n\n\t\t// Avoid overwriting other (e.g. meta) bindings.\n\t\tif ( isConnectedToOtherSources ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// The user-given name for the block was deleted, remove the bindings.\n\t\tif ( ! metadataName?.length && prevMetadataName?.length ) {\n\t\t\tconst updatedBindings = removeBindings(\n\t\t\t\tbindings,\n\t\t\t\tsyncedAttributes\n\t\t\t);\n\t\t\tsetAttributes( {\n\t\t\t\tmetadata: {\n\t\t\t\t\t...attributes.metadata,\n\t\t\t\t\tbindings: updatedBindings,\n\t\t\t\t},\n\t\t\t} );\n\t\t}\n\n\t\t// The user-given name for the block was set, set the bindings.\n\t\tif ( ! prevMetadataName?.length && metadataName.length ) {\n\t\t\tconst updatedBindings = addBindings( bindings, syncedAttributes );\n\t\t\tsetAttributes( {\n\t\t\t\tmetadata: {\n\t\t\t\t\t...attributes.metadata,\n\t\t\t\t\tbindings: updatedBindings,\n\t\t\t\t},\n\t\t\t} );\n\t\t}\n\t}, [\n\t\thasPatternOverridesSource,\n\t\tbindings,\n\t\tprevMetadataName,\n\t\tmetadataName,\n\t\tcurrentPostType,\n\t\tname,\n\t\tattributes.metadata,\n\t\tsetAttributes,\n\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MACD,CAAE,CAAC;IACJ;;IAEA;IACA,IAAK,CAAEoB,gBAAgB,EAAEd,MAAM,IAAIY,YAAY,CAACZ,MAAM,EAAG;MACxD,MAAMN,eAAe,GAAGO,WAAW,CAAET,QAAQ,EAAEC,gBAAiB,CAAC;MACjEY,aAAa,CAAE;QACdQ,QAAQ,EAAE;UACT,GAAGT,UAAU,CAACS,QAAQ;UACtBrB,QAAQ,EAAEE;QACX;MACD,CAAE,CAAC;IACJ;EACD,CAAC,EAAE,CACFe,yBAAyB,EACzBjB,QAAQ,EACRsB,gBAAgB,EAChBF,YAAY,EACZN,eAAe,EACfH,IAAI,EACJC,UAAU,CAACS,QAAQ,EACnBR,aAAa,CACZ,CAAC;AACJ"}
@@ -1,120 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { usePrevious } from '@wordpress/compose';
5
- import { store as blocksStore } from '@wordpress/blocks';
6
- import { useEffect } from '@wordpress/element';
7
- import { useSelect } from '@wordpress/data';
8
-
9
- /**
10
- * Internal dependencies
11
- */
12
- import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';
13
-
14
- import { unlock } from '../lock-unlock';
15
-
16
- function removeBindings( bindings, syncedAttributes ) {
17
- let updatedBindings = {};
18
- for ( const attributeName of syncedAttributes ) {
19
- // Omit any pattern override bindings from the `updatedBindings` object.
20
- if (
21
- bindings?.[ attributeName ]?.source !== 'core/pattern-overrides' &&
22
- bindings?.[ attributeName ]?.source !== undefined
23
- ) {
24
- updatedBindings[ attributeName ] = bindings[ attributeName ];
25
- }
26
- }
27
- if ( ! Object.keys( updatedBindings ).length ) {
28
- updatedBindings = undefined;
29
- }
30
- return updatedBindings;
31
- }
32
-
33
- function addBindings( bindings, syncedAttributes ) {
34
- const updatedBindings = { ...bindings };
35
- for ( const attributeName of syncedAttributes ) {
36
- if ( ! bindings?.[ attributeName ] ) {
37
- updatedBindings[ attributeName ] = {
38
- source: 'core/pattern-overrides',
39
- };
40
- }
41
- }
42
- return updatedBindings;
43
- }
44
-
45
- export default function useSetPatternBindings(
46
- { name, attributes, setAttributes },
47
- currentPostType
48
- ) {
49
- const hasPatternOverridesSource = useSelect( ( select ) => {
50
- const { getBlockBindingsSource } = unlock( select( blocksStore ) );
51
-
52
- // For editing link to the site editor if the theme and user permissions support it.
53
- return !! getBlockBindingsSource( 'core/pattern-overrides' );
54
- }, [] );
55
-
56
- const metadataName = attributes?.metadata?.name ?? '';
57
- const prevMetadataName = usePrevious( metadataName ) ?? '';
58
- const bindings = attributes?.metadata?.bindings;
59
-
60
- useEffect( () => {
61
- // Bindings should only be created when editing a wp_block post type,
62
- // and also when there's a change to the user-given name for the block.
63
- // Also check that the pattern overrides source is registered.
64
- if (
65
- ! hasPatternOverridesSource ||
66
- currentPostType !== 'wp_block' ||
67
- metadataName === prevMetadataName
68
- ) {
69
- return;
70
- }
71
-
72
- const syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];
73
- const attributeSources = syncedAttributes.map(
74
- ( attributeName ) =>
75
- attributes.metadata?.bindings?.[ attributeName ]?.source
76
- );
77
- const isConnectedToOtherSources = attributeSources.every(
78
- ( source ) => source && source !== 'core/pattern-overrides'
79
- );
80
-
81
- // Avoid overwriting other (e.g. meta) bindings.
82
- if ( isConnectedToOtherSources ) {
83
- return;
84
- }
85
-
86
- // The user-given name for the block was deleted, remove the bindings.
87
- if ( ! metadataName?.length && prevMetadataName?.length ) {
88
- const updatedBindings = removeBindings(
89
- bindings,
90
- syncedAttributes
91
- );
92
- setAttributes( {
93
- metadata: {
94
- ...attributes.metadata,
95
- bindings: updatedBindings,
96
- },
97
- } );
98
- }
99
-
100
- // The user-given name for the block was set, set the bindings.
101
- if ( ! prevMetadataName?.length && metadataName.length ) {
102
- const updatedBindings = addBindings( bindings, syncedAttributes );
103
- setAttributes( {
104
- metadata: {
105
- ...attributes.metadata,
106
- bindings: updatedBindings,
107
- },
108
- } );
109
- }
110
- }, [
111
- hasPatternOverridesSource,
112
- bindings,
113
- prevMetadataName,
114
- metadataName,
115
- currentPostType,
116
- name,
117
- attributes.metadata,
118
- setAttributes,
119
- ] );
120
- }