Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separated processing for Tailwind 4 for future setup #7

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ With each class, replace the colour with the string `[colour]` to inject the dif
This is intended to make working with colours easier by generating the different classes to a HTML file for the
Tailwind process to pick up and include in the site's CSS.

You can also pass a `version` of either `3` or `4` - this is separating generation logic for different Tailwind
versions. If omitted, will default to v3.

## Listeners

### Prevent Deleting Mounts
Expand Down
54 changes: 48 additions & 6 deletions src/Console/Commands/GenerateTailwindCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Statamic\Facades\YAML;

class GenerateTailwindCommand extends Command
Expand Down Expand Up @@ -43,9 +44,34 @@ public function handle(): void
$this->error('YAML is missing "classes" property.');
}

// get the tailwind version
$tailwind = Arr::get($yaml, 'version', 3);

// store the html here
$html = [];

if ($tailwind === 3) {
$html = $this->generateV3($yaml);
} elseif ($tailwind === 4) {
$html = $this->generateV4($yaml);
} else {
$this->error('Unknown Tailwind version');
}

$contents = '<!-- THIS FILE IS GENERATED BY THE COMMAND php artisan fuse:generate-tailwind -->'."\r\n";
$contents .= '<!-- Make changes to your config.yaml file, then re-run the command to update this file -->'."\r\n";
$contents .= implode("\r\n", $html);

// save the output
file_put_contents($target, $contents);

$this->info('Huzzah! Your config has been generated auto-magically!');
}

protected function generateV3($yaml)
{
$html = [];

// start the loop
foreach ($yaml['colours'] as $colour) {
$classes = [];
Expand All @@ -58,13 +84,29 @@ public function handle(): void
$html[] = '<div class="'.implode(' ', $classes).'"></div>';
}

$contents = '<!-- THIS FILE IS GENERATED BY THE COMMAND php artisan fuse:generate-tailwind -->'."\r\n";
$contents .= '<!-- Make changes to your config.yaml file, then re-run the command to update this file -->'."\r\n";
$contents .= implode("\r\n", $html);
$html[] = '<div class="'.implode(' ', Arr::get($yaml, 'standard', [])).'"></div>';

// save the output
file_put_contents($target, $contents);
return $html;
}

$this->info('Huzzah! Your config has been generated auto-magically!');
protected function generateV4($yaml)
{
$html = [];

// start the loop
foreach ($yaml['colours'] as $colour) {
$classes = [];
foreach ($yaml['classes'] as $class) {
// merge the colour to the class
$classes[] = str_replace('[colour]', $colour, strtolower($class));
}

// add to the html
$html[] = '<div class="'.implode(' ', $classes).'"></div>';
}

$html[] = '<div class="'.implode(' ', Arr::get($yaml, 'standard', [])).'"></div>';

return $html;
}
}
11 changes: 6 additions & 5 deletions src/Listeners/ImagesWithoutAltListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,32 @@ class ImagesWithoutAltListener
{
public function handleSaving(AssetSaving $event)
{
if (!$this->hasImagesWithoutAltWidget()) {
if (! $this->hasImagesWithoutAltWidget()) {
return;
}

if ($event->asset->isDirty('alt')) {
Cache::forget(FuseUtilities::getImagesMissingAltCache());
}
}

public function handleChange(AssetDeleted|AssetUploaded $event)
{
if (!$this->hasImagesWithoutAltWidget()) {
if (! $this->hasImagesWithoutAltWidget()) {
return;
}

Cache::forget(FuseUtilities::getImagesMissingAltCache());
}

protected function hasImagesWithoutAltWidget() {
protected function hasImagesWithoutAltWidget()
{
return collect(config('statamic.cp.widgets', []))->contains('type', 'images_without_alt');
}

public function subscribe(Dispatcher $events)
{
if (!$this->hasImagesWithoutAltWidget()) {
if (! $this->hasImagesWithoutAltWidget()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Support/FuseUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function getImagesMissingAltCache(): string
return 'fuse-utilities::widgets.images-without-alt';
}

public function isCaptchaEnabled(?string $site = null, ?string $environment = null, Form|string $form = null): bool
public function isCaptchaEnabled(?string $site = null, ?string $environment = null, Form|string|null $form = null): bool
{
$forms = GlobalSet::findByHandle('forms')
->in($site ?? Site::current()->handle);
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Concerns/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function getFormLang()
}

if (is_array($message)) {
$bard = (new Bard())->setField($global->blueprint()->field($fieldHandle));
$bard = (new Bard)->setField($global->blueprint()->field($fieldHandle));

$content = (new Augmentor($bard))
->augment($message);
Expand Down