Skip to content

Commit 2127ef8

Browse files
committed
Add Navigation block toolbar buttons to Template Part editor
- Add 'Edit navigation' button when template part contains Navigation blocks - Button selects the first Navigation block and opens inspector - Remove contentOnly mode restriction - buttons work in all modes - Prevent double-click navigation when editing Navigation blocks - Add visual separation between toolbar button groups Extracted from PR #71137 with contentOnly restrictions removed.
1 parent 4ee682c commit 2127ef8

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

packages/block-library/src/template-part/edit/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ export default function TemplatePartEdit( {
132132
canUserEdit,
133133
hasNavigationBlocks,
134134
firstNavigationBlockId,
135-
blockEditingMode,
136135
} = useSelect(
137136
( select ) => {
138137
const { getEditedEntityRecord, hasFinishedResolution } =
@@ -187,10 +186,6 @@ export default function TemplatePartEdit( {
187186
? navigationBlocksInTemplatePart[ 0 ]
188187
: null;
189188

190-
// Get block editing mode for the current block
191-
const _blockEditingMode =
192-
select( blockEditorStore ).getBlockEditingMode( clientId );
193-
194189
return {
195190
hasInnerBlocks: getBlockCount( clientId ) > 0,
196191
isResolved: hasResolvedEntity,
@@ -205,7 +200,6 @@ export default function TemplatePartEdit( {
205200
canUserEdit: !! _canUserEdit,
206201
hasNavigationBlocks: _hasNavigationBlocks,
207202
firstNavigationBlockId: _firstNavigationBlockId,
208-
blockEditingMode: _blockEditingMode,
209203
};
210204
},
211205
[ templatePartId, attributes.area, clientId ]
@@ -303,7 +297,7 @@ export default function TemplatePartEdit( {
303297
</ToolbarButton>
304298
</BlockControls>
305299
) }
306-
{ hasNavigationBlocks && blockEditingMode === 'contentOnly' && (
300+
{ hasNavigationBlocks && (
307301
<BlockControls group="other">
308302
<ToolbarButton
309303
label={ __( 'Edit navigation' ) }

packages/block-library/src/template-part/edit/inner-blocks.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,35 @@ function EditableTemplatePartInnerBlocks( {
100100
tagName: TagName,
101101
blockProps,
102102
} ) {
103-
const onNavigateToEntityRecord = useSelect(
104-
( select ) =>
105-
select( blockEditorStore ).getSettings().onNavigateToEntityRecord,
103+
const { onNavigateToEntityRecord, isNavigationContext } = useSelect(
104+
( select ) => {
105+
const {
106+
getSelectedBlockClientId,
107+
getBlockName,
108+
getBlockParentsByBlockName,
109+
} = select( blockEditorStore );
110+
const selectedBlockId = getSelectedBlockClientId();
111+
const selectedBlockName =
112+
selectedBlockId && getBlockName( selectedBlockId );
113+
114+
// Check if selected block is a Navigation block or a descendant of one
115+
const navigationParents = getBlockParentsByBlockName(
116+
selectedBlockId,
117+
'core/navigation',
118+
true
119+
);
120+
const isDescendantOfNavigation = navigationParents.length > 0;
121+
const isNavigationBlock = selectedBlockName === 'core/navigation';
122+
const isInNavigationContext =
123+
isNavigationBlock || isDescendantOfNavigation;
124+
125+
return {
126+
onNavigateToEntityRecord:
127+
select( blockEditorStore ).getSettings()
128+
.onNavigateToEntityRecord,
129+
isNavigationContext: isInNavigationContext,
130+
};
131+
},
106132
[]
107133
);
108134

@@ -123,13 +149,16 @@ function EditableTemplatePartInnerBlocks( {
123149
const blockEditingMode = useBlockEditingMode();
124150

125151
const customProps =
126-
blockEditingMode === 'contentOnly' && onNavigateToEntityRecord
152+
blockEditingMode === 'contentOnly' &&
153+
onNavigateToEntityRecord &&
154+
! isNavigationContext
127155
? {
128-
onDoubleClick: () =>
156+
onDoubleClick: () => {
129157
onNavigateToEntityRecord( {
130158
postId: id,
131159
postType: 'wp_template_part',
132-
} ),
160+
} );
161+
},
133162
}
134163
: {};
135164

0 commit comments

Comments
 (0)