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

[RNMobile] Fix a crash related to Reanimated when closing the editor #52320

Merged
merged 2 commits into from
Jul 6, 2023
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 @@ -109,8 +109,12 @@ const BlockDraggableWrapper = ( { children, isRTL } ) => {
draggingScrollHandler( event );
};

const { onBlockDragOver, onBlockDragEnd, onBlockDrop, targetBlockIndex } =
useBlockDropZone();
const {
onBlockDragOverWorklet,
onBlockDragEnd,
onBlockDrop,
targetBlockIndex,
} = useBlockDropZone();

// Stop dragging blocks if the block draggable is unmounted.
useEffect( () => {
Expand Down Expand Up @@ -184,7 +188,7 @@ const BlockDraggableWrapper = ( { children, isRTL } ) => {
chip.y.value = dragPosition.y;
currentYPosition.value = dragPosition.y;

runOnJS( onBlockDragOver )( { x, y: y + scroll.offsetY.value } );
onBlockDragOverWorklet( { x, y: y + scroll.offsetY.value } );

// Update scrolling velocity
scrollOnDragOver( dragPosition.y );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/**
* External dependencies
*/
import { useSharedValue } from 'react-native-reanimated';
import {
runOnJS,
useDerivedValue,
useSharedValue,
} from 'react-native-reanimated';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { useThrottle } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -18,6 +21,8 @@ import { useBlockListContext } from '../block-list/block-list-context';
import { getDistanceToNearestEdge } from '../../utils/math';
import useOnBlockDrop from '../use-on-block-drop';

const UPDATE_TARGET_BLOCK_INDEX_THRESHOLD = 20; // In pixels

/** @typedef {import('../../utils/math').WPPoint} WPPoint */

/**
Expand Down Expand Up @@ -111,50 +116,82 @@ export default function useBlockDropZone( {
rootClientId: targetRootClientId = '',
} = {} ) {
const targetBlockIndex = useSharedValue( null );
const dragPosition = {
x: useSharedValue( 0 ),
y: useSharedValue( 0 ),
};
const prevDragPosition = {
x: useSharedValue( 0 ),
y: useSharedValue( 0 ),
};

const { getBlockListSettings, getSettings } = useSelect( blockEditorStore );
const { blocksLayouts, getBlockLayoutsOrderedByYCoord } =
useBlockListContext();

const getSortedBlocksLayouts = useCallback( () => {
return getBlockLayoutsOrderedByYCoord( blocksLayouts.current );
// We use the value of `blocksLayouts` as the dependency.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ blocksLayouts.current ] );
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved

const isRTL = getSettings().isRTL;

const onBlockDrop = useOnBlockDrop();

const throttled = useThrottle(
useCallback(
( event ) => {
const sortedBlockLayouts = getSortedBlocksLayouts();

const targetIndex = getNearestBlockIndex(
sortedBlockLayouts,
{ x: event.x, y: event.y },
getBlockListSettings( targetRootClientId )?.orientation,
isRTL
);
if ( targetIndex !== null ) {
targetBlockIndex.value = targetIndex ?? 0;
}
},
[
getSortedBlocksLayouts,
getNearestBlockIndex,
getBlockListSettings,
targetBlockIndex,
]
),
200
const updateTargetBlockIndex = useCallback(
( event ) => {
const sortedBlockLayouts = getSortedBlocksLayouts();

const targetIndex = getNearestBlockIndex(
sortedBlockLayouts,
{ x: event.x, y: event.y },
getBlockListSettings( targetRootClientId )?.orientation,
isRTL
);
if ( targetIndex !== null ) {
targetBlockIndex.value = targetIndex ?? 0;
}
},
[
getSortedBlocksLayouts,
getBlockListSettings,
targetRootClientId,
isRTL,
targetBlockIndex,
]
);

useDerivedValue( () => {
const x = dragPosition.x.value;
const y = dragPosition.y.value;
const prevX = prevDragPosition.x.value;
const prevY = prevDragPosition.y.value;
// `updateTargetBlockIndex` performs expensive calculations, so we throttle
// the call using a offset threshold based on the dragging position.
if (
Math.abs( x - prevX ) >= UPDATE_TARGET_BLOCK_INDEX_THRESHOLD ||
Math.abs( y - prevY ) >= UPDATE_TARGET_BLOCK_INDEX_THRESHOLD
Comment on lines +173 to +174
Copy link
Member

Choose a reason for hiding this comment

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

Cool and nice approach!

) {
runOnJS( updateTargetBlockIndex )( { x, y } );
prevDragPosition.x.value = x;
prevDragPosition.y.value = y;
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
} );

return {
onBlockDragOver( event ) {
throttled( event );
onBlockDragOver( { x, y } ) {
dragPosition.x.value = x;
dragPosition.y.value = y;
},
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
onBlockDragOverWorklet( { x, y } ) {
'worklet';
dragPosition.x.value = x;
dragPosition.y.value = y;
},
onBlockDragEnd() {
throttled.cancel();
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
targetBlockIndex.value = null;
},
onBlockDrop: ( event ) => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For each user feature we should also add a importance categorization label to i

## Unreleased
- [*] Rename "Reusable blocks" to "Synced patterns", aligning with the web editor. [#51704]
- [**] Fix a crash related to Reanimated when closing the editor [#52320]

## 1.98.1
- [*] fix: Display heading level dropdown icons and labels [#52004]
Expand Down