|
2 | 2 | * WordPress dependencies
|
3 | 3 | */
|
4 | 4 | import { getActiveFormat, getActiveObject } from '@wordpress/rich-text';
|
| 5 | +import { useContext, useMemo } from '@wordpress/element'; |
5 | 6 |
|
6 |
| -export default function FormatEdit( { |
7 |
| - formatTypes, |
8 |
| - onChange, |
9 |
| - onFocus, |
10 |
| - value, |
11 |
| - forwardedRef, |
12 |
| -} ) { |
13 |
| - return formatTypes.map( ( settings ) => { |
14 |
| - const { name, edit: Edit } = settings; |
15 |
| - |
16 |
| - if ( ! Edit ) { |
17 |
| - return null; |
18 |
| - } |
19 |
| - |
20 |
| - const activeFormat = getActiveFormat( value, name ); |
21 |
| - const isActive = activeFormat !== undefined; |
22 |
| - const activeObject = getActiveObject( value ); |
23 |
| - const isObjectActive = |
24 |
| - activeObject !== undefined && activeObject.type === name; |
25 |
| - |
26 |
| - return ( |
27 |
| - <Edit |
28 |
| - key={ name } |
29 |
| - isActive={ isActive } |
30 |
| - activeAttributes={ |
31 |
| - isActive ? activeFormat.attributes || {} : {} |
32 |
| - } |
33 |
| - isObjectActive={ isObjectActive } |
34 |
| - activeObjectAttributes={ |
35 |
| - isObjectActive ? activeObject.attributes || {} : {} |
36 |
| - } |
37 |
| - value={ value } |
38 |
| - onChange={ onChange } |
39 |
| - onFocus={ onFocus } |
40 |
| - contentRef={ forwardedRef } |
41 |
| - /> |
42 |
| - ); |
43 |
| - } ); |
| 7 | +/** |
| 8 | + * Internal dependencies |
| 9 | + */ |
| 10 | +import BlockContext from '../block-context'; |
| 11 | + |
| 12 | +const DEFAULT_BLOCK_CONTEXT = {}; |
| 13 | + |
| 14 | +export const usesContextKey = Symbol( 'usesContext' ); |
| 15 | + |
| 16 | +function Edit( { onChange, onFocus, value, forwardedRef, settings } ) { |
| 17 | + const { |
| 18 | + name, |
| 19 | + edit: EditFunction, |
| 20 | + [ usesContextKey ]: usesContext, |
| 21 | + } = settings; |
| 22 | + |
| 23 | + const blockContext = useContext( BlockContext ); |
| 24 | + |
| 25 | + // Assign context values using the block type's declared context needs. |
| 26 | + const context = useMemo( () => { |
| 27 | + return usesContext |
| 28 | + ? Object.fromEntries( |
| 29 | + Object.entries( blockContext ).filter( ( [ key ] ) => |
| 30 | + usesContext.includes( key ) |
| 31 | + ) |
| 32 | + ) |
| 33 | + : DEFAULT_BLOCK_CONTEXT; |
| 34 | + }, [ usesContext, blockContext ] ); |
| 35 | + |
| 36 | + if ( ! EditFunction ) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + const activeFormat = getActiveFormat( value, name ); |
| 41 | + const isActive = activeFormat !== undefined; |
| 42 | + const activeObject = getActiveObject( value ); |
| 43 | + const isObjectActive = |
| 44 | + activeObject !== undefined && activeObject.type === name; |
| 45 | + |
| 46 | + return ( |
| 47 | + <EditFunction |
| 48 | + key={ name } |
| 49 | + isActive={ isActive } |
| 50 | + activeAttributes={ isActive ? activeFormat.attributes || {} : {} } |
| 51 | + isObjectActive={ isObjectActive } |
| 52 | + activeObjectAttributes={ |
| 53 | + isObjectActive ? activeObject.attributes || {} : {} |
| 54 | + } |
| 55 | + value={ value } |
| 56 | + onChange={ onChange } |
| 57 | + onFocus={ onFocus } |
| 58 | + contentRef={ forwardedRef } |
| 59 | + context={ context } |
| 60 | + /> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +export default function FormatEdit( { formatTypes, ...props } ) { |
| 65 | + return formatTypes.map( ( settings ) => ( |
| 66 | + <Edit settings={ settings } { ...props } key={ settings.name } /> |
| 67 | + ) ); |
44 | 68 | }
|
0 commit comments