-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
214 lines (201 loc) · 4.65 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
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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useCallback, useState } from '@wordpress/element';
import {
KeyboardShortcuts,
PanelBody,
RangeControl,
TextControl,
ToggleControl,
ToolbarButton,
ToolbarGroup,
Popover,
} from '@wordpress/components';
import {
BlockControls,
InspectorControls,
RichText,
__experimentalBlock as Block,
__experimentalLinkControl as LinkControl,
} from '@wordpress/block-editor';
import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
import { link } from '@wordpress/icons';
/**
* Internal dependencies
*/
import ColorEdit from './color-edit';
import getColorAndStyleProps from './color-props';
const NEW_TAB_REL = 'noreferrer noopener';
const MIN_BORDER_RADIUS_VALUE = 0;
const MAX_BORDER_RADIUS_VALUE = 50;
const INITIAL_BORDER_RADIUS_POSITION = 5;
function BorderPanel( { borderRadius = '', setAttributes } ) {
const setBorderRadius = useCallback(
( newBorderRadius ) => {
setAttributes( { borderRadius: newBorderRadius } );
},
[ setAttributes ]
);
return (
<PanelBody title={ __( 'Border settings' ) }>
<RangeControl
value={ borderRadius }
label={ __( 'Border radius' ) }
min={ MIN_BORDER_RADIUS_VALUE }
max={ MAX_BORDER_RADIUS_VALUE }
initialPosition={ INITIAL_BORDER_RADIUS_POSITION }
allowReset
onChange={ setBorderRadius }
/>
</PanelBody>
);
}
function URLPicker( {
isSelected,
url,
setAttributes,
opensInNewTab,
onToggleOpenInNewTab,
} ) {
const [ isURLPickerOpen, setIsURLPickerOpen ] = useState( false );
const openLinkControl = () => {
setIsURLPickerOpen( true );
};
const linkControl = isURLPickerOpen && (
<Popover
position="bottom center"
onClose={ () => setIsURLPickerOpen( false ) }
>
<LinkControl
className="wp-block-navigation-link__inline-link-input"
value={ { url, opensInNewTab } }
onChange={ ( {
url: newURL = '',
opensInNewTab: newOpensInNewTab,
} ) => {
setAttributes( { url: newURL } );
if ( opensInNewTab !== newOpensInNewTab ) {
onToggleOpenInNewTab( newOpensInNewTab );
}
} }
/>
</Popover>
);
return (
<>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
name="link"
icon={ link }
title={ __( 'Link' ) }
shortcut={ displayShortcut.primary( 'k' ) }
onClick={ openLinkControl }
/>
</ToolbarGroup>
</BlockControls>
{ isSelected && (
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.primary( 'k' ) ]: openLinkControl,
} }
/>
) }
{ linkControl }
</>
);
}
function ButtonEdit( props ) {
const { attributes, setAttributes, className, isSelected } = props;
const {
borderRadius,
linkTarget,
placeholder,
rel,
text,
url,
} = attributes;
const onSetLinkRel = useCallback(
( value ) => {
setAttributes( { rel: value } );
},
[ setAttributes ]
);
const onToggleOpenInNewTab = useCallback(
( value ) => {
const newLinkTarget = value ? '_blank' : undefined;
let updatedRel = rel;
if ( newLinkTarget && ! rel ) {
updatedRel = NEW_TAB_REL;
} else if ( ! newLinkTarget && rel === NEW_TAB_REL ) {
updatedRel = undefined;
}
setAttributes( {
linkTarget: newLinkTarget,
rel: updatedRel,
} );
},
[ rel, setAttributes ]
);
const colorProps = getColorAndStyleProps( attributes );
return (
<>
<ColorEdit { ...props } />
<Block.div>
<RichText
placeholder={ placeholder || __( 'Add text…' ) }
value={ text }
onChange={ ( value ) => setAttributes( { text: value } ) }
withoutInteractiveFormatting
className={ classnames(
className,
'wp-block-button__link',
colorProps.className,
{
'no-border-radius': borderRadius === 0,
}
) }
style={ {
borderRadius: borderRadius
? borderRadius + 'px'
: undefined,
...colorProps.style,
} }
/>
</Block.div>
<URLPicker
url={ url }
setAttributes={ setAttributes }
isSelected={ isSelected }
opensInNewTab={ linkTarget === '_blank' }
onToggleOpenInNewTab={ onToggleOpenInNewTab }
/>
<InspectorControls>
<BorderPanel
borderRadius={ borderRadius }
setAttributes={ setAttributes }
/>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Open in new tab' ) }
onChange={ onToggleOpenInNewTab }
checked={ linkTarget === '_blank' }
/>
<TextControl
label={ __( 'Link rel' ) }
value={ rel || '' }
onChange={ onSetLinkRel }
/>
</PanelBody>
</InspectorControls>
</>
);
}
export default ButtonEdit;