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

配置 Livewire 最大嵌套深度
富文本编辑器将其 TipTap 文档作为嵌套数据与 Livewire 同步。Livewire 默认将嵌套属性路径限制为 10 层,对于列表和表格等结构,这可能不够用。如果遇到 Livewire\Exceptions\MaxNestingDepthExceededException 异常,且你的应用中尚无 config/livewire.php 文件,请发布 Livewire 的配置文件:
php artisan livewire:publish --config
该命令会覆盖现有的 config/livewire.php 文件,因此如果你已经发布了该配置文件,请跳过此步骤。
接下来,调大 config/livewire.php 中现有的 max_nesting_depth 设置。例如,将深度设置为 32 可以容纳深度嵌套的富文本内容:
'payload' => [
// ...
'max_nesting_depth' => 32,
],
仅修改现有 payload 数组中的 max_nesting_depth 值,以保留 Livewire 其他特定于版本的 payload 设置。
将内容存储为 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
{
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'content' => 'array',
];
}
// ...
}
自定义工具栏按钮
使用 toolbarButtons() 方法,你可以设置编辑器的工具栏按钮。此例中的选项为默认值:
use Filament\Forms\Components\RichEditor;
RichEditor::make('content')
->toolbarButtons([
['bold', 'italic', 'underline', 'strike', 'subscript', 'superscript', 'link'],
['h2', 'h3'],
['alignStart', 'alignCenter', 'alignEnd'],
['blockquote', 'codeBlock', 'bulletList', 'orderedList'],
['table', 'attachFiles'], // The `customBlocks` and `mergeTags` tools are also added here if those features are used.
['undo', 'redo'],
])
主数组中的每个嵌套数组都表示工具栏中的一组按钮。

Toolbar 中可以引入的其他组件:
h1- 将 "h1" 标签应用到文本中。h4- 将 "h4" 标签应用到文本中。h5- 将 "h5" 标签应用到文本中。h6- 将 "h6" 标签应用到文本中。alignJustify- 对齐文本。clearFormatting- Clears all formatting from the selected text.details- Inserts a<details>tag, which allows users to create collapsible sections in their content.grid- Inserts a grid layout into the editor, allowing users to create responsive columns of content.gridDelete- Deletes the current grid layout.highlight- 使用<mark>标签高亮显示选中的文本。horizontalRule- Inserts a horizontal rule.lead- 在文本中使用lead类,通常用于文章的第一章。paragraph- Sets the current block to a paragraph, removing any heading formatting.small- 将<small>标签应用到文本中,通常用于小字体打印或免责声明。code- Format the selected text as inline code.textColor- Changes the text color of the selected text.table- Creates a table in the editor with a default layout of 3 columns and 2 rows, with the first row configured as a header row.tableAddColumnBefore- Adds a new column before the current column.tableAddColumnAfter- Adds a new column after the current column.tableDeleteColumn- Deletes the current column.tableAddRowBefore- Adds a new row above the current row.tableAddRowAfter- Adds a new row below the current row.tableDeleteRow- Deletes the current row.tableMergeCells- Merges the selected cells into one cell.tableSplitCell- Splits the selected cell into multiple cells.tableToggleHeaderRow- Toggles the header row of the table.tableToggleHeaderCell- Toggles the header cell of the table.tableDelete- Deletes the table.
除了允许静态值之外,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. |
自定义浮动工具栏
如果工具栏内容过于拥挤,可以使用浮动工具栏——即仅当光标位于特定类型的节点内时,在光标下方显示的工具栏。这样既能保持主工具栏整洁,又能确保在需要时可以使用其他工具。
你可以使用 floatingToolbars() 方法,自定义光标置于特定节点内时显示的浮动工具栏。
在下方的示例中,当光标位于段落节点内时,会显示一个包含加粗、斜体等按钮的浮动工具栏;当光标位于标题节点内时,显示与标题相关的按钮;而当光标位于表格内时,则显示针对表格的控制选项。
use Filament\Forms\Components\RichEditor;
RichEditor::make('content')
->floatingToolbars([
'paragraph' => [
'bold', 'italic', 'underline', 'strike', 'subscript', 'superscript',
],
'heading' => [
'h1', 'h2', 'h3',
],
'table' => [
'tableAddColumnBefore', 'tableAddColumnAfter', 'tableDeleteColumn',
'tableAddRowBefore', 'tableAddRowAfter', 'tableDeleteRow',
'tableMergeCells', 'tableSplitCell',
'tableToggleHeaderRow', 'tableToggleHeaderCell',
'tableDelete',
],
])

将工具栏按钮组合为下拉菜单
你可以使用 ToolbarButtonGroup 将相关的工具栏按钮组合成一个下拉菜单。第一个参数是用于下拉菜单工具提示(tooltip)和无障碍的标签,第二个参数是包含在下拉菜单中的按钮名称数组:
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\ToolbarButtonGroup;
RichEditor::make('content')
->toolbarButtons([
['bold', 'italic', 'underline', 'strike'],
[ToolbarButtonGroup::make('Paragraph', ['paragraph', 'h1', 'h2', 'h3'])],
[ToolbarButtonGroup::make('Alignment', ['alignStart', 'alignCenter', 'alignEnd', 'alignJustify'])],
['blockquote', 'codeBlock', 'bulletList', 'orderedList'],
['undo', 'redo'],
])
默认情况下,第一个按钮的图标被用作下拉触发器,并会根据当前激活的按钮动态更新。点击该触发器即可显示分组的按钮。
你可以使用 icon() 方法为下拉触发器设置固定图标。一旦设置了自定义图标,触发器图标将保持固定,不会随激活按钮的变化而改变:
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\ToolbarButtonGroup;
RichEditor::make('content')
->toolbarButtons([
['bold', 'italic', 'underline', 'strike'],
[ToolbarButtonGroup::make('Heading', ['h1', 'h2', 'h3'])->icon('fi-o-heading')],
[ToolbarButtonGroup::make('Alignment', ['alignStart', 'alignCenter', 'alignEnd', 'alignJustify'])],
['blockquote', 'codeBlock', 'bulletList', 'orderedList'],
['undo', 'redo'],
])

使用文本化的工具栏下拉按钮
默认情况下,工具栏下拉按钮仅显示图标。如果你希望在下拉选项中同时显 示图标和文本标签,可以在 ToolbarButtonGroup 上使用 textualButtons() 方法:
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\ToolbarButtonGroup;
RichEditor::make('content')
->toolbarButtons([
['bold', 'italic', 'underline', 'strike', 'link'],
[ToolbarButtonGroup::make('Paragraph', ['paragraph', 'h1', 'h2', 'h3'])->textualButtons()],
[ToolbarButtonGroup::make('Alignment', ['alignStart', 'alignCenter', 'alignEnd', 'alignJustify'])],
['blockquote', 'codeBlock', 'bulletList', 'orderedList'],
['undo', 'redo'],
])

本例中,Paragraph 下拉菜单的选项同时显示图标和文本标签(e.g., "Paragraph", "Heading 1"),而 Alignment 下拉菜单则仍然仅显示图标。
设置高度
你可以通过定义 minHeight() 和 maxHeight() 方法来控制编辑器的高度,这些方法接受任何 CSS 长度值:
use Filament\Forms\Components\RichEditor;
RichEditor::make('content')
->minHeight('12rem')
->maxHeight('24rem')
编辑器默认的最小高度为 10rem。当内容高度超过 maxHeight() 设定的值时,编辑器将停止自动增高并变为可滚动状态。这两个方法既可以单独使用,也可以组合使用:minHeight() 用于设定初始高度(同时允许编辑器继续增高),而 maxHeight() 则用于限制编辑器的最大高度。向 minHeight() 传入 null 可恢复编辑器默认的 3rem 最小高度,向 maxHeight() 传入 null 则可取消最大高度限制。即使在编辑器处于禁用状态时,这些限制依然有效。
As well as allowing static values, the minHeight() and maxHeight() methods also accept functions to dynamically calculate them. You can inject various utilities into the functions as parameters.
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. |
自定义文本颜色
富文本编辑器包含一个用于设置行内文本样式的颜色工具。默认情况下,它使用 Tailwind CSS 调色板。在浅色模式下,文本使用 600 阶色值;在深色模式下,则使用 400 阶色值。
你可以使用 textColors() 方法自定义颜色选择器中可用的颜色:
use Filament\Forms\Components\RichEditor;
RichEditor::make('content')
->textColors([
'#ef4444' => 'Red',
'#10b981' => 'Green',
'#0ea5e9' => 'Sky',
])

如果你希望为浅色模式和深色模式定义不同的颜色,可以使用 TextColor 对象来定义颜色:
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\TextColor;
RichEditor::make('content')
->textColors([
'brand' => TextColor::make('Brand', '#0ea5e9'),
'warning' => TextColor::make('Warning', '#f59e0b', darkColor: '#fbbf24'),
])
如果你希望在现有的 Tailwind 調色板中添加新颜色,可以将你的颜色合并到 TextColor::getDefaults() 数组中:
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\TextColor;
RichEditor::make('content')
->textColors([
'brand' => TextColor::make('Brand', '#0ea5e9'),
'warning' => TextColor::make('Warning', '#f59e0b', darkColor: '#fbbf24'),
...TextColor::getDefaults(),
])
使用 TextColor 对象时,数组的键(key)会作为 <span> 标签上的 data-color 属性进行存储,从而允许你在 CSS 中引用该颜色。若将颜色直接作为数组的值(value),则实际的颜色值(例如 HEX 字符串)会被存储为 data-color 属性。
你还可以将 textColors() 传递给内容渲染器和富文本内容属性,以确保服务端渲染与编辑器配置保持一致。
此外,你还可以使用 customTextColors() 方法,允许用户选择预定义列表之外的自定义颜色:
use Filament\Forms\Components\RichEditor;
RichEditor::make('content')
->textColors([
// ...
])
->customTextColors()
您无需在富文本内容渲染器上使用 customTextColors(),因为它会自动渲染内容中使用的任何自定义颜色。
渲染富文本内容
如果你将内容存储为 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()
如果你使用了自定义文本颜色,可以向渲染器传入一个颜色数组,以确保颜色能被正确渲染:
use Filament\Forms\Components\RichEditor\RichContentRenderer;
use Filament\Forms\Components\RichEditor\TextColor;
RichContentRenderer::make($record->content)
->textColors([
'brand' => TextColor::make('Brand', '#0ea5e9', darkColor: '#38bdf8'),
])
->toHtml();
设置渲染内容的样式
富文本编辑器生成的 HTML 会结合 HTML 元素、CSS 类名和行内样式来渲染内容,具体取决于编辑器中使用的功能。如果你在 Filament 表格列或信息列表条目中使用 prose() 来渲染内容,Filament 会自动应用必要的样式。如果你是在自定义的 Blade 视图中输出内容,则可能需要添加一些额外的样式,以确保内容渲染正确。
设置内容样式的一种方法是使用 Tailwind CSS Typography 插件。该插件为常见的 HTML 元素(如标题、段落、列表和表格)提供了一套预定义的样式。你可以通过在容器元素上应用 prose 类名来使用这些样式:
<div class="prose dark:prose-invert">
{!! \Filament\Forms\Components\RichEditor\RichContentRenderer::make($record->content) !!}
</div>





