-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathedit.js
More file actions
151 lines (139 loc) · 3.8 KB
/
edit.js
File metadata and controls
151 lines (139 loc) · 3.8 KB
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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
InnerBlocks,
BlockControls,
BlockVerticalAlignmentToolbar,
InspectorControls,
} from '@wordpress/block-editor';
import { PanelBody, RangeControl, Notice } from '@wordpress/components';
import { withDispatch, withSelect, useSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { __, sprintf } from '@wordpress/i18n';
/**
* Component which renders a notice if the sum total width of columns of the
* root block (the Columns parent) are all explicitly assigned and not equal
* to 100%.
*
* @param {Object} props Component props.
* @param {string} props.clientId Client ID of the selected column.
*
* @return {WPElement?} Notice element, if invalid.
*/
function InvalidWidthNotice( { clientId } ) {
const sumWidth = useSelect(
( select ) => {
const {
getBlockOrder,
getBlockAttributes,
getBlockRootClientId,
} = select( 'core/block-editor' );
const columns = getBlockOrder( getBlockRootClientId( clientId ) );
return columns.reduce(
( result, columnClientId ) =>
result + getBlockAttributes( columnClientId ).width,
0
);
},
[ clientId ]
);
// A value of `NaN` is anticipated when any of the columns do not have an
// explicit width assigned, since `result + undefined` in the `Array#reduce`
// above would taint the numeric result (intended and leveraged here). Round
// sum to allow some tolerance +/- 0.5%, which also ensures that the notice
// message would never display "100%" as an invalid width if e.g. 100.4%
// sum total width.
if ( isNaN( sumWidth ) || Math.round( sumWidth ) === 100 ) {
return null;
}
return (
<Notice status="warning" isDismissible={ false }>
{ sprintf(
__( 'The total width of columns (%d%%) does not equal 100%%.' ),
Math.round( sumWidth )
) }
</Notice>
);
}
function ColumnEdit( {
clientId,
attributes,
setAttributes,
className,
updateAlignment,
hasChildBlocks,
} ) {
const { verticalAlignment, width } = attributes;
const classes = classnames( className, 'block-core-columns', {
[ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );
return (
<div className={ classes }>
<BlockControls>
<BlockVerticalAlignmentToolbar
onChange={ updateAlignment }
value={ verticalAlignment }
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Column settings' ) }>
<RangeControl
label={ __( 'Percentage width' ) }
value={ width || '' }
onChange={ ( nextWidth ) => {
setAttributes( { width: nextWidth } );
} }
min={ 0 }
max={ 100 }
step={ 0.1 }
required
allowReset
/>
<InvalidWidthNotice clientId={ clientId } />
</PanelBody>
</InspectorControls>
<InnerBlocks
templateLock={ false }
renderAppender={
hasChildBlocks
? undefined
: () => <InnerBlocks.ButtonBlockAppender />
}
/>
</div>
);
}
export default compose(
withSelect( ( select, ownProps ) => {
const { clientId } = ownProps;
const { getBlockOrder } = select( 'core/block-editor' );
return {
hasChildBlocks: getBlockOrder( clientId ).length > 0,
};
} ),
withDispatch( ( dispatch, ownProps, registry ) => {
return {
updateAlignment( verticalAlignment ) {
const { clientId, setAttributes } = ownProps;
const { updateBlockAttributes } = dispatch(
'core/block-editor'
);
const { getBlockRootClientId } = registry.select(
'core/block-editor'
);
// Update own alignment.
setAttributes( { verticalAlignment } );
// Reset Parent Columns Block
const rootClientId = getBlockRootClientId( clientId );
updateBlockAttributes( rootClientId, {
verticalAlignment: null,
} );
},
};
} )
)( ColumnEdit );