Skip to content
HollyIT
Open menu

Building a Filament Plugin: Lessons From a Rich Editor

What I learned packaging a TipTap-based editor for Filament: keeping PHP and JavaScript in step, treating editor input as untrusted, and testing actions without a browser.

Abstract amber and charcoal blocks

Filament makes admin panels quick to build, and its plugin system makes them quick to extend. Packaging a rich text editor for it taught me a few things that apply to any plugin with a JavaScript half.

One source of truth for PHP and JavaScript

The editor runs in the browser, but the content is parsed, validated and rendered in PHP. If the two disagree about what a node looks like, content silently disappears on save. The fix was to describe every node once, in PHP, and send the browser what it needs from there.

If the server can't read it, the editor shouldn't be able to write it.

Treat the editor as untrusted input

Everything the editor sends is a form submission, and should be checked like one. Paths to uploaded files are the obvious case:

public function isSafePath(string $disk, string $path): bool
{
    if ($disk !== $this->getDisk() || $path === '') {
        return false;
    }

    return str_starts_with($path, trim($this->getDirectory(), '/').'/');
}

Test actions without a browser

Filament actions can be mounted and called from a Livewire test, which covers most of a plugin's behaviour without a browser at all.

Livewire::test(EditPost::class, ['record' => $post->getRouteKey()])
    ->mountAction(TestAction::make('insertLibraryImage')->schemaComponent('body'))
    ->call('callMountedAction', [['resource_id' => $image->id, 'args' => ['alt' => 'Our team']]])
    ->assertDispatched('run-rich-editor-plus-commands');

Where the time went

PartShare of the workNotes
Content schema35%Parsing and serialising every node
Uploads and storage25%Safe paths, types, temporary URLs
Toolbar and menus20%Mostly keyboard and mobile behaviour
Tests20%Worth every minute

The plugin, its code block and embed add-ons all run on this site, including the code blocks above.

Got something to build, fix or keep standing?

Got something to build, fix or keep standing?

Tell me what you are working on. You will get a reply from me, usually within a working day.

Prefer to email or call?