-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathleaf-more-menu.js
169 lines (154 loc) · 4.1 KB
/
leaf-more-menu.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
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import {
addSubmenu,
chevronUp,
chevronDown,
moreVertical,
} from '@wordpress/icons';
import { DropdownMenu, MenuItem, MenuGroup } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { BlockTitle, store as blockEditorStore } from '@wordpress/block-editor';
const POPOVER_PROPS = {
className: 'block-editor-block-settings-menu__popover',
placement: 'bottom-start',
};
const BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU = [
'core/navigation-link',
'core/navigation-submenu',
];
function AddSubmenuItem( {
block,
onClose,
expandedState,
expand,
setInsertedBlock,
} ) {
const { insertBlock, replaceBlock, replaceInnerBlocks } =
useDispatch( blockEditorStore );
const clientId = block.clientId;
const isDisabled = ! BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.includes(
block.name
);
return (
<MenuItem
icon={ addSubmenu }
disabled={ isDisabled }
onClick={ () => {
const updateSelectionOnInsert = false;
const newLink = createBlock( 'core/navigation-link' );
if ( block.name === 'core/navigation-submenu' ) {
insertBlock(
newLink,
block.innerBlocks.length,
clientId,
updateSelectionOnInsert
);
} else {
// Convert to a submenu if the block currently isn't one.
const newSubmenu = createBlock(
'core/navigation-submenu',
block.attributes,
block.innerBlocks
);
// The following must happen as two independent actions.
// Why? Because the offcanvas editor relies on the getLastInsertedBlocksClientIds
// selector to determine which block is "active". As the UX needs the newLink to be
// the "active" block it must be the last block to be inserted.
// Therefore the Submenu is first created and **then** the newLink is inserted
// thus ensuring it is the last inserted block.
replaceBlock( clientId, newSubmenu );
replaceInnerBlocks(
newSubmenu.clientId,
[ newLink ],
updateSelectionOnInsert
);
}
// This call sets the local List View state for the "last inserted block".
// This is required for the Nav Block to determine whether or not to display
// the Link UI for this new block.
setInsertedBlock( newLink );
if ( ! expandedState[ block.clientId ] ) {
expand( block.clientId );
}
onClose();
} }
>
{ __( 'Add submenu link' ) }
</MenuItem>
);
}
export default function LeafMoreMenu( props ) {
const { block } = props;
const { clientId } = block;
const { moveBlocksDown, moveBlocksUp, removeBlocks } =
useDispatch( blockEditorStore );
const removeLabel = sprintf(
/* translators: %s: block name */
__( 'Remove %s' ),
BlockTitle( { clientId, maximumLength: 25 } )
);
const rootClientId = useSelect(
( select ) => {
const { getBlockRootClientId } = select( blockEditorStore );
return getBlockRootClientId( clientId );
},
[ clientId ]
);
return (
<DropdownMenu
icon={ moreVertical }
label={ __( 'Options' ) }
className="block-editor-block-settings-menu"
popoverProps={ POPOVER_PROPS }
noIcons
{ ...props }
>
{ ( { onClose } ) => (
<>
<MenuGroup>
<MenuItem
icon={ chevronUp }
onClick={ () => {
moveBlocksUp( [ clientId ], rootClientId );
onClose();
} }
>
{ __( 'Move up' ) }
</MenuItem>
<MenuItem
icon={ chevronDown }
onClick={ () => {
moveBlocksDown( [ clientId ], rootClientId );
onClose();
} }
>
{ __( 'Move down' ) }
</MenuItem>
<AddSubmenuItem
block={ block }
onClose={ onClose }
expanded
expandedState={ props.expandedState }
expand={ props.expand }
setInsertedBlock={ props.setInsertedBlock }
/>
</MenuGroup>
<MenuGroup>
<MenuItem
onClick={ () => {
removeBlocks( [ clientId ], false );
onClose();
} }
>
{ removeLabel }
</MenuItem>
</MenuGroup>
</>
) }
</DropdownMenu>
);
}