-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from lara-zeus/add-editor-command
add make Editor Command
- Loading branch information
Showing
7 changed files
with
313 additions
and
160 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: Custom Editor | ||
weight: 3 | ||
--- | ||
|
||
## Content Editor | ||
|
||
the default editor is: `MarkdownEditor`. and also included: | ||
|
||
* [RichEditor](https://filamentphp.com/docs/3.x/forms/fields/rich-editor) | ||
* [MarkdownEditor](https://filamentphp.com/docs/3.x/forms/fields/markdown-editor) | ||
* [Tiptap Editor](https://filamentphp.com/plugins/tiptap) | ||
* [Tiny Editor](https://filamentphp.com/plugins/mohamedsabil83-tinyeditor) | ||
|
||
to use them you only need to install the package, and set the `editor` in `zeus-sky` | ||
|
||
```php | ||
/** | ||
* the default editor for pages and posts, Available: | ||
* \LaraZeus\Sky\Editors\TipTapEditor::class, | ||
* \LaraZeus\Sky\Editors\TinyEditor::class, | ||
* \LaraZeus\Sky\Editors\MarkdownEditor::class, | ||
* \LaraZeus\Sky\Editors\RichEditor::class, | ||
*/ | ||
'editor' => \LaraZeus\Sky\Editors\RichEditor::class, | ||
``` | ||
|
||
### Customize the editor | ||
|
||
in some cases you may want to use some options with the editor, like uploading to S3 or adding or removing toolbar buttons. | ||
to do so, you need to copy the class to your app or create a new editor. | ||
this way you will have the editor class available to you to add any more options: | ||
|
||
```php | ||
MarkdownEditor::make('content') | ||
->fileAttachmentsDisk('s3') | ||
->fileAttachmentsDirectory('attachments') | ||
->fileAttachmentsVisibility('private') | ||
``` | ||
|
||
### adding new editor | ||
|
||
you can add any editor available in [filament plugin directory](https://filamentphp.com/plugins) | ||
|
||
* first install the plugin that you want to use. | ||
* implement the `\LaraZeus\Sky\Editors\ContentEditor` interface, and set it in the config file `zeus-sky.php` in the `editor` option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace LaraZeus\Sky\Concerns; | ||
|
||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\Str; | ||
use ReflectionClass; | ||
|
||
trait CanManipulateFiles | ||
{ | ||
protected function copyStubToApp(string $stub, string $targetPath, array $replacements = []): void | ||
{ | ||
$filesystem = app(Filesystem::class); | ||
|
||
if (! $this->fileExists($stubPath = base_path("stubs/zeus-sky/{$stub}.stub"))) { | ||
$stubPath = $this->getDefaultStubPath() . "/{$stub}.stub"; | ||
} | ||
|
||
$stub = Str::of($filesystem->get($stubPath)); | ||
|
||
foreach ($replacements as $key => $replacement) { | ||
$stub = $stub->replace("{{ {$key} }}", $replacement); | ||
} | ||
|
||
$stub = (string) $stub; | ||
|
||
$this->writeFile($targetPath, $stub); | ||
} | ||
|
||
protected function fileExists(string $path): bool | ||
{ | ||
$filesystem = app(Filesystem::class); | ||
|
||
return $filesystem->exists($path); | ||
} | ||
|
||
protected function writeFile(string $path, string $contents): void | ||
{ | ||
$filesystem = app(Filesystem::class); | ||
|
||
$filesystem->ensureDirectoryExists( | ||
(string) Str::of($path) | ||
->beforeLast('/'), | ||
); | ||
|
||
$filesystem->put($path, $contents); | ||
} | ||
|
||
protected function getDefaultStubPath(): string | ||
{ | ||
$reflectionClass = new ReflectionClass($this); | ||
|
||
return (string) Str::of($reflectionClass->getFileName()) | ||
->beforeLast('Console') | ||
->append('../stubs'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace LaraZeus\Sky\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use LaraZeus\Sky\Concerns\CanManipulateFiles; | ||
|
||
class ZeusEditorCommand extends Command | ||
{ | ||
//art make:zeus-editor Filament\\Forms\\Components\\MarkdownEditor | ||
use CanManipulateFiles; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'make:zeus-editor {plugin : filament FQN plugin}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create custom editor for zeus sky'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$filamentPluginFullNamespace = $this->argument('plugin'); | ||
$fieldClassName = str($filamentPluginFullNamespace)->explode('\\')->last(); | ||
|
||
$this->copyStubToApp('ZeusEditor', 'app/Zeus/Editor/' . $fieldClassName . '.php', [ | ||
'namespace' => 'App\\Zeus\\Editor', | ||
'plugin' => $filamentPluginFullNamespace, | ||
'class' => $fieldClassName, | ||
]); | ||
|
||
$this->info('zeus editor created successfully!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace {{ namespace }}; | ||
|
||
use Filament\Forms\Components\Component; | ||
use {{ plugin }} as {{ class }}Alias; | ||
use Filament\Forms\Components\Textarea; | ||
use LaraZeus\Sky\Classes\ContentEditor; | ||
|
||
class {{ class }} implements ContentEditor | ||
{ | ||
public static function component(): Component | ||
{ | ||
if (class_exists({{ class }}Alias::class)) { | ||
return {{ class }}Alias::make('content') | ||
// add more options | ||
->required(); | ||
} | ||
|
||
return Textarea::make('content')->required(); | ||
} | ||
|
||
public static function render(string $content): string | ||
{ | ||
if (class_exists({{ class }}Alias::class)) { | ||
return $content; | ||
} | ||
|
||
return $content; | ||
} | ||
} | ||
|