Skip to content

Commit

Permalink
Revert "Avoid calling redux actions constantly when moving the mouse …
Browse files Browse the repository at this point in the history
…or scrolling (#44325)"

This reverts commit 1703569.
  • Loading branch information
ellatrix committed Oct 3, 2022
1 parent 39b69c0 commit 6bd9134
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 83 deletions.
48 changes: 6 additions & 42 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,9 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
AsyncModeProvider,
useSelect,
useDispatch,
useRegistry,
} from '@wordpress/data';
import {
useViewportMatch,
useMergeRefs,
useDebounce,
} from '@wordpress/compose';
import {
createContext,
useState,
useMemo,
useCallback,
} from '@wordpress/element';
import { AsyncModeProvider, useSelect, useDispatch } from '@wordpress/data';
import { useViewportMatch, useMergeRefs } from '@wordpress/compose';
import { createContext, useState, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -44,7 +30,6 @@ import {
const elementContext = createContext();

export const IntersectionObserver = createContext();
const pendingBlockVisibilityUpdatesPerRegistry = new WeakMap();

function Root( { className, ...settings } ) {
const [ element, setElement ] = useState();
Expand All @@ -62,24 +47,7 @@ function Root( { className, ...settings } ) {
},
[]
);
const registry = useRegistry();
const { setBlockVisibility } = useDispatch( blockEditorStore );

const delayedBlockVisibilityUpdates = useDebounce(
useCallback( () => {
const updates = {};
pendingBlockVisibilityUpdatesPerRegistry
.get( registry )
.forEach( ( [ id, isIntersecting ] ) => {
updates[ id ] = isIntersecting;
} );
setBlockVisibility( updates );
}, [ registry ] ),
300,
{
trailing: true,
}
);
const intersectionObserver = useMemo( () => {
const { IntersectionObserver: Observer } = window;

Expand All @@ -88,16 +56,12 @@ function Root( { className, ...settings } ) {
}

return new Observer( ( entries ) => {
if ( ! pendingBlockVisibilityUpdatesPerRegistry.get( registry ) ) {
pendingBlockVisibilityUpdatesPerRegistry.set( registry, [] );
}
const updates = {};
for ( const entry of entries ) {
const clientId = entry.target.getAttribute( 'data-block' );
pendingBlockVisibilityUpdatesPerRegistry
.get( registry )
.push( [ clientId, entry.isIntersecting ] );
updates[ clientId ] = entry.isIntersecting;
}
delayedBlockVisibilityUpdates();
setBlockVisibility( updates );
} );
}, [] );
const innerBlocksProps = useInnerBlocksProps(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* WordPress dependencies
*/
import { useRefEffect, useDebounce } from '@wordpress/compose';
import { useRefEffect } from '@wordpress/compose';

import { useSelect, useDispatch } from '@wordpress/data';
import { useContext } from '@wordpress/element';

Expand Down Expand Up @@ -32,10 +33,6 @@ export function useInBetweenInserter() {
const { showInsertionPoint, hideInsertionPoint } =
useDispatch( blockEditorStore );

const delayedShowInsertionPoint = useDebounce( showInsertionPoint, 500, {
trailing: true,
} );

return useRefEffect(
( node ) => {
if ( isInBetweenInserterDisabled ) {
Expand All @@ -56,7 +53,6 @@ export function useInBetweenInserter() {
'block-editor-block-list__layout'
)
) {
delayedShowInsertionPoint.cancel();
if ( isBlockInsertionPointVisible() ) {
hideInsertionPoint();
}
Expand Down Expand Up @@ -138,7 +134,6 @@ export function useInBetweenInserter() {
( event.clientX > elementRect.right ||
event.clientX < elementRect.left ) )
) {
delayedShowInsertionPoint.cancel();
if ( isBlockInsertionPointVisible() ) {
hideInsertionPoint();
}
Expand All @@ -150,14 +145,13 @@ export function useInBetweenInserter() {
// Don't show the in-between inserter before the first block in
// the list (preserves the original behaviour).
if ( index === 0 ) {
delayedShowInsertionPoint.cancel();
if ( isBlockInsertionPointVisible() ) {
hideInsertionPoint();
}
return;
}

delayedShowInsertionPoint( rootClientId, index, {
showInsertionPoint( rootClientId, index, {
__unstableWithInserter: true,
} );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ function InsertionPointPopover( {
...( ! isVertical ? horizontalLine.rest : verticalLine.rest ),
opacity: 1,
borderRadius: '2px',
transition: { delay: isInserterShown ? 0.1 : 0, type: 'tween' },
transition: { delay: isInserterShown ? 0.5 : 0, type: 'tween' },
},
hover: {
...( ! isVertical ? horizontalLine.hover : verticalLine.hover ),
opacity: 1,
borderRadius: '2px',
transition: { delay: 0.1, type: 'tween' },
transition: { delay: 0.5, type: 'tween' },
},
};

Expand All @@ -165,7 +165,7 @@ function InsertionPointPopover( {
},
rest: {
scale: 1,
transition: { type: 'tween' },
transition: { delay: 0.4, type: 'tween' },
},
};

Expand Down
36 changes: 14 additions & 22 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ const withBlockReset = ( reducer ) => ( state, action ) => {
order: mapBlockOrder( action.blocks ),
parents: mapBlockParents( action.blocks ),
controlledInnerBlocks: {},
visibility: {},
};

const subTree = buildBlockTree( newState, action.blocks );
Expand Down Expand Up @@ -1161,6 +1162,17 @@ export const blocks = pipe(
}
return state;
},

visibility( state = {}, action ) {
if ( action.type === 'SET_BLOCK_VISIBILITY' ) {
return {
...state,
...action.updates,
};
}

return state;
},
} );

/**
Expand Down Expand Up @@ -1203,25 +1215,6 @@ export function draggedBlocks( state = [], action ) {
return state;
}

/**
* Reducer tracking the visible blocks.
*
* @param {Record<string,boolean>} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Record<string,boolean>} Block visibility.
*/
export function blockVisibility( state = {}, action ) {
if ( action.type === 'SET_BLOCK_VISIBILITY' ) {
return {
...state,
...action.updates,
};
}

return state;
}

/**
* Internal helper reducer for selectionStart and selectionEnd. Can hold a block
* selection, represented by an object with property clientId.
Expand Down Expand Up @@ -1667,7 +1660,7 @@ export function hasBlockMovingClientId( state = null, action ) {
*
* @return {[string,Object]} Updated state.
*/
export function lastBlockAttributesChange( state = null, action ) {
export function lastBlockAttributesChange( state, action ) {
switch ( action.type ) {
case 'UPDATE_BLOCK':
if ( ! action.updates.attributes ) {
Expand All @@ -1688,7 +1681,7 @@ export function lastBlockAttributesChange( state = null, action ) {
);
}

return state;
return null;
}

/**
Expand Down Expand Up @@ -1820,5 +1813,4 @@ export default combineReducers( {
highlightedBlock,
lastBlockInserted,
temporarilyEditingAsBlocks,
blockVisibility,
} );
8 changes: 4 additions & 4 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2670,7 +2670,7 @@ export function wasBlockJustInserted( state, clientId, source ) {
* @return {boolean} True if the block is visible.
*/
export function isBlockVisible( state, clientId ) {
return state.blockVisibility?.[ clientId ] ?? true;
return state.blocks.visibility?.[ clientId ] ?? true;
}

/**
Expand All @@ -2682,12 +2682,12 @@ export function isBlockVisible( state, clientId ) {
export const __unstableGetVisibleBlocks = createSelector(
( state ) => {
return new Set(
Object.keys( state.blockVisibility ).filter(
( key ) => state.blockVisibility[ key ]
Object.keys( state.blocks.visibility ).filter(
( key ) => state.blocks.visibility[ key ]
)
);
},
( state ) => [ state.blockVisibility ]
( state ) => [ state.blocks.visibility ]
);

export const __unstableGetContentLockingParent = createSelector(
Expand Down
17 changes: 17 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ describe( 'state', () => {
chicken: '',
},
controlledInnerBlocks: {},
visibility: {},
} );
expect( state.tree.chicken ).not.toBe(
existingState.tree.chicken
Expand Down Expand Up @@ -374,6 +375,7 @@ describe( 'state', () => {
chicken: '',
},
controlledInnerBlocks: {},
visibility: {},
} );
expect( state.tree.chicken ).not.toBe(
existingState.tree.chicken
Expand Down Expand Up @@ -523,6 +525,7 @@ describe( 'state', () => {
[ newChildBlockId3 ]: 'chicken',
},
controlledInnerBlocks: {},
visibility: {},
} );

expect( state.tree[ '' ].innerBlocks[ 0 ] ).toBe(
Expand Down Expand Up @@ -632,6 +635,7 @@ describe( 'state', () => {
[ newChildBlockId ]: 'chicken',
},
controlledInnerBlocks: {},
visibility: {},
} );

// The block object of the parent should be updated.
Expand All @@ -653,6 +657,7 @@ describe( 'state', () => {
isIgnoredChange: false,
tree: {},
controlledInnerBlocks: {},
visibility: {},
} );
} );

Expand Down Expand Up @@ -3080,6 +3085,18 @@ describe( 'state', () => {
'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1': { food: 'banana' },
} );
} );

it( 'returns null on anything other than block attributes update', () => {
const original = deepFreeze( {
'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1': { food: 'banana' },
} );

const state = lastBlockAttributesChange( original, {
type: '__INERT__',
} );

expect( state ).toBe( null );
} );
} );

describe( 'lastBlockInserted', () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/e2e-tests/specs/editor/plugins/cpt-locking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ describe( 'cpt locking', () => {

it( 'should not allow blocks to be inserted in inner blocks', async () => {
await page.click( 'button[aria-label="Two columns; equal split"]' );
await page.evaluate(
() => new Promise( window.requestIdleCallback )
);
expect(
await page.$(
'.wp-block-column .block-editor-button-block-appender'
Expand Down

0 comments on commit 6bd9134

Please sign in to comment.