-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit.js
132 lines (119 loc) · 3.28 KB
/
edit.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
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import {
InnerBlocks,
BlockControls,
BlockVerticalAlignmentToolbar,
InspectorControls,
useBlockProps,
useSettings,
useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
__experimentalUseCustomUnits as useCustomUnits,
PanelBody,
__experimentalUnitControl as UnitControl,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { sprintf, __ } from '@wordpress/i18n';
function ColumnInspectorControls( { width, setAttributes } ) {
const [ availableUnits ] = useSettings( 'spacing.units' );
const units = useCustomUnits( {
availableUnits: availableUnits || [ '%', 'px', 'em', 'rem', 'vw' ],
} );
return (
<PanelBody title={ __( 'Settings' ) }>
<UnitControl
label={ __( 'Width' ) }
__unstableInputWidth="calc(50% - 8px)"
__next40pxDefaultSize
value={ width || '' }
onChange={ ( nextWidth ) => {
nextWidth = 0 > parseFloat( nextWidth ) ? '0' : nextWidth;
setAttributes( { width: nextWidth } );
} }
units={ units }
/>
</PanelBody>
);
}
function ColumnEdit( {
attributes: { verticalAlignment, width, templateLock, allowedBlocks },
setAttributes,
clientId,
} ) {
const classes = clsx( 'block-core-columns', {
[ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );
const { columnsIds, hasChildBlocks, rootClientId } = useSelect(
( select ) => {
const { getBlockOrder, getBlockRootClientId } =
select( blockEditorStore );
const rootId = getBlockRootClientId( clientId );
return {
hasChildBlocks: getBlockOrder( clientId ).length > 0,
rootClientId: rootId,
columnsIds: getBlockOrder( rootId ),
};
},
[ clientId ]
);
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const updateAlignment = ( value ) => {
// Update own alignment.
setAttributes( { verticalAlignment: value } );
// Reset parent Columns block.
updateBlockAttributes( rootClientId, {
verticalAlignment: null,
} );
};
const widthWithUnit = Number.isFinite( width ) ? width + '%' : width;
const blockProps = useBlockProps( {
className: classes,
style: widthWithUnit ? { flexBasis: widthWithUnit } : undefined,
} );
const columnsCount = columnsIds.length;
const currentColumnPosition = columnsIds.indexOf( clientId ) + 1;
const label = sprintf(
/* translators: 1: Block label (i.e. "Block: Column"), 2: Position of the selected block, 3: Total number of sibling blocks of the same type */
__( '%1$s (%2$d of %3$d)' ),
blockProps[ 'aria-label' ],
currentColumnPosition,
columnsCount
);
const innerBlocksProps = useInnerBlocksProps(
{ ...blockProps, 'aria-label': label },
{
templateLock,
allowedBlocks,
renderAppender: hasChildBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
}
);
return (
<>
<BlockControls>
<BlockVerticalAlignmentToolbar
onChange={ updateAlignment }
value={ verticalAlignment }
controls={ [ 'top', 'center', 'bottom', 'stretch' ] }
/>
</BlockControls>
<InspectorControls>
<ColumnInspectorControls
width={ width }
setAttributes={ setAttributes }
/>
</InspectorControls>
<div { ...innerBlocksProps } />
</>
);
}
export default ColumnEdit;