Skip to content

Commit 9e4e640

Browse files
committed
Add docs
1 parent 0d9bbd4 commit 9e4e640

File tree

4 files changed

+197
-9
lines changed

4 files changed

+197
-9
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Filament Live Preview
22

3-
A Filament plugin to add a preview screen to your pages. The screen will render your website with the data from Filament, without saving it.
3+
A Filament plugin to add a preview screen to your pages using websockets. The screen will render your website with the data from Filament, without saving it. Heavily based on [Filament Peek](https://github.com/pboivin/filament-peek).
44

55
## Installation
66

@@ -29,11 +29,6 @@ Optionally, you can publish the views using
2929
php artisan vendor:publish --tag="filament-live-preview-views"
3030
```
3131

32-
## Usage
33-
34-
```php
35-
```
36-
3732
## Documentation
3833

3934
For the full documentation, check [here](./docs/index.md).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"php": "^8.2",
1313
"illuminate/contracts": "^10.0 || ^11.0 || ^12.0",
1414
"livewire/livewire": "^3.6",
15-
"pboivin/filament-peek": "3.0.0-beta1",
15+
"pboivin/filament-peek": "^2.0 || ^3.0",
1616
"spatie/laravel-package-tools": "^1.12"
1717
},
1818
"require-dev": {

config/filament-live-preview.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
// config for Wotz/FilamentLivePreview
43
return [
54

65
];

docs/index.md

Lines changed: 195 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,200 @@
22

33
## Introduction
44

5-
A Filament plugin to add a preview screen to your pages. The screen will render your website with the data from Filament, without saving it.
5+
A Filament plugin to add a preview screen to your pages using websockets. The screen will render your website with the data from Filament, without saving it.
6+
This is heavily based on [Filament Peek](https://github.com/pboivin/filament-peek).
67

78
## Installation
9+
10+
You can install the package via composer:
11+
12+
```bash
13+
composer require wotz/filament-live-preview
14+
```
15+
16+
You can publish the config file with:
17+
18+
```bash
19+
php artisan vendor:publish --tag="filament-live-preview-config"
20+
```
21+
22+
This is the contents of the published config file:
23+
24+
```php
25+
return [
26+
];
27+
```
28+
29+
Optionally, you can publish the views using
30+
31+
```bash
32+
php artisan vendor:publish --tag="filament-live-preview-views"
33+
```
34+
35+
## Usage
36+
37+
### Install Laravel Reverb
38+
39+
Follow the Laravel Reverb installation instructions [here](https://laravel.com/docs/12.x/reverb)
40+
41+
#### Fix Livewire and Reverb
42+
43+
In `bootstrap/app.php`, add the following lines:
44+
45+
```php
46+
return Application::configure(basePath: dirname(__DIR__))
47+
->withMiddleware(function (Middleware $middleware) {
48+
$middleware->validateCsrfTokens(except: [
49+
'livewire/*',
50+
]);
51+
})
52+
```
53+
54+
Since we reload Livewire via websockets, we need to exclude the Livewire routes from CSRF protection. Else you will get a 419 HTTP status code when Livewire tries to make requests.
55+
56+
### Setup Filament Live Preview
57+
58+
#### Update Filament resource
59+
60+
```php
61+
62+
use Filament\Schemas\Components\Section;public static function form(Schema $schema): Schema
63+
{
64+
return $schema
65+
->columns(3)
66+
->components([
67+
Section::make()
68+
->schema([
69+
// Your fields here
70+
])
71+
->columnSpan(function (Component $livewire) {
72+
return ['lg' => $livewire->isPreviewing() ? 1 : 3];
73+
})
74+
->extraAttributes([
75+
'data-live-preview-form' => '',
76+
]),
77+
78+
View::make('filament-live-preview::components.live-preview-frame')
79+
->hidden(fn (Component $livewire) => ! $livewire->isPreviewing())
80+
->columnSpan(['lg' => 2]),
81+
]);
82+
}
83+
```
84+
85+
#### Update EditPage / CreatePage
86+
87+
```php
88+
use Filament\Actions\Action;
89+
use Filament\Resources\Pages\EditRecord;use Wotz\FilamentLivePreview\Filament\Traits\HasLivePreviewComponent;
90+
91+
class EditPage extends EditRecord
92+
{
93+
use HasLivePreviewComponent;
94+
95+
protected function getHeaderActions(): array
96+
{
97+
return [
98+
Action::make('preview')
99+
->label('Preview')
100+
->icon('heroicon-o-eye')
101+
->color('primary')
102+
->action(fn () => $this->toggleIsPreviewing()),
103+
$this->getSaveFormAction()->submit(null)->action('save'),
104+
DeleteAction::make(),
105+
];
106+
}
107+
108+
protected function getPreviewModalView(): ?string
109+
{
110+
// This corresponds to resources/views/posts/preview.blade.php
111+
return 'page.show';
112+
}
113+
114+
protected function getPreviewModalDataRecordKey(): ?string
115+
{
116+
return 'page';
117+
}
118+
119+
protected function mutatePreviewModalData(array $data): array
120+
{
121+
$data['titleForLayout'] = $data['page']->title;
122+
123+
return $data;
124+
}
125+
}
126+
```
127+
128+
See the [Peek](https://github.com/pboivin/filament-peek/blob/3.x/docs/page-previews.md#adding-extra-data-to-previews) docs for more details on how to customize the preview data.
129+
130+
#### Install the plugin in your panel
131+
132+
```php
133+
use Filament\Support\Enums\Width;
134+
use Pboivin\FilamentPeek\FilamentPeekPlugin;
135+
use Wotz\FilamentLivePreview\LivePreviewPlugin;
136+
137+
public function panel(Panel $panel): Panel
138+
{
139+
return $panel
140+
->plugins([
141+
// Your other plugins...
142+
FilamentPeekPlugin::make() // Disable styles and scripts for peek, since we override them in our package
143+
->disablePluginStyles()
144+
->disablePluginScripts(),
145+
LivePreviewPlugin::make(),
146+
])
147+
->maxContentWidth(Width::Full) // This is optional, but makes that your panel uses the full width of the screen
148+
;
149+
}
150+
```
151+
152+
#### Fix rendering of live preview component
153+
154+
Since we use a [full page Livewire component](https://livewire.laravel.com/docs/components#full-page-components) to render the preview, and you use a Blade component for layout, you can make some changes to
155+
156+
In our projects we have this `AppLayout` component:
157+
158+
```php
159+
<?php
160+
161+
namespace App\View\Components;
162+
163+
use App\Models\Page;
164+
use App\Models\StaticPage;
165+
use Closure;
166+
use Codedor\Seo\Facades\SeoBuilder;
167+
use Illuminate\Contracts\View\View;
168+
use Illuminate\View\Component;
169+
170+
class AppLayout extends Component
171+
{
172+
public function __construct(
173+
public string $titleForLayout,
174+
) {
175+
$this->renderDiv = request()->routeIs('live-preview-frame') || request()->is('livewire/update');
176+
}
177+
178+
public function render(): View|Closure|string
179+
{
180+
if ($this->renderDiv) {
181+
return <<<'blade'
182+
<div>
183+
{{ $slot }}
184+
</div>
185+
blade;
186+
}
187+
188+
return view('layouts.app');
189+
}
190+
}
191+
```
192+
193+
This make sure that the view defined in `getPreviewModalView` is wrapped in a `<div>` instead of the full layout, when rendering the live preview component.
194+
195+
#### Add broadcasting route
196+
197+
In `routes/channels.php`, add the following line:
198+
199+
```php
200+
Broadcast::channel('live-preview', function () {});
201+
```

0 commit comments

Comments
 (0)