Remove top level processor in favour of wrapping column in template, fix column padding
This commit is contained in:
committed by
Rostislav Wolný
parent
b4acac7c52
commit
e0d27db790
@@ -1,56 +0,0 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors;
|
||||
|
||||
class TopLevelPreprocessor implements Preprocessor {
|
||||
const SINGLE_COLUMN_TEMPLATE = [
|
||||
'blockName' => 'core/columns',
|
||||
'attrs' => [],
|
||||
'innerBlocks' => [[
|
||||
'blockName' => 'core/column',
|
||||
'attrs' => [],
|
||||
'innerBlocks' => [],
|
||||
]],
|
||||
];
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public function preprocess(array $parsedBlocks, array $layout, array $styles): array {
|
||||
$wrappedParsedBlocks = [];
|
||||
$nonColumnsBlocksBuffer = [];
|
||||
foreach ($parsedBlocks as $block) {
|
||||
$blockAlignment = $block['attrs']['align'] ?? null;
|
||||
// The next block is columns so we can flush the buffer and add the columns block
|
||||
if ($block['blockName'] === 'core/columns' || $blockAlignment === 'full') {
|
||||
if ($nonColumnsBlocksBuffer) {
|
||||
$columnsBlock = self::SINGLE_COLUMN_TEMPLATE;
|
||||
$columnsBlock['innerBlocks'][0]['innerBlocks'] = $nonColumnsBlocksBuffer;
|
||||
$nonColumnsBlocksBuffer = [];
|
||||
$wrappedParsedBlocks[] = $columnsBlock;
|
||||
}
|
||||
// If the block is full width and is not core/columns, we need to wrap it in a single column block, and it the columns block has to contain only the block
|
||||
if ($blockAlignment === 'full' && $block['blockName'] !== 'core/columns') {
|
||||
$columnsBlock = self::SINGLE_COLUMN_TEMPLATE;
|
||||
$columnsBlock['attrs']['align'] = 'full';
|
||||
$columnsBlock['innerBlocks'][0]['innerBlocks'] = [$block];
|
||||
$wrappedParsedBlocks[] = $columnsBlock;
|
||||
continue;
|
||||
}
|
||||
$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;
|
||||
}
|
||||
}
|
@@ -9,7 +9,6 @@ use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\BlocksWid
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\CleanupPreprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\Preprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\SpacingPreprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\TopLevelPreprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\TypographyPreprocessor;
|
||||
|
||||
class ProcessManager {
|
||||
@@ -21,7 +20,6 @@ class ProcessManager {
|
||||
|
||||
public function __construct(
|
||||
CleanupPreprocessor $cleanupPreprocessor,
|
||||
TopLevelPreprocessor $topLevelPreprocessor,
|
||||
BlocksWidthPreprocessor $blocksWidthPreprocessor,
|
||||
TypographyPreprocessor $typographyPreprocessor,
|
||||
SpacingPreprocessor $spacingPreprocessor,
|
||||
@@ -29,7 +27,6 @@ class ProcessManager {
|
||||
VariablesPostprocessor $variablesPostprocessor
|
||||
) {
|
||||
$this->registerPreprocessor($cleanupPreprocessor);
|
||||
$this->registerPreprocessor($topLevelPreprocessor);
|
||||
$this->registerPreprocessor($blocksWidthPreprocessor);
|
||||
$this->registerPreprocessor($typographyPreprocessor);
|
||||
$this->registerPreprocessor($spacingPreprocessor);
|
||||
|
@@ -52,8 +52,6 @@ class Columns extends AbstractBlockRenderer {
|
||||
$contentCSS = WP_Style_Engine::compile_css($cellStyles, '');
|
||||
$layoutCSS = WP_Style_Engine::compile_css([
|
||||
'margin-top' => $parsedBlock['email_attrs']['margin-top'] ?? '0px',
|
||||
'padding-left' => $block_attributes['align'] !== 'full' ? $settingsController->getEmailStyles()['spacing']['padding']['left'] : '0px',
|
||||
'padding-right' => $block_attributes['align'] !== 'full' ? $settingsController->getEmailStyles()['spacing']['padding']['right'] : '0px',
|
||||
], '');
|
||||
$tableWidth = $block_attributes['align'] !== 'full' ? $block_attributes['width'] : '100%';
|
||||
|
||||
|
@@ -1,110 +0,0 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace unit\EmailEditor\Engine\Renderer\Preprocessors;
|
||||
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\TopLevelPreprocessor;
|
||||
|
||||
class TopLevelPreprocessorTest extends \MailPoetUnitTest {
|
||||
|
||||
private $paragraphBlock = [
|
||||
'blockName' => 'core/paragraph',
|
||||
'attrs' => [],
|
||||
'innerHTML' => 'Paragraph content',
|
||||
];
|
||||
|
||||
private $headingBlock = [
|
||||
'blockName' => 'core/heading',
|
||||
'attrs' => [],
|
||||
'innerHTML' => 'Paragraph content',
|
||||
];
|
||||
|
||||
private $columnsBlock = [
|
||||
'blockName' => 'core/columns',
|
||||
'attrs' => [],
|
||||
'innerBlocks' => [[
|
||||
'blockName' => 'core/column',
|
||||
'attrs' => [],
|
||||
'innerBlocks' => [],
|
||||
]],
|
||||
];
|
||||
|
||||
/** @var TopLevelPreprocessor */
|
||||
private $preprocessor;
|
||||
|
||||
/** @var array{contentSize: string} */
|
||||
private array $layout;
|
||||
|
||||
/** @var array{spacing: array{padding: array{bottom: string, left: string, right: string, top: string}, blockGap: string}} $styles */
|
||||
private array $styles;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
$this->preprocessor = new TopLevelPreprocessor();
|
||||
$this->layout = ['contentSize' => '660px'];
|
||||
$this->styles = ['spacing' => ['padding' => ['left' => '10px', 'right' => '10px', 'top' => '10px', 'bottom' => '10px'], 'blockGap' => '10px']];
|
||||
}
|
||||
|
||||
public function testItWrapsSingleTopLevelBlockIntoColumns() {
|
||||
$parsedDocument = [$this->paragraphBlock];
|
||||
$result = $this->preprocessor->preprocess($parsedDocument, $this->layout, $this->styles);
|
||||
verify($result[0]['blockName'])->equals('core/columns');
|
||||
verify($result[0]['innerBlocks'][0]['blockName'])->equals('core/column');
|
||||
verify($result[0]['innerBlocks'][0]['innerBlocks'][0]['blockName'])->equals('core/paragraph');
|
||||
verify($result[0]['innerBlocks'][0]['innerBlocks'][0]['innerHTML'])->equals('Paragraph content');
|
||||
}
|
||||
|
||||
public function testItDoesntWrapColumns() {
|
||||
$parsedDocumentWithMultipleColumns = [$this->columnsBlock, $this->columnsBlock];
|
||||
$result = $this->preprocessor->preprocess($parsedDocumentWithMultipleColumns, $this->layout, $this->styles);
|
||||
verify($result)->equals($parsedDocumentWithMultipleColumns);
|
||||
}
|
||||
|
||||
public function testItWrapsTopLevelBlocksSpreadBetweenColumns() {
|
||||
$parsedDocument = [$this->paragraphBlock, $this->columnsBlock, $this->paragraphBlock, $this->paragraphBlock];
|
||||
// We expect to wrap top level paragraph blocks into columns so the result should three columns blocks
|
||||
$result = $this->preprocessor->preprocess($parsedDocument, $this->layout, $this->styles);
|
||||
verify($result)->arrayCount(3);
|
||||
// First columns contain columns with one paragraph block
|
||||
verify($result[0]['innerBlocks'][0]['blockName'])->equals('core/column');
|
||||
verify($result[0]['innerBlocks'][0]['innerBlocks'][0]['blockName'])->equals('core/paragraph');
|
||||
// Second columns remains empty
|
||||
verify($result[1]['innerBlocks'][0]['blockName'])->equals('core/column');
|
||||
verify($result[1]['innerBlocks'][0]['innerBlocks'])->empty();
|
||||
// Third columns contain columns with two paragraph blocks
|
||||
verify($result[2]['innerBlocks'][0]['blockName'])->equals('core/column');
|
||||
verify($result[2]['innerBlocks'][0]['innerBlocks'])->arrayCount(2);
|
||||
verify($result[2]['innerBlocks'][0]['innerBlocks'][0]['blockName'])->equals('core/paragraph');
|
||||
verify($result[2]['innerBlocks'][0]['innerBlocks'][1]['blockName'])->equals('core/paragraph');
|
||||
}
|
||||
|
||||
public function testItWrapsFullWidthBlocksIntoColumns(): void {
|
||||
$parsedDocument = [$this->paragraphBlock, $this->columnsBlock, $this->headingBlock, $this->paragraphBlock, $this->headingBlock, $this->columnsBlock];
|
||||
$parsedDocument[0]['attrs']['align'] = 'full';
|
||||
$parsedDocument[4]['attrs']['align'] = 'full';
|
||||
$parsedDocument[5]['attrs']['align'] = 'full';
|
||||
$parsedDocument[5]['innerBlocks'][0]['innerBlocks'][] = $this->paragraphBlock;
|
||||
// We expect to wrap top level paragraph blocks into columns so the result should three columns blocks
|
||||
$result = $this->preprocessor->preprocess($parsedDocument, $this->layout, $this->styles);
|
||||
verify($result)->arrayCount(5);
|
||||
// First block is a full width paragraph and must be wrapped in a single column
|
||||
verify($result[0]['blockName'])->equals('core/columns');
|
||||
verify($result[0]['attrs'])->equals(['align' => 'full']);
|
||||
verify($result[0]['innerBlocks'][0]['blockName'])->equals('core/column');
|
||||
verify($result[0]['innerBlocks'][0]['innerBlocks'][0])->equals($parsedDocument[0]);
|
||||
// Second block is a columns block and must remain unchanged
|
||||
verify($result[1])->equals($parsedDocument[1]);
|
||||
// Third block must contain heading and paragraph blocks
|
||||
verify($result[2]['blockName'])->equals('core/columns');
|
||||
verify($result[2]['innerBlocks'][0]['blockName'])->equals('core/column');
|
||||
verify($result[2]['innerBlocks'][0]['innerBlocks'])->arrayCount(2);
|
||||
verify($result[2]['innerBlocks'][0]['innerBlocks'][0])->equals($parsedDocument[2]);
|
||||
verify($result[2]['innerBlocks'][0]['innerBlocks'][1])->equals($parsedDocument[3]);
|
||||
// Fourth block is a full width heading and must be wrapped in a single column
|
||||
verify($result[3]['blockName'])->equals('core/columns');
|
||||
verify($result[3]['attrs'])->equals(['align' => 'full']);
|
||||
verify($result[3]['innerBlocks'][0]['blockName'])->equals('core/column');
|
||||
verify($result[3]['innerBlocks'][0]['innerBlocks'][0])->equals($parsedDocument[4]);
|
||||
// Fifth block should stay unchanged
|
||||
verify($result[4])->equals($parsedDocument[5]);
|
||||
}
|
||||
}
|
@@ -7,9 +7,7 @@ use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Postprocessors\Variable
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\BlocksWidthPreprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\CleanupPreprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\SpacingPreprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\TopLevelPreprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\TypographyPreprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\ProcessManager;
|
||||
|
||||
class ProcessManagerTest extends \MailPoetUnitTest {
|
||||
public function testItCallsPreprocessorsProperly(): void {
|
||||
@@ -27,8 +25,6 @@ class ProcessManagerTest extends \MailPoetUnitTest {
|
||||
],
|
||||
],
|
||||
];
|
||||
$topLevel = $this->createMock(TopLevelPreprocessor::class);
|
||||
$topLevel->expects($this->once())->method('preprocess')->willReturn([]);
|
||||
|
||||
$cleanup = $this->createMock(CleanupPreprocessor::class);
|
||||
$cleanup->expects($this->once())->method('preprocess')->willReturn([]);
|
||||
@@ -42,18 +38,10 @@ class ProcessManagerTest extends \MailPoetUnitTest {
|
||||
$spacing = $this->createMock(SpacingPreprocessor::class);
|
||||
$spacing->expects($this->once())->method('preprocess')->willReturn([]);
|
||||
|
||||
$secondPreprocessor = $this->createMock(TopLevelPreprocessor::class);
|
||||
$secondPreprocessor->expects($this->once())->method('preprocess')->willReturn([]);
|
||||
|
||||
$highlighting = $this->createMock(HighlightingPostprocessor::class);
|
||||
$highlighting->expects($this->once())->method('postprocess')->willReturn('');
|
||||
|
||||
$variables = $this->createMock(VariablesPostprocessor::class);
|
||||
$variables->expects($this->once())->method('postprocess')->willReturn('');
|
||||
|
||||
$processManager = new ProcessManager($cleanup, $topLevel, $blocksWidth, $typography, $spacing, $highlighting, $variables);
|
||||
$processManager->registerPreprocessor($secondPreprocessor);
|
||||
verify($processManager->preprocess([], $layout, $styles))->equals([]);
|
||||
verify($processManager->postprocess(''))->equals('');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user