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

Filament v3 support #28

Merged
merged 3 commits into from
Aug 26, 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
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
],
"require": {
"php": "^8.0",
"filament/filament": "^2.14.0",
"wire-elements/spotlight": "^1.0",
"spatie/laravel-package-tools": "^1.11"
"filament/filament": "^3.0.0-stable",
"wire-elements/spotlight": "^2.0"
},
"require-dev": {
"laravel/pint": "^1.10"
Expand All @@ -33,7 +32,7 @@
"extra": {
"laravel": {
"providers": [
"pxlrbt\\FilamentSpotlight\\FilamentSpotlightServiceProvider"
"pxlrbt\\FilamentSpotlight\\SpotlightServiceProvider"
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/css/spotlight.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions src/Actions/RegisterPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

namespace pxlrbt\FilamentSpotlight\Actions;

use Filament\Facades\Filament;
use Filament\Pages\Page;
use Filament\Panel;
use LivewireUI\Spotlight\Spotlight;
use pxlrbt\FilamentSpotlight\Commands\PageCommand;

class RegisterPages
{
public function __invoke()
public static function boot(Panel $panel)
{
$pages = Filament::getPages();
$pages = $panel->getPages();

foreach ($pages as $pageClass) {

/**
* @var Page $page
*/
$page = new $pageClass();

$name = collect([
$page->getNavigationGroup(),
$page->getTitle(),
])->filter()->join(' / ');

foreach ($pages as $page) {
$name = \Livewire\invade(new $page())->getTitle();
$url = $page::getUrl();

if (blank($name) || blank($url)) {
Expand Down
14 changes: 9 additions & 5 deletions src/Actions/RegisterResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@

namespace pxlrbt\FilamentSpotlight\Actions;

use Filament\Facades\Filament;
use Filament\Panel;
use Filament\Resources\Pages\PageRegistration;
use LivewireUI\Spotlight\Spotlight;
use pxlrbt\FilamentSpotlight\Commands\ResourceCommand;

class RegisterResources
{
public function __invoke()
public static function boot(Panel $panel)
{
$resources = Filament::getResources();
$resources = $panel->getResources();

foreach ($resources as $resource) {
$pages = $resource::getPages();

foreach ($pages as $key => $page) {
if (blank($key) || blank($page['class'])) {
/**
* @var PageRegistration $page
*/
if (blank($key) || blank($page->getPage())) {
continue;
}

$command = new ResourceCommand(
resource: $resource,
page: $page['class'],
page: $page->getPage(),
key: $key,
);

Expand Down
20 changes: 11 additions & 9 deletions src/Actions/RegisterUserMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
namespace pxlrbt\FilamentSpotlight\Actions;

use Filament\Facades\Filament;
use Filament\Navigation\UserMenuItem;
use Filament\Navigation\MenuItem;
use Filament\Panel;
use LivewireUI\Spotlight\Spotlight;
use pxlrbt\FilamentSpotlight\Commands\PageCommand;

class RegisterUserMenu
{
public function __invoke()
public static function boot(Panel $panel)
{
$self = new static();
/**
* @var array<UserMenuItem> $items
* @var array<MenuItem> $items
*/
$items = Filament::getUserMenuItems();
$items = $panel->getUserMenuItems();

foreach ($items as $key => $item) {
$name = $this->getName($key, $item);
$url = $this->getUrl($key, $item);
$name = $self->getName($key, $item);
$url = $self->getUrl($key, $item);

if (blank($name) || blank($url)) {
continue;
Expand All @@ -33,7 +35,7 @@ public function __invoke()
}
}

protected function getName(string $key, UserMenuItem $item): string
protected function getName(string $key, MenuItem $item): string
{
return match ($key) {
'account' => $item->getLabel() ?? __('Your Account'),
Expand All @@ -42,10 +44,10 @@ protected function getName(string $key, UserMenuItem $item): string
};
}

protected function getUrl(string $key, UserMenuItem $item): string
protected function getUrl(string $key, MenuItem $item): string
{
return match ($key) {
'logout' => $item->getUrl() ?? route('filament.auth.logout'),
'logout' => $item->getUrl() ?? Filament::getLogoutUrl(),
default => $item->getUrl()
};
}
Expand Down
10 changes: 8 additions & 2 deletions src/Commands/ResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ public function getId(): string

public function getName(): string
{
return $this->resource::getBreadcrumb().' – '.$this->page->getBreadcrumb();
return collect([
$this->resource::getNavigationGroup(),
$this->resource::getBreadcrumb(),
$this->page->getBreadcrumb(),
])
->filter()
->join(' / ');
}

public function getUrl(null|int|string $recordKey): string
{
return $this->resource::getUrl($this->key, $recordKey);
return $this->resource::getUrl($this->key, $recordKey ? ['record' => $recordKey] : []);
}

public function shouldBeShown(): bool
Expand Down
52 changes: 0 additions & 52 deletions src/FilamentSpotlightServiceProvider.php

This file was deleted.

46 changes: 46 additions & 0 deletions src/SpotlightPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace pxlrbt\FilamentSpotlight;

use Filament\Contracts\Plugin;
use Filament\Facades\Filament;
use Filament\Panel;
use Illuminate\Support\Facades\Blade;
use pxlrbt\FilamentSpotlight\Actions\RegisterPages;
use pxlrbt\FilamentSpotlight\Actions\RegisterResources;
use pxlrbt\FilamentSpotlight\Actions\RegisterUserMenu;

class SpotlightPlugin implements Plugin
{
public static string $name = 'pxlrbt/filament-spotlight';

public static function make(): static
{
return app(static::class);
}

public function getId(): string
{
return self::$name;
}

public function register(Panel $panel): void
{
$panel->renderHook(
'panels::scripts.after',
fn () => Blade::render("@livewire('livewire-ui-spotlight')")
);
}

public function boot(Panel $panel): void
{
Filament::serving(function () use ($panel) {
config()->set('livewire-ui-spotlight.include_js', false);

RegisterPages::boot($panel);
RegisterResources::boot($panel);
RegisterUserMenu::boot($panel);
});

}
}
21 changes: 21 additions & 0 deletions src/SpotlightServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace pxlrbt\FilamentSpotlight;

use Filament\Support\Assets\Css;
use Filament\Support\Assets\Js;
use Filament\Support\Facades\FilamentAsset;
use Illuminate\Support\ServiceProvider;

class SpotlightServiceProvider extends ServiceProvider
{
public function boot()
{
config()->set('livewire-ui-spotlight.commands', []);

FilamentAsset::register([
Css::make('spotlight-css', __DIR__.'/../resources/dist/css/spotlight.css'),
Js::make('spotlight-js', __DIR__.'/../resources/dist/js/spotlight.js'),
], package: 'pxlrbt/filament-spotlight');
}
}