-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathsite-editor.js
249 lines (218 loc) · 6.46 KB
/
site-editor.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* WordPress dependencies
*/
import { visitAdminPage } from '@wordpress/e2e-test-utils';
import { addQueryArgs } from '@wordpress/url';
/**
* @typedef {import('puppeteer-core').ElementHandle} ElementHandle
*/
const SELECTORS = {
navigationPanel: {
backToDashboard:
'.edit-site-navigation-panel .edit-site-navigation-panel__back-to-dashboard',
goBack: '.components-navigation__back-button',
isOpenState: '.edit-site-navigation-toggle.is-open',
menuItem: ( label ) =>
`//div[contains(@class, "edit-site-navigation-panel")]//button[.//*[text()="${ label }"]]`,
open: '.edit-site-navigation-toggle__button',
panelContainer: '.edit-site-navigation-panel',
},
visualEditor: '.edit-site-visual-editor iframe',
};
/**
* Searches for an item in the navigation panel with the label provided and clicks it.
*
* @param {string} label The label to search the menu item for.
*/
export async function clickSiteEditorMenuItem( label ) {
const item = await getSiteEditorMenuItem( label );
if ( item ) {
await item.click();
} else {
throw new Error(
`Navigation item with label ${ label } was not found.`
);
}
}
/**
* Closes the site editor navigation panel if open
*/
export async function closeSiteEditorNavigationPanel() {
const { navigationPanel } = SELECTORS;
const isOpen = !! ( await page.$( navigationPanel.isOpenState ) );
if ( isOpen ) {
await page.click( navigationPanel.open );
await page.waitForSelector( navigationPanel.panelContainer, {
hidden: true,
} );
}
}
/**
* Skips the welcome guide popping up to first time users of the site editor
*/
export async function disableSiteEditorWelcomeGuide() {
const isWelcomeGuideActive = await page.evaluate( () =>
wp.data.select( 'core/edit-site' ).isFeatureActive( 'welcomeGuide' )
);
const isWelcomeGuideStyesActive = await page.evaluate( () =>
wp.data
.select( 'core/edit-site' )
.isFeatureActive( 'welcomeGuideStyles' )
);
if ( isWelcomeGuideActive ) {
await page.evaluate( () =>
wp.data.dispatch( 'core/edit-site' ).toggleFeature( 'welcomeGuide' )
);
}
if ( isWelcomeGuideStyesActive ) {
await page.evaluate( () =>
wp.data
.dispatch( 'core/edit-site' )
.toggleFeature( 'welcomeGuideStyles' )
);
}
}
/**
* Returns a promise which resolves with the edited post content (HTML string).
*
* @return {Promise<string>} Promise resolving with post content markup.
*/
export function getCurrentSiteEditorContent() {
return page.evaluate( () => {
const postId = window.wp.data
.select( 'core/edit-site' )
.getEditedPostId();
const postType = window.wp.data
.select( 'core/edit-site' )
.getEditedPostType();
const record = window.wp.data
.select( 'core' )
.getEditedEntityRecord( 'postType', postType, postId );
if ( record ) {
if ( typeof record.content === 'function' ) {
return record.content( record );
} else if ( record.blocks ) {
return window.wp.blocks.__unstableSerializeAndClean(
record.blocks
);
} else if ( record.content ) {
return record.content;
}
}
return '';
} );
}
/**
* Searches for an item in the site editor navigation menu with the provided label.
*
* @param {string} label The label to search the menu item for.
*
* @return {Promise<?ElementHandle>} The menu item handle or `null`
*/
export async function getSiteEditorMenuItem( label ) {
const { navigationPanel } = SELECTORS;
const item = await page.waitForXPath( navigationPanel.menuItem( label ), {
visible: true,
} );
return item;
}
/**
* Returns `true` if in the site editor navigation root
*
* Checks whether the “Back to dashboard” button is visible. If
* not in the root, a “Back” button would be visible instead.
*
* @return {Promise<boolean>} Whether it currently is the navigation root or not
*/
export async function isSiteEditorRoot() {
const { navigationPanel } = SELECTORS;
const isBackToDashboardButtonVisible = !! ( await page.$(
navigationPanel.backToDashboard
) );
return isBackToDashboardButtonVisible;
}
/**
* Navigates the site editor back
*/
export async function navigateSiteEditorBack() {
const { navigationPanel } = SELECTORS;
await page.click( navigationPanel.goBack );
}
/**
* Goes back until it gets to the root
*/
export async function navigateSiteEditorBackToRoot() {
while ( ! ( await isSiteEditorRoot() ) ) {
await navigateSiteEditorBack();
}
}
/**
* Opens the site editor navigation panel if closed
*/
export async function openSiteEditorNavigationPanel() {
const { navigationPanel } = SELECTORS;
const isOpen = !! ( await page.$( navigationPanel.isOpenState ) );
if ( ! isOpen ) {
await page.click( navigationPanel.open );
await page.waitForSelector( navigationPanel.panelContainer );
}
}
/**
* Navigates through a sequence of links in the site editor navigation panel
*
* @param {string[] | string} labels Labels to navigate through
*/
export async function siteEditorNavigateSequence( labels ) {
if ( ! Array.isArray( labels ) ) {
labels = [ labels ];
}
for ( const label of labels ) {
await clickSiteEditorMenuItem( label );
}
}
/**
* Visits the Site Editor main page
*
* By default, it also skips the welcome guide. The option can be disabled if need be.
*
* @see disableSiteEditorWelcomeGuide
*
* @param {string} query String to be serialized as query portion of URL.
* @param {boolean} [skipWelcomeGuide=true] Whether to skip the welcome guide as part of the navigation.
*/
export async function visitSiteEditor( query, skipWelcomeGuide = true ) {
query = addQueryArgs( '', {
page: 'gutenberg-edit-site',
...query,
} ).slice( 1 );
await visitAdminPage( 'themes.php', query );
await page.waitForSelector( SELECTORS.visualEditor );
if ( skipWelcomeGuide ) {
await disableSiteEditorWelcomeGuide();
}
}
/**
* Toggles the global styles sidebar (opens it if closed and closes it if open).
*/
export async function toggleGlobalStyles() {
await page.click(
'.edit-site-header__actions button[aria-label="Styles"]'
);
}
/**
* Opens a global styles panel.
*
* @param {string} panelName Name of the panel that is going to be opened.
*/
export async function openGlobalStylesPanel( panelName ) {
const selector = `//div[@aria-label="Settings"]//button[.//*[text()="${ panelName }"]]`;
await ( await page.waitForXPath( selector ) ).click();
}
/**
* Opens the previous global styles panel.
*/
export async function openPreviousGlobalStylesPanel() {
await page.click(
'div[aria-label="Settings"] button[aria-label="Navigate to the previous view"]'
);
}