Skip to content

Commit

Permalink
Add default options
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Nov 23, 2024
1 parent d1c11ec commit 4e648b5
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 1 addition & 6 deletions src/Datastar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 2 additions & 2 deletions src/assets/DatastarAssetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 37 additions & 12 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
];
30 changes: 24 additions & 6 deletions src/models/SettingsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
45 changes: 41 additions & 4 deletions src/services/ResponseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}

Expand All @@ -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));
}

/**
Expand All @@ -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));
}

Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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)) {
Expand Down

0 comments on commit 4e648b5

Please sign in to comment.