-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
140 lines (126 loc) · 3.35 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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* 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 { NavigableRegion } from '@wordpress/interface';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { useActivateTheme } from '../../utils/use-activate-theme';
import {
currentlyPreviewingTheme,
isPreviewingTheme,
} from '../../utils/is-previewing-theme';
const { EntitiesSavedStatesExtensible } = unlock( privateApis );
const EntitiesSavedStatesForPreview = ( { onClose } ) => {
const isDirtyProps = useEntitiesSavedStatesIsDirty();
let activateSaveLabel;
if ( isDirtyProps.isDirty ) {
activateSaveLabel = __( 'Activate & Save' );
} else {
activateSaveLabel = __( 'Activate' );
}
const themeName = useSelect( ( select ) => {
const theme = select( coreStore ).getTheme(
currentlyPreviewingTheme()
);
return theme?.name?.rendered;
}, [] );
const additionalPrompt = (
<p>
{ sprintf(
'Saving your changes will change your active theme to %s.',
themeName
) }
</p>
);
const activateTheme = useActivateTheme();
const onSave = async ( values ) => {
await activateTheme();
return values;
};
return (
<EntitiesSavedStatesExtensible
{ ...{
...isDirtyProps,
additionalPrompt,
close: onClose,
onSave,
saveEnabled: true,
saveLabel: activateSaveLabel,
} }
/>
);
};
const _EntitiesSavedStates = ( { onClose } ) => {
if ( isPreviewingTheme() ) {
return <EntitiesSavedStatesForPreview onClose={ onClose } />;
}
return <EntitiesSavedStates close={ onClose } />;
};
export default function SavePanel() {
const { isSaveViewOpen, canvasMode } = useSelect( ( select ) => {
const { isSaveViewOpened, getCanvasMode } = unlock(
select( editSiteStore )
);
// The currently selected entity to display.
// Typically template or template part in the site editor.
return {
isSaveViewOpen: isSaveViewOpened(),
canvasMode: getCanvasMode(),
};
}, [] );
const { setIsSaveViewOpened } = useDispatch( editSiteStore );
const onClose = () => setIsSaveViewOpened( false );
if ( canvasMode === '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;
}
return (
<NavigableRegion
className={ classnames( 'edit-site-layout__actions', {
'is-entity-save-view-open': isSaveViewOpen,
} ) }
ariaLabel={ __( 'Save panel' ) }
>
{ isSaveViewOpen ? (
<_EntitiesSavedStates onClose={ onClose } />
) : (
<div className="edit-site-editor__toggle-save-panel">
<Button
variant="secondary"
className="edit-site-editor__toggle-save-panel-button"
onClick={ () => setIsSaveViewOpened( true ) }
aria-expanded={ false }
>
{ __( 'Open save panel' ) }
</Button>
</div>
) }
</NavigableRegion>
);
}