-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.native.js
315 lines (286 loc) · 8.14 KB
/
index.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* External dependencies
*/
import { Platform, Clipboard } from 'react-native';
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { isURL, prependHTTP } from '@wordpress/url';
import {
useEffect,
useState,
useRef,
useContext,
useCallback,
} from '@wordpress/element';
import { link, external } from '@wordpress/icons';
/**
* Internal dependencies
*/
import BottomSheet from '../bottom-sheet';
import { BottomSheetContext } from '../bottom-sheet/bottom-sheet-context';
import PanelBody from '../../panel/body';
import TextControl from '../../text-control';
import ToggleControl from '../../toggle-control';
import FooterMessageControl from '../../footer-message-control';
import PanelActions from '../../panel/actions';
import LinkRelIcon from './link-rel';
import styles from './style.scss';
const NEW_TAB_REL = 'noreferrer noopener';
function LinkSettings( {
// Control link settings `BottomSheet` visibility
isVisible,
// Callback that is called on closing bottom sheet
onClose,
// Function called to set attributes
setAttributes,
// Callback that is called when url input field is empty
onEmptyURL,
// Object of available options along with specific, customizable properties.
// Available options keys:
// * url - uses `TextControl` component to set `attributes.url`
// * linkLabel - uses `TextControl` component to set `attributes.label`
// * openInNewTab - uses `ToggleControl` component to set `attributes.linkTarget` and `attributes.rel`
// * linkRel - uses `TextControl` component to set `attributes.rel`
// * footer - uses `FooterMessageControl` component to display message, e.g. about missing functionality
// Available properties:
// * label - control component label, e.g. `Button Link URL`
// * placeholder - control component placeholder, e.g. `Add URL`
// * autoFocus (url only) - whether url input should be focused on sheet opening
// * autoFill (url only) - whether url input should be filled with url from clipboard
// Example:
// const options = {
// url: {
// label: __( 'Button Link URL' ),
// placeholder: __( 'Add URL' ),
// autoFocus: true,
// autoFill: true,
// }
// }
options,
// Specifies whether settings should be wrapped into `BottomSheet`
withBottomSheet,
// Defines buttons which will be displayed below the all options.
// It's an array of objects with following properties:
// * label - button title
// * onPress - callback that is called on pressing button
// Example:
// const actions = [
// {
// label: __( 'Remove link' ),
// onPress: () => setAttributes({ url: '' }),
// },
// ];
actions,
// Specifies whether general `BottomSheet` is opened
editorSidebarOpened,
// Specifies whether icon should be displayed next to the label
showIcon,
onLinkCellPressed,
urlValue,
// Attributes properties
url,
label = '',
linkTarget,
rel = '',
} ) {
const [ urlInputValue, setUrlInputValue ] = useState( '' );
const [ labelInputValue, setLabelInputValue ] = useState( '' );
const [ linkRelInputValue, setLinkRelInputValue ] = useState( '' );
const prevEditorSidebarOpenedRef = useRef();
const { onHandleClosingBottomSheet } = useContext( BottomSheetContext );
useEffect( () => {
if ( onHandleClosingBottomSheet ) {
onHandleClosingBottomSheet( onCloseSettingsSheet );
}
}, [ urlInputValue, labelInputValue, linkRelInputValue ] );
useEffect( () => {
prevEditorSidebarOpenedRef.current = editorSidebarOpened;
} );
const prevEditorSidebarOpened = prevEditorSidebarOpenedRef.current;
useEffect( () => {
if ( url !== urlInputValue ) {
setUrlInputValue( url || '' );
}
}, [ url ] );
useEffect( () => {
setLabelInputValue( label || '' );
}, [ label ] );
useEffect( () => {
setLinkRelInputValue( rel || '' );
}, [ rel ] );
useEffect( () => {
const isSettingSheetOpen = isVisible || editorSidebarOpened;
if ( options.url.autoFill && isSettingSheetOpen && ! url ) {
getURLFromClipboard();
}
if ( prevEditorSidebarOpened && ! editorSidebarOpened ) {
onSetAttributes();
}
}, [ editorSidebarOpened, isVisible ] );
useEffect( () => {
if ( ! urlValue && onEmptyURL ) {
onEmptyURL();
}
if ( prependHTTP( urlValue ) !== url ) {
setAttributes( {
url: prependHTTP( urlValue ),
} );
}
}, [ urlValue ] );
const onChangeURL = useCallback(
( value ) => {
if ( ! value && onEmptyURL ) {
onEmptyURL();
}
setUrlInputValue( value );
},
[ onEmptyURL ]
);
const onChangeLabel = useCallback( ( value ) => {
setLabelInputValue( value );
}, [] );
const onSetAttributes = useCallback( () => {
const newURL = prependHTTP( urlInputValue );
if (
url !== newURL ||
labelInputValue !== label ||
linkRelInputValue !== rel
) {
setAttributes( {
url: newURL,
label: labelInputValue,
rel: linkRelInputValue,
} );
}
}, [ urlInputValue, labelInputValue, linkRelInputValue, setAttributes ] );
const onCloseSettingsSheet = useCallback( () => {
onSetAttributes();
if ( onClose ) {
onClose();
}
}, [ onClose, onSetAttributes ] );
const onChangeOpenInNewTab = useCallback(
( value ) => {
const newLinkTarget = value ? '_blank' : undefined;
let updatedRel = linkRelInputValue;
if ( newLinkTarget && ! linkRelInputValue ) {
updatedRel = NEW_TAB_REL;
} else if ( ! newLinkTarget && linkRelInputValue === NEW_TAB_REL ) {
updatedRel = undefined;
}
setAttributes( {
linkTarget: newLinkTarget,
rel: updatedRel,
} );
},
[ linkRelInputValue ]
);
const onChangeLinkRel = useCallback( ( value ) => {
setLinkRelInputValue( value );
}, [] );
async function getURLFromClipboard() {
const clipboardText = await Clipboard.getString();
if ( ! clipboardText ) {
return;
}
// Check if pasted text is URL
if ( ! isURL( clipboardText ) ) {
return;
}
setAttributes( { url: clipboardText } );
}
function getSettings() {
return (
<>
{ options.url &&
( onLinkCellPressed ? (
<BottomSheet.LinkCell
showIcon={ showIcon }
value={ url }
valueMask={ options.url.valueMask }
onPress={ onLinkCellPressed }
/>
) : (
<TextControl
icon={ showIcon && link }
label={ options.url.label }
value={ urlInputValue }
valuePlaceholder={ options.url.placeholder }
onChange={ onChangeURL }
onSubmit={ onCloseSettingsSheet }
autoCapitalize="none"
autoCorrect={ false }
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={
Platform.OS === 'ios' && options.url.autoFocus
}
keyboardType="url"
/>
) ) }
{ options.linkLabel && (
<TextControl
label={ options.linkLabel.label }
value={ labelInputValue }
valuePlaceholder={ options.linkLabel.placeholder }
onChange={ onChangeLabel }
/>
) }
{ !! urlInputValue && (
<>
{ options.openInNewTab && (
<ToggleControl
icon={ showIcon && external }
label={ options.openInNewTab.label }
checked={ linkTarget === '_blank' }
onChange={ onChangeOpenInNewTab }
/>
) }
{ options.linkRel && (
<TextControl
icon={ showIcon && LinkRelIcon }
label={ options.linkRel.label }
value={ linkRelInputValue }
valuePlaceholder={ options.linkRel.placeholder }
onChange={ onChangeLinkRel }
onSubmit={ onCloseSettingsSheet }
autoCapitalize="none"
autoCorrect={ false }
keyboardType="default"
/>
) }
</>
) }
</>
);
}
if ( ! withBottomSheet ) {
return getSettings();
}
return (
<>
<PanelBody style={ styles.linkSettingsPanel }>
{ getSettings() }
</PanelBody>
{ options.footer && (
<PanelBody style={ styles.linkSettingsPanel }>
<FooterMessageControl
label={ options.footer.label }
separatorType={ options.footer.separatorType }
/>
</PanelBody>
) }
{ actions && <PanelActions actions={ actions } /> }
</>
);
}
export default compose( [
withSelect( ( select ) => {
const { isEditorSidebarOpened } = select( 'core/edit-post' );
return {
editorSidebarOpened: isEditorSidebarOpened(),
};
} ),
] )( LinkSettings );