Get rid of duplicate CSS for template preview

We can reuse the same CSS we pass to editor
[MAILPOET-6249]
This commit is contained in:
Rostislav Wolny
2024-10-22 14:56:49 +02:00
committed by Jan Lysý
parent b6d75410cf
commit 324fedecbf
3 changed files with 10 additions and 25 deletions

View File

@@ -2,19 +2,23 @@
namespace MailPoet\EmailEditor\Engine\Templates; namespace MailPoet\EmailEditor\Engine\Templates;
use MailPoet\EmailEditor\Engine\SettingsController;
use MailPoet\EmailEditor\Engine\ThemeController; use MailPoet\EmailEditor\Engine\ThemeController;
use MailPoet\EmailEditor\Validator\Builder; use MailPoet\EmailEditor\Validator\Builder;
use WP_Theme_JSON; use WP_Theme_JSON;
class TemplatePreview { class TemplatePreview {
private ThemeController $themeController; private ThemeController $themeController;
private SettingsController $settingsController;
private Templates $templates; private Templates $templates;
public function __construct( public function __construct(
ThemeController $themeController, ThemeController $themeController,
SettingsController $settingsController,
Templates $templates Templates $templates
) { ) {
$this->themeController = $themeController; $this->themeController = $themeController;
$this->settingsController = $settingsController;
$this->templates = $templates; $this->templates = $templates;
} }
@@ -40,7 +44,11 @@ class TemplatePreview {
if (is_array($templateTheme)) { if (is_array($templateTheme)) {
$editorTheme->merge(new WP_Theme_JSON($templateTheme, 'custom')); $editorTheme->merge(new WP_Theme_JSON($templateTheme, 'custom'));
} }
$additionalCSS = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'preview.css'); $editorSettings = $this->settingsController->getSettings();
$additionalCSS = '';
foreach ($editorSettings['styles'] as $style) {
$additionalCSS .= $style['css'];
}
// Set proper content width for previews // Set proper content width for previews
$layoutSettings = $this->themeController->getLayoutSettings(); $layoutSettings = $this->themeController->getLayoutSettings();
$additionalCSS .= ".is-root-container { width: {$layoutSettings['contentSize']}; margin: 0 auto; }"; $additionalCSS .= ".is-root-container { width: {$layoutSettings['contentSize']}; margin: 0 auto; }";

View File

@@ -1,24 +0,0 @@
/*
* This file contains additional CSS for template previews.
* It is loaded in the iframe that displays the preview in select modal.
*/
/*
* Alignment CSS - In theme CSS these are prefixed by a layout class
* Alignment rules are copied from the CSS generated from theme from class .wp-site-blocks
*/
.block-editor-block-preview__content-iframe .aligncenter {
justify-content: center;
margin-left: auto;
margin-right: auto;
}
.block-editor-block-preview__content-iframe .alignleft {
float: left;
margin-right: 2em;
}
.block-editor-block-preview__content-iframe .alignright {
float: right;
margin-left: 2em;
}

View File

@@ -109,6 +109,7 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
$container->set(TemplatePreview::class, function ($container) { $container->set(TemplatePreview::class, function ($container) {
return new TemplatePreview( return new TemplatePreview(
$container->get(ThemeController::class), $container->get(ThemeController::class),
$container->get(SettingsController::class),
$container->get(Templates::class), $container->get(Templates::class),
); );
}); });