-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
edit.js
235 lines (220 loc) · 5.89 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
import { withDispatch, withSelect } from '@wordpress/data';
import {
ExternalLink,
PanelBody,
Path,
SVG,
TextareaControl,
TextControl,
Toolbar,
ToggleControl,
ToolbarButton,
} from '@wordpress/components';
import {
LEFT,
RIGHT,
UP,
DOWN,
BACKSPACE,
ENTER,
} from '@wordpress/keycodes';
import { __ } from '@wordpress/i18n';
import {
BlockControls,
InnerBlocks,
InspectorControls,
URLPopover,
RichText,
} from '@wordpress/block-editor';
import {
Fragment,
useRef,
useState,
} from '@wordpress/element';
function NavigationMenuItemEdit( {
attributes,
hasDescendants,
isSelected,
isParentOfSelectedBlock,
setAttributes,
insertMenuItemBlock,
} ) {
const [ isLinkOpen, setIsLinkOpen ] = useState( false );
const [ isEditingLink, setIsEditingLink ] = useState( false );
const [ urlInput, setUrlInput ] = useState( null );
const inputValue = urlInput !== null ? urlInput : url;
const onKeyDown = ( event ) => {
if ( [ LEFT, DOWN, RIGHT, UP, BACKSPACE, ENTER ].indexOf( event.keyCode ) > -1 ) {
// Stop the key event from propagating up to ObserveTyping.startTypingInTextField.
event.stopPropagation();
}
};
const closeURLPopover = () => {
setIsEditingLink( false );
setUrlInput( null );
setIsLinkOpen( false );
};
const autocompleteRef = useRef( null );
const onFocusOutside = ( event ) => {
const autocompleteElement = autocompleteRef.current;
if ( autocompleteElement && autocompleteElement.contains( event.target ) ) {
return;
}
closeURLPopover();
};
const stopPropagation = ( event ) => {
event.stopPropagation();
};
const { label, url } = attributes;
return (
<Fragment>
<BlockControls>
<Toolbar>
<ToolbarButton
name="link"
icon="admin-links"
title={ __( 'Link' ) }
onClick={ () => setIsLinkOpen( ! isLinkOpen ) }
/>
{ <ToolbarButton
name="submenu"
icon={ <SVG xmlns="http://www.w3.org/2000/svg" width="24" height="24"><Path d="M14 5h8v2h-8zm0 5.5h8v2h-8zm0 5.5h8v2h-8zM2 11.5C2 15.08 4.92 18 8.5 18H9v2l3-3-3-3v2h-.5C6.02 16 4 13.98 4 11.5S6.02 7 8.5 7H12V5H8.5C4.92 5 2 7.92 2 11.5z" /><Path fill="none" d="M0 0h24v24H0z" /></SVG> }
title={ __( 'Add submenu item' ) }
onClick={ insertMenuItemBlock }
/> }
</Toolbar>
{ isLinkOpen &&
<>
<URLPopover
className="wp-block-navigation-menu-item__inline-link-input"
onClose={ closeURLPopover }
onFocusOutside={ onFocusOutside }
>
{ ( ! url || isEditingLink ) &&
<URLPopover.LinkEditor
value={ inputValue }
onChangeInputValue={ setUrlInput }
onKeyPress={ stopPropagation }
onKeyDown={ onKeyDown }
onSubmit={ ( event ) => event.preventDefault() }
autocompleteRef={ autocompleteRef }
/>
}
{ ( url && ! isEditingLink ) &&
<URLPopover.LinkViewer
onKeyPress={ stopPropagation }
url={ url }
/>
}
</URLPopover>
</>
}
</BlockControls>
<InspectorControls>
<PanelBody
title={ __( 'Menu Settings' ) }
>
<ToggleControl
checked={ attributes.opensInNewTab }
onChange={ ( opensInNewTab ) => {
setAttributes( { opensInNewTab } );
} }
label={ __( 'Open in new tab' ) }
/>
<TextareaControl
value={ attributes.description || '' }
onChange={ ( description ) => {
setAttributes( { description } );
} }
label={ __( 'Description' ) }
/>
</PanelBody>
<PanelBody
title={ __( 'SEO Settings' ) }
>
<TextControl
value={ attributes.title || '' }
onChange={ ( title ) => {
setAttributes( { title } );
} }
label={ __( 'Title Attribute' ) }
help={ __( 'Provide more context about where the link goes.' ) }
/>
<ToggleControl
checked={ attributes.nofollow }
onChange={ ( nofollow ) => {
setAttributes( { nofollow } );
} }
label={ __( 'Add nofollow to menu item' ) }
help={ (
<Fragment>
{ __( 'Don\'t let search engines follow this link.' ) }
<ExternalLink
className="wp-block-navigation-menu-item__nofollow-external-link"
href={ __( 'https://codex.wordpress.org/Nofollow' ) }
>
{ __( 'What\'s this?' ) }
</ExternalLink>
</Fragment>
) }
/>
</PanelBody>
</InspectorControls>
<div className={ classnames(
'wp-block-navigation-menu-item', {
'is-editing': isSelected || isParentOfSelectedBlock,
'is-selected': isSelected,
} ) }
>
<RichText
className="wp-block-navigation-menu-item__content"
value={ label }
onChange={ ( labelValue ) => setAttributes( { label: labelValue } ) }
placeholder={ __( 'Add item…' ) }
withoutInteractiveFormatting
/>
{ ( isSelected || isParentOfSelectedBlock ) &&
<InnerBlocks
allowedBlocks={ [ 'core/navigation-menu-item' ] }
renderAppender={ hasDescendants ? InnerBlocks.ButtonBlockAppender : false }
/>
}
</div>
</Fragment>
);
}
export default compose( [
withSelect( ( select, ownProps ) => {
const { getClientIdsOfDescendants, hasSelectedInnerBlock } = select( 'core/block-editor' );
const { clientId } = ownProps;
return {
isParentOfSelectedBlock: hasSelectedInnerBlock( clientId, true ),
hasDescendants: !! getClientIdsOfDescendants( [ clientId ] ).length,
};
} ),
withDispatch( ( dispatch, ownProps ) => {
return {
insertMenuItemBlock() {
const { clientId } = ownProps;
const {
insertBlock,
} = dispatch( 'core/block-editor' );
const blockToInsert = createBlock( 'core/navigation-menu-item' );
insertBlock(
blockToInsert,
0,
clientId,
);
},
};
} ),
] )( NavigationMenuItemEdit );