@wordpress/edit-site 4.0.0 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/components/add-new-template/new-template-part.js +4 -1
- package/build/components/add-new-template/new-template-part.js.map +1 -1
- package/build/components/error-boundary/index.js +11 -27
- package/build/components/error-boundary/index.js.map +1 -1
- package/build/components/error-boundary/warning.js +70 -0
- package/build/components/error-boundary/warning.js.map +1 -0
- package/build/components/global-styles/navigation-button.js +7 -27
- package/build/components/global-styles/navigation-button.js.map +1 -1
- package/build/components/global-styles/typography-panel.js +5 -2
- package/build/components/global-styles/typography-panel.js.map +1 -1
- package/build/components/global-styles/use-global-styles-output.js +21 -4
- package/build/components/global-styles/use-global-styles-output.js.map +1 -1
- package/build/components/list/table.js +3 -1
- package/build/components/list/table.js.map +1 -1
- package/build/components/navigation-sidebar/navigation-toggle/index.js +15 -4
- package/build/components/navigation-sidebar/navigation-toggle/index.js.map +1 -1
- package/build/components/routes/redirect-to-homepage.js +22 -3
- package/build/components/routes/redirect-to-homepage.js.map +1 -1
- package/build/components/secondary-sidebar/list-view-sidebar.js +0 -11
- package/build/components/secondary-sidebar/list-view-sidebar.js.map +1 -1
- package/build/components/template-part-converter/convert-to-template-part.js +4 -1
- package/build/components/template-part-converter/convert-to-template-part.js.map +1 -1
- package/build/index.js +15 -1
- package/build/index.js.map +1 -1
- package/build/store/actions.js +191 -155
- package/build/store/actions.js.map +1 -1
- package/build/store/index.js +0 -3
- package/build/store/index.js.map +1 -1
- package/build/store/selectors.js +3 -2
- package/build/store/selectors.js.map +1 -1
- package/build-module/components/add-new-template/new-template-part.js +4 -1
- package/build-module/components/add-new-template/new-template-part.js.map +1 -1
- package/build-module/components/error-boundary/index.js +9 -26
- package/build-module/components/error-boundary/index.js.map +1 -1
- package/build-module/components/error-boundary/warning.js +60 -0
- package/build-module/components/error-boundary/warning.js.map +1 -0
- package/build-module/components/global-styles/navigation-button.js +8 -28
- package/build-module/components/global-styles/navigation-button.js.map +1 -1
- package/build-module/components/global-styles/typography-panel.js +6 -3
- package/build-module/components/global-styles/typography-panel.js.map +1 -1
- package/build-module/components/global-styles/use-global-styles-output.js +20 -4
- package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
- package/build-module/components/list/table.js +2 -1
- package/build-module/components/list/table.js.map +1 -1
- package/build-module/components/navigation-sidebar/navigation-toggle/index.js +12 -4
- package/build-module/components/navigation-sidebar/navigation-toggle/index.js.map +1 -1
- package/build-module/components/routes/redirect-to-homepage.js +22 -3
- package/build-module/components/routes/redirect-to-homepage.js.map +1 -1
- package/build-module/components/secondary-sidebar/list-view-sidebar.js +1 -12
- package/build-module/components/secondary-sidebar/list-view-sidebar.js.map +1 -1
- package/build-module/components/template-part-converter/convert-to-template-part.js +4 -1
- package/build-module/components/template-part-converter/convert-to-template-part.js.map +1 -1
- package/build-module/index.js +13 -1
- package/build-module/index.js.map +1 -1
- package/build-module/store/actions.js +170 -145
- package/build-module/store/actions.js.map +1 -1
- package/build-module/store/index.js +0 -2
- package/build-module/store/index.js.map +1 -1
- package/build-module/store/selectors.js +3 -2
- package/build-module/store/selectors.js.map +1 -1
- package/build-style/style-rtl.css +11 -10
- package/build-style/style.css +11 -10
- package/package.json +18 -18
- package/src/components/add-new-template/new-template-part.js +7 -1
- package/src/components/error-boundary/index.js +11 -28
- package/src/components/error-boundary/warning.js +59 -0
- package/src/components/global-styles/navigation-button.js +6 -25
- package/src/components/global-styles/typography-panel.js +12 -5
- package/src/components/global-styles/use-global-styles-output.js +19 -3
- package/src/components/list/table.js +5 -2
- package/src/components/navigation-sidebar/navigation-toggle/index.js +13 -4
- package/src/components/navigation-sidebar/navigation-toggle/style.scss +32 -31
- package/src/components/routes/redirect-to-homepage.js +27 -4
- package/src/components/secondary-sidebar/list-view-sidebar.js +1 -11
- package/src/components/template-part-converter/convert-to-template-part.js +6 -1
- package/src/index.js +17 -1
- package/src/store/actions.js +161 -205
- package/src/store/index.js +0 -2
- package/src/store/selectors.js +3 -0
- package/src/store/test/actions.js +186 -92
- package/src/store/test/selectors.js +8 -1
|
@@ -20,10 +20,17 @@ function getNeedsHomepageRedirect( params ) {
|
|
|
20
20
|
);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Returns the postType and postId of the default homepage.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} siteUrl The URL of the site.
|
|
27
|
+
* @return {Object} An object containing the postType and postId properties
|
|
28
|
+
* or `undefined` if a homepage could not be found.
|
|
29
|
+
*/
|
|
23
30
|
async function getHomepageParams( siteUrl ) {
|
|
24
31
|
const siteSettings = await apiFetch( { path: '/wp/v2/settings' } );
|
|
25
32
|
if ( ! siteSettings ) {
|
|
26
|
-
|
|
33
|
+
throw new Error( '`getHomepageParams`: unable to load site settings.' );
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
const {
|
|
@@ -44,11 +51,27 @@ async function getHomepageParams( siteUrl ) {
|
|
|
44
51
|
// (packages/core-data/src/resolvers.js)
|
|
45
52
|
const template = await window
|
|
46
53
|
.fetch( addQueryArgs( siteUrl, { '_wp-find-template': true } ) )
|
|
47
|
-
.then( (
|
|
48
|
-
|
|
54
|
+
.then( ( response ) => {
|
|
55
|
+
if ( ! response.ok ) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`\`getHomepageParams\`: HTTP status error, ${ response.status } ${ response.statusText }`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return response.json();
|
|
62
|
+
} )
|
|
63
|
+
.then( ( { data } ) => {
|
|
64
|
+
if ( data.message ) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`\`getHomepageParams\`: REST API error, ${ data.message }`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return data;
|
|
71
|
+
} );
|
|
49
72
|
|
|
50
73
|
if ( ! template?.id ) {
|
|
51
|
-
|
|
74
|
+
throw new Error( '`getHomepageParams`: unable to find home template.' );
|
|
52
75
|
}
|
|
53
76
|
|
|
54
77
|
return {
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
__experimentalListView as ListView,
|
|
6
|
-
store as blockEditorStore,
|
|
7
|
-
} from '@wordpress/block-editor';
|
|
4
|
+
import { __experimentalListView as ListView } from '@wordpress/block-editor';
|
|
8
5
|
import { Button } from '@wordpress/components';
|
|
9
6
|
import {
|
|
10
7
|
useFocusOnMount,
|
|
@@ -25,12 +22,6 @@ import { store as editSiteStore } from '../../store';
|
|
|
25
22
|
export default function ListViewSidebar() {
|
|
26
23
|
const { setIsListViewOpened } = useDispatch( editSiteStore );
|
|
27
24
|
|
|
28
|
-
const { clearSelectedBlock, selectBlock } = useDispatch( blockEditorStore );
|
|
29
|
-
async function selectEditorBlock( clientId ) {
|
|
30
|
-
await clearSelectedBlock();
|
|
31
|
-
selectBlock( clientId, -1 );
|
|
32
|
-
}
|
|
33
|
-
|
|
34
25
|
const focusOnMountRef = useFocusOnMount( 'firstElement' );
|
|
35
26
|
const headerFocusReturnRef = useFocusReturn();
|
|
36
27
|
const contentFocusReturnRef = useFocusReturn();
|
|
@@ -69,7 +60,6 @@ export default function ListViewSidebar() {
|
|
|
69
60
|
] ) }
|
|
70
61
|
>
|
|
71
62
|
<ListView
|
|
72
|
-
onSelect={ selectEditorBlock }
|
|
73
63
|
showNestedBlocks
|
|
74
64
|
__experimentalFeatures
|
|
75
65
|
__experimentalPersistentListViewFeatures
|
|
@@ -30,11 +30,16 @@ export default function ConvertToTemplatePart( { clientIds, blocks } ) {
|
|
|
30
30
|
const { createSuccessNotice } = useDispatch( noticesStore );
|
|
31
31
|
|
|
32
32
|
const onConvert = async ( { title, area } ) => {
|
|
33
|
+
// Currently template parts only allow latin chars.
|
|
34
|
+
// Fallback slug will receive suffix by default.
|
|
35
|
+
const cleanSlug =
|
|
36
|
+
kebabCase( title ).replace( /[^\w-]+/g, '' ) || 'wp-custom-part';
|
|
37
|
+
|
|
33
38
|
const templatePart = await saveEntityRecord(
|
|
34
39
|
'postType',
|
|
35
40
|
'wp_template_part',
|
|
36
41
|
{
|
|
37
|
-
slug:
|
|
42
|
+
slug: cleanSlug,
|
|
38
43
|
title,
|
|
39
44
|
content: serialize( blocks ),
|
|
40
45
|
area,
|
package/src/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
__experimentalFetchUrlData as fetchUrlData,
|
|
14
14
|
} from '@wordpress/core-data';
|
|
15
15
|
import { store as editorStore } from '@wordpress/editor';
|
|
16
|
+
import { __ } from '@wordpress/i18n';
|
|
16
17
|
import { store as viewportStore } from '@wordpress/viewport';
|
|
17
18
|
import { getQueryArgs } from '@wordpress/url';
|
|
18
19
|
|
|
@@ -24,6 +25,7 @@ import { store as editSiteStore } from './store';
|
|
|
24
25
|
import EditSiteApp from './components/app';
|
|
25
26
|
import getIsListPage from './utils/get-is-list-page';
|
|
26
27
|
import redirectToHomepage from './components/routes/redirect-to-homepage';
|
|
28
|
+
import ErrorBoundaryWarning from './components/error-boundary/warning';
|
|
27
29
|
|
|
28
30
|
/**
|
|
29
31
|
* Reinitializes the editor after the user chooses to reboot the editor after
|
|
@@ -38,7 +40,21 @@ export async function reinitializeEditor( target, settings ) {
|
|
|
38
40
|
// define what's being edited. When visiting via the dashboard link, these
|
|
39
41
|
// won't be present. Do a client side redirect to the 'homepage' if that's
|
|
40
42
|
// the case.
|
|
41
|
-
|
|
43
|
+
try {
|
|
44
|
+
await redirectToHomepage( settings.siteUrl );
|
|
45
|
+
} catch ( error ) {
|
|
46
|
+
render(
|
|
47
|
+
<ErrorBoundaryWarning
|
|
48
|
+
message={ __(
|
|
49
|
+
'The editor is unable to find a block template for the homepage.'
|
|
50
|
+
) }
|
|
51
|
+
error={ error }
|
|
52
|
+
dashboardLink="index.php"
|
|
53
|
+
/>,
|
|
54
|
+
target
|
|
55
|
+
);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
42
58
|
|
|
43
59
|
// This will be a no-op if the target doesn't have any React nodes.
|
|
44
60
|
unmountComponentAtNode( target );
|
package/src/store/actions.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
+
import apiFetch from '@wordpress/api-fetch';
|
|
4
5
|
import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
|
|
5
|
-
import { controls, dispatch } from '@wordpress/data';
|
|
6
|
-
import { apiFetch } from '@wordpress/data-controls';
|
|
7
6
|
import { addQueryArgs, getPathAndQueryString } from '@wordpress/url';
|
|
8
7
|
import { __, sprintf } from '@wordpress/i18n';
|
|
9
8
|
import { store as noticesStore } from '@wordpress/notices';
|
|
@@ -19,7 +18,7 @@ import { STORE_NAME as editSiteStoreName } from './constants';
|
|
|
19
18
|
import isTemplateRevertable from '../utils/is-template-revertable';
|
|
20
19
|
|
|
21
20
|
/**
|
|
22
|
-
*
|
|
21
|
+
* Action that toggles a feature flag.
|
|
23
22
|
*
|
|
24
23
|
* @param {string} feature Feature name.
|
|
25
24
|
*
|
|
@@ -33,7 +32,7 @@ export function toggleFeature( feature ) {
|
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
/**
|
|
36
|
-
*
|
|
35
|
+
* Action that changes the width of the editing canvas.
|
|
37
36
|
*
|
|
38
37
|
* @param {string} deviceType
|
|
39
38
|
*
|
|
@@ -47,97 +46,83 @@ export function __experimentalSetPreviewDeviceType( deviceType ) {
|
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
/**
|
|
50
|
-
*
|
|
49
|
+
* Action that sets a template, optionally fetching it from REST API.
|
|
51
50
|
*
|
|
52
51
|
* @param {number} templateId The template ID.
|
|
53
52
|
* @param {string} templateSlug The template slug.
|
|
54
53
|
* @return {Object} Action object.
|
|
55
54
|
*/
|
|
56
|
-
export
|
|
57
|
-
|
|
55
|
+
export const setTemplate = ( templateId, templateSlug ) => async ( {
|
|
56
|
+
dispatch,
|
|
57
|
+
registry,
|
|
58
|
+
} ) => {
|
|
58
59
|
if ( ! templateSlug ) {
|
|
59
|
-
const template =
|
|
60
|
-
coreStore
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
'wp_template',
|
|
64
|
-
templateId
|
|
65
|
-
);
|
|
66
|
-
pageContext.templateSlug = template?.slug;
|
|
60
|
+
const template = await registry
|
|
61
|
+
.resolveSelect( coreStore )
|
|
62
|
+
.getEntityRecord( 'postType', 'wp_template', templateId );
|
|
63
|
+
templateSlug = template?.slug;
|
|
67
64
|
}
|
|
68
|
-
|
|
65
|
+
|
|
66
|
+
dispatch( {
|
|
69
67
|
type: 'SET_TEMPLATE',
|
|
70
68
|
templateId,
|
|
71
|
-
page: { context:
|
|
72
|
-
};
|
|
73
|
-
}
|
|
69
|
+
page: { context: { templateSlug } },
|
|
70
|
+
} );
|
|
71
|
+
};
|
|
74
72
|
|
|
75
73
|
/**
|
|
76
|
-
*
|
|
74
|
+
* Action that adds a new template and sets it as the current template.
|
|
77
75
|
*
|
|
78
76
|
* @param {Object} template The template.
|
|
79
77
|
*
|
|
80
78
|
* @return {Object} Action object used to set the current template.
|
|
81
79
|
*/
|
|
82
|
-
export
|
|
83
|
-
const newTemplate =
|
|
84
|
-
coreStore
|
|
85
|
-
|
|
86
|
-
'postType',
|
|
87
|
-
'wp_template',
|
|
88
|
-
template
|
|
89
|
-
);
|
|
80
|
+
export const addTemplate = ( template ) => async ( { dispatch, registry } ) => {
|
|
81
|
+
const newTemplate = await registry
|
|
82
|
+
.dispatch( coreStore )
|
|
83
|
+
.saveEntityRecord( 'postType', 'wp_template', template );
|
|
90
84
|
|
|
91
85
|
if ( template.content ) {
|
|
92
|
-
|
|
93
|
-
coreStore
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
86
|
+
registry
|
|
87
|
+
.dispatch( coreStore )
|
|
88
|
+
.editEntityRecord(
|
|
89
|
+
'postType',
|
|
90
|
+
'wp_template',
|
|
91
|
+
newTemplate.id,
|
|
92
|
+
{ blocks: parse( template.content ) },
|
|
93
|
+
{ undoIgnore: true }
|
|
94
|
+
);
|
|
101
95
|
}
|
|
102
96
|
|
|
103
|
-
|
|
97
|
+
dispatch( {
|
|
104
98
|
type: 'SET_TEMPLATE',
|
|
105
99
|
templateId: newTemplate.id,
|
|
106
100
|
page: { context: { templateSlug: newTemplate.slug } },
|
|
107
|
-
};
|
|
108
|
-
}
|
|
101
|
+
} );
|
|
102
|
+
};
|
|
109
103
|
|
|
110
104
|
/**
|
|
111
|
-
*
|
|
105
|
+
* Action that removes a template.
|
|
112
106
|
*
|
|
113
107
|
* @param {Object} template The template object.
|
|
114
108
|
*/
|
|
115
|
-
export
|
|
109
|
+
export const removeTemplate = ( template ) => async ( { registry } ) => {
|
|
116
110
|
try {
|
|
117
|
-
|
|
118
|
-
coreStore
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
template.id,
|
|
123
|
-
{ force: true }
|
|
124
|
-
);
|
|
111
|
+
await registry
|
|
112
|
+
.dispatch( coreStore )
|
|
113
|
+
.deleteEntityRecord( 'postType', template.type, template.id, {
|
|
114
|
+
force: true,
|
|
115
|
+
} );
|
|
125
116
|
|
|
126
|
-
const lastError =
|
|
127
|
-
coreStore
|
|
128
|
-
|
|
129
|
-
'postType',
|
|
130
|
-
template.type,
|
|
131
|
-
template.id
|
|
132
|
-
);
|
|
117
|
+
const lastError = registry
|
|
118
|
+
.select( coreStore )
|
|
119
|
+
.getLastEntityDeleteError( 'postType', template.type, template.id );
|
|
133
120
|
|
|
134
121
|
if ( lastError ) {
|
|
135
122
|
throw lastError;
|
|
136
123
|
}
|
|
137
124
|
|
|
138
|
-
|
|
139
|
-
noticesStore,
|
|
140
|
-
'createSuccessNotice',
|
|
125
|
+
registry.dispatch( noticesStore ).createSuccessNotice(
|
|
141
126
|
sprintf(
|
|
142
127
|
/* translators: The template/part's name. */
|
|
143
128
|
__( '"%s" deleted.' ),
|
|
@@ -151,17 +136,14 @@ export function* removeTemplate( template ) {
|
|
|
151
136
|
? error.message
|
|
152
137
|
: __( 'An error occurred while deleting the template.' );
|
|
153
138
|
|
|
154
|
-
|
|
155
|
-
noticesStore
|
|
156
|
-
|
|
157
|
-
errorMessage,
|
|
158
|
-
{ type: 'snackbar' }
|
|
159
|
-
);
|
|
139
|
+
registry
|
|
140
|
+
.dispatch( noticesStore )
|
|
141
|
+
.createErrorNotice( errorMessage, { type: 'snackbar' } );
|
|
160
142
|
}
|
|
161
|
-
}
|
|
143
|
+
};
|
|
162
144
|
|
|
163
145
|
/**
|
|
164
|
-
*
|
|
146
|
+
* Action that sets a template part.
|
|
165
147
|
*
|
|
166
148
|
* @param {string} templatePartId The template part ID.
|
|
167
149
|
*
|
|
@@ -175,8 +157,8 @@ export function setTemplatePart( templatePartId ) {
|
|
|
175
157
|
}
|
|
176
158
|
|
|
177
159
|
/**
|
|
178
|
-
*
|
|
179
|
-
* from
|
|
160
|
+
* Action that sets the home template ID to the template ID of the page resolved
|
|
161
|
+
* from a given path.
|
|
180
162
|
*
|
|
181
163
|
* @param {number} homeTemplateId The template ID for the homepage.
|
|
182
164
|
*/
|
|
@@ -199,41 +181,46 @@ export function setHomeTemplateId( homeTemplateId ) {
|
|
|
199
181
|
*
|
|
200
182
|
* @return {number} The resolved template ID for the page route.
|
|
201
183
|
*/
|
|
202
|
-
export
|
|
184
|
+
export const setPage = ( page ) => async ( { dispatch, registry } ) => {
|
|
203
185
|
if ( ! page.path && page.context?.postId ) {
|
|
204
|
-
const entity =
|
|
205
|
-
coreStore
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
186
|
+
const entity = await registry
|
|
187
|
+
.resolveSelect( coreStore )
|
|
188
|
+
.getEntityRecord(
|
|
189
|
+
'postType',
|
|
190
|
+
page.context.postType || 'post',
|
|
191
|
+
page.context.postId
|
|
192
|
+
);
|
|
211
193
|
// If the entity is undefined for some reason, path will resolve to "/"
|
|
212
194
|
page.path = getPathAndQueryString( entity?.link );
|
|
213
195
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
page.path
|
|
218
|
-
|
|
219
|
-
|
|
196
|
+
|
|
197
|
+
const template = await registry
|
|
198
|
+
.resolveSelect( coreStore )
|
|
199
|
+
.__experimentalGetTemplateForLink( page.path );
|
|
200
|
+
|
|
201
|
+
if ( ! template ) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
dispatch( {
|
|
220
206
|
type: 'SET_PAGE',
|
|
221
|
-
page:
|
|
222
|
-
?
|
|
223
|
-
: {
|
|
207
|
+
page: template.slug
|
|
208
|
+
? {
|
|
224
209
|
...page,
|
|
225
210
|
context: {
|
|
226
211
|
...page.context,
|
|
227
|
-
templateSlug,
|
|
212
|
+
templateSlug: template.slug,
|
|
228
213
|
},
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
214
|
+
}
|
|
215
|
+
: page,
|
|
216
|
+
templateId: template.id,
|
|
217
|
+
} );
|
|
218
|
+
|
|
219
|
+
return template.id;
|
|
220
|
+
};
|
|
234
221
|
|
|
235
222
|
/**
|
|
236
|
-
*
|
|
223
|
+
* Action that sets the active navigation panel menu.
|
|
237
224
|
*
|
|
238
225
|
* @param {string} menu Menu prop of active menu.
|
|
239
226
|
*
|
|
@@ -272,7 +259,7 @@ export function setIsNavigationPanelOpened( isOpen ) {
|
|
|
272
259
|
}
|
|
273
260
|
|
|
274
261
|
/**
|
|
275
|
-
*
|
|
262
|
+
* Opens or closes the inserter.
|
|
276
263
|
*
|
|
277
264
|
* @param {boolean|Object} value Whether the inserter should be
|
|
278
265
|
* opened (true) or closed (false).
|
|
@@ -325,33 +312,33 @@ export function setIsListViewOpened( isOpen ) {
|
|
|
325
312
|
* @param {boolean} [options.allowUndo] Whether to allow the user to undo
|
|
326
313
|
* reverting the template. Default true.
|
|
327
314
|
*/
|
|
328
|
-
export
|
|
315
|
+
export const revertTemplate = (
|
|
316
|
+
template,
|
|
317
|
+
{ allowUndo = true } = {}
|
|
318
|
+
) => async ( { registry } ) => {
|
|
329
319
|
if ( ! isTemplateRevertable( template ) ) {
|
|
330
|
-
|
|
331
|
-
noticesStore
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
);
|
|
320
|
+
registry
|
|
321
|
+
.dispatch( noticesStore )
|
|
322
|
+
.createErrorNotice( __( 'This template is not revertable.' ), {
|
|
323
|
+
type: 'snackbar',
|
|
324
|
+
} );
|
|
336
325
|
return;
|
|
337
326
|
}
|
|
338
327
|
|
|
339
328
|
try {
|
|
340
|
-
const templateEntity =
|
|
341
|
-
coreStore
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
template.type
|
|
345
|
-
);
|
|
329
|
+
const templateEntity = registry
|
|
330
|
+
.select( coreStore )
|
|
331
|
+
.getEntity( 'postType', template.type );
|
|
332
|
+
|
|
346
333
|
if ( ! templateEntity ) {
|
|
347
|
-
|
|
348
|
-
noticesStore
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
334
|
+
registry
|
|
335
|
+
.dispatch( noticesStore )
|
|
336
|
+
.createErrorNotice(
|
|
337
|
+
__(
|
|
338
|
+
'The editor has encountered an unexpected error. Please reload.'
|
|
339
|
+
),
|
|
340
|
+
{ type: 'snackbar' }
|
|
341
|
+
);
|
|
355
342
|
return;
|
|
356
343
|
}
|
|
357
344
|
|
|
@@ -359,33 +346,30 @@ export function* revertTemplate( template, { allowUndo = true } = {} ) {
|
|
|
359
346
|
`${ templateEntity.baseURL }/${ template.id }`,
|
|
360
347
|
{ context: 'edit', source: 'theme' }
|
|
361
348
|
);
|
|
362
|
-
|
|
349
|
+
|
|
350
|
+
const fileTemplate = await apiFetch( { path: fileTemplatePath } );
|
|
363
351
|
if ( ! fileTemplate ) {
|
|
364
|
-
|
|
365
|
-
noticesStore
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
352
|
+
registry
|
|
353
|
+
.dispatch( noticesStore )
|
|
354
|
+
.createErrorNotice(
|
|
355
|
+
__(
|
|
356
|
+
'The editor has encountered an unexpected error. Please reload.'
|
|
357
|
+
),
|
|
358
|
+
{ type: 'snackbar' }
|
|
359
|
+
);
|
|
372
360
|
return;
|
|
373
361
|
}
|
|
374
362
|
|
|
375
363
|
const serializeBlocks = ( { blocks: blocksForSerialization = [] } ) =>
|
|
376
364
|
__unstableSerializeAndClean( blocksForSerialization );
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
'postType',
|
|
381
|
-
|
|
382
|
-
template.id
|
|
383
|
-
);
|
|
365
|
+
|
|
366
|
+
const edited = registry
|
|
367
|
+
.select( coreStore )
|
|
368
|
+
.getEditedEntityRecord( 'postType', template.type, template.id );
|
|
369
|
+
|
|
384
370
|
// We are fixing up the undo level here to make sure we can undo
|
|
385
371
|
// the revert in the header toolbar correctly.
|
|
386
|
-
|
|
387
|
-
coreStore,
|
|
388
|
-
'editEntityRecord',
|
|
372
|
+
registry.dispatch( coreStore ).editEntityRecord(
|
|
389
373
|
'postType',
|
|
390
374
|
template.type,
|
|
391
375
|
template.id,
|
|
@@ -400,37 +384,28 @@ export function* revertTemplate( template, { allowUndo = true } = {} ) {
|
|
|
400
384
|
);
|
|
401
385
|
|
|
402
386
|
const blocks = parse( fileTemplate?.content?.raw );
|
|
403
|
-
|
|
404
|
-
coreStore
|
|
405
|
-
|
|
406
|
-
'postType',
|
|
407
|
-
template.type,
|
|
408
|
-
fileTemplate.id,
|
|
409
|
-
{
|
|
387
|
+
registry
|
|
388
|
+
.dispatch( coreStore )
|
|
389
|
+
.editEntityRecord( 'postType', template.type, fileTemplate.id, {
|
|
410
390
|
content: serializeBlocks,
|
|
411
391
|
blocks,
|
|
412
392
|
source: 'theme',
|
|
413
|
-
}
|
|
414
|
-
);
|
|
393
|
+
} );
|
|
415
394
|
|
|
416
395
|
if ( allowUndo ) {
|
|
417
|
-
const undoRevert =
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
template.type,
|
|
421
|
-
edited.id,
|
|
422
|
-
{
|
|
396
|
+
const undoRevert = () => {
|
|
397
|
+
registry
|
|
398
|
+
.dispatch( coreStore )
|
|
399
|
+
.editEntityRecord( 'postType', template.type, edited.id, {
|
|
423
400
|
content: serializeBlocks,
|
|
424
401
|
blocks: edited.blocks,
|
|
425
402
|
source: 'custom',
|
|
426
|
-
}
|
|
427
|
-
);
|
|
403
|
+
} );
|
|
428
404
|
};
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
__( 'Template reverted.' ),
|
|
433
|
-
{
|
|
405
|
+
|
|
406
|
+
registry
|
|
407
|
+
.dispatch( noticesStore )
|
|
408
|
+
.createSuccessNotice( __( 'Template reverted.' ), {
|
|
434
409
|
type: 'snackbar',
|
|
435
410
|
actions: [
|
|
436
411
|
{
|
|
@@ -438,72 +413,53 @@ export function* revertTemplate( template, { allowUndo = true } = {} ) {
|
|
|
438
413
|
onClick: undoRevert,
|
|
439
414
|
},
|
|
440
415
|
],
|
|
441
|
-
}
|
|
442
|
-
);
|
|
416
|
+
} );
|
|
443
417
|
} else {
|
|
444
|
-
|
|
445
|
-
noticesStore
|
|
446
|
-
|
|
447
|
-
__( 'Template reverted.' )
|
|
448
|
-
);
|
|
418
|
+
registry
|
|
419
|
+
.dispatch( noticesStore )
|
|
420
|
+
.createSuccessNotice( __( 'Template reverted.' ) );
|
|
449
421
|
}
|
|
450
422
|
} catch ( error ) {
|
|
451
423
|
const errorMessage =
|
|
452
424
|
error.message && error.code !== 'unknown_error'
|
|
453
425
|
? error.message
|
|
454
426
|
: __( 'Template revert failed. Please reload.' );
|
|
455
|
-
|
|
456
|
-
noticesStore
|
|
457
|
-
|
|
458
|
-
errorMessage,
|
|
459
|
-
{ type: 'snackbar' }
|
|
460
|
-
);
|
|
427
|
+
registry
|
|
428
|
+
.dispatch( noticesStore )
|
|
429
|
+
.createErrorNotice( errorMessage, { type: 'snackbar' } );
|
|
461
430
|
}
|
|
462
|
-
}
|
|
431
|
+
};
|
|
463
432
|
/**
|
|
464
|
-
*
|
|
433
|
+
* Action that opens an editor sidebar.
|
|
465
434
|
*
|
|
466
435
|
* @param {?string} name Sidebar name to be opened.
|
|
467
|
-
*
|
|
468
|
-
* @yield {Object} Action object.
|
|
469
436
|
*/
|
|
470
|
-
export
|
|
471
|
-
|
|
472
|
-
interfaceStore
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
name
|
|
476
|
-
);
|
|
477
|
-
}
|
|
437
|
+
export const openGeneralSidebar = ( name ) => ( { registry } ) => {
|
|
438
|
+
registry
|
|
439
|
+
.dispatch( interfaceStore )
|
|
440
|
+
.enableComplementaryArea( editSiteStoreName, name );
|
|
441
|
+
};
|
|
478
442
|
|
|
479
443
|
/**
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
* @yield {Object} Action object.
|
|
444
|
+
* Action that closes the sidebar.
|
|
483
445
|
*/
|
|
484
|
-
export
|
|
485
|
-
|
|
486
|
-
interfaceStore
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
);
|
|
490
|
-
}
|
|
446
|
+
export const closeGeneralSidebar = () => ( { registry } ) => {
|
|
447
|
+
registry
|
|
448
|
+
.dispatch( interfaceStore )
|
|
449
|
+
.disableComplementaryArea( editSiteStoreName );
|
|
450
|
+
};
|
|
491
451
|
|
|
492
|
-
export
|
|
493
|
-
|
|
494
|
-
type: 'SWITCH_MODE',
|
|
495
|
-
mode,
|
|
496
|
-
};
|
|
452
|
+
export const switchEditorMode = ( mode ) => ( { dispatch, registry } ) => {
|
|
453
|
+
dispatch( { type: 'SWITCH_MODE', mode } );
|
|
497
454
|
|
|
498
455
|
// Unselect blocks when we switch to a non visual mode.
|
|
499
456
|
if ( mode !== 'visual' ) {
|
|
500
|
-
|
|
457
|
+
registry.dispatch( blockEditorStore ).clearSelectedBlock();
|
|
501
458
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
speak( messages[ mode ], 'assertive' );
|
|
459
|
+
|
|
460
|
+
if ( mode === 'visual' ) {
|
|
461
|
+
speak( __( 'Visual editor selected' ), 'assertive' );
|
|
462
|
+
} else if ( mode === 'mosaic' ) {
|
|
463
|
+
speak( __( 'Mosaic view selected' ), 'assertive' );
|
|
508
464
|
}
|
|
509
|
-
}
|
|
465
|
+
};
|