富文本编辑器
简介
富文本编辑器允许你编辑和预览 HTML 内容,以及上传图片。它使用 TipTap 作为底层编辑器。
use Filament\Forms\Components\RichEditor;
RichEditor::make('content')

将内容存储为 JSON
默认情况下,富文本编辑器将内容存储成 HTML,如果你想将其存储为 JSON 格式,你可以使用 json()
方法:
use Filament\Forms\Components\RichEditor;
RichEditor::make('content')
->json()
该 JSON 是以 TipTap 的格式存储,它是内容的结构化表示。
如果你使用 Eloquent 来保存 JSON 标签,你应该确保将 array
cast 添加到模型属性中:
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $casts = [
'content' => 'array',
];
// ...
}
自定义工具栏按钮
使用 toolbarButtons()
方法,你可以设置编辑器的工具栏按钮。此例中的选项为默认值。此外,'h1'
也可用:
use Filament\Forms\Components\RichEditor;
RichEditor::make('content')
->toolbarButtons([
['bold', 'italic', 'underline', 'strike', 'subscript', 'superscript', 'link'],
['h2', 'h3'],
['blockquote', 'codeBlock', 'bulletList', 'orderedList'],
['attachFiles'], // The `customBlocks` and `mergeTags` tools are also added here if those features are used.
['undo', 'redo'],
])
主数组中的每个嵌套数组都表示工具栏中的一组按钮。
除了允许静态值之外,toolbarButtons()
方法也接收函数来计算它的值。你可以将各种 utility 作为参数注入到该函数中。
Learn more about utility injection.Utility | Type | Parameter | Description |
---|---|---|---|
Field | Filament\Forms\Components\Field | $component | The current field component instance. |
Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
Livewire | Livewire\Component | $livewire | The Livewire component instance. |
Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
Operation | string | $operation | The current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>. |
Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
State | mixed | $state | The current value of the field. Validation is not run. |
渲染富文本内容
如果你将内容存储为 JSON而非 HTML,或者你的内容需要处理以注入私有图片 URL等,你将需要使用 Filament 中 RichContentRenderer
工具来输出 HTML:
use Filament\Forms\Components\RichEditor\RichContentRenderer;
RichContentRenderer::make($record->content)->toHtml()
toHtml()
方法返回一个字符串。如果你想要在 Blade 视图中输出 HTML 而不进行转义,你可以输出 RichContentRender
而不调用 toHtml()
{{ \Filament\Forms\Components\RichEditor\RichContentRenderer::make($record->content) }}
如果你已经配置了编辑器的文件附件行为以修改上传文件的磁盘或可见性,则还必须将这些设置传递给渲染器,以确保生成正确的 URL:
use Filament\Forms\Components\RichEditor\RichContentRenderer;
RichContentRenderer::make($record->content)
->fileAttachmentsDisk('s3')
->fileAttachmentsVisibility('private')
->toHtml()
如果你在富文本编辑器中使用了自定义 Block,你可以将自定义 Block 数组传入到渲染器,以确保其正确渲染:
use Filament\Forms\Components\RichEditor\RichContentRenderer;
RichContentRenderer::make($record->content)
->customBlocks([
HeroBlock::class => [
'categoryUrl' => $record->category->getUrl(),
],
CallToActionBlock::class,
])
->toHtml()
如果你要使用合并标签,你可以传入值数组来替换要合并标签:
use Filament\Forms\Components\RichEditor\RichContentRenderer;
RichContentRenderer::make($record->content)
->mergeTags([
'name' => $record->user->name,
'today' => now()->toFormattedDateString(),
])
->toHtml()
安全
默认情况下,该编辑器输出原始 HTML,并将其发送到后端。攻击者能够拦截组件的值,并将不同的原始 HTML 字符串发送到后端。因此,从富文本编辑器输出 HTML 时,对其进行净化非常重要;否则,你的网站可能会暴露于跨站点脚本(XSS)漏洞。
当 Filament 在 TextColumn
和 TextEntry
等组件中从数据库输出原始 HTML 时,它会对其进行净化,以删除任何危险的 JavaScript。但是,如果你在自己的 Blade 视图中输出来自富文本编辑器的 HTML,这是你的责任。一种选择是使用 Filament 的 sanctizeHtml()
助手函数来执行此操作,这与我们在上述组件中用于净化 HTML 的工具相同:
{!! str($record->content)->sanitizeHtml() !!}
如果你将内容存储为 JSON而非 HTML,或者你的内容需要处理以注入私有图像 URL或类似行为,你可以使用