-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[Mobile] - Drag & drop blocks - Fetch and share blocks layout size and position coordinates #39089
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
Merged
geriux
merged 14 commits into
rnmobile/feature/drag-and-drop
from
rnmobile/feature/drag-and-drop-fetch-share-blocks-layouts
Mar 17, 2022
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7315fb7
Mobile - Block list - Extract block list context into a separate file…
0880031
Mobile - Block list - Adds block list item cell to get the onLayout d…
5068948
Mobile - Block list - Store block layouts data for inner blocks in a …
a09bcee
Mobile - BlockList ItemCell - Destructuring props
6bfc5cb
Mobile - BlockListContext - Rename findByRootId to findBlockLayoutByC…
b1b3107
Mobile - BlockListContext - Rename deleteByClientId to deleteBlockLay…
7ce1aae
Mobile - BlockListContext - Store default context and use it for init…
daf5b14
Mobile - BlockListContext - Add param's docs
23f2c39
Mobile - Block list context - Export findBlockLayoutByClientId
b1ee8f6
Mobile - Block list context - Update comments
b5906a2
Mobile - Block list context - Unit tests
50d02e6
Merge branch 'trunk' into rnmobile/feature/drag-and-drop-fetch-share-…
d8f7cf8
Mobile - Block list context - update unit tests
a38cfc5
Merge branch 'rnmobile/feature/drag-and-drop' into rnmobile/feature/d…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
packages/block-editor/src/components/block-list/block-list-context.native.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { createContext, useContext } from '@wordpress/element'; | ||
|
|
||
| export const DEFAULT_BLOCK_LIST_CONTEXT = { | ||
| scrollRef: null, | ||
| blocksLayouts: { current: {} }, | ||
| findBlockLayoutByClientId, | ||
| updateBlocksLayouts, | ||
| }; | ||
|
|
||
| const Context = createContext( DEFAULT_BLOCK_LIST_CONTEXT ); | ||
| const { Provider, Consumer } = Context; | ||
|
|
||
| /** | ||
| * Finds a block's layout data by its client Id. | ||
| * | ||
| * @param {Object} data Blocks layouts object. | ||
| * @param {string} clientId Block's clientId. | ||
| * | ||
| * @return {Object} Found block layout data. | ||
| */ | ||
| function findBlockLayoutByClientId( data, clientId ) { | ||
| return Object.entries( data ).reduce( ( acc, entry ) => { | ||
| const item = entry[ 1 ]; | ||
| if ( acc ) { | ||
| return acc; | ||
| } | ||
| if ( item?.clientId === clientId ) { | ||
| return item; | ||
| } | ||
| if ( item?.innerBlocks && Object.keys( item.innerBlocks ).length > 0 ) { | ||
| return findBlockLayoutByClientId( item.innerBlocks, clientId ); | ||
| } | ||
| return null; | ||
| }, null ); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes the layout data of a block by its client Id. | ||
| * | ||
| * @param {Object} data Blocks layouts object. | ||
| * @param {string} clientId Block's clientsId. | ||
| * | ||
| * @return {Object} Updated data object. | ||
| */ | ||
| export function deleteBlockLayoutByClientId( data, clientId ) { | ||
| return Object.keys( data ).reduce( ( acc, key ) => { | ||
| if ( key !== clientId ) { | ||
| acc[ key ] = data[ key ]; | ||
| } | ||
| if ( | ||
| data[ key ]?.innerBlocks && | ||
| Object.keys( data[ key ].innerBlocks ).length > 0 | ||
| ) { | ||
| if ( acc[ key ] ) { | ||
| acc[ key ].innerBlocks = deleteBlockLayoutByClientId( | ||
| data[ key ].innerBlocks, | ||
| clientId | ||
| ); | ||
| } | ||
| } | ||
| return acc; | ||
| }, {} ); | ||
| } | ||
|
|
||
| /** | ||
| * Updates or deletes a block's layout data in the blocksLayouts object, | ||
| * in case of deletion, the layout data is not required. | ||
| * | ||
| * @param {Object} blocksLayouts Blocks layouts object. | ||
| * @param {Object} blockData Block's layout data to add or remove to/from the blockLayouts object. | ||
| * @param {string} blockData.clientId Block's clientId. | ||
| * @param {?string} blockData.rootClientId Optional. Block's rootClientId. | ||
| * @param {?boolean} blockData.shouldRemove Optional. Flag to remove it from the blocksLayout list. | ||
| * @param {number} blockData.width Block's width. | ||
| * @param {number} blockData.height Block's height. | ||
| * @param {number} blockData.x Block's x coordinate (relative to the parent). | ||
| * @param {number} blockData.y Block's y coordinate (relative to the parent). | ||
| */ | ||
|
|
||
| function updateBlocksLayouts( blocksLayouts, blockData ) { | ||
fluiddot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { clientId, rootClientId, shouldRemove, ...layoutProps } = blockData; | ||
|
|
||
| if ( clientId && shouldRemove ) { | ||
| blocksLayouts.current = deleteBlockLayoutByClientId( | ||
| blocksLayouts.current, | ||
| clientId | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if ( clientId && ! rootClientId ) { | ||
| blocksLayouts.current[ clientId ] = { | ||
| clientId, | ||
| rootClientId, | ||
| ...layoutProps, | ||
| innerBlocks: { | ||
| ...blocksLayouts.current[ clientId ]?.innerBlocks, | ||
| }, | ||
| }; | ||
| } else if ( clientId && rootClientId ) { | ||
| const block = findBlockLayoutByClientId( | ||
| blocksLayouts.current, | ||
| rootClientId | ||
| ); | ||
|
|
||
| if ( block ) { | ||
| block.innerBlocks[ clientId ] = { | ||
| clientId, | ||
| rootClientId, | ||
| ...layoutProps, | ||
| innerBlocks: { | ||
| ...block.innerBlocks[ clientId ]?.innerBlocks, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export { Provider as BlockListProvider, Consumer as BlockListConsumer }; | ||
|
|
||
| /** | ||
| * Hook that returns the block list context. | ||
| * | ||
| * @return {Object} Block list context | ||
| */ | ||
| export const useBlockListContext = () => { | ||
| return useContext( Context ); | ||
| }; | ||
42 changes: 42 additions & 0 deletions
42
packages/block-editor/src/components/block-list/block-list-item-cell.native.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { View } from 'react-native'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useEffect, useCallback } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { useBlockListContext } from './block-list-context'; | ||
|
|
||
| function BlockListItemCell( { children, clientId, rootClientId } ) { | ||
| const { blocksLayouts, updateBlocksLayouts } = useBlockListContext(); | ||
|
|
||
| useEffect( () => { | ||
| return () => { | ||
| updateBlocksLayouts( blocksLayouts, { | ||
| clientId, | ||
| shouldRemove: true, | ||
| } ); | ||
| }; | ||
| }, [] ); | ||
|
|
||
| const onLayout = useCallback( | ||
| ( { nativeEvent: { layout } } ) => { | ||
| updateBlocksLayouts( blocksLayouts, { | ||
| clientId, | ||
| rootClientId, | ||
| ...layout, | ||
| } ); | ||
| }, | ||
| [ clientId, rootClientId, updateBlocksLayouts ] | ||
| ); | ||
|
|
||
| return <View onLayout={ onLayout }>{ children }</View>; | ||
| } | ||
|
|
||
| export default BlockListItemCell; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.