Skip to content

Commit

Permalink
✨ Add interactive block creation (Fixes #253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Aug 13, 2024
1 parent bcc91d8 commit cb3913d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 19 deletions.
118 changes: 115 additions & 3 deletions src/Console/BlockMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Log1x\AcfComposer\Console;

use Illuminate\Support\Str;

use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;

class BlockMakeCommand extends MakeCommand
{
/**
Expand All @@ -10,7 +16,7 @@ class BlockMakeCommand extends MakeCommand
* @var string
*/
protected $signature = 'acf:block {name* : The name of the block}
{--construct : Generate block properties inside of `__construct`}
{--localize : Localize the block name and description}
{--force : Overwrite any existing files}';

/**
Expand All @@ -34,15 +40,121 @@ class BlockMakeCommand extends MakeCommand
*/
protected $view = 'block';

/**
* The block supports array.
*/
protected array $supports = [
'align',
'align_text',
'align_content',
'full_height',
'anchor',
'mode',
'multiple',
'jsx',
'color' => ['background', 'text', 'gradient'],
];

/**
* {@inheritdoc}
*/
public function buildClass($name)
{
$stub = parent::buildClass($name);

$name = Str::of($name)
->afterLast('\\')
->kebab()
->headline()
->replace('-', ' ');

$description = "A beautiful {$name} block.";

$description = text(
label: 'Enter the block description',
placeholder: $description,
) ?: $description;

$categories = get_default_block_categories();

$category = select(
label: 'Select the block category',
options: collect($categories)->mapWithKeys(fn ($category) => [$category['slug'] => $category['title']]),
default: 'common',
);

$postTypes = multiselect(
label: 'Select the supported post types',
options: collect(
get_post_types(['public' => true])
)->mapWithKeys(fn ($postType) => [$postType => Str::headline($postType)])->all(),
hint: 'Leave empty to support all post types.',
);

$postTypes = collect($postTypes)
->map(fn ($postType) => sprintf("'%s'", $postType))
->join(', ');

$supports = multiselect(
label: 'Select the supported block features',
options: $this->getSupports(),
default: config('acf.generators.supports', []),
scroll: 8,
);

$stub = str_replace(
['DummySupports', 'DummyDescription', 'DummyCategory', 'DummyPostTypes'],
[$this->buildSupports($supports), $description, $category, $postTypes],
$stub
);

return $stub;
}

/**
* Build the block supports array.
*/
protected function buildSupports(array $selected): string
{
return collect($this->supports)->map(function ($value, $key) use ($selected) {
if (is_int($key)) {
return sprintf("'%s' => %s,", $value, in_array($value, $selected) ? 'true' : 'false');
}

$options = collect($value)
->map(fn ($option) => sprintf(
"%s'%s' => %s,",
Str::repeat(' ', 12),
$option,
in_array($option, $selected) ? 'true' : 'false'
))
->join("\n");

return sprintf("'%s' => [\n%s\n ],", $key, $options);
})->join("\n ");
}

/**
* Retrieve the support options.
*/
protected function getSupports(): array
{
return collect($this->supports)
->mapWithKeys(fn ($value, $key) => is_array($value)
? collect($value)->mapWithKeys(fn ($option) => [$option => Str::of($option)->finish(" {$key}")->headline()->toString()])->all()
: [$value => Str::headline($value)]
)->all();
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
if ($this->option('construct')) {
return $this->resolveStub('block.construct');
if ($this->option('localize')) {
return $this->resolveStub('block.localized');
}

return $this->resolveStub('block');
Expand Down
20 changes: 4 additions & 16 deletions src/Console/stubs/block.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class DummyClass extends Block
*
* @var string
*/
public $description = 'A simple DummyTitle block.';
public $description = 'DummyDescription';

/**
* The block category.
*
* @var string
*/
public $category = 'formatting';
public $category = 'DummyCategory';

/**
* The block icon.
Expand All @@ -47,7 +47,7 @@ class DummyClass extends Block
*
* @var array
*/
public $post_types = [];
public $post_types = [DummyPostTypes];

/**
* The parent block type allow list.
Expand Down Expand Up @@ -97,19 +97,7 @@ class DummyClass extends Block
* @var array
*/
public $supports = [
'align' => true,
'align_text' => false,
'align_content' => false,
'full_height' => false,
'anchor' => false,
'mode' => false,
'multiple' => true,
'jsx' => true,
'color' => [
'background' => true,
'text' => true,
'gradient' => true,
],
DummySupports
];

/**
Expand Down

0 comments on commit cb3913d

Please sign in to comment.