-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.js
170 lines (161 loc) · 4.43 KB
/
index.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
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useState, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
privateApis as componentsPrivateApis,
Button,
Modal,
} from '@wordpress/components';
import { moreVertical } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { usePostActions } from './actions';
import { store as editorStore } from '../../store';
const {
DropdownMenuV2: DropdownMenu,
DropdownMenuGroupV2: DropdownMenuGroup,
DropdownMenuItemV2: DropdownMenuItem,
DropdownMenuItemLabelV2: DropdownMenuItemLabel,
kebabCase,
} = unlock( componentsPrivateApis );
export default function PostActions( { onActionPerformed, buttonProps } ) {
const [ isActionsMenuOpen, setIsActionsMenuOpen ] = useState( false );
const { item, postType } = useSelect( ( select ) => {
const { getCurrentPostType, getCurrentPostId } = select( editorStore );
const { getEditedEntityRecord } = select( coreStore );
const _postType = getCurrentPostType();
return {
item: getEditedEntityRecord(
'postType',
_postType,
getCurrentPostId()
),
postType: _postType,
};
}, [] );
const allActions = usePostActions( { postType, onActionPerformed } );
const actions = useMemo( () => {
return allActions.filter( ( action ) => {
return ! action.isEligible || action.isEligible( item );
} );
}, [ allActions, item ] );
return (
<DropdownMenu
open={ isActionsMenuOpen }
trigger={
<Button
size="small"
icon={ moreVertical }
label={ __( 'Actions' ) }
disabled={ ! actions.length }
accessibleWhenDisabled
className="editor-all-actions-button"
onClick={ () =>
setIsActionsMenuOpen( ! isActionsMenuOpen )
}
{ ...buttonProps }
/>
}
onOpenChange={ setIsActionsMenuOpen }
placement="bottom-end"
>
<ActionsDropdownMenuGroup
actions={ actions }
item={ item }
onClose={ () => {
setIsActionsMenuOpen( false );
} }
/>
</DropdownMenu>
);
}
// From now on all the functions on this file are copied as from the dataviews packages,
// The editor packages should not be using the dataviews packages directly,
// and the dataviews package should not be using the editor packages directly,
// so duplicating the code here seems like the least bad option.
// Copied as is from packages/dataviews/src/item-actions.js
function DropdownMenuItemTrigger( { action, onClick, items } ) {
const label =
typeof action.label === 'string' ? action.label : action.label( items );
return (
<DropdownMenuItem
onClick={ onClick }
hideOnClick={ ! action.RenderModal }
>
<DropdownMenuItemLabel>{ label }</DropdownMenuItemLabel>
</DropdownMenuItem>
);
}
// Copied as is from packages/dataviews/src/item-actions.js
// With an added onClose prop.
function ActionWithModal( { action, item, ActionTrigger, onClose } ) {
const [ isModalOpen, setIsModalOpen ] = useState( false );
const actionTriggerProps = {
action,
onClick: () => setIsModalOpen( true ),
items: [ item ],
};
const { RenderModal, hideModalHeader } = action;
return (
<>
<ActionTrigger { ...actionTriggerProps } />
{ isModalOpen && (
<Modal
title={ action.modalHeader || action.label }
__experimentalHideHeader={ !! hideModalHeader }
onRequestClose={ () => {
setIsModalOpen( false );
} }
overlayClassName={ `editor-action-modal editor-action-modal__${ kebabCase(
action.id
) }` }
focusOnMount="firstContentElement"
size="small"
>
<RenderModal
items={ [ item ] }
closeModal={ () => {
setIsModalOpen( false );
onClose();
} }
/>
</Modal>
) }
</>
);
}
// Copied as is from packages/dataviews/src/item-actions.js
// With an added onClose prop.
function ActionsDropdownMenuGroup( { actions, item, onClose } ) {
return (
<DropdownMenuGroup>
{ actions.map( ( action ) => {
if ( action.RenderModal ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
item={ item }
ActionTrigger={ DropdownMenuItemTrigger }
onClose={ onClose }
/>
);
}
return (
<DropdownMenuItemTrigger
key={ action.id }
action={ action }
onClick={ () => action.callback( [ item ] ) }
items={ [ item ] }
/>
);
} ) }
</DropdownMenuGroup>
);
}