Skip to content

Commit e6e28d4

Browse files
authored
feat: markdown editor (#16)
* feat: markdown editor * Fix styling --------- Co-authored-by: Baspa <[email protected]>
1 parent f997389 commit e6e28d4

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/Concerns/CanMapDynamicFields.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Backstage\Fields\Fields\Color;
1111
use Backstage\Fields\Fields\DateTime;
1212
use Backstage\Fields\Fields\KeyValue;
13+
use Backstage\Fields\Fields\MarkdownEditor;
1314
use Backstage\Fields\Fields\Radio;
1415
use Backstage\Fields\Fields\Repeater;
1516
use Backstage\Fields\Fields\RichEditor;
@@ -41,6 +42,7 @@ trait CanMapDynamicFields
4142
'text' => Text::class,
4243
'textarea' => Textarea::class,
4344
'rich-editor' => RichEditor::class,
45+
'markdown-editor' => MarkdownEditor::class,
4446
'repeater' => Repeater::class,
4547
'select' => Select::class,
4648
'checkbox' => Checkbox::class,

src/Fields/MarkdownEditor.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Backstage\Fields\Fields;
4+
5+
use Backstage\Fields\Contracts\FieldContract;
6+
use Backstage\Fields\Enums\ToolbarButton;
7+
use Backstage\Fields\Models\Field;
8+
use Filament\Forms;
9+
use Filament\Forms\Components\RichEditor as Input;
10+
11+
class MarkdownEditor extends Base implements FieldContract
12+
{
13+
public static function getDefaultConfig(): array
14+
{
15+
return [
16+
...parent::getDefaultConfig(),
17+
'toolbarButtons' => ['attachFiles', 'blockquote', 'bold', 'bulletList', 'codeBlock', 'heading', 'italic', 'link', 'orderedList', 'redo', 'strike', 'table', 'undo'],
18+
'disableToolbarButtons' => [],
19+
'fileAttachmentsDisk' => 'public',
20+
'fileAttachmentsDirectory' => 'attachments',
21+
'fileAttachmentsVisibility' => 'public',
22+
];
23+
}
24+
25+
public static function make(string $name, ?Field $field = null): Input
26+
{
27+
$input = self::applyDefaultSettings(Input::make($name), $field);
28+
29+
$input = $input->label($field->name ?? null)
30+
->toolbarButtons($field->config['toolbarButtons'] ?? self::getDefaultConfig()['toolbarButtons'])
31+
->disableToolbarButtons($field->config['disableToolbarButtons'] ?? self::getDefaultConfig()['disableToolbarButtons'])
32+
->fileAttachmentsDisk($field->config['fileAttachmentsDisk'] ?? self::getDefaultConfig()['fileAttachmentsDisk'])
33+
->fileAttachmentsDirectory($field->config['fileAttachmentsDirectory'] ?? self::getDefaultConfig()['fileAttachmentsDirectory'])
34+
->fileAttachmentsVisibility($field->config['fileAttachmentsVisibility'] ?? self::getDefaultConfig()['fileAttachmentsVisibility']);
35+
36+
return $input;
37+
}
38+
39+
public function getForm(): array
40+
{
41+
return [
42+
Forms\Components\Tabs::make()
43+
->schema([
44+
Forms\Components\Tabs\Tab::make('General')
45+
->label(__('General'))
46+
->schema([
47+
...parent::getForm(),
48+
]),
49+
Forms\Components\Tabs\Tab::make('Field specific')
50+
->label(__('Field specific'))
51+
->schema([
52+
Forms\Components\Grid::make(2)
53+
->schema([
54+
Forms\Components\Select::make('config.toolbarButtons')
55+
->label(__('Toolbar buttons'))
56+
->default(['attachFiles', 'blockquote', 'bold', 'bulletList', 'codeBlock', 'heading', 'italic', 'link', 'orderedList', 'redo', 'strike', 'table', 'undo'])
57+
->default(ToolbarButton::array()) // Not working in Filament yet.
58+
->multiple()
59+
->options(ToolbarButton::array())
60+
->columnSpanFull(),
61+
Forms\Components\Grid::make(3)
62+
->schema([
63+
Forms\Components\TextInput::make('config.fileAttachmentsDisk')
64+
->label(__('File attachments disk'))
65+
->default('public'),
66+
Forms\Components\TextInput::make('config.fileAttachmentsDirectory')
67+
->label(__('File attachments directory'))
68+
->default('attachments'),
69+
Forms\Components\TextInput::make('config.fileAttachmentsVisibility')
70+
->label(__('File attachments visibility'))
71+
->default('public'),
72+
]),
73+
]),
74+
]),
75+
])->columnSpanFull(),
76+
];
77+
}
78+
}

0 commit comments

Comments
 (0)