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

add make Editor Command #165

Merged
merged 1 commit into from
Oct 8, 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
266 changes: 133 additions & 133 deletions composer.lock

Large diffs are not rendered by default.

27 changes: 0 additions & 27 deletions docs/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,3 @@ return [
'editor' => \LaraZeus\Sky\Editors\RichEditor::class,
];
```

## Content Editor

the default editor is: `MarkdownEditor`. and included:

* RichEditor
* [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,
```

### 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 `editor`
46 changes: 46 additions & 0 deletions docs/getting-started/editor.md
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
57 changes: 57 additions & 0 deletions src/Concerns/CanManipulateFiles.php
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');
}
}
43 changes: 43 additions & 0 deletions src/Console/ZeusEditorCommand.php
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!');
}
}
2 changes: 2 additions & 0 deletions src/SkyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use LaraZeus\Sky\Console\InstallCommand;
use LaraZeus\Sky\Console\migrateCommand;
use LaraZeus\Sky\Console\PublishCommand;
use LaraZeus\Sky\Console\ZeusEditorCommand;
use RyanChandler\FilamentNavigation\Filament\Resources\NavigationResource;
use RyanChandler\FilamentNavigation\FilamentNavigation;
use Spatie\LaravelPackageTools\Package;
Expand Down Expand Up @@ -44,6 +45,7 @@ protected function getCommands(): array
migrateCommand::class,
PublishCommand::class,
InstallCommand::class,
ZeusEditorCommand::class,
];
}

Expand Down
32 changes: 32 additions & 0 deletions stubs/ZeusEditor.stub
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;
}
}