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

Block editor: reduce appender sync! subscriptions #58556

Merged
merged 2 commits into from
Feb 1, 2024
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 @@ -39,77 +39,12 @@ function DefaultAppender( { rootClientId } ) {
);
}

function useAppender( rootClientId, CustomAppender ) {
const isVisible = useSelect(
( select ) => {
const {
getTemplateLock,
getSelectedBlockClientId,
__unstableGetEditorMode,
getBlockEditingMode,
} = select( blockEditorStore );

if ( ! CustomAppender ) {
const selectedBlockClientId = getSelectedBlockClientId();
const isParentSelected =
rootClientId === selectedBlockClientId ||
( ! rootClientId && ! selectedBlockClientId );
if ( ! isParentSelected ) {
return false;
}
}

if (
getTemplateLock( rootClientId ) ||
getBlockEditingMode( rootClientId ) === 'disabled' ||
__unstableGetEditorMode() === 'zoom-out'
) {
return false;
}

return true;
},
[ rootClientId, CustomAppender ]
);

if ( ! isVisible ) {
return null;
}

return CustomAppender ? (
<CustomAppender />
) : (
<DefaultAppender rootClientId={ rootClientId } />
);
}

function BlockListAppender( {
export default function BlockListAppender( {
rootClientId,
renderAppender,
CustomAppender,
className,
tagName: TagName = 'div',
} ) {
if ( renderAppender === false ) {
return null;
}

return (
<BlockListAppenderInner
rootClientId={ rootClientId }
renderAppender={ renderAppender }
className={ className }
tagName={ TagName }
/>
);
}

function BlockListAppenderInner( {
rootClientId,
renderAppender,
className,
tagName: TagName,
} ) {
const appender = useAppender( rootClientId, renderAppender );
const isDragOver = useSelect(
( select ) => {
const {
Expand All @@ -130,10 +65,6 @@ function BlockListAppenderInner( {
[ rootClientId ]
);

if ( ! appender ) {
return null;
}

return (
<TagName
// A `tabIndex` is used on the wrapping `div` element in order to
Expand Down Expand Up @@ -162,9 +93,11 @@ function BlockListAppenderInner( {
// have commonly targeted that attribute for margins.
data-block
>
{ appender }
{ CustomAppender ? (
<CustomAppender />
) : (
<DefaultAppender rootClientId={ rootClientId } />
) }
</TagName>
);
}

export default BlockListAppender;
74 changes: 49 additions & 25 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,51 @@ export default function BlockList( settings ) {
function Items( {
placeholder,
rootClientId,
renderAppender,
renderAppender: CustomAppender,
__experimentalAppenderTagName,
layout = defaultLayout,
} ) {
const { order, selectedBlocks, visibleBlocks, temporarilyEditingAsBlocks } =
useSelect(
( select ) => {
const {
getBlockOrder,
getSelectedBlockClientIds,
__unstableGetVisibleBlocks,
__unstableGetTemporarilyEditingAsBlocks,
} = select( blockEditorStore );
return {
order: getBlockOrder( rootClientId ),
selectedBlocks: getSelectedBlockClientIds(),
visibleBlocks: __unstableGetVisibleBlocks(),
temporarilyEditingAsBlocks:
__unstableGetTemporarilyEditingAsBlocks(),
};
},
[ rootClientId ]
);
// Avoid passing CustomAppender to useSelect because it could be a new
// function on every render.
const hasAppender = CustomAppender !== false;
const hasCustomAppender = !! CustomAppender;
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the difference between these two flags?

Copy link
Member Author

@ellatrix ellatrix Feb 1, 2024

Choose a reason for hiding this comment

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

I hoped the const name was descriptive 😄
One is to check if there's an appender in general: either CustomAppender === undefined or a component
The other is to check if there's a custom appender defined: CustomAppender must be a function (truthy)

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the "undefined" is what tricked me

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we should set a default to true

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually that won't work because we have another check CustomAppender ? ...

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we could check boolean there, anyway I left it as is for now.

const {
order,
selectedBlocks,
visibleBlocks,
temporarilyEditingAsBlocks,
shouldRenderAppender,
} = useSelect(
( select ) => {
const {
getBlockOrder,
getSelectedBlockClientId,
getSelectedBlockClientIds,
__unstableGetVisibleBlocks,
__unstableGetTemporarilyEditingAsBlocks,
getTemplateLock,
getBlockEditingMode,
__unstableGetEditorMode,
} = select( blockEditorStore );
const selectedBlockClientId = getSelectedBlockClientId();
return {
order: getBlockOrder( rootClientId ),
selectedBlocks: getSelectedBlockClientIds(),
visibleBlocks: __unstableGetVisibleBlocks(),
temporarilyEditingAsBlocks:
__unstableGetTemporarilyEditingAsBlocks(),
shouldRenderAppender:
hasAppender &&
( hasCustomAppender
? ! getTemplateLock( rootClientId ) &&
getBlockEditingMode( rootClientId ) !== 'disabled' &&
__unstableGetEditorMode() !== 'zoom-out'
: rootClientId === selectedBlockClientId ||
( ! rootClientId && ! selectedBlockClientId ) ),
};
},
[ rootClientId, hasAppender, hasCustomAppender ]
);

return (
<LayoutProvider value={ layout }>
Expand All @@ -199,11 +221,13 @@ function Items( {
clientId={ temporarilyEditingAsBlocks }
/>
) }
<BlockListAppender
tagName={ __experimentalAppenderTagName }
rootClientId={ rootClientId }
renderAppender={ renderAppender }
/>
{ shouldRenderAppender && (
<BlockListAppender
tagName={ __experimentalAppenderTagName }
rootClientId={ rootClientId }
CustomAppender={ CustomAppender }
/>
) }
</LayoutProvider>
);
}
Expand Down
Loading