-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Show top level sections in List View #65202
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,8 @@ import { BlockSettingsDropdown } from '../block-settings-menu/block-settings-dro | |
import { focusListItem } from './utils'; | ||
import useClipboardHandler from './use-clipboard-handler'; | ||
|
||
import { unlock } from '../../lock-unlock'; | ||
|
||
const expanded = ( state, action ) => { | ||
if ( action.type === 'clear' ) { | ||
return {}; | ||
|
@@ -119,24 +121,36 @@ function ListViewComponent( | |
const blockIndexes = useListViewBlockIndexes( clientIdsTree ); | ||
|
||
const { getBlock } = useSelect( blockEditorStore ); | ||
const { visibleBlockCount, shouldShowInnerBlocks } = useSelect( | ||
( select ) => { | ||
const { | ||
getGlobalBlockCount, | ||
getClientIdsOfDescendants, | ||
__unstableGetEditorMode, | ||
} = select( blockEditorStore ); | ||
const draggedBlockCount = | ||
draggedClientIds?.length > 0 | ||
? getClientIdsOfDescendants( draggedClientIds ).length + 1 | ||
: 0; | ||
return { | ||
visibleBlockCount: getGlobalBlockCount() - draggedBlockCount, | ||
shouldShowInnerBlocks: __unstableGetEditorMode() !== 'zoom-out', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's allow inner blocks to show in Zoom Out mode because we need to see the "sections" which are inner blocks of the sectionRoot. |
||
}; | ||
}, | ||
[ draggedClientIds ] | ||
); | ||
|
||
const { sectionBlockClientIds, isZoomOutMode, visibleBlockCount } = | ||
useSelect( | ||
( select ) => { | ||
const { | ||
getGlobalBlockCount, | ||
getClientIdsOfDescendants, | ||
__unstableGetEditorMode, | ||
getSectionRootClientId, | ||
getBlockOrder, | ||
} = unlock( select( blockEditorStore ) ); | ||
const draggedBlockCount = | ||
draggedClientIds?.length > 0 | ||
? getClientIdsOfDescendants( draggedClientIds ).length + | ||
1 | ||
: 0; | ||
|
||
const _isZoomOutMode = __unstableGetEditorMode() === 'zoom-out'; | ||
|
||
return { | ||
visibleBlockCount: | ||
getGlobalBlockCount() - draggedBlockCount, | ||
sectionBlockClientIds: getBlockOrder( | ||
getSectionRootClientId() | ||
), | ||
isZoomOutMode: _isZoomOutMode, | ||
}; | ||
}, | ||
[ draggedClientIds ] | ||
); | ||
|
||
const { updateBlockSelection } = useBlockSelection(); | ||
|
||
|
@@ -305,6 +319,8 @@ function ListViewComponent( | |
setInsertedBlock, | ||
treeGridElementRef: elementRef, | ||
rootClientId, | ||
sectionBlockClientIds, | ||
isZoomOutMode, | ||
} ), | ||
[ | ||
blockDropPosition, | ||
|
@@ -322,6 +338,8 @@ function ListViewComponent( | |
insertedBlock, | ||
setInsertedBlock, | ||
rootClientId, | ||
sectionBlockClientIds, | ||
isZoomOutMode, | ||
] | ||
); | ||
|
||
|
@@ -397,7 +415,6 @@ function ListViewComponent( | |
fixedListWindow={ fixedListWindow } | ||
selectedClientIds={ selectedClientIds } | ||
isExpanded={ isExpanded } | ||
shouldShowInnerBlocks={ shouldShowInnerBlocks } | ||
showAppender={ showAppender } | ||
/> | ||
</ListViewContext.Provider> | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -114,6 +114,7 @@ export const getEnabledClientIdsTree = createSelector( | |||
state.blockEditingModes, | ||||
state.settings.templateLock, | ||||
state.blockListSettings, | ||||
state.editorMode, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List View depends on this selector to determine which clientIds to display. As it's memoized, when we switch editor modes we will need to recompute the value to ensure it's accurate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd really like a confidence check on this one. I would have thought that having the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @talldan This turns out to still be required otherwise List View doesn't not reflect the changes correctly. Going to have to continue digging to work out why though... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To define the dependencies of a selector, we need to just look what selectors it uses and what state it users So we need to look at. so It's a very hard selector to memoize IMO. We have options:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is missing a lot of dependencies, not just For now, I think we should keep things as-is to fix the obvious bug of not updating the list view correctly. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I came to the same conclusion about dependencies here.
Agreed.
On balance I'd say running with the fix in this PR would be an acceptable trade off. Would it help if I added a comment to the selector to note that it may be missing dependencies and referring to this discussion? That way if it becomes a problem in the future we know where to look. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's definitely acceptable to run with this PR, my worry is that one of the underlying selectors change and we don't update the dependencies here. It's indirect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. The only viable options seems to be:
It would be cool if you could declare a selector as dependency and then it would automatically use that selector's dependencies as dependencies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can kind of do that:
But I'm not sure it'd work here because of the way it depends on |
||||
] | ||||
); | ||||
|
||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding custom code to the inner parts of List View to filter out blocks, I think a better alternative might be to pass in a filtered block tree at the top of List View.
This approach is already used for filtering out disabled blocks. Some references:
useListViewClientIds
, this previously showed all blocks, but was updated to usegetEnabledClientIdsTree
that instead only shows enabled blocks -gutenberg/packages/block-editor/src/components/list-view/use-list-view-client-ids.js
Lines 25 to 26 in 07c7821
getEnabledClientIdsTree
, this is the selector -gutenberg/packages/block-editor/src/store/private-selectors.js
Lines 82 to 99 in 07c7821
My thinking is you could add another private selector
getZoomedOutClientIdsTree
and then switch to using that inuseListViewClientIds
whenever zoomed out is active.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I'm also not sure why
getEnabledClientIdsTree
is not enough. Does zoom out disable the inner block via some means that isn'tblockEditingMode
?Maybe that's the thing to solve, along the lines of what @youknowriad mentioned here - #65027 (comment) 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeh I agree. Great feedback. I'll take a look at that approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look and worked out that we don't need to do anything as
getEnabledClientIdsTree
already does what we want. I've updated the PR to reflect this.The problem is that
getEnabledClientIdsTree
is memozied and doesn't re-compute when we switch to/from Zoom Out mode unless we addstate.editorMode
to the dependencies array. This is what we discussed before.Why is this?
Surely switching to Zoom Out changes the blocks'
blockEditingModes
and asstate.blockEditingModes
is listed as a dependency of thegetEnabledClientIdsTree
createSelector
it should recompute when editor mode changes to Zoom Out?gutenberg/packages/block-editor/src/store/private-selectors.js
Lines 105 to 113 in b96f5a6
Wrong 😓
In fact that assumption turns out to be incorrect. We never actually update the
blockEditingModes
state (viasetBlockEditingMode
) when switching to Zoom Out mode.Instead the
getBlockEditingMode
selector determines the correctblockEditingMode
based on it's own internal logic rather than the state.gutenberg/packages/block-editor/src/store/selectors.js
Lines 2910 to 2925 in b96f5a6
This means that the
state.blockEditingModes
does not change which means that thestate.blockEditingModes
dependency does not causegetEnabledClientIdsTree
to recompute. This in turn means the List View does not update to reflect the change.I believe this is the reason why we also have
state.settings.templateLock
in the dependencies array as this is also used within thegetBlockEditingMode
selector:gutenberg/packages/block-editor/src/store/selectors.js
Lines 2935 to 2943 in b96f5a6
I thought perhaps wrapping
getBlockEditingMode
in acreateSelector
and addingstate.editorMode
to the dependencies would fix the issue. However, I believe this isn't a subscription dependency, it merely determines whether the selector cache should be invalidated the next time it is called. So it doesn't work.Therefore I think the only logical course for now is to add the
state.editorMode
to the dependencies list ofgetEnabledClientIdsTree
.I'd appreciate a confidence check on the above in case my logic is flawed 🙏
Yes I think that is highly relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that makes sense. Thanks for digging into the issue!