Skip to content

Commit

Permalink
fix: add option to open documentation in new tab
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Frey <[email protected]>
  • Loading branch information
lukas-frey committed May 7, 2024
1 parent ab40491 commit 4d22a8c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion resources/views/livewire/help-menu.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@php
use Filament\Facades\Filament;
$hasModalPreviews = Filament::getPlugin('guava::filament-knowledge-base')->hasModalPreviews();
$hasSlideOverPreviews = Filament::getPlugin('guava::filament-knowledge-base')->hasSlideOverPreviews();
$hasModalTitleBreadcrumbs = Filament::getPlugin('guava::filament-knowledge-base')->hasModalTitleBreadcrumbs();
$target = Filament::getPlugin('guava::filament-knowledge-base')->shouldOpenDocumentationInNewTab() ? '_blank' : '_self';
@endphp

<div @class([
Expand Down Expand Up @@ -43,7 +45,8 @@ class="[&_.fi-modal-content]:px-12 [&_.fi-modal-content]:gap-y-0"
{!! $documentable->getSimpleHtml() !!}
<x-slot name="footerActions">
<x-filament::button tag="a"
:href="$documentable->getUrl()">
:href="$documentable->getUrl()"
:target="$target">
{{ __('filament-knowledge-base::translations.open-documentation') }}
</x-filament::button>
<x-filament::button color="gray"
Expand Down
3 changes: 2 additions & 1 deletion resources/views/sidebar-footer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
:active="$active"
icon="heroicon-o-book-open"
class="ring-1 ring-gray-950/10 dark:ring-white/20 rounded-lg"
:url="$url">
:url="$url"
:should-open-url-in-new-tab="$shouldOpenDocumentationInNewTab">
<x-filament::button
class="!font-medium !text-gray-700 dark:!text-gray-200"
style="box-shadow: none; background: none; padding:0;"
Expand Down
1 change: 1 addition & 0 deletions src/Filament/Panels/KnowledgeBasePanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ protected function setUp(): void
fn (): string => view('filament-knowledge-base::sidebar-footer', [
'active' => Filament::getCurrentPanel()->getId() === config('filament-knowledge-base.panel.id', 'knowledge-base'),
'url' => Filament::getPanel(config('filament-knowledge-base.panel.id', 'knowledge-base'))->getUrl(),
'shouldOpenDocumentationInNewTab' => false,
])
)

Expand Down
15 changes: 15 additions & 0 deletions src/KnowledgeBasePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class KnowledgeBasePlugin implements Plugin

protected bool $modalTitleBreadcrumbs = false;

protected bool $openDocumentationInNewTab = false;

protected string $helpMenuRenderHook = PanelsRenderHook::TOPBAR_END;

public function helpMenuRenderHook(string $renderHook): static
Expand Down Expand Up @@ -51,6 +53,13 @@ public function modalTitleBreadcrumbs(bool $condition = true): static
return $this;
}

public function openDocumentationInNewTab(bool $condition = true): static
{
$this->openDocumentationInNewTab = $condition;

return $this;
}

public function hasModalPreviews(): bool
{
return $this->modalPreviews;
Expand All @@ -66,6 +75,11 @@ public function hasModalTitleBreadcrumbs(): bool
return $this->modalTitleBreadcrumbs;
}

public function shouldOpenDocumentationInNewTab(): bool
{
return $this->openDocumentationInNewTab;
}

public function getId(): string
{
return 'guava::filament-knowledge-base';
Expand All @@ -83,6 +97,7 @@ public function register(Panel $panel): void
fn (): string => view('filament-knowledge-base::sidebar-footer', [
'active' => Filament::getCurrentPanel()->getId() === config('filament-knowledge-base.panel.id', 'knowledge-base'),
'url' => Filament::getPanel(config('filament-knowledge-base.panel.id', 'knowledge-base'))->getUrl(),
'shouldOpenDocumentationInNewTab' => $this->shouldOpenDocumentationInNewTab(),
])
)
;
Expand Down
11 changes: 10 additions & 1 deletion src/Livewire/HelpMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Filament\Actions\ActionGroup;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Facades\Filament;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Resources\Pages\Page;
Expand All @@ -22,10 +23,14 @@ class HelpMenu extends Component implements HasActions, HasForms

public array $documentation;

protected bool $shouldOpenDocumentationInNewTab;

public function mount(): void
{
$controller = request()->route()->controller;

$this->shouldOpenDocumentationInNewTab = Filament::getPlugin('guava::filament-knowledge-base')->shouldOpenDocumentationInNewTab();

$this->documentation = Arr::wrap(match (true) {
$controller instanceof HasKnowledgeBase => $controller::getDocumentation(),
$controller instanceof Page && in_array(HasKnowledgeBase::class, class_implements($controller::getResource())) => $controller::getResource()::getDocumentation(),
Expand All @@ -46,6 +51,7 @@ public function actions(): array
// ->map(fn (string $class) => KnowledgeBasePanel::getDocumentationAction($class))
->map(
fn (Documentable $documentable) => HelpAction::forDocumentable($documentable)
->openUrlInNewTab($this->shouldOpenDocumentationInNewTab)
)
->toArray()
;
Expand All @@ -58,7 +64,10 @@ public function shouldShowAsMenu(): bool

public function getSingleAction(): HelpAction
{
return HelpAction::forDocumentable($this->getDocumentation()->first())->generic();
return HelpAction::forDocumentable($this->getDocumentation()->first())
->generic()
->openUrlInNewTab($this->shouldOpenDocumentationInNewTab)
;
}

public function getMenuAction(): ActionGroup
Expand Down

0 comments on commit 4d22a8c

Please sign in to comment.