Add blocks preprocessor
Currently, we need to wrap top-level non-column blocks into a single column. This is done in the preprocessor. [MAILPOET-5540]
This commit is contained in:
committed by
Jan Lysý
parent
69a87e8146
commit
968ff6754e
52
mailpoet/lib/EmailEditor/Engine/Renderer/Preprocessor.php
Normal file
52
mailpoet/lib/EmailEditor/Engine/Renderer/Preprocessor.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\EmailEditor\Engine\Renderer;
|
||||
|
||||
class Preprocessor {
|
||||
|
||||
const SINGLE_COLUMN_TEMPLATE = [
|
||||
'blockName' => 'core/columns',
|
||||
'attrs' => [],
|
||||
'innerBlocks' => [[
|
||||
'blockName' => 'core/column',
|
||||
'attrs' => [],
|
||||
'innerBlocks' => [],
|
||||
]],
|
||||
];
|
||||
|
||||
public function preprocess(array $parsedBlocks): array {
|
||||
return $this->addTopLevelColumns($parsedBlocks);
|
||||
}
|
||||
|
||||
/**
|
||||
* In the editor we allow putting content blocks directly into the root level of the email.
|
||||
* But for rendering purposes it is more convenient to have them wrapped in a single column.
|
||||
* This method walks through the first level of blocks and wraps non column blocks into a single column.
|
||||
*/
|
||||
private function addTopLevelColumns(array $parsedBlocks): array {
|
||||
$wrappedParsedBlocks = [];
|
||||
$nonColumnsBlocksBuffer = [];
|
||||
foreach ($parsedBlocks as $block) {
|
||||
// The next block is columns so we can flush the buffer and add the columns block
|
||||
if ($block['blockName'] === 'core/columns') {
|
||||
if ($nonColumnsBlocksBuffer) {
|
||||
$columnsBlock = self::SINGLE_COLUMN_TEMPLATE;
|
||||
$columnsBlock['innerBlocks'][0]['innerBlocks'] = $nonColumnsBlocksBuffer;
|
||||
$nonColumnsBlocksBuffer = [];
|
||||
$wrappedParsedBlocks[] = $columnsBlock;
|
||||
}
|
||||
$wrappedParsedBlocks[] = $block;
|
||||
continue;
|
||||
}
|
||||
// Non columns block so we add it to the buffer
|
||||
$nonColumnsBlocksBuffer[] = $block;
|
||||
}
|
||||
// Flush the buffer if there are any blocks left
|
||||
if ($nonColumnsBlocksBuffer) {
|
||||
$columnsBlock = self::SINGLE_COLUMN_TEMPLATE;
|
||||
$columnsBlock['innerBlocks'][0]['innerBlocks'] = $nonColumnsBlocksBuffer;
|
||||
$wrappedParsedBlocks[] = $columnsBlock;
|
||||
}
|
||||
return $wrappedParsedBlocks;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user