-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.js
More file actions
205 lines (189 loc) · 4.92 KB
/
index.js
File metadata and controls
205 lines (189 loc) · 4.92 KB
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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __, isRTL } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import {
Button,
VisuallyHidden,
__experimentalText as Text,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { BlockIcon } from '@wordpress/block-editor';
import { store as commandsStore } from '@wordpress/commands';
import {
chevronLeftSmall,
chevronRightSmall,
page as pageIcon,
navigation as navigationIcon,
symbol,
} from '@wordpress/icons';
import { displayShortcut } from '@wordpress/keycodes';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { useRef, useState, useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import useEditedEntityRecord from '../../use-edited-entity-record';
import { store as editSiteStore } from '../../../store';
import {
TEMPLATE_POST_TYPE,
NAVIGATION_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
PATTERN_TYPES,
PATTERN_SYNC_TYPES,
} from '../../../utils/constants';
const typeLabels = {
[ PATTERN_TYPES.user ]: __( 'Editing pattern:' ),
[ NAVIGATION_POST_TYPE ]: __( 'Editing navigation menu:' ),
[ TEMPLATE_POST_TYPE ]: __( 'Editing template:' ),
[ TEMPLATE_PART_POST_TYPE ]: __( 'Editing template part:' ),
};
export default function DocumentActions() {
const isPage = useSelect(
( select ) => select( editSiteStore ).isPage(),
[]
);
return isPage ? <PageDocumentActions /> : <TemplateDocumentActions />;
}
function PageDocumentActions() {
const { isEditingPage, hasResolved, isFound, title } = useSelect(
( select ) => {
const { getEditedPostContext } = select( editSiteStore );
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const { getRenderingMode } = select( editorStore );
const context = getEditedPostContext();
const queryArgs = [ 'postType', context.postType, context.postId ];
const page = getEditedEntityRecord( ...queryArgs );
return {
isEditingPage:
!! context.postId && getRenderingMode() !== 'template-only',
hasResolved: hasFinishedResolution(
'getEditedEntityRecord',
queryArgs
),
isFound: !! page,
title: page?.title,
};
},
[]
);
const { setRenderingMode } = useDispatch( editorStore );
const [ isAnimated, setIsAnimated ] = useState( false );
const isLoading = useRef( true );
useEffect( () => {
if ( ! isLoading.current ) {
setIsAnimated( true );
}
isLoading.current = false;
}, [ isEditingPage ] );
if ( ! hasResolved ) {
return null;
}
if ( ! isFound ) {
return (
<div className="edit-site-document-actions">
{ __( 'Document not found' ) }
</div>
);
}
return isEditingPage ? (
<BaseDocumentActions
className={ classnames( 'is-page', {
'is-animated': isAnimated,
} ) }
icon={ pageIcon }
>
{ title }
</BaseDocumentActions>
) : (
<TemplateDocumentActions
className={ classnames( {
'is-animated': isAnimated,
} ) }
onBack={ () => setRenderingMode( 'template-locked' ) }
/>
);
}
function TemplateDocumentActions( { className, onBack } ) {
const { isLoaded, record, getTitle, icon } = useEditedEntityRecord();
if ( ! isLoaded ) {
return null;
}
if ( ! record ) {
return (
<div className="edit-site-document-actions">
{ __( 'Document not found' ) }
</div>
);
}
let typeIcon = icon;
if ( record.type === NAVIGATION_POST_TYPE ) {
typeIcon = navigationIcon;
} else if ( record.type === PATTERN_TYPES.user ) {
typeIcon = symbol;
}
return (
<BaseDocumentActions
className={ classnames( className, {
'is-synced-entity':
record.wp_pattern_sync_status !==
PATTERN_SYNC_TYPES.unsynced,
} ) }
icon={ typeIcon }
onBack={ onBack }
>
<VisuallyHidden as="span">
{ typeLabels[ record.type ] ??
typeLabels[ TEMPLATE_POST_TYPE ] }
</VisuallyHidden>
{ getTitle() }
</BaseDocumentActions>
);
}
function BaseDocumentActions( { className, icon, children, onBack } ) {
const { open: openCommandCenter } = useDispatch( commandsStore );
return (
<div
className={ classnames( 'edit-site-document-actions', className ) }
>
{ onBack && (
<Button
className="edit-site-document-actions__back"
icon={ isRTL() ? chevronRightSmall : chevronLeftSmall }
onClick={ ( event ) => {
event.stopPropagation();
onBack();
} }
>
{ __( 'Back' ) }
</Button>
) }
<Button
className="edit-site-document-actions__command"
onClick={ () => openCommandCenter() }
size="compact"
>
<HStack
className="edit-site-document-actions__title"
spacing={ 1 }
justify="center"
>
<BlockIcon icon={ icon } />
<Text size="body" as="h1">
{ children }
</Text>
</HStack>
<span className="edit-site-document-actions__shortcut">
{ displayShortcut.primary( 'k' ) }
</span>
</Button>
</div>
);
}