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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import { Locator, Page } from 'playwright';
import envVariables from '../../env-variables';
import { translateFromPage } from '../utils';
import { EditorComponent } from './editor-component';
import type { EditorPreviewOptions, EditorToolbarSettingsButton } from './types';

const panel = '.interface-navigable-region[class*="header"]';
const moreOptionsLabel = 'Options';
const selectors = {
	// Block Inserter
	// Note the partial class match. This is to support site and post editor. We can't use aria-label because of i18n. :(
	blockInserterButton: `${ panel } button[class*="inserter-toggle"]`,

	// Draft
	switchToDraftButton: `${ panel } button.editor-post-switch-to-draft`,

	// Preview
	previewButton: `${ panel } :text("View"):visible, [aria-label="View"]:visible`,

	// Post status
	postStatusButton: '.editor-post-status > button',

	desktopPreviewMenuItem: ( target: EditorPreviewOptions ) =>
		`button[role*="menuitem"] span:text-is("${ target }")`,
	previewPane: '.edit-post-visual-editor',

	// Publish
	publishButton: ( state: 'disabled' | 'enabled' ) => {
		const buttonState = state === 'disabled' ? 'true' : 'false';
		return `${ panel } button.editor-post-publish-button__button[aria-disabled="${ buttonState }"]`;
	},

	// Document overview
	documentOverviewButton: `${ panel } button[aria-label="Document Overview"]`,

	// Details popover
	detailsButton: `${ panel } button[aria-label="Details"]`,

	// Document Actions dropdown
	documentActionsDropdown: `${ panel } button[aria-label="Show template details"]`,
	documentActionsDropdownItem: ( itemSelector: string ) => `.popover-slot ${ itemSelector }`,
	documentActionsDropdownShowAll: '.popover-slot .edit-site-template-details__show-all-button',

	// Undo/Redo
	undoButton: 'button[aria-disabled=false][aria-label="Undo"]',
	redoButton: 'button[aria-disabled=false][aria-label="Redo"]',

	// More options
	moreOptionsButton: ( label = moreOptionsLabel ) => `${ panel } button[aria-label="${ label }"]`,

	// Site editor save
	saveSiteEditorButton: `${ panel } button.edit-site-save-button__button`,

	// Nav sidebar
	navSidebarButton:
		'button[aria-label="Block editor sidebar"],button[aria-label="Toggle navigation"]',
};

/**
 * Represents an instance of the WordPress.com Editor's persistent toolbar.
 */
export class EditorToolbarComponent {
	private page: Page;
	private editor: EditorComponent;

	/**
	 * Constructs an instance of the component.
	 *
	 * @param {Page} page The underlying page.
	 * @param {EditorComponent} editor The EditorComponent instance.
	 */
	constructor( page: Page, editor: EditorComponent ) {
		this.page = page;
		this.editor = editor;
	}

	/**
	 * Translate string.
	 */
	private async translateFromPage( string: string, context?: string ): Promise< string > {
		const editorParent = await this.editor.parent();
		return translateFromPage( editorParent, string, context );
	}

	/* General helper */

	/**
	 * Given a Locator, determines whether the target button/toggle is
	 * in an expanded state.
	 *
	 * If the toggle is in the on state or otherwise in an expanded
	 * state, this method will return true. Otherwise, false.
	 *
	 * @param {Locator} target Target button.
	 * @returns {Promise<boolean>} True if target is in an expanded state. False otherwise.
	 */
	private async targetIsOpen( target: Locator ): Promise< boolean > {
		const checked = await target.getAttribute( 'aria-checked' );
		const pressed = await target.getAttribute( 'aria-pressed' );
		const expanded = await target.getAttribute( 'aria-expanded' );
		return checked === 'true' || pressed === 'true' || expanded === 'true';
	}

	/* Block Inserter */

	/**
	 * Opens the block inserter.
	 */
	async openBlockInserter(): Promise< void > {
		const editorParent = await this.editor.parent();

		// TODO: Once WordPress/gutenberg#63669 is everywhere, remove the old name.
		const translatedButtonNameOld = await this.translateFromPage(
			'Toggle block inserter',
			'Generic label for block inserter button'
		);
		const translatedButtonNameNew = await this.translateFromPage(
			'Block Inserter',
			'Generic label for block inserter button'
		);
		const blockInserterButton = editorParent.getByRole( 'button', {
			name: new RegExp( `^(${ translatedButtonNameOld }|${ translatedButtonNameNew })` ),
			exact: true,
		} );

		if ( ! ( await this.targetIsOpen( blockInserterButton ) ) ) {
			const editorParent = await this.editor.parent();
			const locator = editorParent.locator( selectors.blockInserterButton );
			await locator.click();
		}
	}

	/**
	 * Closes the block inserter.
	 */
	async closeBlockInserter(): Promise< void > {
		const editorParent = await this.editor.parent();

		// TODO: Once WordPress/gutenberg#63669 is everywhere, remove the old name.
		const translatedButtonNameOld = await this.translateFromPage(
			'Toggle block inserter',
			'Generic label for block inserter button'
		);
		const translatedButtonNameNew = await this.translateFromPage(
			'Block Inserter',
			'Generic label for block inserter button'
		);
		const blockInserterButton = editorParent.getByRole( 'button', {
			name: new RegExp( `^(${ translatedButtonNameOld }|${ translatedButtonNameNew })` ),
			exact: true,
		} );

		if ( await this.targetIsOpen( blockInserterButton ) ) {
			// We click on the panel instead of on the block inserter button as a workaround for an issue
			// that disables the block inserter button after inserting a block using the block API V2.
			// See https://github.com/WordPress/gutenberg/issues/43090.
			const editorParent = await this.editor.parent();
			const locator = editorParent.locator( panel ).locator( 'button[class*="inserter-toggle"]' );
			await locator.click();
		}
	}

	/* Draft */

	/**
	 * Clicks the Save draft button and waits until it's saved.
	 */
	async saveDraft(): Promise< void > {
		const editorParent = await this.editor.parent();
		// On a Simple site (desktop viewport) the domain upsell banner can
		// be covering the "Save draft" button. We need to close that banner to
		// be able to perform the click action.
		// See https://github.com/Automattic/wp-calypso/pull/76987
		await Promise.any( [
			editorParent.getByRole( 'button', { name: 'Save draft' } ).click( { trial: true } ),
		] );

		await editorParent.getByRole( 'button', { name: 'Save draft' } ).click();
		await editorParent.getByRole( 'button', { name: 'Saved' } ).waitFor();
	}

	/* Preview */

	/**
	 * Launches the Preview when in mobile mode.
	 *
	 * @returns {Page} Handler for the new page object.
	 */
	async openMobilePreview(): Promise< Page > {
		const editorParent = await this.editor.parent();
		const mobilePreviewButtonLocator = editorParent.locator( selectors.previewButton );

		const [ popup ] = await Promise.all( [
			this.page.waitForEvent( 'popup' ),
			mobilePreviewButtonLocator.click(),
		] );
		return popup;
	}

	/**
	 * Launches the Preview when in Desktop mode, then selects the
	 * target preview option.
	 */
	async openDesktopPreview( target: EditorPreviewOptions ): Promise< void > {
		// Click on the Preview button to open the menu.
		await this.openDesktopPreviewMenu();

		// Locate and click on the intended preview target.
		const editorParent = await this.editor.parent();
		const desktopPreviewMenuItemLocator = editorParent.locator(
			selectors.desktopPreviewMenuItem( target )
		);
		await desktopPreviewMenuItemLocator.click();

		// Verify the editor panel is resized and stable.
		const desktopPreviewPaneLocator = editorParent.locator( selectors.previewPane );
		await desktopPreviewPaneLocator.waitFor();
		const elementHandle = await desktopPreviewPaneLocator.elementHandle();
		await elementHandle?.waitForElementState( 'stable' );

		// Click on the Preview button to close the menu.
		await this.closeDesktopPreviewMenu();
	}

	/**
	 * Opens the Preview menu for Desktop viewport.
	 */
	async openDesktopPreviewMenu(): Promise< void > {
		const editorParent = await this.editor.parent();

		const translatedButtonName = await this.translateFromPage( 'View' );
		const previewButton = editorParent.getByRole( 'button', {
			name: translatedButtonName,
			exact: true,
		} );

		if ( ! ( await this.targetIsOpen( previewButton ) ) ) {
			const editorParent = await this.editor.parent();
			const desktopPreviewButtonLocator = editorParent.locator( selectors.previewButton );
			await desktopPreviewButtonLocator.click();
		}
	}

	/**
	 * Closes the Preview menu for the Desktop viewport.
	 */
	async closeDesktopPreviewMenu(): Promise< void > {
		const editorParent = await this.editor.parent();

		const translatedButtonName = await this.translateFromPage( 'View' );
		const previewButton = editorParent.getByRole( 'button', {
			name: translatedButtonName,
			exact: true,
		} );

		if ( await this.targetIsOpen( previewButton ) ) {
			const editorParent = await this.editor.parent();
			const desktopPreviewButtonLocator = editorParent.locator( selectors.previewButton );
			await desktopPreviewButtonLocator.click();
		}
	}

	/* Publish and unpublish */

	/**
	 * Waits for the save/publish button.
	 *
	 * @returns {Promise<string>} String found on the button.
	 */
	async waitForPublishButton(): Promise< void > {
		const editorParent = await this.editor.parent();
		const publishButtonLocator = editorParent.locator( selectors.publishButton( 'enabled' ) );

		await publishButtonLocator.waitFor();
	}

	/**
	 * Clicks on the primary button to publish the article.
	 *
	 * This is applicable for the following scenarios:
	 * 	- publish of a new article (Publish)
	 * 	- update/save an existing article (Update)
	 * 	- schedule a post (Schedule)
	 */
	async clickPublish(): Promise< void > {
		const editorParent = await this.editor.parent();
		const publishButtonLocator = editorParent.locator( selectors.publishButton( 'enabled' ) );
		await publishButtonLocator.click();
	}

	/**
	 * Clicks on the `Switch to Draft` button and unpublish
	 * the article.
	 */
	async switchToDraft(): Promise< void > {
		const editorParent = await this.editor.parent();

		await Promise.race( [
			( async () => {
				// Works with Gutenberg >=v18.2.0
				await editorParent.locator( selectors.postStatusButton ).click();
				await editorParent.getByRole( 'radio', { name: 'Draft' } ).click();
			} )(),
			( async () => {
				// Works with Gutenberg >=v15.8.0
				await this.openSettings( 'Settings' );
				await editorParent.getByRole( 'button', { name: 'Switch to draft' } ).click();
			} )(),
			( async () => {
				// Works with Gutenberg <v15.8.0
				await editorParent.getByRole( 'button', { name: 'Draft' } ).click();
			} )(),
		] );
	}

	/* Editor Settings sidebar */

	/**
	 * Opens the editor settings.
	 */
	async openSettings( target: EditorToolbarSettingsButton ): Promise< void > {
		const editorParent = await this.editor.parent();

		// To support i18n tests.
		const translatedTargetName = await this.translateFromPage( target );

		let button = editorParent
			.locator( '.editor-header__settings, .edit-post-header__settings' )
			.getByLabel( translatedTargetName );

		// For other pinned settings, we need to open the options menu
		// because those are hidden on mobile/small screens
		if ( target !== 'Settings' ) {
			await this.openMoreOptionsMenu();

			button = editorParent.getByRole( 'menuitemcheckbox', { name: translatedTargetName } );
		}

		if ( await this.targetIsOpen( button ) ) {
			await this.closeMoreOptionsMenu();
			return;
		}

		await button.click();
	}

	/**
	 * Closes the editor settings.
	 */
	async closeSettings(): Promise< void > {
		const editorParent = await this.editor.parent();

		// Post/Page settings and Jetpack settings close buttons have slightly
		// different names. When building the accessible selector, a RegExp
		// must be used in order to support multiple accessible names.
		const translatedCloseSettingsName = await this.translateFromPage( 'Close Settings' );
		const translatedCloseJetpackSettingsName = await this.translateFromPage( 'Close plugin' );

		const buttonNames =
			envVariables.VIEWPORT_NAME === 'mobile'
				? 'Settings'
				: `${ translatedCloseJetpackSettingsName }|${ translatedCloseSettingsName }`;

		const button = editorParent.getByRole( 'button', {
			name: new RegExp( buttonNames ),
		} );

		if ( ! ( await this.targetIsOpen( button ) ) ) {
			return;
		}

		await button.click();
	}

	/**
	 * Closes the editor.
	 *
	 * Clicks the `W` logo in the corner to close the editor.
	 */
	async closeEditor(): Promise< void > {
		const editorParent = await this.editor.parent();

		const target = editorParent.getByRole( 'link', {
			name: 'View Posts',
		} );
		if ( await this.targetIsOpen( target ) ) {
			return;
		}

		await target.click();
	}

	/* List view */

	/**
	 * Opens the list view.
	 */
	async openListView(): Promise< void > {
		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			// List view is not available on mobile!
			return;
		}

		const editorParent = await this.editor.parent();

		const target = editorParent.getByRole( 'button', {
			name: 'Document Overview',
		} );

		if ( await this.targetIsOpen( target ) ) {
			return;
		}

		await target.click();
	}

	/**
	 * Closes the list view.
	 */
	async closeListView(): Promise< void > {
		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			// List view is not available on mobile!
			return;
		}

		const editorParent = await this.editor.parent();

		const target = editorParent.getByRole( 'button', {
			name: 'Document Overview',
		} );

		if ( ! ( await this.targetIsOpen( target ) ) ) {
			return;
		}

		await target.click();
	}

	/**
	 * Click the editor undo button. Throws an error if the button is not enabled.
	 *
	 * @throws If the undo button is not enabled.
	 */
	async undo(): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.undoButton );
		await locator.click();
	}

	/**
	 * Click the editor redo button. Throws an error if the button is not enabled.
	 *
	 * @throws If the redo button is not enabled.
	 */
	async redo(): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.redoButton );
		await locator.click();
	}

	/**
	 * Opens the more options menu (three dots).
	 */
	async openMoreOptionsMenu(): Promise< void > {
		const button = await this.getMoreOptionsButton();

		if ( await this.targetIsOpen( button ) ) {
			return;
		}

		await button.click();
	}

	/**
	 * Closes the more options menu.
	 */
	async closeMoreOptionsMenu(): Promise< void > {
		const button = await this.getMoreOptionsButton();

		if ( await this.targetIsOpen( button ) ) {
			await button.click();
		}
	}

	/**
	 * Returns the more options button instance.
	 */
	async getMoreOptionsButton() {
		const editorParent = await this.editor.parent();

		// To support i18n tests.
		const translatedTargetName = await this.translateFromPage( 'Options' );
		// Narrowing down to the "Editor top bar" is needed because it might conflict with
		// the options button for the block toolbar, causing a strict-mode violation error
		// due to duplicate elements on the page.
		return editorParent.getByLabel( 'Editor top bar' ).getByRole( 'button', {
			name: translatedTargetName,
			exact: true,
		} );
	}

	/** FSE unique buttons */

	/**
	 * Click the save button (publish equivalent) for the full site editor.
	 */
	async saveSiteEditor(): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.saveSiteEditorButton );
		await locator.click();
	}

	/**
	 * Click the document actions icon for the full site editor.
	 */
	async clickDocumentActionsIcon(): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.documentActionsDropdown );
		await locator.click();
	}

	/**
	 * Click the document actions icon for the full site editor.
	 */
	async clickDocumentActionsDropdownItem( itemName: string ): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.documentActionsDropdownItem( itemName ) );
		await locator.click();
	}
}