-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
101 lines (97 loc) · 2.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* WordPress dependencies
*/
import { useMemo, useContext } from '@wordpress/element';
import { hasBlockSupport } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import Edit from './edit';
import {
BlockEditContextProvider,
useBlockEditContext,
mayDisplayControlsKey,
mayDisplayParentControlsKey,
blockEditingModeKey,
blockBindingsKey,
isPreviewModeKey,
} from './context';
import { MultipleUsageWarning } from './multiple-usage-warning';
import { PrivateBlockContext } from '../block-list/private-block-context';
/**
* The `useBlockEditContext` hook provides information about the block this hook is being used in.
* It returns an object with the `name`, `isSelected` state, and the `clientId` of the block.
* It is useful if you want to create custom hooks that need access to the current blocks clientId
* but don't want to rely on the data getting passed in as a parameter.
*
* @return {Object} Block edit context
*/
export { useBlockEditContext };
export default function BlockEdit( {
mayDisplayControls,
mayDisplayParentControls,
blockEditingMode,
isPreviewMode,
// The remaining props are passed through the BlockEdit filters and are thus
// public API!
...props
} ) {
const {
name,
isSelected,
clientId,
attributes = {},
__unstableLayoutClassNames,
} = props;
const { layout = null, metadata = {} } = attributes;
const { bindings } = metadata;
const layoutSupport =
hasBlockSupport( name, 'layout', false ) ||
hasBlockSupport( name, '__experimentalLayout', false );
const { originalBlockClientId } = useContext( PrivateBlockContext );
return (
<BlockEditContextProvider
// It is important to return the same object if props haven't
// changed to avoid unnecessary rerenders.
// See https://reactjs.org/docs/context.html#caveats.
value={ useMemo(
() => ( {
name,
isSelected,
clientId,
layout: layoutSupport ? layout : null,
__unstableLayoutClassNames,
// We use symbols in favour of an __unstable prefix to avoid
// usage outside of the package (this context is exposed).
[ mayDisplayControlsKey ]: mayDisplayControls,
[ mayDisplayParentControlsKey ]: mayDisplayParentControls,
[ blockEditingModeKey ]: blockEditingMode,
[ blockBindingsKey ]: bindings,
[ isPreviewModeKey ]: isPreviewMode,
} ),
[
name,
isSelected,
clientId,
layoutSupport,
layout,
__unstableLayoutClassNames,
mayDisplayControls,
mayDisplayParentControls,
blockEditingMode,
bindings,
isPreviewMode,
]
) }
>
<Edit { ...props } />
{ originalBlockClientId && (
<MultipleUsageWarning
originalBlockClientId={ originalBlockClientId }
name={ name }
onReplace={ props.onReplace }
/>
) }
</BlockEditContextProvider>
);
}