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

Update: Guard against null block in off canvas editor. #46594

Merged
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
116 changes: 60 additions & 56 deletions packages/block-editor/src/components/off-canvas-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,68 @@ function ListViewBlock( {
[ clientId ]
);

// If ListView has experimental features related to the Persistent List View,
// only focus the selected list item on mount; otherwise the list would always
// try to steal the focus from the editor canvas.
useEffect( () => {
if ( ! isTreeGridMounted && isSelected ) {
cellRef.current.focus();
}
}, [] );

const onMouseEnter = useCallback( () => {
setIsHovered( true );
toggleBlockHighlight( clientId, true );
}, [ clientId, setIsHovered, toggleBlockHighlight ] );
const onMouseLeave = useCallback( () => {
setIsHovered( false );
toggleBlockHighlight( clientId, false );
}, [ clientId, setIsHovered, toggleBlockHighlight ] );

const selectEditorBlock = useCallback(
( event ) => {
selectBlock( event, clientId );
event.preventDefault();
},
[ clientId, selectBlock ]
);

const updateSelection = useCallback(
( newClientId ) => {
selectBlock( undefined, newClientId );
},
[ selectBlock ]
);

const { isTreeGridMounted, expand, collapse } = useListViewContext();

const toggleExpanded = useCallback(
( event ) => {
// Prevent shift+click from opening link in a new window when toggling.
event.preventDefault();
event.stopPropagation();
if ( isExpanded === true ) {
collapse( clientId );
} else if ( isExpanded === false ) {
expand( clientId );
}
},
[ clientId, expand, collapse, isExpanded ]
);

const instanceId = useInstanceId( ListViewBlock );

if ( ! block ) {
return null;
}

// When a block hides its toolbar it also hides the block settings menu,
// since that menu is part of the toolbar in the editor canvas.
// List View respects this by also hiding the block settings menu.
const showBlockActions = hasBlockSupport(
block.name,
'__experimentalToolbar',
true
);
const instanceId = useInstanceId( ListViewBlock );
const showBlockActions =
!! block &&
hasBlockSupport( block.name, '__experimentalToolbar', true );

const descriptionId = `list-view-block-select-button__${ instanceId }`;
const blockPositionDescription = getBlockPositionDescription(
position,
Expand Down Expand Up @@ -142,9 +195,7 @@ function ListViewBlock( {
)
: __( 'Edit' );

const { isTreeGridMounted, expand, collapse } = useListViewContext();

const isEditable = block.name !== 'core/page-list-item';
const isEditable = !! block && block.name !== 'core/page-list-item';
const hasSiblings = siblingBlockCount > 0;
const hasRenderedMovers = showBlockMovers && hasSiblings;
const moverCellClassName = classnames(
Expand All @@ -162,53 +213,6 @@ function ListViewBlock( {
{ 'is-visible': isHovered || isFirstSelectedBlock }
);

// If ListView has experimental features related to the Persistent List View,
// only focus the selected list item on mount; otherwise the list would always
// try to steal the focus from the editor canvas.
useEffect( () => {
if ( ! isTreeGridMounted && isSelected ) {
cellRef.current.focus();
}
}, [] );

const onMouseEnter = useCallback( () => {
setIsHovered( true );
toggleBlockHighlight( clientId, true );
}, [ clientId, setIsHovered, toggleBlockHighlight ] );
const onMouseLeave = useCallback( () => {
setIsHovered( false );
toggleBlockHighlight( clientId, false );
}, [ clientId, setIsHovered, toggleBlockHighlight ] );

const selectEditorBlock = useCallback(
( event ) => {
selectBlock( event, clientId );
event.preventDefault();
},
[ clientId, selectBlock ]
);

const updateSelection = useCallback(
( newClientId ) => {
selectBlock( undefined, newClientId );
},
[ selectBlock ]
);

const toggleExpanded = useCallback(
( event ) => {
// Prevent shift+click from opening link in a new window when toggling.
event.preventDefault();
event.stopPropagation();
if ( isExpanded === true ) {
collapse( clientId );
} else if ( isExpanded === false ) {
expand( clientId );
}
},
[ clientId, expand, collapse, isExpanded ]
);

let colSpan;
if ( hasRenderedMovers ) {
colSpan = 2;
Expand Down