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

Zoom Out: fix crash due to absence of selected block #63642

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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 @@ -29,10 +29,11 @@ export function useShowBlockTools() {
const clientId =
getSelectedBlockClientId() || getFirstMultiSelectedBlockClientId();

const block = getBlock( clientId ) || { name: '', attributes: {} };
const block = getBlock( clientId );
Copy link
Contributor

Choose a reason for hiding this comment

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

If I recall, we wanted to keep the structure the same if nothing was found due to memoization: { name: '', attributes: {} };. I think @Mamaduka recommended this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like the only reason for this is so that block can be passed to isUnmodifiedDefaultBlock which doesn’t guard against nullish values. Beyond that block is unused so it’s okay to be nullish.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it's just safe guard - #58979 (comment).

const editorMode = __unstableGetEditorMode();
const hasSelectedBlock = clientId && block?.name;
const isEmptyDefaultBlock = isUnmodifiedDefaultBlock( block );
const hasSelectedBlock = !! clientId && !! block;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the !! for this line is a good change, but undo the other refactoring.

const isEmptyDefaultBlock =
hasSelectedBlock && isUnmodifiedDefaultBlock( block );
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should tie the hasSelectedBlock condition here to the isEmptyDefaultBlock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why even call isUnmodifiedDefaultBlock if there is no selected block? I.e. without a selection it can’t be a empty default block right?

const _showEmptyBlockSideInserter =
clientId &&
! isTyping() &&
Expand All @@ -43,8 +44,9 @@ export function useShowBlockTools() {
! hasMultiSelection() &&
editorMode === 'navigation';

const isZoomOut = editorMode === 'zoom-out';
const _showBlockToolbarPopover =
editorMode !== 'zoom-out' &&
! isZoomOut &&
! getSettings().hasFixedToolbar &&
! _showEmptyBlockSideInserter &&
hasSelectedBlock &&
Expand All @@ -57,7 +59,8 @@ export function useShowBlockTools() {
! _showEmptyBlockSideInserter && maybeShowBreadcrumb,
showBlockToolbarPopover: _showBlockToolbarPopover,
showZoomOutToolbar:
editorMode === 'zoom-out' &&
hasSelectedBlock &&
Copy link
Contributor Author

@stokesman stokesman Jul 17, 2024

Choose a reason for hiding this comment

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

So this was probably the only key change to fix the issue. The other changes I felt were minor code quality improvements.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice find!

isZoomOut &&
! _showEmptyBlockSideInserter &&
! maybeShowBreadcrumb &&
! _showBlockToolbarPopover,
Expand Down
Loading