From 0dbac74adc7867045afba97c769cdd05aebdcb58 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Wed, 29 Sep 2021 15:52:12 -0700 Subject: [PATCH] List View: experiment with only rendering a fixed number of items --- bin/plugin/commands/performance.js | 18 +- .../src/components/list-view/block.js | 2 + .../src/components/list-view/branch.js | 181 +++++++++++++----- .../src/components/list-view/index.js | 39 +++- .../src/hooks/use-fixed-window-list/index.js | 159 +++++++++++++++ packages/compose/src/index.js | 1 + .../e2e-tests/config/performance-reporter.js | 16 ++ .../specs/performance/post-editor.test.js | 29 ++- .../specs/performance/site-editor.test.js | 11 +- 9 files changed, 397 insertions(+), 59 deletions(-) create mode 100644 packages/compose/src/hooks/use-fixed-window-list/index.js diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index 6bb98ec4646bb..96f8674098ea7 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -40,6 +40,7 @@ const config = require( '../config' ); * @property {number[]} inserterOpen Average time to open global inserter. * @property {number[]} inserterSearch Average time to search the inserter. * @property {number[]} inserterHover Average time to move mouse between two block item in the inserter. + * @property {number[]} listViewOpen Average time to open listView */ /** @@ -52,7 +53,7 @@ const config = require( '../config' ); * @property {number=} firstContentfulPaint Represents the time when the browser first renders any text or media. * @property {number=} firstBlock Represents the time when Puppeteer first sees a block selector in the DOM. * @property {number=} type Average type time. - * @property {number=} minType Minium type time. + * @property {number=} minType Minimum type time. * @property {number=} maxType Maximum type time. * @property {number=} focus Average block selection time. * @property {number=} minFocus Min block selection time. @@ -66,6 +67,9 @@ const config = require( '../config' ); * @property {number=} inserterHover Average time to move mouse between two block item in the inserter. * @property {number=} minInserterHover Min time to move mouse between two block item in the inserter. * @property {number=} maxInserterHover Max time to move mouse between two block item in the inserter. + * @property {number=} listViewOpen Average time to open list view. + * @property {number=} minListViewOpen Min time to open list view. + * @property {number=} maxListViewOpen Max time to open list view. */ /** @@ -136,6 +140,9 @@ function curateResults( results ) { inserterHover: average( results.inserterHover ), minInserterHover: Math.min( ...results.inserterHover ), maxInserterHover: Math.max( ...results.inserterHover ), + listViewOpen: average( results.listViewOpen ), + minListViewOpen: Math.min( ...results.listViewOpen ), + maxListViewOpen: Math.max( ...results.listViewOpen ), }; } @@ -378,6 +385,15 @@ async function runPerformanceTests( branches, options ) { maxInserterHover: rawResults.map( ( r ) => r[ branch ].maxInserterHover ), + listViewOpen: rawResults.map( + ( r ) => r[ branch ].listViewOpen + ), + minListViewOpen: rawResults.map( + ( r ) => r[ branch ].minListViewOpen + ), + maxListViewOpen: rawResults.map( + ( r ) => r[ branch ].maxListViewOpen + ), }, median ); diff --git a/packages/block-editor/src/components/list-view/block.js b/packages/block-editor/src/components/list-view/block.js index 97be704702f6e..c4987d59ee83f 100644 --- a/packages/block-editor/src/components/list-view/block.js +++ b/packages/block-editor/src/components/list-view/block.js @@ -39,6 +39,7 @@ export default function ListViewBlock( { showBlockMovers, path, isExpanded, + style, } ) { const cellRef = useRef( null ); const [ isHovered, setIsHovered ] = useState( false ); @@ -184,6 +185,7 @@ export default function ListViewBlock( { className="block-editor-list-view-block__contents-cell" colSpan={ colSpan } ref={ cellRef } + style={ style } > { ( { ref, tabIndex, onFocus } ) => (
diff --git a/packages/block-editor/src/components/list-view/branch.js b/packages/block-editor/src/components/list-view/branch.js index 222fbad712b59..3d262910f55be 100644 --- a/packages/block-editor/src/components/list-view/branch.js +++ b/packages/block-editor/src/components/list-view/branch.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { map, compact } from 'lodash'; +import { compact } from 'lodash'; /** * WordPress dependencies @@ -14,6 +14,38 @@ import { Fragment } from '@wordpress/element'; import ListViewBlock from './block'; import { useListViewContext } from './context'; +function countBlocks( block, expandedState, draggedClientIds ) { + const isDragged = draggedClientIds?.includes( block.clientId ); + if ( isDragged ) { + return 0; + } + const isExpanded = expandedState[ block.clientId ] ?? true; + if ( isExpanded ) { + return ( + 1 + + block.innerBlocks.reduce( + countReducer( expandedState, draggedClientIds ), + 0 + ) + ); + } + return 1; +} +const countReducer = ( expandedState, draggedClientIds ) => ( + count, + block +) => { + const isDragged = draggedClientIds?.includes( block.clientId ); + if ( isDragged ) { + return count; + } + const isExpanded = expandedState[ block.clientId ] ?? true; + if ( isExpanded && block.innerBlocks.length > 0 ) { + return count + countBlocks( block, expandedState, draggedClientIds ); + } + return count + 1; +}; + export default function ListViewBranch( props ) { const { blocks, @@ -22,62 +54,109 @@ export default function ListViewBranch( props ) { showNestedBlocks, level = 1, path = '', + listPosition = 0, + fixedListWindow, } = props; - const { expandedState, draggedClientIds } = useListViewContext(); + const { + expandedState, + draggedClientIds, + __experimentalPersistentListViewFeatures, + } = useListViewContext(); const filteredBlocks = compact( blocks ); const blockCount = filteredBlocks.length; + let nextPosition = listPosition; + + const listItems = []; + for ( let index = 0; index < filteredBlocks.length; index++ ) { + const block = filteredBlocks[ index ]; + const { clientId, innerBlocks } = block; + + if ( index > 0 ) { + nextPosition += countBlocks( + filteredBlocks[ index - 1 ], + expandedState, + draggedClientIds + ); + } + + const usesWindowing = __experimentalPersistentListViewFeatures; + const { + start, + end, + itemInView, + startPadding, + endPadding, + } = fixedListWindow; + + const blockInView = ! usesWindowing || itemInView( nextPosition ); + + const isDragging = draggedClientIds?.length > 0; + if ( + usesWindowing && + ! isDragging && + ! blockInView && + nextPosition > start + ) { + // found the end of the window, don't bother processing the rest of the items + break; + } + const style = usesWindowing + ? { + paddingTop: start === nextPosition ? startPadding : 0, + paddingBottom: end === nextPosition ? endPadding : 0, + } + : undefined; + + const position = index + 1; + const updatedPath = + path.length > 0 ? `${ path }_${ position }` : `${ position }`; + const hasNestedBlocks = + showNestedBlocks && !! innerBlocks && !! innerBlocks.length; + + const isExpanded = hasNestedBlocks + ? expandedState[ clientId ] ?? true + : undefined; + + // Make updates to the selected or dragged blocks synchronous, + // but asynchronous for any other block. + const isDragged = !! draggedClientIds?.includes( clientId ); - return ( - <> - { map( filteredBlocks, ( block, index ) => { - const { clientId, innerBlocks } = block; - const position = index + 1; - // This string value is used to trigger an animation change. - // This may be removed if we use a different animation library in the future. - const updatedPath = - path.length > 0 - ? `${ path }_${ position }` - : `${ position }`; - const hasNestedBlocks = - showNestedBlocks && !! innerBlocks && !! innerBlocks.length; - - const isExpanded = hasNestedBlocks - ? expandedState[ clientId ] ?? true - : undefined; - - const isDragged = !! draggedClientIds?.includes( clientId ); - - return ( - - - { hasNestedBlocks && isExpanded && ! isDragged && ( - - ) } - - ); - } ) } - - ); + listItems.push( + + { ( isDragged || blockInView ) && ( + + ) } + { hasNestedBlocks && isExpanded && ! isDragged && ( + + ) } + + ); + } + return <>{ listItems }; } ListViewBranch.defaultProps = { diff --git a/packages/block-editor/src/components/list-view/index.js b/packages/block-editor/src/components/list-view/index.js index 8ce9f492b44ce..7f8379a53a3ac 100644 --- a/packages/block-editor/src/components/list-view/index.js +++ b/packages/block-editor/src/components/list-view/index.js @@ -1,10 +1,12 @@ /** * WordPress dependencies */ - -import { useMergeRefs } from '@wordpress/compose'; +import { + useMergeRefs, + __experimentalUseFixedWindowList as useFixedWindowList, +} from '@wordpress/compose'; import { __experimentalTreeGrid as TreeGrid } from '@wordpress/components'; -import { AsyncModeProvider, useDispatch } from '@wordpress/data'; +import { AsyncModeProvider, useDispatch, useSelect } from '@wordpress/data'; import { useCallback, useEffect, @@ -67,6 +69,21 @@ function ListView( ) { const { clientIdsTree, draggedClientIds } = useListViewClientIds( blocks ); const { selectBlock } = useDispatch( blockEditorStore ); + const { visibleBlockCount } = useSelect( + ( select ) => { + const { getGlobalBlockCount, getClientIdsOfDescendants } = select( + blockEditorStore + ); + const draggedBlockCount = + draggedClientIds?.length > 0 + ? getClientIdsOfDescendants( draggedClientIds ).length + 1 + : 0; + return { + visibleBlockCount: getGlobalBlockCount() - draggedBlockCount, + }; + }, + [ draggedClientIds ] + ); const selectEditorBlock = useCallback( ( clientId ) => { selectBlock( clientId ); @@ -85,6 +102,16 @@ function ListView( isMounted.current = true; }, [] ); + const [ fixedListWindow ] = useFixedWindowList( + elementRef, + 36, + visibleBlockCount, + { + windowOverscan: 1, + useWindowing: __experimentalPersistentListViewFeatures, + } + ); + const expand = useCallback( ( clientId ) => { if ( ! clientId ) { @@ -151,6 +178,11 @@ function ListView( ref={ treeGridRef } onCollapseRow={ collapseRow } onExpandRow={ expandRow } + aria-rowcount={ + __experimentalPersistentListViewFeatures + ? visibleBlockCount + : undefined + } > diff --git a/packages/compose/src/hooks/use-fixed-window-list/index.js b/packages/compose/src/hooks/use-fixed-window-list/index.js new file mode 100644 index 0000000000000..d47b1cbf109f5 --- /dev/null +++ b/packages/compose/src/hooks/use-fixed-window-list/index.js @@ -0,0 +1,159 @@ +/** + * External dependencies + */ +import { throttle } from 'lodash'; + +/** + * WordPress dependencies + */ +import { useState, useLayoutEffect } from '@wordpress/element'; +import { getScrollContainer } from '@wordpress/dom'; +import { PAGEUP, PAGEDOWN, HOME, END } from '@wordpress/keycodes'; + +const DEFAULT_INIT_WINDOW_SIZE = 30; + +/** + * @typedef {Object} WPFixedWindowList + * + * @property {number} visibleItems Items visible in the current viewport + * @property {number} start Start index of the window + * @property {number} end End index of the window + * @property {(index:number)=>boolean} itemInView Returns true if item is in the window + * @property {number} startPadding Padding in px to add before the start item + * @property {number} endPadding Padding in px to add after the end item + */ + +/** + * @typedef {Object} WPFixedWindowListOptions + * + * @property {number} [windowOverscan] Renders windowOverscan number of items before and after the calculated visible window. + * @property {boolean} [useWindowing] When false avoids calculating the window size + * @property {number} [initWindowSize] Initial window size to use on first render before we can calculate the window size. + */ + +/** + * + * @param {import('react').RefObject} elementRef Used to find the closest scroll container that contains element. + * @param { number } itemHeight Fixed item height in pixels + * @param { number } totalItems Total items in list + * @param { WPFixedWindowListOptions } [options] Options object + * @return {[ WPFixedWindowList, setFixedListWindow:(nextWindow:WPFixedWindowList)=>void]} Array with the fixed window list and setter + */ +export default function useFixedWindowList( + elementRef, + itemHeight, + totalItems, + options +) { + const initWindowSize = options?.initWindowSize ?? DEFAULT_INIT_WINDOW_SIZE; + const useWindowing = options?.useWindowing ?? true; + + const [ fixedListWindow, setFixedListWindow ] = useState( { + visibleItems: initWindowSize, + start: 0, + end: initWindowSize, + itemInView: ( /** @type {number} */ index ) => { + return index >= 0 && index <= initWindowSize; + }, + startPadding: 0, + endPadding: 0, + } ); + + useLayoutEffect( () => { + if ( ! useWindowing ) { + return; + } + const scrollContainer = getScrollContainer( elementRef.current ); + const measureWindow = () => { + if ( ! scrollContainer ) { + return; + } + const visibleItems = Math.ceil( + scrollContainer.clientHeight / itemHeight + ); + const windowOverscan = options?.windowOverscan ?? visibleItems; + const start = Math.max( + 0, + Math.floor( scrollContainer.scrollTop / itemHeight ) - + windowOverscan + ); + const end = Math.min( + totalItems - 1, + start + visibleItems + windowOverscan + ); + setFixedListWindow( { + visibleItems, + start, + end, + itemInView: ( index ) => { + return start <= index && index <= end; + }, + startPadding: itemHeight * start, + endPadding: + totalItems > end + ? itemHeight * ( totalItems - end - 1 ) + : 0, + } ); + }; + const handleKeyDown = ( /** @type {KeyboardEvent} */ event ) => { + switch ( event.keyCode ) { + case HOME: { + return scrollContainer?.scrollTo( { top: 0 } ); + } + case END: { + return scrollContainer?.scrollTo( { + top: totalItems * itemHeight, + } ); + } + case PAGEUP: { + return scrollContainer?.scrollTo( { + top: + scrollContainer.scrollTop - + fixedListWindow.visibleItems * itemHeight, + } ); + } + case PAGEDOWN: { + return scrollContainer?.scrollTo( { + top: + scrollContainer.scrollTop + + fixedListWindow.visibleItems * itemHeight, + } ); + } + } + }; + + measureWindow(); + const throttleMeasureList = throttle( () => { + measureWindow(); + }, 16 ); + scrollContainer?.addEventListener( 'scroll', throttleMeasureList ); + scrollContainer?.ownerDocument?.defaultView?.addEventListener( + 'resize', + throttleMeasureList + ); + scrollContainer?.ownerDocument?.defaultView?.addEventListener( + 'resize', + throttleMeasureList + ); + scrollContainer?.ownerDocument?.defaultView?.addEventListener( + 'keydown', + handleKeyDown + ); + return () => { + scrollContainer?.removeEventListener( + 'scroll', + throttleMeasureList + ); + scrollContainer?.ownerDocument?.defaultView?.removeEventListener( + 'resize', + throttleMeasureList + ); + scrollContainer?.ownerDocument?.defaultView?.removeEventListener( + 'keydown', + handleKeyDown + ); + }; + }, [ totalItems, itemHeight, elementRef ] ); + + return [ fixedListWindow, setFixedListWindow ]; +} diff --git a/packages/compose/src/index.js b/packages/compose/src/index.js index 8f7a1022cf7b1..2ce3a2ab33f6f 100644 --- a/packages/compose/src/index.js +++ b/packages/compose/src/index.js @@ -37,3 +37,4 @@ export { default as useMergeRefs } from './hooks/use-merge-refs'; export { default as useRefEffect } from './hooks/use-ref-effect'; export { default as __experimentalUseDropZone } from './hooks/use-drop-zone'; export { default as useFocusableIframe } from './hooks/use-focusable-iframe'; +export { default as __experimentalUseFixedWindowList } from './hooks/use-fixed-window-list'; diff --git a/packages/e2e-tests/config/performance-reporter.js b/packages/e2e-tests/config/performance-reporter.js index 6c314917f8ac2..549855bf29713 100644 --- a/packages/e2e-tests/config/performance-reporter.js +++ b/packages/e2e-tests/config/performance-reporter.js @@ -37,6 +37,7 @@ class PerformanceReporter { firstBlock, type, focus, + listViewOpen, inserterOpen, inserterHover, inserterSearch, @@ -90,6 +91,21 @@ Fastest time to select a block: ${ success( ) }` ); } + if ( listViewOpen && listViewOpen.length ) { + // eslint-disable-next-line no-console + console.log( ` +${ title( 'Opening List View Performance:' ) } +Average time to open list view: ${ success( + round( average( listViewOpen ) ) + 'ms' + ) } +Slowest time to open list view: ${ success( + round( Math.max( ...listViewOpen ) ) + 'ms' + ) } +Fastest time to open list view: ${ success( + round( Math.min( ...listViewOpen ) ) + 'ms' + ) }` ); + } + if ( inserterOpen && inserterOpen.length ) { // eslint-disable-next-line no-console console.log( ` diff --git a/packages/e2e-tests/specs/performance/post-editor.test.js b/packages/e2e-tests/specs/performance/post-editor.test.js index f9862f18c9448..fa1893923a29f 100644 --- a/packages/e2e-tests/specs/performance/post-editor.test.js +++ b/packages/e2e-tests/specs/performance/post-editor.test.js @@ -11,9 +11,10 @@ import { sum } from 'lodash'; import { createNewPost, saveDraft, - insertBlock, openGlobalBlockInserter, closeGlobalBlockInserter, + openListView, + closeListView, } from '@wordpress/e2e-test-utils'; /** @@ -41,6 +42,7 @@ describe( 'Post Editor Performance', () => { firstBlock: [], type: [], focus: [], + listViewOpen: [], inserterOpen: [], inserterHover: [], inserterSearch: [], @@ -118,7 +120,9 @@ describe( 'Post Editor Performance', () => { it( 'Typing', async () => { // Measuring typing performance - await insertBlock( 'Paragraph' ); + await openListView(); + await page.click( '.edit-post-visual-editor__post-title-wrapper' ); + await page.keyboard.press( 'Enter' ); let i = 20; await page.tracing.start( { path: traceFile, @@ -157,6 +161,7 @@ describe( 'Post Editor Performance', () => { it( 'Selecting blocks', async () => { // Measuring block selection performance await createNewPost(); + await openListView(); await page.evaluate( () => { const { createBlock } = window.wp.blocks; const { dispatch } = window.wp.data; @@ -184,6 +189,26 @@ describe( 'Post Editor Performance', () => { results.focus = focusEvents; } ); + it( 'Opening persistent list view', async () => { + // Measure time to open inserter + await page.waitForSelector( '.edit-post-layout' ); + for ( let j = 0; j < 10; j++ ) { + await page.tracing.start( { + path: traceFile, + screenshots: false, + categories: [ 'devtools.timeline' ], + } ); + await openListView(); + await page.tracing.stop(); + traceResults = JSON.parse( readFile( traceFile ) ); + const [ mouseClickEvents ] = getClickEventDurations( traceResults ); + for ( let k = 0; k < mouseClickEvents.length; k++ ) { + results.listViewOpen.push( mouseClickEvents[ k ] ); + } + await closeListView(); + } + } ); + it( 'Opening the inserter', async () => { // Measure time to open inserter await page.waitForSelector( '.edit-post-layout' ); diff --git a/packages/e2e-tests/specs/performance/site-editor.test.js b/packages/e2e-tests/specs/performance/site-editor.test.js index 80fa8cecc3c95..0b7196fb805dc 100644 --- a/packages/e2e-tests/specs/performance/site-editor.test.js +++ b/packages/e2e-tests/specs/performance/site-editor.test.js @@ -13,7 +13,7 @@ import { canvas, createNewPost, saveDraft, - insertBlock, + openListView, } from '@wordpress/e2e-test-utils'; /** @@ -55,6 +55,7 @@ describe( 'Site Editor Performance', () => { inserterOpen: [], inserterHover: [], inserterSearch: [], + listViewOpen: [], }; const html = readFile( @@ -117,7 +118,13 @@ describe( 'Site Editor Performance', () => { await canvas().click( '[data-type="core/post-content"] [data-type="core/paragraph"]' ); - await insertBlock( 'Paragraph' ); + await openListView(); + await canvas().click( + '[data-type="core/post-content"] [data-type="core/paragraph"]' + ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.press( 'ArrowUp' ); i = 200; const traceFile = __dirname + '/trace.json'; await page.tracing.start( {