File size: 5,710 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Page } from 'playwright';
import { DimensionsSettings, EditorDimensionsComponent } from './editor-dimensions-component';
import {
	ColorSettings,
	EditorColorPickerComponent,
	EditorTypographyComponent,
	EditorComponent,
	TypographySettings,
} from '.';

const parentSelector = '.edit-site-global-styles-sidebar';

const selectors = {
	menuButton: ( buttonName: string ) =>
		`${ parentSelector } button.components-navigator-button:has-text("${ buttonName }")`,
	closeSidebarButton: 'button:visible[aria-label="Close Styles sidebar"]',
	backButton: `${ parentSelector } button[aria-label="Navigate to the previous view"]`,
	moreActionsMenuButton: `${ parentSelector } button[aria-label="More Styles actions"]`,
	styleVariation: ( styleVariationName: string ) =>
		`${ parentSelector } .edit-site-global-styles-variations_item[aria-label="${ styleVariationName }"]`,
};

export type ColorLocation = 'Background' | 'Text' | 'Links';

/**
 * Represents the site editor site styles sidebar/panel.
 */
export class EditorSiteStylesComponent {
	private page: Page;
	private editor: EditorComponent;

	private editorColorPickerComponent: EditorColorPickerComponent;
	private editorTypographyComponent: EditorTypographyComponent;
	private editorDimensionsComponent: EditorDimensionsComponent;

	/**
	 * 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;
		this.editorColorPickerComponent = new EditorColorPickerComponent( page, editor );
		this.editorTypographyComponent = new EditorTypographyComponent( page, editor );
		this.editorDimensionsComponent = new EditorDimensionsComponent( page, editor );
	}

	/**
	 * Checks if the site styles sidebar/panel is open.
	 * Not reliable for immediate validation after an open/close action,
	 * but can be used to determine starting state.
	 *
	 * @returns true if the site styles sidebar/panel is open, false otherwise.
	 */
	async siteStylesIsOpen(): Promise< boolean > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( parentSelector );
		return ( await locator.count() ) > 0;
	}

	/**
	 * Closes the site styles sidebar/panel.
	 */
	async closeSiteStyles(): Promise< void > {
		if ( await this.siteStylesIsOpen() ) {
			const editorParent = await this.editor.parent();
			const locator = editorParent.locator( selectors.closeSidebarButton );
			await locator.click();
		}
	}

	/**
	 * Sets a color style setting globaly for the site.
	 * This auto-handles returning to top menu and navigating down.
	 *
	 * @param {ColorLocation} colorLocation What part of the site we are updating the color for.
	 * @param {ColorSettings} colorSettings Settings for the color to set.
	 */
	async setGlobalColor(
		colorLocation: ColorLocation,
		colorSettings: ColorSettings
	): Promise< void > {
		await this.returnToTopMenu();
		await this.clickMenuButton( 'Colors' );
		await this.clickMenuButton( colorLocation );
		await this.editorColorPickerComponent.setColor( colorSettings );
	}

	// In future: setBlockColor()

	// In future: setGlobalTypography()

	/**
	 * Sets a typography style for a block.
	 * This auto-handles returning to top menu and navigating down.
	 *
	 * @param {string} blockName Block name (as appears in list).
	 * @param {TypographySettings} typographySettings Typography settings to set.
	 */
	async setBlockTypography(
		blockName: string,
		typographySettings: TypographySettings
	): Promise< void > {
		await this.returnToTopMenu();
		await this.clickMenuButton( 'Blocks' );
		await this.clickMenuButton( blockName );
		await this.clickMenuButton( 'Typography' );
		await this.editorTypographyComponent.setTypography( typographySettings );
	}

	/**
	 * Set global layout settings for the site.
	 * Note that only the "Padding" dimension is available globally.
	 *
	 * @param {DimensionsSettings} settings The dimensions settings to set.
	 */
	async setGlobalLayout( settings: DimensionsSettings ): Promise< void > {
		await this.returnToTopMenu();
		await this.clickMenuButton( 'Layout' );
		await this.editorDimensionsComponent.setDimensions( settings );
	}

	/**
	 * Reset the global layout dimensions.
	 * (To empty layout defaults, not the theme defaults.)
	 */
	async resetGlobalLayout(): Promise< void > {
		await this.returnToTopMenu();
		await this.clickMenuButton( 'Layout' );
		await this.editorDimensionsComponent.resetAll();
	}

	/**
	 * Clicks a menu button in the site styles sidebar/panel.
	 *
	 * @param {string} buttonName Button name.
	 */
	async clickMenuButton( buttonName: string ): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.menuButton( buttonName ) );
		await locator.click();
	}

	/**
	 * Returns to the top-level menu in the site styles sidebar/panel.
	 */
	async returnToTopMenu(): Promise< void > {
		const editorParent = await this.editor.parent();
		const backButtonLocator = editorParent.locator( selectors.backButton );
		// The DOM node of the current active panel is directly replaced on re-render.
		// This means that we can safely rely on "count()" as an indicator of if there's
		// back navigation to do.
		while ( ( await backButtonLocator.count() ) > 0 ) {
			await backButtonLocator.click();
		}
	}

	/**
	 * Open the more actions menu in the site styles sidebar/panel.
	 */
	async openMoreActionsMenu(): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.moreActionsMenuButton );
		await locator.click();
	}
}