refactors + support for onAdminCompilePresetSCSS() event

This commit is contained in:
Andy Miller
2020-04-20 13:08:23 -06:00
parent 6bfbaf5ce4
commit c295cb0322
5 changed files with 71 additions and 53 deletions

View File

@@ -291,7 +291,7 @@ class AdminPlugin extends Plugin
if ($obj instanceof Data && $obj->blueprints()->getFilename() === 'admin/blueprints') { if ($obj instanceof Data && $obj->blueprints()->getFilename() === 'admin/blueprints') {
[$status, $msg] = $this->grav['admin-whitelabel']->compileScss($obj); [$status, $msg] = $this->grav['admin-whitelabel']->compilePresetScss($obj);
if (!$status) { if (!$status) {
$this->grav['messages']->add($msg, 'error'); $this->grav['messages']->add($msg, 'error');
} }
@@ -618,7 +618,7 @@ class AdminPlugin extends Plugin
$preset_css = 'asset://admin-preset.css'; $preset_css = 'asset://admin-preset.css';
$preset_path = $this->grav['locator']->findResource($preset_css); $preset_path = $this->grav['locator']->findResource($preset_css);
if (!$preset_path) { if (!$preset_path) {
$this->grav['admin-whitelabel']->compileScss($this->config->get('plugins.admin.whitelabel')); $this->grav['admin-whitelabel']->compilePresetScss($this->config->get('plugins.admin.whitelabel'));
} }
} }

View File

@@ -2172,7 +2172,7 @@ class AdminController extends AdminBaseController
'output' => 'asset://' .$output_file 'output' => 'asset://' .$output_file
]; ];
[$compile_status, $msg] = $this->grav['admin-whitelabel']->compileScss($data, $options); [$compile_status, $msg] = $this->grav['admin-whitelabel']->compilePresetScss($data, $options);
$json_response = [ $json_response = [
'status' => $compile_status ? 'success' : 'error', 'status' => $compile_status ? 'success' : 'error',

View File

@@ -21,19 +21,19 @@ class ScssCompiler
return $this; return $this;
} }
public function setVariables($variables) public function setVariables(array $variables)
{ {
$this->compiler()->setVariables($variables); $this->compiler()->setVariables($variables);
return $this; return $this;
} }
public function setImportPaths($paths) public function setImportPaths(array $paths)
{ {
$this->compiler()->setImportPaths($paths); $this->compiler()->setImportPaths($paths);
return $this; return $this;
} }
public function compile($input_file, $output_file) public function compile(string $input_file, string $output_file)
{ {
$input = file_get_contents($input_file); $input = file_get_contents($input_file);
$output = $this->compiler()->compile($input); $output = $this->compiler()->compile($input);
@@ -41,4 +41,15 @@ class ScssCompiler
return $this; return $this;
} }
public function compileAll(array $input_paths, string $output_file)
{
$input = '';
foreach ($input_paths as $input_file) {
$input .= trim(file_get_contents($input_file)) . "\n\n";
}
$output = $this->compiler()->compile($input);
file_put_contents($output_file, $output);
return $this;
}
} }

View File

@@ -0,0 +1,31 @@
<?php
namespace Grav\Plugin\Admin;
class ScssList {
protected $list= [];
public function __construct($item = null)
{
if ($item) {
$this->add($item);
}
}
public function all()
{
return $this->list;
}
public function add($item)
{
$this->list[] = $item;
}
public function remove($item)
{
if (in_array($item)) {
unset($item);
}
}
}

View File

@@ -1,7 +1,9 @@
<?php <?php
namespace Grav\Plugin\Admin; namespace Grav\Plugin\Admin;
use Grav\Common\Data\Data;
use Grav\Common\Grav; use Grav\Common\Grav;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
class WhiteLabel class WhiteLabel
@@ -15,7 +17,7 @@ class WhiteLabel
$this->scss = new ScssCompiler(); $this->scss = new ScssCompiler();
} }
public function compileScss($config, $options = [ public function compilePresetScss($config, $options = [
'input' => 'plugin://admin/themes/grav/scss/preset.scss', 'input' => 'plugin://admin/themes/grav/scss/preset.scss',
'output' => 'asset://admin-preset.css' 'output' => 'asset://admin-preset.css'
]) ])
@@ -28,66 +30,40 @@ class WhiteLabel
if ($color_scheme) { if ($color_scheme) {
/** @var UniformResourceLocator $locator */ /** @var UniformResourceLocator $locator */
$locator = $this->grav['locator']; $locator = $this->grav['locator'];
$input_scss = $locator->findResource($options['input']); // Use ScssList object to make it easier ot handle in event
$output_css = $locator->findResource(($options['output']), true, true); $scss_list = new ScssList($locator->findResource($options['input']));
$output_css = $locator->findResource(($options['output']), true, true);
Grav::instance()->fireEvent('onAdminCompilePresetSCSS', new Event(['scss' => $scss_list]));
// Convert bak to regular array now we have run the event
$input_scss = $scss_list->all();
$input_path = dirname($input_scss);
$imports = [$locator->findResource('plugin://admin/themes/grav/scss')]; $imports = [$locator->findResource('plugin://admin/themes/grav/scss')];
if (!in_array($input_path, $imports)) { foreach ($input_scss as $scss) {
$imports[] = $input_path; $input_path = dirname($scss);
if (!in_array($input_path, $imports)) {
$imports[] = $input_path;
}
} }
try { try {
$this->compilePresetScss($color_scheme, $input_scss, $output_css, $imports); $compiler = $this->scss->reset();
$compiler->setVariables($color_scheme['colors'] + $color_scheme['accents']);
$compiler->setImportPaths($imports);
$compiler->compileAll($input_scss, $output_css);
} catch (\Exception $e) { } catch (\Exception $e) {
return [false, $e->getMessage()]; return [false, $e->getMessage()];
} }
return [true, 'Recompiled successfully']; return [true, 'Recompiled successfully'];
} }
return [false, ' Could not be recompiled, missing color scheme...']; return [false, ' Could not be recompiled, missing color scheme...'];
} }
public function compilePresetScss($colors, $in_path, $out_path, $imports)
{
$compiler = $this->scss->reset();
$compiler->setVariables($colors['colors'] + $colors['accents']);
$compiler->setImportPaths($imports);
$compiler->compile($in_path, $out_path);
}
public function colorContrast($color)
{
$opacity = 1;
$RGB = [];
if (substr($color, 0, 1) === '#') {
$color = ltrim($color, '#');
$RGB = [
hexdec(substr($color, 0, 2)),
hexdec(substr($color, 2, 2)),
hexdec(substr($color, 4, 2))
];
}
if (substr($color, 0, 3) === 'rgb') {
preg_match("/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/", $color, $matches);
array_shift($matches);
if (!$matches) { return $color; }
$RGB = [$matches[0], $matches[1], $matches[3]];
if (count($matches) === 4) {
$opacity = $matches[3];
}
}
$YIQ = (($RGB[0] * 299) + ($RGB[1] * 587) + ($RGB[2] * 114)) / 1000;
return ($YIQ >= 128) || $opacity <= 0.50 ? 'dark' : 'light';
}
} }