信息列表

Code entry

简介

代码条目(CodeEntry)允许你在信息列表中高亮显示代码段。它使用 Phiki 在服务器上对代码进行高亮显示:

use Filament\Infolists\Components\CodeEntry;
use Phiki\Grammar\Grammar;

CodeEntry::make('code')
    ->grammar(Grammar::Php)
Code entry

To use the code entry, you need to first install the phiki/phiki Composer package. Filament does not include it by default to allow you to choose which major version of Phiki to use explicitly, since major versions can have different grammars and themes available. You can install the latest version of Phiki using the following command:

composer require phiki/phiki

修改代码的语言(language)

使用 grammar() 方法,你可以修改代码语言。可以使用超过 200 个语言,你可以通过 Phiki\Grammar\Grammar 枚举类查看全列表。比如,要切换到 JavaScript,你可以使用 Grammar::Javascript 枚举值:

use Filament\Infolists\Components\CodeEntry;
use Phiki\Grammar\Grammar;

CodeEntry::make('code')
    ->grammar(Grammar::Javascript)
除了允许静态值之外,grammar() 方法也接受函数以动态计算它的值。你可以将多个 utility 作为参数注入到该函数中。 了解更多 utility 注入详情。
Utility 类型 参数 描述
Entry Filament\Infolists\Components\Entry $component The current entry component instance.
Get function Filament\Schemas\Components\Utilities\Get $get A function for retrieving values from the current schema data. Validation is not run on form fields.
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 create, edit, or view.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current schema.
State mixed $state The current value of the entry.
Code entry with JavaScript syntax highlighting

TIP

如果你的代码内容是 PHP 数组,它会自动转换成 JSON 字符串,而语言将会设置成 Grammar::Json

修改代码主题(高亮)

你可以使用 lightTheme()darkTheme() 方法修改代码主题。可以使用超过 50 个主题,你可以打开 Phiki\Theme\Theme 枚举类查看全列表。比如,要使用流行的 Dracula 主题,你可以使用 Theme::Dracula 枚举值:

use Filament\Infolists\Components\CodeEntry;
use Phiki\Theme\Theme;

CodeEntry::make('code')
    ->lightTheme(Theme::Dracula)
    ->darkTheme(Theme::Dracula)
除了允许静态值之外,lightTheme()darkTheme() 方法也接受函数以动态计算它们的值。你可以将多个 utility 作为参数注入到这些函数中。 了解更多 utility 注入详情。
Utility 类型 参数 描述
Entry Filament\Infolists\Components\Entry $component The current entry component instance.
Get function Filament\Schemas\Components\Utilities\Get $get A function for retrieving values from the current schema data. Validation is not run on form fields.
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 create, edit, or view.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current schema.
State mixed $state The current value of the entry.
Code entry with the Dracula theme

允许将代码复制到剪贴板

你可以使文本可复制(copyable),比如点击代码将文本复制到剪切板,且可选地指定自定义确认消息及持续时长(以毫秒计)。该特性只有在应用启用 SSL 时有效。

use Filament\Infolists\Components\CodeEntry;

CodeEntry::make('code')
    ->copyable()
    ->copyMessage('Copied!')
    ->copyMessageDuration(1500)

此外,你也可以传入一个布尔值,控制代码是否可复制:

use Filament\Infolists\Components\ColorEntry;

ColorEntry::make('color')
    ->copyable(FeatureFlag::active())
除了允许静态值之外,copyable()copyMessage()copyMessageDuration() 方法也接受函数以动态计算它们的值。你可以将多个 utility 作为参数注入到这些函数中。 了解更多 utility 注入详情。
Utility 类型 参数 描述
Entry Filament\Infolists\Components\Entry $component The current entry component instance.
Get function Filament\Schemas\Components\Utilities\Get $get A function for retrieving values from the current schema data. Validation is not run on form fields.
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 create, edit, or view.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current schema.
State mixed $state The current value of the entry.
Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion

Previous
Color entry