diff --git a/composer.json b/composer.json index 38aa13c..7ac0063 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "require": { "php": "^8.2", "craftcms/cms": "^5.0", - "starfederation/datastar-php": "dev-main" + "starfederation/datastar-php": "1.0.0-alpha.1" }, "require-dev": { "craftcms/ecs": "dev-main", diff --git a/src/Datastar.php b/src/Datastar.php index ca859cd..32edaef 100644 --- a/src/Datastar.php +++ b/src/Datastar.php @@ -23,12 +23,7 @@ class Datastar extends Module { /** - * The version of the Datastar framework. - */ - public const DATASTAR_VERSION = '0.20.0'; - - /** - * The ID of the module. + * The module ID. */ public const ID = 'datastar-module'; diff --git a/src/assets/DatastarAssetBundle.php b/src/assets/DatastarAssetBundle.php index 493b5d0..09311e0 100644 --- a/src/assets/DatastarAssetBundle.php +++ b/src/assets/DatastarAssetBundle.php @@ -3,14 +3,14 @@ namespace putyourlightson\datastar\assets; use craft\web\AssetBundle; -use putyourlightson\datastar\Datastar; +use starfederation\datastar\Consts; class DatastarAssetBundle extends AssetBundle { /** * @inheritdoc */ - public $sourcePath = __DIR__ . '/../resources/lib/datastar/' . Datastar::DATASTAR_VERSION; + public $sourcePath = __DIR__ . '/../resources/lib/datastar/' . Consts::VERSION; /** * @inheritdoc diff --git a/src/config.php b/src/config.php index 2e0d91b..6a34113 100644 --- a/src/config.php +++ b/src/config.php @@ -19,19 +19,44 @@ return [ '*' => [ - // Whether to register the Datastar script on the front-end. - //'registerScript' => true, + /** + * Whether to register the Datastar script on the front-end. + */ + 'registerScript' => true, - // The name of the store variable that will be injected into Datastar templates. - //'storeVariableName' => 'store', + /** + * The name of the store variable that will be injected into Datastar templates. + */ + 'storeVariableName' => 'store', - // The fragment options to override the Datastar defaults. Null values will be ignored. - // https://data-star.dev/reference/plugins_backend#datastar-fragment - //'defaultFragmentOptions' => [ - // 'selector' => null, - // 'mergeMode' => null, - // 'settleDuration' => null, - // 'useViewTransition' => null, - //], + /** + * The event options to override the Datastar defaults. Null values will be ignored. + */ + 'defaultEventOptions' => [ + 'retryDuration' => null, + ], + + /** + * The fragment options to override the Datastar defaults. Null values will be ignored. + */ + 'defaultFragmentOptions' => [ + 'settleDuration' => null, + 'useViewTransition' => null, + ], + + /** + * The signal options to override the Datastar defaults. Null values will be ignored. + */ + 'defaultSignalOptions' => [ + 'onlyIfMissing' => null, + ], + + /** + * The execute script options to override the Datastar defaults. Null values will be ignored. + */ + 'defaultExecuteScriptOptions' => [ + 'autoRemove' => null, + 'attributes' => null, + ], ], ]; diff --git a/src/models/SettingsModel.php b/src/models/SettingsModel.php index cecea09..d1f58ce 100644 --- a/src/models/SettingsModel.php +++ b/src/models/SettingsModel.php @@ -19,15 +19,33 @@ class SettingsModel extends Model */ public string $storeVariableName = 'store'; + /** + * The event options to override the Datastar defaults. Null values will be ignored. + */ + public array $defaultEventOptions = [ + 'retryDuration' => null, + ]; + /** * The fragment options to override the Datastar defaults. Null values will be ignored. - * https://data-star.dev/reference/plugins_backend#datastar-fragment - * */ public array $defaultFragmentOptions = [ - 'selector' => null, - 'merge' => null, - 'settle' => null, - 'vt' => null, + 'settleDuration' => null, + 'useViewTransition' => null, + ]; + + /** + * The signal options to override the Datastar defaults. Null values will be ignored. + */ + public array $defaultSignalOptions = [ + 'onlyIfMissing' => null, + ]; + + /** + * The execute script options to override the Datastar defaults. Null values will be ignored. + */ + public array $defaultExecuteScriptOptions = [ + 'autoRemove' => null, + 'attributes' => null, ]; } diff --git a/src/services/ResponseService.php b/src/services/ResponseService.php index 40dfa3a..1e60e2a 100644 --- a/src/services/ResponseService.php +++ b/src/services/ResponseService.php @@ -37,11 +37,10 @@ class ResponseService extends Component */ public function mergeFragments(string $data, array $options = []): void { - // Merge and remove empty values - $options = array_filter(array_merge( + $options = $this->mergeEventOptions( Datastar::getInstance()->settings->defaultFragmentOptions, $options - )); + ); $this->callSse(fn(SSE $sse) => $sse->mergeFragments($data, $options)); } @@ -51,6 +50,11 @@ public function mergeFragments(string $data, array $options = []): void */ public function removeFragments(string $selector, array $options = []): void { + $options = $this->mergeEventOptions( + Datastar::getInstance()->settings->defaultFragmentOptions, + $options + ); + $this->callSse(fn(SSE $sse) => $sse->removeFragments($selector, $options)); } @@ -59,7 +63,12 @@ public function removeFragments(string $selector, array $options = []): void */ public function mergeSignals(array $signals, array $options = []): void { - $this->callSse(fn(SSE $sse) => $sse->mergeSignals(Json::encode($signals), $options)); + $options = $this->mergeEventOptions( + Datastar::getInstance()->settings->defaultSignalOptions, + $options + ); + + $this->callSse(fn(SSE $sse) => $sse->mergeSignals($signals, $options)); } /** @@ -77,6 +86,11 @@ public function removeSignals(array $paths, array $options = []): void */ public function executeScript(string $script, array $options = []): void { + $options = $this->mergeEventOptions( + Datastar::getInstance()->settings->defaultExecuteScriptOptions, + $options + ); + $this->callSse(fn(SSE $sse) => $sse->executeScript($script, $options)); } @@ -126,6 +140,23 @@ public function stream(string $config, array $store): array return []; } + /** + * Returns merged event options with null values removed. + */ + private function mergeEventOptions(array ...$optionSets): array + { + $options = Datastar::getInstance()->settings->defaultEventOptions; + + foreach ($optionSets as $optionSet) { + $options = array_merge($options, $optionSet); + } + + return array_filter($options, fn($value) => $value !== null); + } + + /** + * Calls a callable, passing in an SSE object and cleaning output buffers. + */ private function callSse(callable $callable): void { // Clean and end all existing output buffers. @@ -143,6 +174,9 @@ private function callSse(callable $callable): void ob_start(); } + /** + * Returns a validated config model. + */ private function getConfigForResponse(string $config): ConfigModel { $data = Craft::$app->getSecurity()->validateData($config); @@ -153,6 +187,9 @@ private function getConfigForResponse(string $config): ConfigModel return new ConfigModel(Json::decodeIfJson($data)); } + /** + * Renders a template, catching exceptions. + */ private function renderTemplate(string $template, array $variables): void { if (!Craft::$app->getView()->doesTemplateExist($template)) {