Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Inserter on the navigation sidebar. (#48049 #48049

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,23 @@ import useBlockDisplayTitle from '../block-title/use-block-display-title';
import Inserter from '../inserter';

export const Appender = forwardRef(
( { nestingLevel, blockCount, ...props }, ref ) => {
( { nestingLevel, blockCount, clientId, ...props }, ref ) => {
const [ insertedBlock, setInsertedBlock ] = useState( null );

const instanceId = useInstanceId( Appender );
const { hideInserter, clientId } = useSelect( ( select ) => {
const {
getTemplateLock,
__unstableGetEditorMode,
getSelectedBlockClientId,
} = select( blockEditorStore );
const { hideInserter } = useSelect(
( select ) => {
const { getTemplateLock, __unstableGetEditorMode } =
select( blockEditorStore );

const _clientId = getSelectedBlockClientId();

return {
clientId: getSelectedBlockClientId(),
hideInserter:
!! getTemplateLock( _clientId ) ||
__unstableGetEditorMode() === 'zoom-out',
};
}, [] );
return {
hideInserter:
!! getTemplateLock( clientId ) ||
__unstableGetEditorMode() === 'zoom-out',
};
},
[ clientId ]
);

const blockTitle = useBlockDisplayTitle( {
clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ function ListViewBranch( props ) {
return null;
}

// Only show the appender at the first level.
const showAppender = level === 1;
// Only show the appender at the first level and if there is a parent block.
const showAppender = level === 1 && parentId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this addition do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no parentId, we don't show the appender. The case where we may not have a parentId is if for example, we don't have navigation menus on the page the sidebar shows the offsite navigation editor which says correctly navigation is empty but without this change shows an appender that adds things to the root which is unexpected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. Ok it's a good guard, but we should never be in this situation:

we don't have navigation menus on the page

I think the navigation is not empty just because the current page is not showing it. This is tricky and outside the scope of this PR but, what if the page with no navigation block is linked in the navigation, clicking on it in the sidebar would then hide the navigation from the sidebar - quite unexpected.


const filteredBlocks = blocks.filter( Boolean );
const blockCount = filteredBlocks.length;
Expand Down Expand Up @@ -220,6 +220,7 @@ function ListViewBranch( props ) {
<TreeGridCell>
{ ( treeGridCellProps ) => (
<Appender
clientId={ parentId }
nestingLevel={ level }
blockCount={ blockCount }
{ ...treeGridCellProps }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ function OffCanvasEditor(
const { clientIdsTree, draggedClientIds, selectedClientIds } =
useListViewClientIds( blocks );

const { visibleBlockCount, shouldShowInnerBlocks } = useSelect(
const { visibleBlockCount, shouldShowInnerBlocks, parentId } = useSelect(
( select ) => {
const {
getBlockRootClientId,
getGlobalBlockCount,
getClientIdsOfDescendants,
__unstableGetEditorMode,
Expand All @@ -94,9 +95,13 @@ function OffCanvasEditor(
return {
visibleBlockCount: getGlobalBlockCount() - draggedBlockCount,
shouldShowInnerBlocks: __unstableGetEditorMode() !== 'zoom-out',
parentId:
blocks.length > 0
? getBlockRootClientId( blocks[ 0 ].clientId )
: undefined,
};
},
[ draggedClientIds ]
[ draggedClientIds, blocks ]
);

const { updateBlockSelection } = useBlockSelection();
Expand Down Expand Up @@ -227,6 +232,7 @@ function OffCanvasEditor(
>
<ListViewContext.Provider value={ contextValue }>
<ListViewBranch
parentId={ parentId }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is a better name for this rootBlockId or something as the list view is not really a child of a block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The component ListViewBranch already had the parentId property, I'm just passing the property in a case where we were not passing it before, but the prop already existed.
The list view component also calls it parentId

.
I can change it to rootBlockId on the offsite canvas editor and the list view if you prefer. I don't have a specific preference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I lost this fact in reviewing the code, thought we just added parentId. Let's not change anything then :)

blocks={ clientIdsTree }
selectBlock={ selectEditorBlock }
showBlockMovers={ showBlockMovers }
Expand Down