From dec5ba0b257e9e6dbdfc8f2a9897eb8fdf7e0ed9 Mon Sep 17 00:00:00 2001 From: Rob de Kort Date: Tue, 5 Dec 2023 20:08:29 +0100 Subject: [PATCH] Add icon and instructions options to add:set command --- src/Commands/AddBlock.php | 27 +----------------- src/Commands/AddSet.php | 15 ++++++++-- src/Commands/InstallPreset.php | 2 +- src/Commands/InstallPresetPresets.php | 1 + src/Commands/SharedFunctions.php | 40 ++++++++++++++++++++++++++- 5 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/Commands/AddBlock.php b/src/Commands/AddBlock.php index 2f65ca2..436dc99 100644 --- a/src/Commands/AddBlock.php +++ b/src/Commands/AddBlock.php @@ -8,7 +8,6 @@ use Statamic\Console\RunsInPlease; use Statamic\Facades\Config; use Stringy\StaticStringy as Stringy; -use function Laravel\Prompts\search; use function Laravel\Prompts\text; class AddBlock extends Command @@ -42,31 +41,7 @@ public function handle() required: true ); - $reflection = new \ReflectionClass(\Statamic\Fieldtypes\Sets::class); - $iconsDirectory = $reflection->getStaticPropertyValue('iconsDirectory') ?? base_path('/vendor/statamic/cms/resources/svg/icons'); - $iconsFolder = $reflection->getStaticPropertyValue('iconsFolder'); - - $icons = collect(File::allFiles("$iconsDirectory/$iconsFolder"))->map(function ($file) { - return str_replace('.svg', '', $file->getBasename('.'.$file->getExtension())); - }); - - $this->icon = search( - label: 'Which icon do you want to use for this block?', - options: function (string $value) use ($icons) { - if (!$value) { - return $icons - ->values() - ->all(); - } - - return $icons - ->filter(fn(string $item) => Str::contains($item, $value, true)) - ->values() - ->all(); - }, - placeholder: 'file-content-list', - required: true - ); + $this->icon = $this->promptsIconPicker('Which icon do you want to use for this block?'); try { $this->checkExistence('Fieldset', "resources/fieldsets/{$this->filename}.yaml"); diff --git a/src/Commands/AddSet.php b/src/Commands/AddSet.php index bb084d6..afd12e7 100644 --- a/src/Commands/AddSet.php +++ b/src/Commands/AddSet.php @@ -7,8 +7,8 @@ use Illuminate\Support\Facades\File; use Statamic\Console\RunsInPlease; use Statamic\Facades\Config; -use Statamic\Support\Arr; use Stringy\StaticStringy as Stringy; +use function Laravel\Prompts\search; use function Laravel\Prompts\text; class AddSet extends Command @@ -19,6 +19,8 @@ class AddSet extends Command protected $description = "Add an Article (Bard) set."; protected $set_name = ''; protected $filename = ''; + protected $instructions = ''; + protected $icon = ''; public function handle() { @@ -27,15 +29,24 @@ public function handle() placeholder: 'E.g. Card', required: true ); + $this->filename = Stringy::slugify($this->set_name, '_', Config::getShortLocale()); + $this->instructions = text( + label: 'What should be the instructions for this set?', + placeholder: 'E.g. Lead text that renders big and bold.', + required: true + ); + + $this->icon = $this->promptsIconPicker('Which icon do you want to use for this set?'); + try { $this->checkExistence('Fieldset', "resources/fieldsets/{$this->filename}.yaml"); $this->checkExistence('Partial', "resources/views/components/_{$this->filename}.antlers.html"); $this->createFieldset(); $this->createPartial(); - $this->updateArticleSets($this->set_name, $this->filename); + $this->updateArticleSets($this->set_name, $this->filename, $this->instructions, $this->icon); } catch (\Exception $e) { return $this->error($e->getMessage()); } diff --git a/src/Commands/InstallPreset.php b/src/Commands/InstallPreset.php index 48d1638..ab88999 100644 --- a/src/Commands/InstallPreset.php +++ b/src/Commands/InstallPreset.php @@ -103,7 +103,7 @@ public function handle() } elseif ($operation['type'] == 'update_article_sets') { - $this->updateArticleSets($operation['block']['name'], $operation['block']['handle']); + $this->updateArticleSets($operation['block']['name'], $operation['block']['handle'], $operation['block']['description'], $operation['block']['icon']); $this->info("Installed article set: '{$operation['block']['name']}'."); } diff --git a/src/Commands/InstallPresetPresets.php b/src/Commands/InstallPresetPresets.php index 2fb00e2..e33519e 100644 --- a/src/Commands/InstallPresetPresets.php +++ b/src/Commands/InstallPresetPresets.php @@ -332,6 +332,7 @@ public function getPresets() { 'block' => [ 'name' => 'Invoke modal', 'icon' => 'alert-warning-exclamation-mark', + 'description' => 'Invokes a modal.', 'handle' => 'invoke_modal', ] ], diff --git a/src/Commands/SharedFunctions.php b/src/Commands/SharedFunctions.php index ba141d5..4eaaf1a 100644 --- a/src/Commands/SharedFunctions.php +++ b/src/Commands/SharedFunctions.php @@ -2,10 +2,12 @@ namespace Studio1902\PeakCommands\Commands; +use Illuminate\Support\Str; use Illuminate\Support\Facades\File; use Statamic\Support\Arr; use Symfony\Component\Yaml\Yaml; use function Laravel\Prompts\confirm; +use function Laravel\Prompts\search; use function Laravel\Prompts\select; trait SharedFunctions { @@ -29,16 +31,52 @@ public function checkExistence($type, $path) } } + /** + * Prompt a search dialogue requesting an icon. + * + * @return string|null + */ + protected function promptsIconPicker($label) + { + $reflection = new \ReflectionClass(\Statamic\Fieldtypes\Sets::class); + $iconsDirectory = $reflection->getStaticPropertyValue('iconsDirectory') ?? base_path('/vendor/statamic/cms/resources/svg/icons'); + $iconsFolder = $reflection->getStaticPropertyValue('iconsFolder'); + + $icons = collect(File::allFiles("$iconsDirectory/$iconsFolder"))->map(function ($file) { + return str_replace('.svg', '', $file->getBasename('.'.$file->getExtension())); + }); + + return $this->icon = search( + label: $label, + options: function (string $value) use ($icons) { + if (!$value) { + return $icons + ->values() + ->all(); + } + + return $icons + ->filter(fn(string $item) => Str::contains($item, $value, true)) + ->values() + ->all(); + }, + placeholder: 'file-content-list', + required: true + ); + } + /** * Update article.yaml. * * @return bool|null */ - protected function updateArticleSets($name, $filename) + protected function updateArticleSets($name, $filename, $instructions, $icon) { $fieldset = Yaml::parseFile(base_path('resources/fieldsets/article.yaml')); $newSet = [ 'display' => $name, + 'instructions' => $instructions, + 'icon' => $icon, 'fields' => [ [ 'import' => $filename