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

代码编辑器

简介​

代码编辑器允许你在文本框中显示行号编写代码。默认情况下,代码高亮并不启用。

use Filament\Forms\Components\CodeEditor;

CodeEditor::make('code')
Code editor

语言语法高亮​

使用 language() 方法,你可以修改代码编辑器的语言语法高亮。该编辑器支持以下语言的高亮显示:

  • C++
  • CSS
  • Go
  • HTML
  • Java
  • JavaScript
  • JSON
  • Markdown
  • PHP
  • Python
  • SQL
  • XML
  • YAML

你可以打开 Filament\Forms\Components\CodeEditor\Enums\Language 枚举类查看它支持的全列表。比如,要启用 JavaScript 的语法高亮,你可以使用 Language::JavaScript 枚举值:

use Filament\Forms\Components\CodeEditor;
use Filament\Forms\Components\CodeEditor\Enums\Language;

CodeEditor::make('code')
->language(Language::JavaScript)
除了允许静态值外,language() 方法同时也接受通过函数动态设置其值。你可以将多个 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
FieldFilament\Forms\Components\Field$componentThe current field component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current form data. Validation is not run.
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>.
Raw statemixed$rawStateThe current value of the field, before state casts were applied. Validation is not run.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Statemixed$stateThe current value of the field. Validation is not run.
Code editor with syntax highlighting

允许换行显示一行​

默认情况下,如果代码编辑器中行内的内容比较多,将会创建一个水平滚动条。如果你想允许允许长行自动换行,可以使用 wrap() 方法:

use Filament\Forms\Components\CodeEditor;

CodeEditor::make('code')
->wrap()
Code editor with line wrapping