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

Fix insertion point showing up unexpectedly #44702

Merged
merged 3 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 0 additions & 4 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1516,10 +1516,6 @@ _Parameters_
- _index_ `?number`: Index at which block should be inserted.
- _\_\_unstableOptions_ `Object`: Whether or not to show an inserter button.

_Returns_

- `Object`: Action object.

### startDraggingBlocks

Returns an action object used in signalling that the user has begun to drag blocks.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* 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 +32,6 @@ export function useInBetweenInserter() {
const { showInsertionPoint, hideInsertionPoint } =
useDispatch( blockEditorStore );

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

return useRefEffect(
( node ) => {
if ( isInBetweenInserterDisabled ) {
Expand All @@ -56,10 +52,7 @@ export function useInBetweenInserter() {
'block-editor-block-list__layout'
)
) {
delayedShowInsertionPoint.cancel();
if ( isBlockInsertionPointVisible() ) {
hideInsertionPoint();
}
hideInsertionPoint();
return;
}

Expand Down Expand Up @@ -138,10 +131,7 @@ export function useInBetweenInserter() {
( event.clientX > elementRect.right ||
event.clientX < elementRect.left ) )
) {
delayedShowInsertionPoint.cancel();
if ( isBlockInsertionPointVisible() ) {
hideInsertionPoint();
}
hideInsertionPoint();
return;
}

Expand All @@ -150,15 +140,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();
}
hideInsertionPoint();
return;
}

delayedShowInsertionPoint( rootClientId, index, {
showInsertionPoint( rootClientId, index, {
__unstableWithInserter: true,
delay: 500,
} );
}

Expand Down
52 changes: 34 additions & 18 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,40 +621,56 @@ export const insertBlocks =
}
};

const delayedInsertionPointCalls = new WeakMap();

/**
* Action that shows the insertion point.
*
* @param {?string} rootClientId Optional root client ID of block list on
* which to insert.
* @param {?number} index Index at which block should be inserted.
* @param {Object} __unstableOptions Whether or not to show an inserter button.
*
* @return {Object} Action object.
*/
export function showInsertionPoint(
rootClientId,
index,
__unstableOptions = {}
) {
const { __unstableWithInserter } = __unstableOptions;
return {
type: 'SHOW_INSERTION_POINT',
rootClientId,
index,
__unstableWithInserter,
export const showInsertionPoint =
( rootClientId, index, __unstableOptions = {} ) =>
( { dispatch, registry } ) => {
const previousCall = delayedInsertionPointCalls.get( registry );
if ( previousCall ) {
clearTimeout( previousCall );
}
const { __unstableWithInserter, delay } = __unstableOptions;
delayedInsertionPointCalls.set(
registry,
setTimeout( () => {
dispatch( {
type: 'SHOW_INSERTION_POINT',
rootClientId,
index,
__unstableWithInserter,
} );
}, delay )
);
};
}

/**
* Action that hides the insertion point.
*
* @return {Object} Action object.
*/
export function hideInsertionPoint() {
return {
type: 'HIDE_INSERTION_POINT',
export const hideInsertionPoint =
() =>
( { select, dispatch, registry } ) => {
const previousCall = delayedInsertionPointCalls.get( registry );
if ( previousCall ) {
clearTimeout( previousCall );
}
if ( ! select.isBlockInsertionPointVisible() ) {
return;
}
dispatch( {
type: 'HIDE_INSERTION_POINT',
} );
};
}

/**
* Action that resets the template validity.
Expand Down