@wordpress/patterns 1.16.0 → 1.18.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 +1 -1
  3. package/build/api/index.js.map +1 -1
  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.map +1 -1
  11. package/build/components/pattern-convert-button.js.map +1 -1
  12. package/build/components/pattern-overrides-controls.js +110 -0
  13. package/build/components/pattern-overrides-controls.js.map +1 -0
  14. package/build/components/patterns-manage-button.js +2 -4
  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 +3 -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 +1 -1
  34. package/build-module/api/index.js.map +1 -1
  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.map +1 -1
  42. package/build-module/components/pattern-convert-button.js.map +1 -1
  43. package/build-module/components/pattern-overrides-controls.js +103 -0
  44. package/build-module/components/pattern-overrides-controls.js.map +1 -0
  45. package/build-module/components/patterns-manage-button.js +2 -4
  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 +2 -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 +1 -0
  67. package/src/components/allow-overrides-modal.js +149 -0
  68. package/src/components/pattern-overrides-controls.js +148 -0
  69. package/src/components/patterns-manage-button.js +8 -10
  70. package/src/components/style.scss +6 -0
  71. package/src/constants.js +2 -0
  72. package/src/private-apis.js +2 -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
@@ -96,7 +96,7 @@
96
96
  --wp-admin-border-width-focus: 2px;
97
97
  --wp-block-synced-color: #7a00df;
98
98
  --wp-block-synced-color--rgb: 122, 0, 223;
99
- --wp-bound-block-color: #9747ff;
99
+ --wp-bound-block-color: var(--wp-block-synced-color);
100
100
  }
101
101
  @media (min-resolution: 192dpi) {
102
102
  :root {
@@ -139,4 +139,9 @@
139
139
  .patterns-rename-pattern-category-modal__validation-message {
140
140
  width: 320px;
141
141
  }
142
+ }
143
+
144
+ .pattern-overrides-control__allow-overrides-button {
145
+ width: 100%;
146
+ justify-content: center;
142
147
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/patterns",
3
- "version": "1.16.0",
3
+ "version": "1.18.0",
4
4
  "description": "Management of user pattern editing.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -31,20 +31,20 @@
31
31
  ],
32
32
  "dependencies": {
33
33
  "@babel/runtime": "^7.16.0",
34
- "@wordpress/a11y": "^3.55.0",
35
- "@wordpress/block-editor": "^12.23.0",
36
- "@wordpress/blocks": "^12.32.0",
37
- "@wordpress/components": "^27.3.0",
38
- "@wordpress/compose": "^6.32.0",
39
- "@wordpress/core-data": "^6.32.0",
40
- "@wordpress/data": "^9.25.0",
41
- "@wordpress/element": "^5.32.0",
42
- "@wordpress/html-entities": "^3.55.0",
43
- "@wordpress/i18n": "^4.55.0",
44
- "@wordpress/icons": "^9.46.0",
45
- "@wordpress/notices": "^4.23.0",
46
- "@wordpress/private-apis": "^0.37.0",
47
- "@wordpress/url": "^3.56.0"
34
+ "@wordpress/a11y": "^3.57.0",
35
+ "@wordpress/block-editor": "^12.25.0",
36
+ "@wordpress/blocks": "^12.34.0",
37
+ "@wordpress/components": "^27.5.0",
38
+ "@wordpress/compose": "^6.34.0",
39
+ "@wordpress/core-data": "^6.34.0",
40
+ "@wordpress/data": "^9.27.0",
41
+ "@wordpress/element": "^5.34.0",
42
+ "@wordpress/html-entities": "^3.57.0",
43
+ "@wordpress/i18n": "^4.57.0",
44
+ "@wordpress/icons": "^9.48.0",
45
+ "@wordpress/notices": "^4.25.0",
46
+ "@wordpress/private-apis": "^0.39.0",
47
+ "@wordpress/url": "^3.58.0"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "react": "^18.0.0",
@@ -53,5 +53,5 @@
53
53
  "publishConfig": {
54
54
  "access": "public"
55
55
  },
56
- "gitHead": "ac2b13783c28f959770cf029a797a712f59e1958"
56
+ "gitHead": "581d8a5580dba8f600b7268d51eb554771ae482c"
57
57
  }
package/src/api/index.js CHANGED
@@ -15,6 +15,7 @@ export function isOverridableBlock( block ) {
15
15
  Object.keys( PARTIAL_SYNCING_SUPPORTED_BLOCKS ).includes(
16
16
  block.name
17
17
  ) &&
18
+ !! block.attributes.metadata?.name &&
18
19
  !! block.attributes.metadata?.bindings &&
19
20
  Object.values( block.attributes.metadata.bindings ).some(
20
21
  ( binding ) => binding.source === 'core/pattern-overrides'
@@ -0,0 +1,149 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import {
5
+ __experimentalHStack as HStack,
6
+ __experimentalVStack as VStack,
7
+ Button,
8
+ __experimentalText as Text,
9
+ TextControl,
10
+ Modal,
11
+ } from '@wordpress/components';
12
+ import { __, sprintf } from '@wordpress/i18n';
13
+ import { useState, useId } from '@wordpress/element';
14
+ import { speak } from '@wordpress/a11y';
15
+
16
+ export function AllowOverridesModal( {
17
+ placeholder,
18
+ initialName = '',
19
+ onClose,
20
+ onSave,
21
+ } ) {
22
+ const [ editedBlockName, setEditedBlockName ] = useState( initialName );
23
+ const descriptionId = useId();
24
+
25
+ const isNameValid = !! editedBlockName.trim();
26
+
27
+ const handleSubmit = () => {
28
+ if ( editedBlockName !== initialName ) {
29
+ const message = sprintf(
30
+ /* translators: %s: new name/label for the block */
31
+ __( 'Block name changed to: "%s".' ),
32
+ editedBlockName
33
+ );
34
+
35
+ // Must be assertive to immediately announce change.
36
+ speak( message, 'assertive' );
37
+ }
38
+ onSave( editedBlockName );
39
+
40
+ // Immediate close avoids ability to hit save multiple times.
41
+ onClose();
42
+ };
43
+
44
+ return (
45
+ <Modal
46
+ title={ __( 'Enable overrides' ) }
47
+ onRequestClose={ onClose }
48
+ focusOnMount="firstContentElement"
49
+ aria={ { describedby: descriptionId } }
50
+ size="small"
51
+ >
52
+ <form
53
+ onSubmit={ ( event ) => {
54
+ event.preventDefault();
55
+
56
+ if ( ! isNameValid ) {
57
+ return;
58
+ }
59
+
60
+ handleSubmit();
61
+ } }
62
+ >
63
+ <VStack spacing="6">
64
+ <Text id={ descriptionId }>
65
+ { __(
66
+ 'Overrides are changes you make to a block within a synced pattern instance. Use overrides to customize a synced pattern instance to suit its new context. Name this block to specify an override.'
67
+ ) }
68
+ </Text>
69
+ <TextControl
70
+ __nextHasNoMarginBottom
71
+ __next40pxDefaultSize
72
+ value={ editedBlockName }
73
+ label={ __( 'Name' ) }
74
+ help={ __(
75
+ 'For example, if you are creating a recipe pattern, you use "Recipe Title", "Recipe Description", etc.'
76
+ ) }
77
+ placeholder={ placeholder }
78
+ onChange={ setEditedBlockName }
79
+ />
80
+ <HStack justify="right">
81
+ <Button
82
+ __next40pxDefaultSize
83
+ variant="tertiary"
84
+ onClick={ onClose }
85
+ >
86
+ { __( 'Cancel' ) }
87
+ </Button>
88
+
89
+ <Button
90
+ __next40pxDefaultSize
91
+ aria-disabled={ ! isNameValid }
92
+ variant="primary"
93
+ type="submit"
94
+ >
95
+ { __( 'Enable' ) }
96
+ </Button>
97
+ </HStack>
98
+ </VStack>
99
+ </form>
100
+ </Modal>
101
+ );
102
+ }
103
+
104
+ export function DisallowOverridesModal( { onClose, onSave } ) {
105
+ const descriptionId = useId();
106
+
107
+ return (
108
+ <Modal
109
+ title={ __( 'Disable overrides' ) }
110
+ onRequestClose={ onClose }
111
+ aria={ { describedby: descriptionId } }
112
+ size="small"
113
+ >
114
+ <form
115
+ onSubmit={ ( event ) => {
116
+ event.preventDefault();
117
+ onSave();
118
+ onClose();
119
+ } }
120
+ >
121
+ <VStack spacing="6">
122
+ <Text id={ descriptionId }>
123
+ { __(
124
+ 'Are you sure you want to disable overrides? Disabling overrides will revert all applied overrides for this block throughout instances of this pattern.'
125
+ ) }
126
+ </Text>
127
+
128
+ <HStack justify="right">
129
+ <Button
130
+ __next40pxDefaultSize
131
+ variant="tertiary"
132
+ onClick={ onClose }
133
+ >
134
+ { __( 'Cancel' ) }
135
+ </Button>
136
+
137
+ <Button
138
+ __next40pxDefaultSize
139
+ variant="primary"
140
+ type="submit"
141
+ >
142
+ { __( 'Disable' ) }
143
+ </Button>
144
+ </HStack>
145
+ </VStack>
146
+ </form>
147
+ </Modal>
148
+ );
149
+ }
@@ -0,0 +1,148 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useState, useId } from '@wordpress/element';
5
+ import { InspectorControls } from '@wordpress/block-editor';
6
+ import { BaseControl, Button } from '@wordpress/components';
7
+ import { __ } from '@wordpress/i18n';
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ import {
13
+ PARTIAL_SYNCING_SUPPORTED_BLOCKS,
14
+ PATTERN_OVERRIDES_BINDING_SOURCE,
15
+ } from '../constants';
16
+ import {
17
+ AllowOverridesModal,
18
+ DisallowOverridesModal,
19
+ } from './allow-overrides-modal';
20
+
21
+ function removeBindings( bindings, syncedAttributes ) {
22
+ let updatedBindings = {};
23
+ for ( const attributeName of syncedAttributes ) {
24
+ // Omit any bindings that's not the same source from the `updatedBindings` object.
25
+ if (
26
+ bindings?.[ attributeName ]?.source !==
27
+ PATTERN_OVERRIDES_BINDING_SOURCE &&
28
+ bindings?.[ attributeName ]?.source !== undefined
29
+ ) {
30
+ updatedBindings[ attributeName ] = bindings[ attributeName ];
31
+ }
32
+ }
33
+ if ( ! Object.keys( updatedBindings ).length ) {
34
+ updatedBindings = undefined;
35
+ }
36
+ return updatedBindings;
37
+ }
38
+
39
+ function addBindings( bindings, syncedAttributes ) {
40
+ const updatedBindings = { ...bindings };
41
+ for ( const attributeName of syncedAttributes ) {
42
+ if ( ! bindings?.[ attributeName ] ) {
43
+ updatedBindings[ attributeName ] = {
44
+ source: PATTERN_OVERRIDES_BINDING_SOURCE,
45
+ };
46
+ }
47
+ }
48
+ return updatedBindings;
49
+ }
50
+
51
+ function PatternOverridesControls( { attributes, name, setAttributes } ) {
52
+ const controlId = useId();
53
+ const [ showAllowOverridesModal, setShowAllowOverridesModal ] =
54
+ useState( false );
55
+ const [ showDisallowOverridesModal, setShowDisallowOverridesModal ] =
56
+ useState( false );
57
+
58
+ const syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];
59
+ const attributeSources = syncedAttributes.map(
60
+ ( attributeName ) =>
61
+ attributes.metadata?.bindings?.[ attributeName ]?.source
62
+ );
63
+ const isConnectedToOtherSources = attributeSources.every(
64
+ ( source ) => source && source !== 'core/pattern-overrides'
65
+ );
66
+
67
+ function updateBindings( isChecked, customName ) {
68
+ const prevBindings = attributes?.metadata?.bindings;
69
+ const updatedBindings = isChecked
70
+ ? addBindings( prevBindings, syncedAttributes )
71
+ : removeBindings( prevBindings, syncedAttributes );
72
+
73
+ const updatedMetadata = {
74
+ ...attributes.metadata,
75
+ bindings: updatedBindings,
76
+ };
77
+
78
+ if ( customName ) {
79
+ updatedMetadata.name = customName;
80
+ }
81
+
82
+ setAttributes( {
83
+ metadata: updatedMetadata,
84
+ } );
85
+ }
86
+
87
+ // Avoid overwriting other (e.g. meta) bindings.
88
+ if ( isConnectedToOtherSources ) {
89
+ return null;
90
+ }
91
+
92
+ const hasName = !! attributes.metadata?.name;
93
+ const allowOverrides =
94
+ hasName &&
95
+ attributeSources.some(
96
+ ( source ) => source === PATTERN_OVERRIDES_BINDING_SOURCE
97
+ );
98
+
99
+ return (
100
+ <>
101
+ <InspectorControls group="advanced">
102
+ <BaseControl
103
+ id={ controlId }
104
+ label={ __( 'Overrides' ) }
105
+ help={ __(
106
+ 'Allow changes to this block throughout instances of this pattern.'
107
+ ) }
108
+ >
109
+ <Button
110
+ __next40pxDefaultSize
111
+ className="pattern-overrides-control__allow-overrides-button"
112
+ variant="secondary"
113
+ aria-haspopup="dialog"
114
+ onClick={ () => {
115
+ if ( allowOverrides ) {
116
+ setShowDisallowOverridesModal( true );
117
+ } else {
118
+ setShowAllowOverridesModal( true );
119
+ }
120
+ } }
121
+ >
122
+ { allowOverrides
123
+ ? __( 'Disable overrides' )
124
+ : __( 'Enable overrides' ) }
125
+ </Button>
126
+ </BaseControl>
127
+ </InspectorControls>
128
+
129
+ { showAllowOverridesModal && (
130
+ <AllowOverridesModal
131
+ initialName={ attributes.metadata?.name }
132
+ onClose={ () => setShowAllowOverridesModal( false ) }
133
+ onSave={ ( newName ) => {
134
+ updateBindings( true, newName );
135
+ } }
136
+ />
137
+ ) }
138
+ { showDisallowOverridesModal && (
139
+ <DisallowOverridesModal
140
+ onClose={ () => setShowDisallowOverridesModal( false ) }
141
+ onSave={ () => updateBindings( false ) }
142
+ />
143
+ ) }
144
+ </>
145
+ );
146
+ }
147
+
148
+ export default PatternOverridesControls;
@@ -18,11 +18,10 @@ import { unlock } from '../lock-unlock';
18
18
  function PatternsManageButton( { clientId } ) {
19
19
  const { canRemove, isVisible, managePatternsUrl } = useSelect(
20
20
  ( select ) => {
21
- const { getBlock, canRemoveBlock, getBlockCount, getSettings } =
21
+ const { getBlock, canRemoveBlock, getBlockCount } =
22
22
  select( blockEditorStore );
23
23
  const { canUser } = select( coreStore );
24
24
  const reusableBlock = getBlock( clientId );
25
- const isBlockTheme = getSettings().__unstableIsBlockBasedTheme;
26
25
 
27
26
  return {
28
27
  canRemove: canRemoveBlock( clientId ),
@@ -38,14 +37,13 @@ function PatternsManageButton( { clientId } ) {
38
37
  // The site editor and templates both check whether the user
39
38
  // has edit_theme_options capabilities. We can leverage that here
40
39
  // and omit the manage patterns link if the user can't access it.
41
- managePatternsUrl:
42
- isBlockTheme && canUser( 'read', 'templates' )
43
- ? addQueryArgs( 'site-editor.php', {
44
- path: '/patterns',
45
- } )
46
- : addQueryArgs( 'edit.php', {
47
- post_type: 'wp_block',
48
- } ),
40
+ managePatternsUrl: canUser( 'read', 'templates' )
41
+ ? addQueryArgs( 'site-editor.php', {
42
+ path: '/patterns',
43
+ } )
44
+ : addQueryArgs( 'edit.php', {
45
+ post_type: 'wp_block',
46
+ } ),
49
47
  };
50
48
  },
51
49
  [ clientId ]
@@ -38,3 +38,9 @@
38
38
  width: $grid-unit * 40;
39
39
  }
40
40
  }
41
+
42
+ .pattern-overrides-control__allow-overrides-button {
43
+ width: 100%;
44
+ justify-content: center;
45
+ }
46
+
package/src/constants.js CHANGED
@@ -22,3 +22,5 @@ export const PARTIAL_SYNCING_SUPPORTED_BLOCKS = {
22
22
  'core/button': [ 'text', 'url', 'linkTarget', 'rel' ],
23
23
  'core/image': [ 'id', 'url', 'title', 'alt' ],
24
24
  };
25
+
26
+ export const PATTERN_OVERRIDES_BINDING_SOURCE = 'core/pattern-overrides';
@@ -15,7 +15,7 @@ import { isOverridableBlock } from './api';
15
15
  import RenamePatternModal from './components/rename-pattern-modal';
16
16
  import PatternsMenuItems from './components';
17
17
  import RenamePatternCategoryModal from './components/rename-pattern-category-modal';
18
- import useSetPatternBindings from './components/use-set-pattern-bindings';
18
+ import PatternOverridesControls from './components/pattern-overrides-controls';
19
19
  import ResetOverridesControl from './components/reset-overrides-control';
20
20
  import { useAddPatternCategory } from './private-hooks';
21
21
  import {
@@ -38,7 +38,7 @@ lock( privateApis, {
38
38
  RenamePatternModal,
39
39
  PatternsMenuItems,
40
40
  RenamePatternCategoryModal,
41
- useSetPatternBindings,
41
+ PatternOverridesControls,
42
42
  ResetOverridesControl,
43
43
  useAddPatternCategory,
44
44
  PATTERN_TYPES,
@@ -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;AACA;AACA;;AAMA;AACA;AACA;;AAKA,SAASM,cAAcA,CAAEC,QAAQ,EAAEC,gBAAgB,EAAG;EACrD,IAAIC,eAAe,GAAG,CAAC,CAAC;EACxB,KAAM,MAA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F,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