@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.
- package/CHANGELOG.md +8 -0
- package/build/api/index.js +17 -0
- package/build/api/index.js.map +1 -1
- package/build/components/allow-overrides-modal.js +72 -58
- package/build/components/allow-overrides-modal.js.map +1 -1
- package/build/components/category-selector.js +2 -2
- package/build/components/category-selector.js.map +1 -1
- package/build/components/create-pattern-modal.js +56 -48
- package/build/components/create-pattern-modal.js.map +1 -1
- package/build/components/duplicate-pattern-modal.js +2 -2
- package/build/components/duplicate-pattern-modal.js.map +1 -1
- package/build/components/index.js +15 -11
- package/build/components/index.js.map +1 -1
- package/build/components/overrides-panel.js +7 -6
- package/build/components/overrides-panel.js.map +1 -1
- package/build/components/pattern-convert-button.js +22 -20
- package/build/components/pattern-convert-button.js.map +1 -1
- package/build/components/pattern-overrides-controls.js +49 -54
- package/build/components/pattern-overrides-controls.js.map +1 -1
- package/build/components/patterns-manage-button.js +11 -7
- package/build/components/patterns-manage-button.js.map +1 -1
- package/build/components/rename-pattern-category-modal.js +42 -34
- package/build/components/rename-pattern-category-modal.js.map +1 -1
- package/build/components/rename-pattern-modal.js +31 -25
- package/build/components/rename-pattern-modal.js.map +1 -1
- package/build/components/reset-overrides-control.js +55 -42
- package/build/components/reset-overrides-control.js.map +1 -1
- package/build/private-apis.js +1 -0
- package/build/private-apis.js.map +1 -1
- package/build/store/actions.js +15 -1
- package/build/store/actions.js.map +1 -1
- package/build-module/api/index.js +16 -0
- package/build-module/api/index.js.map +1 -1
- package/build-module/components/allow-overrides-modal.js +73 -58
- package/build-module/components/allow-overrides-modal.js.map +1 -1
- package/build-module/components/category-selector.js +2 -2
- package/build-module/components/category-selector.js.map +1 -1
- package/build-module/components/create-pattern-modal.js +59 -50
- package/build-module/components/create-pattern-modal.js.map +1 -1
- package/build-module/components/duplicate-pattern-modal.js +2 -2
- package/build-module/components/duplicate-pattern-modal.js.map +1 -1
- package/build-module/components/index.js +17 -11
- package/build-module/components/index.js.map +1 -1
- package/build-module/components/overrides-panel.js +7 -6
- package/build-module/components/overrides-panel.js.map +1 -1
- package/build-module/components/pattern-convert-button.js +23 -18
- package/build-module/components/pattern-convert-button.js.map +1 -1
- package/build-module/components/pattern-overrides-controls.js +52 -55
- package/build-module/components/pattern-overrides-controls.js.map +1 -1
- package/build-module/components/patterns-manage-button.js +13 -7
- package/build-module/components/patterns-manage-button.js.map +1 -1
- package/build-module/components/rename-pattern-category-modal.js +43 -34
- package/build-module/components/rename-pattern-category-modal.js.map +1 -1
- package/build-module/components/rename-pattern-modal.js +32 -25
- package/build-module/components/rename-pattern-modal.js.map +1 -1
- package/build-module/components/reset-overrides-control.js +56 -43
- package/build-module/components/reset-overrides-control.js.map +1 -1
- package/build-module/private-apis.js +2 -1
- package/build-module/private-apis.js.map +1 -1
- package/build-module/store/actions.js +16 -2
- package/build-module/store/actions.js.map +1 -1
- package/package.json +18 -17
- package/src/api/index.js +16 -0
- package/src/components/create-pattern-modal.js +16 -5
- package/src/components/pattern-overrides-controls.js +19 -44
- package/src/components/patterns-manage-button.js +1 -1
- package/src/components/reset-overrides-control.js +54 -50
- package/src/private-apis.js +2 -1
- 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( '
|
|
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 {
|
|
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
|
-
|
|
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
|
|
16
|
+
const registry = useRegistry();
|
|
17
|
+
const isOverriden = useSelect(
|
|
31
18
|
( select ) => {
|
|
32
19
|
if ( ! name ) {
|
|
33
|
-
return
|
|
20
|
+
return;
|
|
34
21
|
}
|
|
35
22
|
|
|
36
|
-
const {
|
|
23
|
+
const { getBlockAttributes, getBlockParentsByBlockName } =
|
|
37
24
|
select( blockEditorStore );
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
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 ( !
|
|
43
|
-
return
|
|
37
|
+
if ( ! overrides ) {
|
|
38
|
+
return;
|
|
44
39
|
}
|
|
45
40
|
|
|
46
|
-
return
|
|
41
|
+
return overrides.hasOwnProperty( name );
|
|
47
42
|
},
|
|
48
43
|
[ props.clientId, name ]
|
|
49
44
|
);
|
|
50
45
|
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
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={
|
|
82
|
-
disabled={ !
|
|
85
|
+
onClick={ onClick }
|
|
86
|
+
disabled={ ! isOverriden }
|
|
83
87
|
__experimentalIsFocusable
|
|
84
88
|
>
|
|
85
89
|
{ __( 'Reset' ) }
|
package/src/private-apis.js
CHANGED
|
@@ -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,
|
package/src/store/actions.js
CHANGED
|
@@ -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(
|
|
145
|
+
cloneBlocksAndRemoveBindings( patternInnerBlocks )
|
|
123
146
|
);
|
|
124
147
|
};
|
|
125
148
|
|