-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathflow.js
245 lines (230 loc) · 6.24 KB
/
flow.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* WordPress dependencies
*/
import {
Button,
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { Icon, positionCenter, stretchWide } from '@wordpress/icons';
/**
* Internal dependencies
*/
import useSetting from '../components/use-setting';
import { appendSelectors } from './utils';
import { getGapBoxControlValueFromStyle } from '../hooks/gap';
import { shouldSkipSerialization } from '../hooks/utils';
export default {
name: 'default',
label: __( 'Flow' ),
inspectorControls: function DefaultLayoutInspectorControls( {
layout,
onChange,
} ) {
const { wideSize, contentSize } = layout;
const units = useCustomUnits( {
availableUnits: useSetting( 'spacing.units' ) || [
'%',
'px',
'em',
'rem',
'vw',
],
} );
return (
<>
<div className="block-editor-hooks__layout-controls">
<div className="block-editor-hooks__layout-controls-unit">
<UnitControl
label={ __( 'Content' ) }
labelPosition="top"
__unstableInputWidth="80px"
value={ contentSize || wideSize || '' }
onChange={ ( nextWidth ) => {
nextWidth =
0 > parseFloat( nextWidth )
? '0'
: nextWidth;
onChange( {
...layout,
contentSize: nextWidth,
} );
} }
units={ units }
/>
<Icon icon={ positionCenter } />
</div>
<div className="block-editor-hooks__layout-controls-unit">
<UnitControl
label={ __( 'Wide' ) }
labelPosition="top"
__unstableInputWidth="80px"
value={ wideSize || contentSize || '' }
onChange={ ( nextWidth ) => {
nextWidth =
0 > parseFloat( nextWidth )
? '0'
: nextWidth;
onChange( {
...layout,
wideSize: nextWidth,
} );
} }
units={ units }
/>
<Icon icon={ stretchWide } />
</div>
</div>
<div className="block-editor-hooks__layout-controls-reset">
<Button
variant="secondary"
isSmall
disabled={ ! contentSize && ! wideSize }
onClick={ () =>
onChange( {
contentSize: undefined,
wideSize: undefined,
inherit: false,
} )
}
>
{ __( 'Reset' ) }
</Button>
</div>
<p className="block-editor-hooks__layout-controls-helptext">
{ __(
'Customize the width for all elements that are assigned to the center or wide columns.'
) }
</p>
</>
);
},
toolBarControls: function DefaultLayoutToolbarControls() {
return null;
},
save: function DefaultLayoutStyle( {
selector,
layout = {},
style,
blockName,
} ) {
const { contentSize, wideSize } = layout;
const blockGapSupport = useSetting( 'spacing.blockGap' );
const hasBlockGapStylesSupport = blockGapSupport !== null;
const blockGapStyleValue = getGapBoxControlValueFromStyle(
style?.spacing?.blockGap
);
// If a block's block.json skips serialization for spacing or
// spacing.blockGap, don't apply the user-defined value to the styles.
const blockGapValue =
blockGapStyleValue?.top &&
! shouldSkipSerialization( blockName, 'spacing', 'blockGap' )
? blockGapStyleValue?.top
: 'var( --wp--style--block-gap )';
let output =
!! contentSize || !! wideSize
? `
${ appendSelectors(
selector,
'> :where(:not(.alignleft):not(.alignright))'
) } {
max-width: ${ contentSize ?? wideSize };
margin-left: auto !important;
margin-right: auto !important;
}
${ appendSelectors( selector, '> .alignwide' ) } {
max-width: ${ wideSize ?? contentSize };
}
${ appendSelectors( selector, '> .alignfull' ) } {
max-width: none;
}
`
: '';
output += `
${ appendSelectors( selector, '> .alignleft' ) } {
float: left;
margin-inline-start: 0;
margin-inline-end: 2em;
}
${ appendSelectors( selector, '> .alignright' ) } {
float: right;
margin-inline-start: 2em;
margin-inline-end: 0;
}
${ appendSelectors( selector, '> .aligncenter' ) } {
margin-left: auto !important;
margin-right: auto !important;
}
`;
if ( hasBlockGapStylesSupport ) {
output += `
${ appendSelectors( selector, '> *' ) } {
margin-block-start: 0;
margin-block-end: 0;
}
${ appendSelectors( selector, '> * + *' ) } {
margin-block-start: ${ blockGapValue };
}
`;
}
return <style>{ output }</style>;
},
getOrientation() {
return 'vertical';
},
getAlignments( layout ) {
const alignmentInfo = getAlignmentsInfo( layout );
if ( layout.alignments !== undefined ) {
if ( ! layout.alignments.includes( 'none' ) ) {
layout.alignments.unshift( 'none' );
}
return layout.alignments.map( ( alignment ) => ( {
name: alignment,
info: alignmentInfo[ alignment ],
} ) );
}
const { contentSize, wideSize } = layout;
const alignments = [
{ name: 'left' },
{ name: 'center' },
{ name: 'right' },
];
if ( contentSize ) {
alignments.unshift( { name: 'full' } );
}
if ( wideSize ) {
alignments.unshift( { name: 'wide', info: alignmentInfo.wide } );
}
alignments.unshift( { name: 'none', info: alignmentInfo.none } );
return alignments;
},
};
/**
* Helper method to assign contextual info to clarify
* alignment settings.
*
* Besides checking if `contentSize` and `wideSize` have a
* value, we now show this information only if their values
* are not a `css var`. This needs to change when parsing
* css variables land.
*
* @see https://github.com/WordPress/gutenberg/pull/34710#issuecomment-918000752
*
* @param {Object} layout The layout object.
* @return {Object} An object with contextual info per alignment.
*/
function getAlignmentsInfo( layout ) {
const { contentSize, wideSize } = layout;
const alignmentInfo = {};
const sizeRegex = /^(?!0)\d+(px|em|rem|vw|vh|%)?$/i;
if ( sizeRegex.test( contentSize ) ) {
// translators: %s: container size (i.e. 600px etc)
alignmentInfo.none = sprintf( __( 'Max %s wide' ), contentSize );
}
if ( sizeRegex.test( wideSize ) ) {
// translators: %s: container size (i.e. 600px etc)
alignmentInfo.wide = sprintf( __( 'Max %s wide' ), wideSize );
}
return alignmentInfo;
}