Skip to content

Commit

Permalink
Merge branch '5.1' into 5
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jan 27, 2024
2 parents 98951c0 + 80c3961 commit 1eadc84
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/Forms/HTMLEditor/HTMLEditorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,10 @@ public function __construct($name, $title = null, $value = '', $config = null)

public function getAttributes()
{
$config = $this->getEditorConfig();
// Fix CSS height based on rows
$rowHeight = $this->config()->get('fixed_row_height');
$attributes = [];
if ($rowHeight && ($config instanceof TinyMCEConfig)) {
$height = $this->getRows() * $rowHeight;
$attributes['style'] = sprintf('height: %dpx;', $height);
$config = clone $config;
$config->setOption('height', 'auto');
}
$config = $this->setEditorHeight($this->getEditorConfig());

// Merge attributes
return array_merge(
$attributes,
parent::getAttributes(),
$config->getAttributes()
);
Expand Down Expand Up @@ -190,7 +180,7 @@ public function Field($properties = [])
public function getSchemaStateDefaults()
{
$stateDefaults = parent::getSchemaStateDefaults();
$config = $this->getEditorConfig();
$config = $this->setEditorHeight($this->getEditorConfig());
$stateDefaults['data'] = $config->getConfigSchemaData();
return $stateDefaults;
}
Expand All @@ -204,4 +194,23 @@ public function ValueEntities()
{
return htmlentities($this->Value() ?? '', ENT_COMPAT, 'UTF-8', false);
}

/**
* Set height of editor based on number of rows
*/
private function setEditorHeight(HTMLEditorConfig $config): HTMLEditorConfig
{
$rowHeight = $this->config()->get('fixed_row_height');
if ($rowHeight && ($config instanceof TinyMCEConfig)) {
$rows = (int) $this->getRows();
$height = $rows * $rowHeight;
$config = clone $config;
if ($height) {
$config->setOption('height', 'auto');
$config->setOption('row_height', sprintf('%dpx', $height));
}
}

return $config;
}
}
39 changes: 39 additions & 0 deletions tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,43 @@ public function testFieldConfigSanitization()
$editor->saveInto($obj);
$this->assertEquals($htmlValue, $obj->Content, 'Table is removed');
}

public function testGetAttributes()
{
// Create an editor and set fixed_row_height to 0
$editor = HTMLEditorField::create('Content');
$editor->config()->set('fixed_row_height', 0);
// Get the attributes and config from the editor
$attributes = $editor->getAttributes();
$data_config = json_decode($attributes['data-config'], true);
// If fixed_row_height is 0 then row_height and height config are not set
$this->assertArrayNotHasKey('height', $data_config, 'Config height should not be set');
$this->assertArrayNotHasKey('row_height', $data_config, 'Config row_height should not be set');
// Set the fixed_row_height back to 20px
$editor->config()->set('fixed_row_height', 20);
// Set the rows to 0
$editor->setRows(0);
// Get the attributes and config from the editor
$attributes = $editor->getAttributes();
$data_config = json_decode($attributes['data-config'], true);
// If rows is 0 then row_height and height config are not set
$this->assertArrayNotHasKey('height', $data_config, 'Config height should not be set');
$this->assertArrayNotHasKey('row_height', $data_config, 'Config row_height should not be set');
// Set the rows to 5
$editor->setRows(5);
// Get the attributes and config from the editor
$attributes = $editor->getAttributes();
$data_config = json_decode($attributes['data-config']);
// Check the height is set to auto and the row height is set to 100px (5 rows * 20px)
$this->assertEquals("auto", $data_config->height, 'Config height is not set');
$this->assertEquals("100px", $data_config->row_height, 'Config row_height is not set');
// Change the row height to 60px and set the rows to 3
$editor->setRows(3);
// Get the attributes and config from the editor
$attributes = $editor->getSchemaStateDefaults();
$data_config = json_decode($attributes['data']['attributes']['data-config']);
// Check the height is set to auto and the row height is set to 60px (3 rows * 20px)
$this->assertEquals("auto", $data_config->height, 'Config height is not set');
$this->assertEquals("60px", $data_config->row_height, 'Config row_height is not set');
}
}

0 comments on commit 1eadc84

Please sign in to comment.