跳到主要内容
版本:4.x

Code entry

简介

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

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

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

修改代码的语言(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 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
EntryFilament\Infolists\Components\Entry$componentThe current entry component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Statemixed$stateThe current value of the entry.

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 作为参数注入到这些函数中。Learn more about utility injection.
UtilityTypeParameterDescription
EntryFilament\Infolists\Components\Entry$componentThe current entry component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Statemixed$stateThe 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 作为参数注入到这些函数中。Learn more about utility injection.
UtilityTypeParameterDescription
EntryFilament\Infolists\Components\Entry$componentThe current entry component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Statemixed$stateThe current value of the entry.