Skip to content

Commit

Permalink
Add component configurations to Sprig variable
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Aug 7, 2024
1 parent d16ccbd commit 4d18cf7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

### Added

- Added the logging of component configurations to the browser console when `devMode` is enabled.
- Added the component configurations to a `Sprig` variable in the browser console when `devMode` is enabled.
- Added the `sprig.registerJs(js)` function.
- Added the `sprig.consoleLog(value)` function.

### Changed

Expand Down
14 changes: 0 additions & 14 deletions src/base/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,4 @@ public static function registerJs(string $js): void
$html = Html::tag('div', $content, ['s-swap-oob' => 'beforeend:body']);
Craft::$app->getView()->registerHtml($html);
}

/**
* Logs a value to the console.
*
* @since 2.10.0
*/
public static function consoleLog(mixed $value, bool $devModeOnly = true): void
{
if ($devModeOnly && Craft::$app->getConfig()->getGeneral()->devMode === false) {
return;
}

self::registerJs('console.log(' . Json::encode($value) . ')');
}
}
3 changes: 2 additions & 1 deletion src/controllers/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use craft\web\Controller;
use craft\web\UrlManager;
use putyourlightson\sprig\base\Component;
use putyourlightson\sprig\helpers\Console;
use putyourlightson\sprig\Sprig;
use yii\base\Event;
use yii\web\ForbiddenHttpException;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function actionRender(): Response
$content = Craft::$app->getView()->renderTemplate($config->template, $variables);
}

Component::consoleLog($config->getAttributes());
Console::addComponent($config);

// Remove flashes after rendering, so they don’t appear on subsequent requests.
Craft::$app->getSession()->removeAllFlashes();
Expand Down
51 changes: 51 additions & 0 deletions src/helpers/Console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* @copyright Copyright (c) PutYourLightsOn
*/

namespace putyourlightson\sprig\helpers;

use Craft;
use craft\helpers\Json;
use putyourlightson\sprig\base\Component;
use putyourlightson\sprig\models\ConfigModel;

/**
* Manages the Sprig object in the console.
*
* @since 2.10.0
*/
class Console
{
private static bool $initialized = false;

/**
* Initialises the Sprig object in the console.
*/
public static function init(): void
{
if (self::$initialized || Component::getIsRequest()) {
return;
}

self::$initialized = true;

Component::registerJs('Sprig = {components: []}');
}

/**
* Adds a component to the Sprig object in the console.
*/
public static function addComponent(ConfigModel $config): void
{
if (Craft::$app->getConfig()->getGeneral()->devMode === false) {
return;
}

self::init();

$value = Json::encode($config->getAttributes());

Component::registerJs('Sprig.components.push(' . $value . ')');
}
}
3 changes: 2 additions & 1 deletion src/services/ComponentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use putyourlightson\sprig\base\Component;
use putyourlightson\sprig\errors\FriendlyInvalidVariableException;
use putyourlightson\sprig\events\ComponentEvent;
use putyourlightson\sprig\helpers\Console;
use putyourlightson\sprig\helpers\Html;
use putyourlightson\sprig\models\ConfigModel;
use putyourlightson\sprig\plugin\components\SprigPlayground;
Expand Down Expand Up @@ -250,7 +251,7 @@ public function create(string $value, array $variables = [], array $attributes =
$this->validateVariables($variables);
$config->variables = $variables;

Component::consoleLog($config->getAttributes());
Console::addComponent($config);

// Add token to values if this is a preview request.
// https://github.com/putyourlightson/craft-sprig/issues/162
Expand Down
10 changes: 0 additions & 10 deletions src/variables/SprigVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,6 @@ public function registerJs(string $js): void
Component::registerJs($js);
}

/**
* Logs a value to the console.
*
* @since 2.10.0
*/
public static function consoleLog(mixed $value, bool $devModeOnly = true): void
{
Component::consoleLog($value, $devModeOnly);
}

/**
* Returns a new component.
*/
Expand Down

0 comments on commit 4d18cf7

Please sign in to comment.