Newer
Older

Yassine Doghri
committed
<?php
declare(strict_types=1);
namespace App\Views\Components\Forms;

Yassine Doghri
committed
class MarkdownEditor extends FormComponent

Yassine Doghri
committed
{

Yassine Doghri
committed
/**
* @var string[]
*/
protected array $disallowList = [];
public function setDisallowList(string $value): void
{
$this->disallowList = explode(',', $value);
}

Yassine Doghri
committed
public function render(): string
{

Yassine Doghri
committed
$editorClass = 'w-full flex flex-col bg-elevated border-3 border-contrast rounded-lg overflow-hidden focus-within:ring-accent ' . $this->class;

Yassine Doghri
committed
$this->attributes['class'] = 'bg-elevated border-none focus:border-none focus:outline-none focus:ring-0 w-full h-full';
$this->attributes['rows'] = 6;

Yassine Doghri
committed
$value = htmlspecialchars_decode($this->value);

Yassine Doghri
committed
$oldValue = old($this->name);
if ($oldValue === null) {
$oldValue = $value;
}
$textarea = form_textarea($this->attributes, $oldValue);
$markdownIcon = icon(
'markdown',
'mr-1 text-lg opacity-40'
);
$translations = [
'write' => lang('Common.forms.editor.write'),
'preview' => lang('Common.forms.editor.preview'),
'help' => lang('Common.forms.editor.help'),
];

Yassine Doghri
committed

Yassine Doghri
committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
$toolbarGroups = [
[
[
'name' => 'header',
'tag' => 'md-header',
'icon' => icon('heading'),
],
[
'name' => 'bold',
'tag' => 'md-bold',
'icon' => icon('bold'),
],
[
'name' => 'italic',
'tag' => 'md-italic',
'icon' => icon('italic'),
],
],
[
[
'name' => 'unordered-list',
'tag' => 'md-unordered-list',
'icon' => icon('list-unordered'),
],
[
'name' => 'ordered-list',
'tag' => 'md-ordered-list ',
'icon' => icon('list-ordered'),
],
],
[
[
'name' => 'quote',
'tag' => 'md-quote',
'icon' => icon('quote'),
],
[
'name' => 'link',
'tag' => 'md-link',
'icon' => icon('link'),
],
[
'name' => 'image',
'tag' => 'md-image',
'icon' => icon('image-add'),
],
],
];
$toolbarContent = '';
foreach ($toolbarGroups as $buttonsGroup) {
$toolbarContent .= '<div class="inline-flex text-2xl gap-x-1">';
foreach ($buttonsGroup as $button) {
if (! in_array($button['name'], $this->disallowList, true)) {
$toolbarContent .= '<' . $button['tag'] . ' class="opacity-50 hover:opacity-100 focus:ring-accent focus:opacity-100">' . $button['icon'] . '</' . $button['tag'] . '>';
}
}
$toolbarContent .= '</div>';
}
return <<<HTML
<div class="{$editorClass}">
<header class="px-2">

Yassine Doghri
committed
<div class="sticky top-0 z-20 flex flex-wrap justify-between border-b border-gray-300 bg-elevated">
<markdown-write-preview for="{$this->id}" class="relative inline-flex h-8">

Yassine Doghri
committed
<button type="button" slot="write" class="px-2 font-semibold focus:ring-inset focus:ring-accent">{$translations['write']}</button>
<button type="button" slot="preview" class="px-2 font-semibold focus:ring-inset focus:ring-accent">{$translations['preview']}</button>
</markdown-write-preview>

Yassine Doghri
committed
<markdown-toolbar for="{$this->id}" class="flex gap-4 px-2 py-1">{$toolbarContent}</markdown-toolbar>
</div>
</header>
<div class="relative">
{$textarea}

Yassine Doghri
committed
<markdown-preview for="{$this->id}" class="absolute top-0 left-0 hidden w-full h-full max-w-full px-3 py-2 overflow-y-auto prose bg-base" showClass="bg-elevated" />
</div>

Yassine Doghri
committed
<footer class="flex px-2 py-1 border-t bg-base">

Yassine Doghri
committed
<a href="https://commonmark.org/help/" class="inline-flex items-center text-xs font-semibold text-skin-muted hover:text-skin-base" target="_blank" rel="noopener noreferrer">{$markdownIcon}{$translations['help']}</a>
</footer>
</div>
HTML;