@wordpress/patterns 1.18.0 → 2.0.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 (69) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/build/api/index.js +17 -0
  3. package/build/api/index.js.map +1 -1
  4. package/build/components/allow-overrides-modal.js +72 -58
  5. package/build/components/allow-overrides-modal.js.map +1 -1
  6. package/build/components/category-selector.js +2 -2
  7. package/build/components/category-selector.js.map +1 -1
  8. package/build/components/create-pattern-modal.js +56 -48
  9. package/build/components/create-pattern-modal.js.map +1 -1
  10. package/build/components/duplicate-pattern-modal.js +2 -2
  11. package/build/components/duplicate-pattern-modal.js.map +1 -1
  12. package/build/components/index.js +15 -11
  13. package/build/components/index.js.map +1 -1
  14. package/build/components/overrides-panel.js +7 -6
  15. package/build/components/overrides-panel.js.map +1 -1
  16. package/build/components/pattern-convert-button.js +22 -20
  17. package/build/components/pattern-convert-button.js.map +1 -1
  18. package/build/components/pattern-overrides-controls.js +49 -54
  19. package/build/components/pattern-overrides-controls.js.map +1 -1
  20. package/build/components/patterns-manage-button.js +11 -7
  21. package/build/components/patterns-manage-button.js.map +1 -1
  22. package/build/components/rename-pattern-category-modal.js +42 -34
  23. package/build/components/rename-pattern-category-modal.js.map +1 -1
  24. package/build/components/rename-pattern-modal.js +31 -25
  25. package/build/components/rename-pattern-modal.js.map +1 -1
  26. package/build/components/reset-overrides-control.js +55 -42
  27. package/build/components/reset-overrides-control.js.map +1 -1
  28. package/build/private-apis.js +1 -0
  29. package/build/private-apis.js.map +1 -1
  30. package/build/store/actions.js +15 -1
  31. package/build/store/actions.js.map +1 -1
  32. package/build-module/api/index.js +16 -0
  33. package/build-module/api/index.js.map +1 -1
  34. package/build-module/components/allow-overrides-modal.js +73 -58
  35. package/build-module/components/allow-overrides-modal.js.map +1 -1
  36. package/build-module/components/category-selector.js +2 -2
  37. package/build-module/components/category-selector.js.map +1 -1
  38. package/build-module/components/create-pattern-modal.js +59 -50
  39. package/build-module/components/create-pattern-modal.js.map +1 -1
  40. package/build-module/components/duplicate-pattern-modal.js +2 -2
  41. package/build-module/components/duplicate-pattern-modal.js.map +1 -1
  42. package/build-module/components/index.js +17 -11
  43. package/build-module/components/index.js.map +1 -1
  44. package/build-module/components/overrides-panel.js +7 -6
  45. package/build-module/components/overrides-panel.js.map +1 -1
  46. package/build-module/components/pattern-convert-button.js +23 -18
  47. package/build-module/components/pattern-convert-button.js.map +1 -1
  48. package/build-module/components/pattern-overrides-controls.js +52 -55
  49. package/build-module/components/pattern-overrides-controls.js.map +1 -1
  50. package/build-module/components/patterns-manage-button.js +13 -7
  51. package/build-module/components/patterns-manage-button.js.map +1 -1
  52. package/build-module/components/rename-pattern-category-modal.js +43 -34
  53. package/build-module/components/rename-pattern-category-modal.js.map +1 -1
  54. package/build-module/components/rename-pattern-modal.js +32 -25
  55. package/build-module/components/rename-pattern-modal.js.map +1 -1
  56. package/build-module/components/reset-overrides-control.js +56 -43
  57. package/build-module/components/reset-overrides-control.js.map +1 -1
  58. package/build-module/private-apis.js +2 -1
  59. package/build-module/private-apis.js.map +1 -1
  60. package/build-module/store/actions.js +16 -2
  61. package/build-module/store/actions.js.map +1 -1
  62. package/package.json +18 -17
  63. package/src/api/index.js +16 -0
  64. package/src/components/create-pattern-modal.js +16 -5
  65. package/src/components/pattern-overrides-controls.js +19 -44
  66. package/src/components/patterns-manage-button.js +1 -1
  67. package/src/components/reset-overrides-control.js +54 -50
  68. package/src/private-apis.js +2 -1
  69. package/src/store/actions.js +25 -2
@@ -37,7 +37,7 @@ function PatternsManageButton( { clientId } ) {
37
37
  // The site editor and templates both check whether the user
38
38
  // has edit_theme_options capabilities. We can leverage that here
39
39
  // and omit the manage patterns link if the user can't access it.
40
- managePatternsUrl: canUser( 'read', 'templates' )
40
+ managePatternsUrl: canUser( 'create', 'templates' )
41
41
  ? addQueryArgs( 'site-editor.php', {
42
42
  path: '/patterns',
43
43
  } )
@@ -6,80 +6,84 @@ import {
6
6
  BlockControls,
7
7
  } from '@wordpress/block-editor';
8
8
  import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
9
- import { useSelect, useRegistry } from '@wordpress/data';
10
- import { store as coreStore } from '@wordpress/core-data';
11
- import { parse } from '@wordpress/blocks';
9
+ import { useRegistry, useSelect } from '@wordpress/data';
12
10
  import { __ } from '@wordpress/i18n';
13
11
 
14
- function recursivelyFindBlockWithName( blocks, name ) {
15
- for ( const block of blocks ) {
16
- if ( block.attributes.metadata?.name === name ) {
17
- return block;
18
- }
19
-
20
- const found = recursivelyFindBlockWithName( block.innerBlocks, name );
21
- if ( found ) {
22
- return found;
23
- }
24
- }
25
- }
12
+ const CONTENT = 'content';
26
13
 
27
14
  export default function ResetOverridesControl( props ) {
28
- const registry = useRegistry();
29
15
  const name = props.attributes.metadata?.name;
30
- const patternWithOverrides = useSelect(
16
+ const registry = useRegistry();
17
+ const isOverriden = useSelect(
31
18
  ( select ) => {
32
19
  if ( ! name ) {
33
- return undefined;
20
+ return;
34
21
  }
35
22
 
36
- const { getBlockParentsByBlockName, getBlocksByClientId } =
23
+ const { getBlockAttributes, getBlockParentsByBlockName } =
37
24
  select( blockEditorStore );
38
- const patternBlock = getBlocksByClientId(
39
- getBlockParentsByBlockName( props.clientId, 'core/block' )
40
- )[ 0 ];
25
+ const [ patternClientId ] = getBlockParentsByBlockName(
26
+ props.clientId,
27
+ 'core/block',
28
+ true
29
+ );
30
+
31
+ if ( ! patternClientId ) {
32
+ return;
33
+ }
34
+
35
+ const overrides = getBlockAttributes( patternClientId )[ CONTENT ];
41
36
 
42
- if ( ! patternBlock?.attributes.content?.[ name ] ) {
43
- return undefined;
37
+ if ( ! overrides ) {
38
+ return;
44
39
  }
45
40
 
46
- return patternBlock;
41
+ return overrides.hasOwnProperty( name );
47
42
  },
48
43
  [ props.clientId, name ]
49
44
  );
50
45
 
51
- const resetOverrides = async () => {
52
- const editedRecord = await registry
53
- .resolveSelect( coreStore )
54
- .getEditedEntityRecord(
55
- 'postType',
56
- 'wp_block',
57
- patternWithOverrides.attributes.ref
58
- );
59
- const blocks = editedRecord.blocks ?? parse( editedRecord.content );
60
- const block = recursivelyFindBlockWithName( blocks, name );
61
-
62
- const newAttributes = Object.assign(
63
- // Reset every existing attribute to undefined.
64
- Object.fromEntries(
65
- Object.keys( props.attributes ).map( ( key ) => [
66
- key,
67
- undefined,
68
- ] )
69
- ),
70
- // Then assign the original attributes.
71
- block.attributes
46
+ function onClick() {
47
+ const { getBlockAttributes, getBlockParentsByBlockName } =
48
+ registry.select( blockEditorStore );
49
+ const [ patternClientId ] = getBlockParentsByBlockName(
50
+ props.clientId,
51
+ 'core/block',
52
+ true
72
53
  );
73
54
 
74
- props.setAttributes( newAttributes );
75
- };
55
+ if ( ! patternClientId ) {
56
+ return;
57
+ }
58
+
59
+ const overrides = getBlockAttributes( patternClientId )[ CONTENT ];
60
+
61
+ if ( ! overrides.hasOwnProperty( name ) ) {
62
+ return;
63
+ }
64
+
65
+ const { updateBlockAttributes, __unstableMarkLastChangeAsPersistent } =
66
+ registry.dispatch( blockEditorStore );
67
+ __unstableMarkLastChangeAsPersistent();
68
+
69
+ let newOverrides = { ...overrides };
70
+ delete newOverrides[ name ];
71
+
72
+ if ( ! Object.keys( newOverrides ).length ) {
73
+ newOverrides = undefined;
74
+ }
75
+
76
+ updateBlockAttributes( patternClientId, {
77
+ [ CONTENT ]: newOverrides,
78
+ } );
79
+ }
76
80
 
77
81
  return (
78
82
  <BlockControls group="other">
79
83
  <ToolbarGroup>
80
84
  <ToolbarButton
81
- onClick={ resetOverrides }
82
- disabled={ ! patternWithOverrides }
85
+ onClick={ onClick }
86
+ disabled={ ! isOverriden }
83
87
  __experimentalIsFocusable
84
88
  >
85
89
  { __( 'Reset' ) }
@@ -11,7 +11,7 @@ import {
11
11
  default as DuplicatePatternModal,
12
12
  useDuplicatePatternProps,
13
13
  } from './components/duplicate-pattern-modal';
14
- import { isOverridableBlock } from './api';
14
+ import { isOverridableBlock, hasOverridableBlocks } 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';
@@ -34,6 +34,7 @@ lock( privateApis, {
34
34
  CreatePatternModalContents,
35
35
  DuplicatePatternModal,
36
36
  isOverridableBlock,
37
+ hasOverridableBlocks,
37
38
  useDuplicatePatternProps,
38
39
  RenamePatternModal,
39
40
  PatternsMenuItems,
@@ -2,7 +2,7 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
 
5
- import { cloneBlock } from '@wordpress/blocks';
5
+ import { getBlockType, cloneBlock } from '@wordpress/blocks';
6
6
  import { store as coreStore } from '@wordpress/core-data';
7
7
  import { store as blockEditorStore } from '@wordpress/block-editor';
8
8
 
@@ -93,6 +93,7 @@ export const convertSyncedPatternToStatic =
93
93
  const patternBlock = registry
94
94
  .select( blockEditorStore )
95
95
  .getBlock( clientId );
96
+ const existingOverrides = patternBlock.attributes?.content;
96
97
 
97
98
  function cloneBlocksAndRemoveBindings( blocks ) {
98
99
  return blocks.map( ( block ) => {
@@ -101,6 +102,24 @@ export const convertSyncedPatternToStatic =
101
102
  metadata = { ...metadata };
102
103
  delete metadata.id;
103
104
  delete metadata.bindings;
105
+ // Use overridden values of the pattern block if they exist.
106
+ if ( existingOverrides?.[ metadata.name ] ) {
107
+ // Iterate over each overriden attribute.
108
+ for ( const [ attributeName, value ] of Object.entries(
109
+ existingOverrides[ metadata.name ]
110
+ ) ) {
111
+ // Skip if the attribute does not exist in the block type.
112
+ if (
113
+ ! getBlockType( block.name )?.attributes[
114
+ attributeName
115
+ ]
116
+ ) {
117
+ continue;
118
+ }
119
+ // Update the block attribute with the override value.
120
+ block.attributes[ attributeName ] = value;
121
+ }
122
+ }
104
123
  }
105
124
  return cloneBlock(
106
125
  block,
@@ -115,11 +134,15 @@ export const convertSyncedPatternToStatic =
115
134
  } );
116
135
  }
117
136
 
137
+ const patternInnerBlocks = registry
138
+ .select( blockEditorStore )
139
+ .getBlocks( patternBlock.clientId );
140
+
118
141
  registry
119
142
  .dispatch( blockEditorStore )
120
143
  .replaceBlocks(
121
144
  patternBlock.clientId,
122
- cloneBlocksAndRemoveBindings( patternBlock.innerBlocks )
145
+ cloneBlocksAndRemoveBindings( patternInnerBlocks )
123
146
  );
124
147
  };
125
148