Skip to content

Commit 407e5c8

Browse files
talldancbravobernal
authored andcommitted
Add content schema to pattern editing view (WordPress#59977)
* Add new quick navigation component for pattern schema * Implement panel and fix minor issues * Rename to pattern content, and avoid showing panel when there are no blocks * Don't try to unlock what's already unlocked, just turn the handle. * Rename content panel title from 'Content' to 'Overrides' * Rename component to `OverridesPanel`
1 parent 6218a56 commit 407e5c8

File tree

8 files changed

+110
-19
lines changed

8 files changed

+110
-19
lines changed

packages/block-library/src/block/edit.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { name as patternBlockName } from './index';
3838
import { unlock } from '../lock-unlock';
3939

4040
const { useLayoutClasses } = unlock( blockEditorPrivateApis );
41-
const { PARTIAL_SYNCING_SUPPORTED_BLOCKS } = unlock( patternsPrivateApis );
41+
const { isOverridableBlock } = unlock( patternsPrivateApis );
4242

4343
const fullAlignments = [ 'full', 'wide', 'left', 'right' ];
4444

@@ -90,21 +90,9 @@ const useInferredLayout = ( blocks, parentLayout ) => {
9090
}, [ blocks, parentLayout ] );
9191
};
9292

93-
function hasOverridableAttributes( block ) {
94-
return (
95-
Object.keys( PARTIAL_SYNCING_SUPPORTED_BLOCKS ).includes(
96-
block.name
97-
) &&
98-
!! block.attributes.metadata?.bindings &&
99-
Object.values( block.attributes.metadata.bindings ).some(
100-
( binding ) => binding.source === 'core/pattern-overrides'
101-
)
102-
);
103-
}
104-
10593
function hasOverridableBlocks( blocks ) {
10694
return blocks.some( ( block ) => {
107-
if ( hasOverridableAttributes( block ) ) return true;
95+
if ( isOverridableBlock( block ) ) return true;
10896
return hasOverridableBlocks( block.innerBlocks );
10997
} );
11098
}
@@ -133,7 +121,7 @@ function applyInitialContentValuesToInnerBlocks(
133121
const metadataName =
134122
legacyIdMap?.[ block.clientId ] ?? block.attributes.metadata?.name;
135123

136-
if ( ! metadataName || ! hasOverridableAttributes( block ) ) {
124+
if ( ! metadataName || ! isOverridableBlock( block ) ) {
137125
return { ...block, innerBlocks };
138126
}
139127

@@ -184,7 +172,7 @@ function getContentValuesFromInnerBlocks( blocks, defaultValues, legacyIdMap ) {
184172
}
185173
const metadataName =
186174
legacyIdMap?.[ block.clientId ] ?? block.attributes.metadata?.name;
187-
if ( ! metadataName || ! hasOverridableAttributes( block ) ) {
175+
if ( ! metadataName || ! isOverridableBlock( block ) ) {
188176
continue;
189177
}
190178

@@ -217,7 +205,7 @@ function setBlockEditMode( setEditMode, blocks, mode ) {
217205
blocks.forEach( ( block ) => {
218206
const editMode =
219207
mode ||
220-
( hasOverridableAttributes( block ) ? 'contentOnly' : 'disabled' );
208+
( isOverridableBlock( block ) ? 'contentOnly' : 'disabled' );
221209
setEditMode( block.clientId, editMode );
222210

223211
setBlockEditMode(

packages/edit-post/src/components/sidebar/settings-sidebar/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { unlock } from '../../../lock-unlock';
4343
const { PostCardPanel } = unlock( editorPrivateApis );
4444

4545
const { Tabs } = unlock( componentsPrivateApis );
46+
const { PatternOverridesPanel } = unlock( editorPrivateApis );
4647

4748
const SIDEBAR_ACTIVE_BY_DEFAULT = Platform.select( {
4849
web: true,
@@ -123,6 +124,7 @@ const SidebarContent = ( {
123124
<PostExcerptPanel />
124125
<PostDiscussionPanel />
125126
<PageAttributesPanel />
127+
<PatternOverridesPanel />
126128
<MetaBoxes location="side" />
127129
</>
128130
) }

packages/edit-site/src/components/sidebar-edit-mode/template-panel/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
PostExcerptPanel,
1010
PostLastRevisionPanel,
1111
PostTaxonomiesPanel,
12-
store as editorStore,
1312
privateApis as editorPrivateApis,
13+
store as editorStore,
1414
} from '@wordpress/editor';
1515
import { store as coreStore } from '@wordpress/core-data';
1616
import { __ } from '@wordpress/i18n';
@@ -31,7 +31,7 @@ import { TEMPLATE_PART_POST_TYPE } from '../../../utils/constants';
3131
import { unlock } from '../../../lock-unlock';
3232

3333
const { PostCardPanel } = unlock( editorPrivateApis );
34-
34+
const { PatternOverridesPanel } = unlock( editorPrivateApis );
3535
const { useHistory } = unlock( routerPrivateApis );
3636

3737
function TemplatesList( { availableTemplates, onSelect } ) {
@@ -141,6 +141,7 @@ export default function TemplatePanel() {
141141
<PostExcerptPanel />
142142
<PostDiscussionPanel />
143143
<PageAttributesPanel />
144+
<PatternOverridesPanel />
144145
</>
145146
);
146147
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { useSelect } from '@wordpress/data';
5+
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
6+
7+
/**
8+
* Internal dependencies
9+
*/
10+
import { store as editorStore } from '../../store';
11+
import { unlock } from '../../lock-unlock';
12+
13+
const { OverridesPanel } = unlock( patternsPrivateApis );
14+
15+
export default function PatternOverridesPanel() {
16+
const supportsPatternOverridesPanel = useSelect(
17+
( select ) => select( editorStore ).getCurrentPostType() === 'wp_block',
18+
[]
19+
);
20+
21+
if ( ! supportsPatternOverridesPanel ) {
22+
return null;
23+
}
24+
25+
return <OverridesPanel />;
26+
}

packages/editor/src/private-apis.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import DocumentTools from './components/document-tools';
1010
import InserterSidebar from './components/inserter-sidebar';
1111
import ListViewSidebar from './components/list-view-sidebar';
1212
import ModeSwitcher from './components/mode-switcher';
13+
import PatternOverridesPanel from './components/pattern-overrides-panel';
1314
import PluginPostExcerpt from './components/post-excerpt/plugin';
1415
import PostPanelRow from './components/post-panel-row';
1516
import PostViewLink from './components/post-view-link';
@@ -27,6 +28,7 @@ lock( privateApis, {
2728
InserterSidebar,
2829
ListViewSidebar,
2930
ModeSwitcher,
31+
PatternOverridesPanel,
3032
PluginPostExcerpt,
3133
PostPanelRow,
3234
PostViewLink,

packages/patterns/src/api/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';
5+
6+
/**
7+
* Determines whether a block is overridable.
8+
*
9+
* @param {WPBlock} block The block to test.
10+
*
11+
* @return {boolean} `true` if a block is overridable, `false` otherwise.
12+
*/
13+
export function isOverridableBlock( block ) {
14+
return (
15+
Object.keys( PARTIAL_SYNCING_SUPPORTED_BLOCKS ).includes(
16+
block.name
17+
) &&
18+
!! block.attributes.metadata?.bindings &&
19+
Object.values( block.attributes.metadata.bindings ).some(
20+
( binding ) => binding.source === 'core/pattern-overrides'
21+
)
22+
);
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import {
5+
privateApis as blockEditorPrivateApis,
6+
store as blockEditorStore,
7+
} from '@wordpress/block-editor';
8+
import { PanelBody } from '@wordpress/components';
9+
import { useSelect } from '@wordpress/data';
10+
import { useMemo } from '@wordpress/element';
11+
import { __ } from '@wordpress/i18n';
12+
13+
/**
14+
* Internal dependencies
15+
*/
16+
import { isOverridableBlock } from '../api';
17+
import { unlock } from '../lock-unlock';
18+
19+
const { BlockQuickNavigation } = unlock( blockEditorPrivateApis );
20+
21+
export default function OverridesPanel() {
22+
const allClientIds = useSelect(
23+
( select ) => select( blockEditorStore ).getClientIdsWithDescendants(),
24+
[]
25+
);
26+
const { getBlock } = useSelect( blockEditorStore );
27+
const clientIdsWithOverrides = useMemo(
28+
() =>
29+
allClientIds.filter( ( clientId ) => {
30+
const block = getBlock( clientId );
31+
return isOverridableBlock( block );
32+
} ),
33+
[ allClientIds, getBlock ]
34+
);
35+
36+
if ( ! clientIdsWithOverrides?.length ) {
37+
return null;
38+
}
39+
40+
return (
41+
<PanelBody title={ __( 'Overrides' ) }>
42+
<BlockQuickNavigation clientIds={ clientIdsWithOverrides } />
43+
</PanelBody>
44+
);
45+
}

packages/patterns/src/private-apis.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Internal dependencies
33
*/
44
import { lock } from './lock-unlock';
5+
import OverridesPanel from './components/overrides-panel';
56
import {
67
default as CreatePatternModal,
78
CreatePatternModalContents,
@@ -10,6 +11,7 @@ import {
1011
default as DuplicatePatternModal,
1112
useDuplicatePatternProps,
1213
} from './components/duplicate-pattern-modal';
14+
import { isOverridableBlock } from './api';
1315
import RenamePatternModal from './components/rename-pattern-modal';
1416
import PatternsMenuItems from './components';
1517
import RenamePatternCategoryModal from './components/rename-pattern-category-modal';
@@ -27,9 +29,11 @@ import {
2729

2830
export const privateApis = {};
2931
lock( privateApis, {
32+
OverridesPanel,
3033
CreatePatternModal,
3134
CreatePatternModalContents,
3235
DuplicatePatternModal,
36+
isOverridableBlock,
3337
useDuplicatePatternProps,
3438
RenamePatternModal,
3539
PatternsMenuItems,

0 commit comments

Comments
 (0)