diff --git a/admin.php b/admin.php index 2defbdf4..9be95e6a 100644 --- a/admin.php +++ b/admin.php @@ -291,7 +291,7 @@ class AdminPlugin extends Plugin 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) { $this->grav['messages']->add($msg, 'error'); } @@ -618,7 +618,7 @@ class AdminPlugin extends Plugin $preset_css = 'asset://admin-preset.css'; $preset_path = $this->grav['locator']->findResource($preset_css); 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')); } } diff --git a/classes/plugin/AdminController.php b/classes/plugin/AdminController.php index 3f1e73d3..08aa3a05 100644 --- a/classes/plugin/AdminController.php +++ b/classes/plugin/AdminController.php @@ -2172,7 +2172,7 @@ class AdminController extends AdminBaseController '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 = [ 'status' => $compile_status ? 'success' : 'error', diff --git a/classes/plugin/ScssCompiler.php b/classes/plugin/ScssCompiler.php index bbebf303..57f4a443 100644 --- a/classes/plugin/ScssCompiler.php +++ b/classes/plugin/ScssCompiler.php @@ -21,19 +21,19 @@ class ScssCompiler return $this; } - public function setVariables($variables) + public function setVariables(array $variables) { $this->compiler()->setVariables($variables); return $this; } - public function setImportPaths($paths) + public function setImportPaths(array $paths) { $this->compiler()->setImportPaths($paths); return $this; } - public function compile($input_file, $output_file) + public function compile(string $input_file, string $output_file) { $input = file_get_contents($input_file); $output = $this->compiler()->compile($input); @@ -41,4 +41,15 @@ class ScssCompiler 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; + } + } diff --git a/classes/plugin/ScssList.php b/classes/plugin/ScssList.php new file mode 100644 index 00000000..5942e26c --- /dev/null +++ b/classes/plugin/ScssList.php @@ -0,0 +1,31 @@ +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); + } + } + +} diff --git a/classes/plugin/WhiteLabel.php b/classes/plugin/WhiteLabel.php index b878eeb4..30f9832f 100644 --- a/classes/plugin/WhiteLabel.php +++ b/classes/plugin/WhiteLabel.php @@ -1,7 +1,9 @@ scss = new ScssCompiler(); } - public function compileScss($config, $options = [ + public function compilePresetScss($config, $options = [ 'input' => 'plugin://admin/themes/grav/scss/preset.scss', 'output' => 'asset://admin-preset.css' ]) @@ -28,66 +30,40 @@ class WhiteLabel if ($color_scheme) { /** @var UniformResourceLocator $locator */ - $locator = $this->grav['locator']; + $locator = $this->grav['locator']; - $input_scss = $locator->findResource($options['input']); - $output_css = $locator->findResource(($options['output']), true, true); + // Use ScssList object to make it easier ot handle in event + $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')]; - if (!in_array($input_path, $imports)) { - $imports[] = $input_path; + foreach ($input_scss as $scss) { + $input_path = dirname($scss); + if (!in_array($input_path, $imports)) { + $imports[] = $input_path; + } } 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) { return [false, $e->getMessage()]; } + return [true, 'Recompiled successfully']; } 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'; - } }