Merge pull request #11114 from creative-commoners/pulls/5.1/fix-elemental-htmleditor

FIX HTMLEditorField::setRows with Elemental
This commit is contained in:
Guy Sartorelli 2024-01-25 15:13:42 +13:00 committed by GitHub
commit 80c3961b03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 12 deletions

View File

@ -114,20 +114,10 @@ class HTMLEditorField extends TextareaField
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()
);
@ -190,7 +180,7 @@ class HTMLEditorField extends TextareaField
public function getSchemaStateDefaults()
{
$stateDefaults = parent::getSchemaStateDefaults();
$config = $this->getEditorConfig();
$config = $this->setEditorHeight($this->getEditorConfig());
$stateDefaults['data'] = $config->getConfigSchemaData();
return $stateDefaults;
}
@ -204,4 +194,23 @@ class HTMLEditorField extends TextareaField
{
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;
}
}

View File

@ -267,4 +267,43 @@ EOS
$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');
}
}