File size: 2,278 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
import { Page } from 'playwright';
import { EditorComponent } from './editor-component';
import { EditorPopoverMenuComponent } from './editor-popover-menu-component';

const selectors = {
	paddingInput: 'input[aria-label="Padding"]',
	optionsButton: 'button[aria-label="Dimensions options"]',
};

export interface DimensionsSettings {
	padding?: number;
	margin?: number;
}

/**
 * Represents a dimenstions settings component (used in blocks and site styles).
 */
export class EditorDimensionsComponent {
	private page: Page;
	private editor: EditorComponent;

	// Usually low-level components don't contain other components like this.
	// In this case however, because the popover is always used whereever this component is,
	// and tieing it together at a higher level is kind of messy, it makes sense to add it here.
	private editorPopoverMenuComponent: EditorPopoverMenuComponent;

	/**
	 * 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.editorPopoverMenuComponent = new EditorPopoverMenuComponent( page, editor );
	}

	/**
	 * Set dimension settings.
	 *
	 * @param {DimensionsSettings} settings Settings to set. Only properties provided will be set.
	 */
	async setDimensions( settings: DimensionsSettings ): Promise< void > {
		if ( settings.margin !== undefined ) {
			throw new Error( 'Margin is not yet implemented.' );
		}

		if ( settings.padding !== undefined ) {
			await this.setPadding( settings.padding );
		}
	}

	/**
	 * Reset all of the dimension settings.
	 */
	async resetAll(): Promise< void > {
		const editorParent = await this.editor.parent();
		const optionsButtonLocator = editorParent.locator( selectors.optionsButton );
		await optionsButtonLocator.click();
		await this.editorPopoverMenuComponent.clickMenuButton( 'Reset all' );
	}

	/**
	 * Sets the padding.
	 *
	 * @param {number} padding Padding dimension to select.
	 */
	private async setPadding( padding: number ): Promise< void > {
		const editorParent = await this.editor.parent();
		await editorParent.getByLabel( 'All sides padding' ).fill( padding.toString() );
	}
}