Skip to content

Commit

Permalink
Add icon and instructions options to add:set command
Browse files Browse the repository at this point in the history
  • Loading branch information
robdekort committed Dec 5, 2023
1 parent 7c924d5 commit dec5ba0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
27 changes: 1 addition & 26 deletions src/Commands/AddBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down
15 changes: 13 additions & 2 deletions src/Commands/AddSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
{
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/InstallPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']}'.");
}

Expand Down
1 change: 1 addition & 0 deletions src/Commands/InstallPresetPresets.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ public function getPresets() {
'block' => [
'name' => 'Invoke modal',
'icon' => 'alert-warning-exclamation-mark',
'description' => 'Invokes a modal.',
'handle' => 'invoke_modal',
]
],
Expand Down
40 changes: 39 additions & 1 deletion src/Commands/SharedFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit dec5ba0

Please sign in to comment.