From 843ac75e29ca6b47bedefaca87fa20960abf7753 Mon Sep 17 00:00:00 2001 From: kekefreedog <70959083+kekefreedog@users.noreply.github.com> Date: Tue, 26 Nov 2024 17:50:14 +0100 Subject: [PATCH] Improve color schema --- src/Front/Library/Crazycolor.ts | 56 +++++++++++++++++++++++- src/Front/Library/Loader/Page.ts | 20 +++++++-- src/Library/Html/Structure.php | 69 +++++++++++++++++++++++++++++- src/Library/String/Color.php | 31 ++++++++++++++ tests/Library/String/ColorTest.php | 27 ++++++++++++ 5 files changed, 196 insertions(+), 7 deletions(-) diff --git a/src/Front/Library/Crazycolor.ts b/src/Front/Library/Crazycolor.ts index c28233f..00daa39 100644 --- a/src/Front/Library/Crazycolor.ts +++ b/src/Front/Library/Crazycolor.ts @@ -106,7 +106,7 @@ export default class Crazycolor { * * @return {boolean} */ - public applyTheme = ():boolean => { + public applyTheme = (target:HTMLElement = document.body):boolean => { // Set result let result = false; @@ -118,7 +118,7 @@ export default class Crazycolor { applyTheme( this.theme, { - target: document.body, + target: target, dark: ColorSchema.getTheme() == "dark" ? true : false, } ); @@ -145,6 +145,58 @@ export default class Crazycolor { } + /** Public static methods + ****************************************************** + */ + + /** + * Scan Crazy Color + * + * Scan all el with attribute data-crazy-color + * + * @parm parent + * @returns {null|HTMLElement[]} all element found + */ + public static scanCrazyColor = (parent:HTMLElement|Document):null|HTMLElement[] => { + + // Set result + let result:null|HTMLElement[] = null; + + // Search els + let els = parent.querySelectorAll("[data-crazy-color]"); + + // Check els + if(els.length) + + // Iteration els + els.forEach((el) => { + + // Check el + if(el instanceof HTMLElement){ + + // Get attribute of el + var currentCrazyColor = el.dataset.crazyColor; + + // Check currentCrazyColor + if(currentCrazyColor){ + + // Apply color on el + let currentColorInstance = new Crazycolor(currentCrazyColor); + + // Apply on el + currentColorInstance.applyTheme(el); + + } + + } + + }); + + // Return result + return result; + + } + /** Private methods ****************************************************** */ diff --git a/src/Front/Library/Loader/Page.ts b/src/Front/Library/Loader/Page.ts index fea9cb0..8006312 100644 --- a/src/Front/Library/Loader/Page.ts +++ b/src/Front/Library/Loader/Page.ts @@ -673,10 +673,24 @@ export default class Page { * @param options:LoaderPageOptions Options with all page details * @return Promise */ - public static applyColorSchema = async(options:LoaderPageOptions):Promise => { + public static applyColorSchema = async(options:LoaderPageOptions):Promise => { + + console.log(options); + + // Get document + let doc = document; + + // Scan crazy color + let result = Crazycolor.scanCrazyColor(doc); + + // Check + if(result?.length) + + // Set status + options = this.setStatus(options, "hasColorApplied", true); // Check colors - if(options.status?.hasColor && options.color){ + /* if(options.status?.hasColor && options.color){ // Apply theme if(options.color.applyTheme()) @@ -684,7 +698,7 @@ export default class Page { // Set status options = this.setStatus(options, "hasColorApplied", true); - } + } */ // Return options return options diff --git a/src/Library/Html/Structure.php b/src/Library/Html/Structure.php index e36ce62..6c5be1d 100644 --- a/src/Library/Html/Structure.php +++ b/src/Library/Html/Structure.php @@ -24,6 +24,7 @@ use CrazyPHP\Library\File\Config; use CrazyPHP\Library\Html\Head; use CrazyPHP\Library\File\File; +use CrazyPHP\Library\String\Color; use CrazyPHP\Model\Context; /** @@ -408,7 +409,7 @@ public function setLanguage(?string $language = null, array $keys = ["LANG", "LA return $this; } - + /** Public methods | body ****************************************************** */ @@ -451,6 +452,30 @@ public function setHead(string $config = "main", array $attributes = []):self { */ public function setBody(string|array|null $attributes = null):self { + # Push color attribute + $this->_pushColorSchema($attributes); + + # Set element + $this->setElement("html", $attributes, "body"); + + # Return current instance + return $this; + + } + + /** + * Set Body Color Schema + * + * Set Body Tag + * + * @param ?string $source For override current colorschema + * @return self + */ + public function setBodyColorSchema(?string $source = null):self { + + # Push color attribute + $this->_pushColorSchema($attributes, $source); + # Set element $this->setElement("html", $attributes, "body"); @@ -470,11 +495,13 @@ public function setBody(string|array|null $attributes = null):self { public function setBodyContent(?string $content = ""):self { # Check content - if($content !== null) + if($content !== null){ # Set element $this->setElement("html.body", $content, null); + } + # Return current instance return $this; @@ -783,6 +810,44 @@ private function _setHash(string &$input):void { } + /** + * Push Color schema on attributes + * + * @param mixed &$attributes attributes to update + * @param ?string $source to override default color schema + * @return void + */ + private function _pushColorSchema(mixed &$attributes, ?string $source = null):void { + + # Get color source + $appSource = Config::getValue("Style.materialDynamicColors.source"); + + # Push language in response + $attributes["data-default-color"] = $appSource; + + # Check source + if(!$source){ + + # Get url parameter + $source = $appSource; + + }else + # Check source + if($source && Color::isValid($source)){ + + # check attributes + if(!is_array($attributes)) + + # Convert to array + $attributes = []; + + # Set language in response + $attributes["data-crazy-color"] = $source; + + } + + } + /** Public constants ****************************************************** */ diff --git a/src/Library/String/Color.php b/src/Library/String/Color.php index dae83a7..6a7967c 100644 --- a/src/Library/String/Color.php +++ b/src/Library/String/Color.php @@ -17,6 +17,8 @@ */ use OzdemirBurak\Iris\Color\Factory; use OzdemirBurak\Iris\BaseColor; +use OzdemirBurak\Iris\Exceptions\AmbiguousColorString; +use OzdemirBurak\Iris\Exceptions\InvalidColorException; /** * Color @@ -228,4 +230,33 @@ public static function randomHex():string { } + /** + * Is Valid + * + * @param string $input + * @return bool + */ + public static function isValid(string $input):bool { + + # Set result + $result = true; + + # Try + try{ + + # New color + new Color($input); + + # Catch error + }catch(AmbiguousColorString|InvalidColorException $e){ + + $result = false; + + } + + # Return result + return $result; + + } + } \ No newline at end of file diff --git a/tests/Library/String/ColorTest.php b/tests/Library/String/ColorTest.php index 47ddea9..f6102ec 100644 --- a/tests/Library/String/ColorTest.php +++ b/tests/Library/String/ColorTest.php @@ -70,6 +70,33 @@ public static function tearDownAfterClass():void { ****************************************************** */ + /** + * Test Color Valid + * + * @return void + */ + public static function testColorValid():void { + + # Check hex + static::assertTrue(Color::isValid("#a2837a")); + + # Check rgb + static::assertTrue(Color::isValid("rgb(255, 0, 255)")); + + } + + /** + * Test Color Invalid + * + * @return void + */ + public static function testColorInvalid():void { + + # Check valid + static::assertFalse(Color::isValid("toto")); + + } + /** * test Color Package *