Skip to content

Commit

Permalink
Merge pull request #207 from awcodes/feat/block-stubs
Browse files Browse the repository at this point in the history
Feat: make:filament-block command
  • Loading branch information
awcodes authored Nov 20, 2023
2 parents e89e398 + f198950 commit 08ac6a3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/Commands/MakeBlockCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace FilamentTiptapEditor\Commands;

use Filament\Support\Commands\Concerns\CanManipulateFiles;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use function Laravel\Prompts\text;

class MakeBlockCommand extends Command
{
use CanManipulateFiles;

protected $description = 'Create a new Tiptap Editor block';

protected $signature = 'make:tiptap-block {name?} {--F|force}';

public function handle(): int
{
$block = (string) str(
$this->argument('name') ??
text(
label: 'What is the block name?',
placeholder: 'CustomBlock',
required: true,
),
)
->trim('/')
->trim('\\')
->trim(' ')
->replace('/', '\\');

$blockClass = (string) str($block)->afterLast('\\');
$blockNamespace = str($block)->contains('\\')
? (string) str($block)->beforeLast('\\')
: '';

$namespace = 'App\\TiptapBlocks';

$path = app_path('TiptapBlocks/');

$preview = str($block)
->prepend(
(string) str("{$namespace}\\Previews\\")
->replaceFirst('App\\', '')
)
->replace('\\', '/')
->explode('/')
->map(fn ($segment) => Str::lower(Str::kebab($segment)))
->implode('.');

$rendered = str($block)
->prepend(
(string) str("{$namespace}\\Rendered\\")
->replaceFirst('App\\', '')
)
->replace('\\', '/')
->explode('/')
->map(fn ($segment) => Str::lower(Str::kebab($segment)))
->implode('.');

$path = (string) str($block)
->prepend('/')
->prepend($path ?? '')
->replace('\\', '/')
->replace('//', '/')
->append('.php');

$previewPath = resource_path(
(string) str($preview)
->replace('.', '/')
->prepend('views/')
->append('.blade.php'),
);

$renderedViewPath = resource_path(
(string) str($rendered)
->replace('.', '/')
->prepend('views/')
->append('.blade.php'),
);

$files = [
$path,
$previewPath,
$renderedViewPath,
];

if (! $this->option('force') && $this->checkForCollision($files)) {
return static::INVALID;
}

$this->copyStubToApp('Block', $path, [
'class' => $blockClass,
'namespace' => str($namespace) . ($blockNamespace !== '' ? "\\{$blockNamespace}" : ""),
'preview' => $preview,
'rendered' => $rendered,
]);

$this->copyStubToApp('Preview', $previewPath);

$this->copyStubToApp('Rendered', $renderedViewPath);

$this->components->info("Tiptap Editor Block [{$path}] created successfully.");

return self::SUCCESS;
}
}
4 changes: 4 additions & 0 deletions src/FilamentTiptapEditorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Filament\Support\Assets\AlpineComponent;
use Filament\Support\Assets\Css;
use Filament\Support\Facades\FilamentAsset;
use FilamentTiptapEditor\Commands\MakeBlockCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -17,6 +18,9 @@ public function configurePackage(Package $package): void
->hasConfigFile()
->hasAssets()
->hasTranslations()
->hasCommands([
MakeBlockCommand::class
])
->hasViews();
}

Expand Down
19 changes: 19 additions & 0 deletions stubs/Block.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace {{ namespace }};

use FilamentTiptapEditor\TiptapBlock;

class {{ class }} extends TiptapBlock
{
public string $preview = '{{ preview }}';

public string $rendered = '{{ rendered }}';

public function getFormSchema(): array
{
return [
//
];
}
}
3 changes: 3 additions & 0 deletions stubs/Preview.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
// block preview in editor
</div>
3 changes: 3 additions & 0 deletions stubs/Rendered.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
// rendered block view outside editor
</div>

0 comments on commit 08ac6a3

Please sign in to comment.