-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
173 lines (160 loc) · 4.59 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
171
172
173
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { Button, Modal } from '@wordpress/components';
import {
EntitiesSavedStates,
useEntitiesSavedStatesIsDirty,
privateApis,
} from '@wordpress/editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { useActivateTheme } from '../../utils/use-activate-theme';
import { useActualCurrentTheme } from '../../utils/use-actual-current-theme';
import { isPreviewingTheme } from '../../utils/is-previewing-theme';
const { EntitiesSavedStatesExtensible, NavigableRegion } =
unlock( privateApis );
const { useLocation } = unlock( routerPrivateApis );
const EntitiesSavedStatesForPreview = ( { onClose, renderDialog } ) => {
const isDirtyProps = useEntitiesSavedStatesIsDirty();
let activateSaveLabel;
if ( isDirtyProps.isDirty ) {
activateSaveLabel = __( 'Activate & Save' );
} else {
activateSaveLabel = __( 'Activate' );
}
const currentTheme = useActualCurrentTheme();
const previewingTheme = useSelect(
( select ) => select( coreStore ).getCurrentTheme(),
[]
);
const additionalPrompt = (
<p>
{ sprintf(
/* translators: 1: The name of active theme, 2: The name of theme to be activated. */
__(
'Saving your changes will change your active theme from %1$s to %2$s.'
),
currentTheme?.name?.rendered ?? '...',
previewingTheme?.name?.rendered ?? '...'
) }
</p>
);
const activateTheme = useActivateTheme();
const onSave = async ( values ) => {
await activateTheme();
return values;
};
return (
<EntitiesSavedStatesExtensible
{ ...{
...isDirtyProps,
additionalPrompt,
close: onClose,
onSave,
saveEnabled: true,
saveLabel: activateSaveLabel,
renderDialog,
} }
/>
);
};
const _EntitiesSavedStates = ( { onClose, renderDialog } ) => {
if ( isPreviewingTheme() ) {
return (
<EntitiesSavedStatesForPreview
onClose={ onClose }
renderDialog={ renderDialog }
/>
);
}
return (
<EntitiesSavedStates close={ onClose } renderDialog={ renderDialog } />
);
};
export default function SavePanel() {
const { query } = useLocation();
const { canvas = 'view' } = query;
const { isSaveViewOpen, isDirty, isSaving } = useSelect( ( select ) => {
const {
__experimentalGetDirtyEntityRecords,
isSavingEntityRecord,
isResolving,
} = select( coreStore );
const dirtyEntityRecords = __experimentalGetDirtyEntityRecords();
const isActivatingTheme = isResolving( 'activateTheme' );
const { isSaveViewOpened } = unlock( select( editSiteStore ) );
// The currently selected entity to display.
// Typically template or template part in the site editor.
return {
isSaveViewOpen: isSaveViewOpened(),
isDirty: dirtyEntityRecords.length > 0,
isSaving:
dirtyEntityRecords.some( ( record ) =>
isSavingEntityRecord( record.kind, record.name, record.key )
) || isActivatingTheme,
};
}, [] );
const { setIsSaveViewOpened } = useDispatch( editSiteStore );
const onClose = () => setIsSaveViewOpened( false );
useEffect( () => {
setIsSaveViewOpened( false );
}, [ canvas, setIsSaveViewOpened ] );
if ( canvas === 'view' ) {
return isSaveViewOpen ? (
<Modal
className="edit-site-save-panel__modal"
onRequestClose={ onClose }
__experimentalHideHeader
contentLabel={ __(
'Save site, content, and template changes'
) }
>
<_EntitiesSavedStates onClose={ onClose } />
</Modal>
) : null;
}
const activateSaveEnabled = isPreviewingTheme() || isDirty;
const disabled = isSaving || ! activateSaveEnabled;
return (
<NavigableRegion
className={ clsx( 'edit-site-layout__actions', {
'is-entity-save-view-open': isSaveViewOpen,
} ) }
ariaLabel={ __( 'Save panel' ) }
>
<div
className={ clsx( 'edit-site-editor__toggle-save-panel', {
'screen-reader-text': isSaveViewOpen,
} ) }
>
<Button
__next40pxDefaultSize
variant="secondary"
className="edit-site-editor__toggle-save-panel-button"
onClick={ () => setIsSaveViewOpened( true ) }
aria-haspopup="dialog"
disabled={ disabled }
accessibleWhenDisabled
>
{ __( 'Open save panel' ) }
</Button>
</div>
{ isSaveViewOpen && (
<_EntitiesSavedStates onClose={ onClose } renderDialog />
) }
</NavigableRegion>
);
}