-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
167 lines (147 loc) · 4.28 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* External dependencies
*/
import { pick, isEqual, map } from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { withViewportMatch } from '@wordpress/viewport';
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { synchronizeBlocksWithTemplate, withBlockContentContext } from '@wordpress/blocks';
import isShallowEqual from '@wordpress/is-shallow-equal';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockList from '../block-list';
import { withBlockEditContext } from '../block-edit/context';
class InnerBlocks extends Component {
constructor() {
super( ...arguments );
this.updateNestedSettings();
}
getTemplateLock() {
const {
templateLock,
parentLock,
} = this.props;
return templateLock === undefined ? parentLock : templateLock;
}
componentDidMount() {
const { innerBlocks } = this.props.block;
// only synchronize innerBlocks with template if innerBlocks are empty or a locking all exists
if ( innerBlocks.length === 0 || this.getTemplateLock() === 'all' ) {
return this.synchronizeBlocksWithTemplate();
}
}
componentDidUpdate( prevProps ) {
const { template, block } = this.props;
const { innerBlocks } = block;
this.updateNestedSettings();
// only synchronize innerBlocks with template if innerBlocks are empty or a locking all exists
if ( innerBlocks.length === 0 || this.getTemplateLock() === 'all' ) {
const hasTemplateChanged = ! isEqual( template, prevProps.template );
if ( hasTemplateChanged ) {
this.synchronizeBlocksWithTemplate();
}
}
}
/**
* Called on mount or when a mismatch exists between the templates and
* inner blocks, synchronizes inner blocks with the template, replacing
* current blocks.
*/
synchronizeBlocksWithTemplate() {
const { template, block, replaceInnerBlocks } = this.props;
const { innerBlocks } = block;
// Synchronize with templates. If the next set differs, replace.
const nextBlocks = synchronizeBlocksWithTemplate( innerBlocks, template );
if ( ! isEqual( nextBlocks, innerBlocks ) ) {
replaceInnerBlocks( nextBlocks );
}
}
updateNestedSettings() {
const {
blockListSettings,
allowedBlocks,
updateNestedSettings,
} = this.props;
const newSettings = {
allowedBlocks,
templateLock: this.getTemplateLock(),
};
if ( ! isShallowEqual( blockListSettings, newSettings ) ) {
updateNestedSettings( newSettings );
}
}
render() {
const {
clientId,
allowedBlocks,
templateLock,
template,
isSmallScreen,
isSelectedBlockInRoot,
} = this.props;
const classes = classnames( 'editor-inner-blocks', {
'has-overlay': isSmallScreen && ! isSelectedBlockInRoot,
} );
return (
<div className={ classes }>
<BlockList
rootClientId={ clientId }
{ ...{ allowedBlocks, templateLock, template } }
/>
</div>
);
}
}
InnerBlocks = compose( [
withBlockEditContext( ( context ) => pick( context, [ 'clientId' ] ) ),
withViewportMatch( { isSmallScreen: '< medium' } ),
withSelect( ( select, ownProps ) => {
const {
isBlockSelected,
hasSelectedInnerBlock,
getBlock,
getBlockListSettings,
getBlockRootClientId,
getTemplateLock,
} = select( 'core/editor' );
const { clientId } = ownProps;
const rootClientId = getBlockRootClientId( clientId );
return {
isSelectedBlockInRoot: isBlockSelected( clientId ) || hasSelectedInnerBlock( clientId ),
block: getBlock( clientId ),
blockListSettings: getBlockListSettings( clientId ),
parentLock: getTemplateLock( rootClientId ),
};
} ),
withDispatch( ( dispatch, ownProps ) => {
const {
replaceBlocks,
insertBlocks,
updateBlockListSettings,
} = dispatch( 'core/editor' );
const { block, clientId } = ownProps;
return {
replaceInnerBlocks( blocks ) {
const clientIds = map( block.innerBlocks, 'clientId' );
if ( clientIds.length ) {
replaceBlocks( clientIds, blocks );
} else {
insertBlocks( blocks, undefined, clientId );
}
},
updateNestedSettings( settings ) {
dispatch( updateBlockListSettings( clientId, settings ) );
},
};
} ),
] )( InnerBlocks );
InnerBlocks.Content = withBlockContentContext(
( { BlockContent } ) => <BlockContent />
);
export default InnerBlocks;