Add a WYSIWYG editor to Laravel
Turn a Blade textarea into a rich text editor with image upload in four steps. No API key, no account, MIT licensed.
Step 1: Load the editor
Add the stylesheet and script to your layout from the jsDelivr CDN. With Vite, install the package with npm install wds-editor and import it in resources/js/app.js instead.
<head>
...
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/wds-editor@1/dist/wds-editor.min.css">
<script src="https://cdn.jsdelivr.net/npm/wds-editor@1/dist/wds-editor.min.js"></script>
@stack('scripts')
</head>
{{-- Or with Vite (resources/js/app.js):
import { WDSEditor } from 'wds-editor';
import 'wds-editor/style.css';
window.WDSEditor = WDSEditor; --}}
Step 2: Turn the textarea into an editor
Keep a normal <textarea> in the form: the editor writes the HTML back into it, so the form posts as before and old() keeps working after a validation error. The file manager sends the CSRF token in a header.
<form method="POST" action="{{ route('posts.store') }}">
@csrf
<textarea id="body" name="body">{{ old('body', $post->body ?? '') }}</textarea>
<button type="submit">Save</button>
</form>
@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', () => {
WDSEditor.init({
selector: '#body',
preset: 'standard',
height: 480,
fileManager: {
listUrl: '/editor/files',
uploadUrl: '/editor/files',
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' }
}
});
});
</script>
@endpush
Step 3: Add the file manager endpoints
Two routes behind auth: a list of uploaded files and an upload that stores files on the public disk. Run php artisan storage:link once so the files get a public URL.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class EditorFileController extends Controller
{
public function index()
{
$disk = Storage::disk('public');
$files = collect($disk->files('editor'))
->map(fn (string $path) => [
'url' => $disk->url($path),
'name' => basename($path),
'size' => $disk->size($path),
'modified' => date(DATE_ATOM, $disk->lastModified($path)),
])
->sortByDesc('modified')
->values();
return response()->json(['files' => $files]);
}
public function store(Request $request)
{
$validator = validator($request->all(), [
'file' => 'required|file|max:10240|mimes:jpg,jpeg,png,gif,webp,pdf',
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()->first()], 422);
}
$file = $request->file('file');
$base = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) ?: 'file';
$name = $base . '-' . Str::lower(Str::random(6)) . '.' . $file->extension();
$path = $file->storeAs('editor', $name, 'public');
return response()->json(['url' => Storage::disk('public')->url($path), 'name' => $name]);
}
}
// routes/web.php
// Route::middleware('auth')->group(function () {
// Route::get('/editor/files', [EditorFileController::class, 'index']);
// Route::post('/editor/files', [EditorFileController::class, 'store']);
// });
Step 4: Clean the HTML when you save
The editor only produces safe HTML, but a request can be sent without it, so clean the HTML on the server too. composer require symfony/html-sanitizer installs a sanitizer that works well with Laravel. Print the saved HTML with {!! $post->body !!}.
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
public function store(Request $request)
{
$request->validate(['body' => 'required|string|max:200000']);
$sanitizer = new HtmlSanitizer(
(new HtmlSanitizerConfig())
->allowSafeElements()
->allowRelativeLinks()
->allowRelativeMedias()
->allowAttribute('class', '*') // snippet library blocks
->allowAttribute('style', '*') // colors, alignment, image width
->allowAttribute('data-type', '*') // checklists
->allowAttribute('data-checked', '*')
->allowAttribute('data-mention', '*') // @mentions and #tags
->allowAttribute('data-id', '*')
);
$post = Post::create(['body' => $sanitizer->sanitize($request->input('body'))]);
return redirect()->route('posts.show', $post);
}
Good to know
- Livewire: put the textarea inside
wire:ignoreand copy the HTML to a property in the editor'schangeevent:editor.on('change', e => $wire.set('body', e.getHTML(), false)). - Several editors on one page: call
WDSEditor.initonce per textarea with its own selector. - Comment forms: use
preset: 'basic'andmaxLength, and leave out the file manager. - Every option, the toolbar items and the file manager JSON format are in the documentation; the server examples add folders, rename, delete and an AI endpoint.
FAQ
Does WDS Editor work with Laravel Livewire?
Yes. Wrap the textarea in an element with wire:ignore so Livewire does not redraw the editor, and send the HTML to your component in the editor's change event. Alpine.js works the same way with x-ignore.
Where are uploaded images stored in Laravel?
In the example they go to storage/app/public/editor and get a URL through the public disk. Change the disk in the controller to store them on S3 or another driver; the editor only needs the URL that the upload returns.
Do I need a Laravel package for WDS Editor?
No. The editor is a JavaScript file and a stylesheet. The file manager needs two small routes, shown above, that return JSON in the editor's simple format.
Guides for other frameworks
Try WDS Editor in your browser
Free and MIT licensed, with a file manager, tables, Markdown and code blocks built in. No key, no account, no load limits.