-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
edit.native.js
169 lines (151 loc) · 4.47 KB
/
edit.native.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
/**
* External dependencies
*/
import { debounce } from 'lodash';
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import {
BlockControls,
InnerBlocks,
JustifyContentControl,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { useResizeObserver } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { useState, useEffect, useRef, useCallback } from '@wordpress/element';
import { alignmentHelpers } from '@wordpress/components';
/**
* Internal dependencies
*/
import { name as buttonBlockName } from '../button/';
import styles from './editor.scss';
const ALLOWED_BLOCKS = [ buttonBlockName ];
const layoutProp = { type: 'default', alignments: [] };
export default function ButtonsEdit( {
attributes: { contentJustification, align },
clientId,
isSelected,
setAttributes,
blockWidth,
} ) {
const [ resizeObserver, sizes ] = useResizeObserver();
const [ maxWidth, setMaxWidth ] = useState( 0 );
const { marginLeft: spacing } = styles.spacing;
const { isInnerButtonSelected, shouldDelete } = useSelect(
( select ) => {
const {
getBlockCount,
getBlockParents,
getSelectedBlockClientId,
} = select( blockEditorStore );
const selectedBlockClientId = getSelectedBlockClientId();
const selectedBlockParents = getBlockParents(
selectedBlockClientId,
true
);
return {
isInnerButtonSelected: selectedBlockParents[ 0 ] === clientId,
// The purpose of `shouldDelete` check is giving the ability to
// pass to mobile toolbar function called `onDelete` which removes
// the whole `Buttons` container along with the last inner button
// when there is exactly one button.
shouldDelete: getBlockCount( clientId ) === 1,
};
},
[ clientId ]
);
const preferredStyle = useSelect( ( select ) => {
const preferredStyleVariations = select(
blockEditorStore
).getSettings().__experimentalPreferredStyleVariations;
return preferredStyleVariations?.value?.[ buttonBlockName ];
}, [] );
const { getBlockOrder } = useSelect( blockEditorStore );
const { insertBlock, removeBlock, selectBlock } = useDispatch(
blockEditorStore
);
useEffect( () => {
const { width } = sizes || {};
const { isFullWidth } = alignmentHelpers;
if ( width ) {
const isFullWidthBlock = isFullWidth( align );
setMaxWidth( isFullWidthBlock ? blockWidth : width );
}
}, [ sizes, align ] );
const onAddNextButton = useCallback(
debounce( ( selectedId ) => {
const order = getBlockOrder( clientId );
const selectedButtonIndex = order.findIndex(
( i ) => i === selectedId
);
const index =
selectedButtonIndex === -1
? order.length + 1
: selectedButtonIndex;
const insertedBlock = createBlock( 'core/button' );
insertBlock( insertedBlock, index, clientId );
selectBlock( insertedBlock.clientId );
}, 200 ),
[]
);
const renderFooterAppender = useRef( () => (
<View style={ styles.appenderContainer }>
<InnerBlocks.ButtonBlockAppender
isFloating={ true }
onAddBlock={ onAddNextButton }
/>
</View>
) );
const justifyControls = [ 'left', 'center', 'right' ];
const remove = useCallback( () => removeBlock( clientId ), [ clientId ] );
const shouldRenderFooterAppender = isSelected || isInnerButtonSelected;
return (
<>
{ isSelected && (
<BlockControls group="block">
<JustifyContentControl
allowedControls={ justifyControls }
value={ contentJustification }
onChange={ ( value ) =>
setAttributes( { contentJustification: value } )
}
popoverProps={ {
position: 'bottom right',
isAlternate: true,
} }
/>
</BlockControls>
) }
{ resizeObserver }
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
template={ [
[
buttonBlockName,
{
className:
preferredStyle &&
`is-style-${ preferredStyle }`,
},
],
] }
renderFooterAppender={
shouldRenderFooterAppender && renderFooterAppender.current
}
orientation="horizontal"
horizontalAlignment={ contentJustification }
onDeleteBlock={ shouldDelete ? remove : undefined }
onAddBlock={ onAddNextButton }
parentWidth={ maxWidth } // This value controls the width of that the buttons are able to expand to.
marginHorizontal={ spacing }
marginVertical={ spacing }
__experimentalLayout={ layoutProp }
templateInsertUpdatesSelection
blockWidth={ blockWidth }
/>
</>
);
}