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

Feat: make:filament-block command #207

Merged
merged 1 commit into from
Nov 20, 2023
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
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>