-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.native.js
149 lines (135 loc) · 3.53 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
/**
* External dependencies
*/
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import {
InnerBlocks,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { withDispatch, withSelect } from '@wordpress/data';
import { useRef, useEffect, useState } from '@wordpress/element';
import { compose, usePreferredColorSchemeStyle } from '@wordpress/compose';
/**
* Internal dependencies
*/
import styles from './editor.scss';
import variations from '../social-link/variations';
const ALLOWED_BLOCKS = variations.map(
( v ) => `core/social-link-${ v.name }`
);
// Template contains the links that show when start.
const TEMPLATE = [
[
'core/social-link-wordpress',
{ service: 'wordpress', url: 'https://wordpress.org' },
],
[ 'core/social-link-facebook', { service: 'facebook' } ],
[ 'core/social-link-twitter', { service: 'twitter' } ],
[ 'core/social-link-instagram', { service: 'instagram' } ],
];
function SocialLinksEdit( {
shouldDelete,
onDelete,
isSelected,
isInnerIconSelected,
innerBlocks,
attributes,
activeInnerBlocks,
getBlock,
blockWidth,
} ) {
const [ initialCreation, setInitialCreation ] = useState( true );
const shouldRenderFooterAppender = isSelected || isInnerIconSelected;
const { align } = attributes;
const { marginLeft: spacing } = styles.spacing;
useEffect( () => {
if ( ! shouldRenderFooterAppender ) {
setInitialCreation( false );
}
}, [ shouldRenderFooterAppender ] );
const renderFooterAppender = useRef( () => (
<View>
<InnerBlocks.ButtonBlockAppender isFloating={ true } />
</View>
) );
const placeholderStyle = usePreferredColorSchemeStyle(
styles.placeholder,
styles.placeholderDark
);
function renderPlaceholder() {
return [
...new Array( innerBlocks.length || 1 ),
].map( ( _, index ) => (
<View style={ placeholderStyle } key={ index } />
) );
}
function filterInnerBlocks( blockIds ) {
return blockIds.filter(
( blockId ) => getBlock( blockId ).attributes.url
);
}
if ( ! shouldRenderFooterAppender && activeInnerBlocks.length === 0 ) {
return (
<View style={ styles.placeholderWrapper }>
{ renderPlaceholder() }
</View>
);
}
return (
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
templateLock={ false }
template={ initialCreation && TEMPLATE }
renderFooterAppender={
shouldRenderFooterAppender && renderFooterAppender.current
}
orientation={ 'horizontal' }
onDeleteBlock={ shouldDelete ? onDelete : undefined }
marginVertical={ spacing }
marginHorizontal={ spacing }
horizontalAlignment={ align }
filterInnerBlocks={
! shouldRenderFooterAppender && filterInnerBlocks
}
blockWidth={ blockWidth }
/>
);
}
export default compose(
withSelect( ( select, { clientId } ) => {
const {
getBlockCount,
getBlockParents,
getSelectedBlockClientId,
getBlocks,
getBlock,
} = select( blockEditorStore );
const selectedBlockClientId = getSelectedBlockClientId();
const selectedBlockParents = getBlockParents(
selectedBlockClientId,
true
);
const innerBlocks = getBlocks( clientId );
const activeInnerBlocks = innerBlocks.filter(
( block ) => block.attributes?.url
);
return {
shouldDelete: getBlockCount( clientId ) === 1,
isInnerIconSelected: selectedBlockParents[ 0 ] === clientId,
innerBlocks,
activeInnerBlocks,
getBlock,
};
} ),
withDispatch( ( dispatch, { clientId } ) => {
const { removeBlock } = dispatch( blockEditorStore );
return {
onDelete: () => {
removeBlock( clientId, false );
},
};
} )
)( SocialLinksEdit );