-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
62 changes: 21 additions & 41 deletions
62
packages/block-editor/src/components/block-list/use-block-props/use-block-refs.js
This file contains 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 |
---|---|---|
@@ -1,54 +1,34 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useLayoutEffect, useReducer } from '@wordpress/element'; | ||
import { useContext, useLayoutEffect, useReducer } from '@wordpress/element'; | ||
|
||
const store = { | ||
add( clientId, ref ) { | ||
store[ clientId ] = store[ clientId ] || new Set(); | ||
store[ clientId ].add( ref ); | ||
|
||
if ( listeners[ clientId ] ) { | ||
listeners[ clientId ](); | ||
} | ||
}, | ||
remove( clientId, ref ) { | ||
store[ clientId ].delete( ref ); | ||
|
||
if ( listeners[ clientId ] ) { | ||
listeners[ clientId ](); | ||
} | ||
}, | ||
}; | ||
|
||
const listeners = { | ||
add( clientId, fn ) { | ||
listeners[ clientId ] = fn; | ||
}, | ||
remove( clientId ) { | ||
delete listeners[ clientId ]; | ||
}, | ||
}; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { BlockRefs } from '../../provider/block-refs-provider'; | ||
|
||
function useUpdate( object, key, value ) { | ||
export function useRegisteredBlockRefs( clientId ) { | ||
const { listeners, store } = useContext( BlockRefs ); | ||
const [ , forceRender ] = useReducer( ( s ) => ! s ); | ||
useLayoutEffect( () => { | ||
if ( ! key ) { | ||
return; | ||
} | ||
|
||
object.add( key, value ); | ||
listeners.add( clientId, forceRender ); | ||
return () => { | ||
object.remove( key, value ); | ||
listeners.remove( clientId, forceRender ); | ||
}; | ||
}, [ key, value ] ); | ||
} | ||
|
||
export function useRegisteredBlockRefs( clientId ) { | ||
const [ , forceRender ] = useReducer( ( s ) => ! s ); | ||
useUpdate( listeners, clientId, forceRender ); | ||
}, [ listeners, clientId, forceRender ] ); | ||
return store[ clientId ] ? Array.from( store[ clientId ] ) : []; | ||
} | ||
|
||
export function useBlockRef( clientId, ref ) { | ||
useUpdate( store, clientId, ref ); | ||
const { listeners, store } = useContext( BlockRefs ); | ||
const [ , forceRender ] = useReducer( ( s ) => ! s ); | ||
useLayoutEffect( () => { | ||
store.add( clientId, ref ); | ||
listeners.add( clientId, forceRender ); | ||
return () => { | ||
store.remove( clientId, ref ); | ||
listeners.remove( clientId, forceRender ); | ||
}; | ||
}, [ listeners, store, clientId, ref, forceRender ] ); | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/block-editor/src/components/provider/block-refs-provider.js
This file contains 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 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext, useMemo } from '@wordpress/element'; | ||
|
||
export const BlockRefs = createContext(); | ||
|
||
function createStore() { | ||
const listeners = { | ||
add( clientId, fn ) { | ||
listeners[ clientId ] = fn; | ||
}, | ||
remove( clientId ) { | ||
delete listeners[ clientId ]; | ||
}, | ||
}; | ||
const store = { | ||
add( clientId, ref ) { | ||
store[ clientId ] = store[ clientId ] || new Set(); | ||
store[ clientId ].add( ref ); | ||
|
||
if ( listeners[ clientId ] ) { | ||
listeners[ clientId ](); | ||
} | ||
}, | ||
remove( clientId, ref ) { | ||
store[ clientId ].delete( ref ); | ||
|
||
if ( listeners[ clientId ] ) { | ||
listeners[ clientId ](); | ||
} | ||
}, | ||
}; | ||
return { listeners, store }; | ||
} | ||
|
||
export function BlockRefsProvider( { children } ) { | ||
const value = useMemo( () => createStore(), [] ); | ||
return ( | ||
<BlockRefs.Provider value={ value }>{ children }</BlockRefs.Provider> | ||
); | ||
} |
This file contains 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