-
Notifications
You must be signed in to change notification settings - Fork 554
/
Wysiwyg.php
223 lines (182 loc) · 5.24 KB
/
Wysiwyg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace A17\Twill\Services\Forms\Fields;
use A17\Twill\Services\Forms\Fields\Helpers\TiptapWrapper;
use A17\Twill\Services\Forms\Fields\Traits\HasMaxlength;
use A17\Twill\Services\Forms\Fields\Traits\HasOnChange;
use A17\Twill\Services\Forms\Fields\Traits\HasPlaceholder;
use A17\Twill\Services\Forms\Fields\Traits\HasDirection;
use A17\Twill\Services\Forms\Fields\Traits\IsTranslatable;
class Wysiwyg extends BaseFormField
{
use IsTranslatable;
use HasMaxlength;
use HasPlaceholder;
use HasDirection;
use HasOnChange;
public bool $hideCounter = false;
public bool $editSource = false;
public ?array $toolbarOptions = [
['header' => [2, 3, 4, 5, 6, false]],
'bold',
'italic',
'underline',
'strike',
'blockquote',
"code-block",
'ordered',
'bullet',
'hr',
'code',
'link',
'clean',
'table',
];
/**
* @var TiptapWrapper[]
*/
protected array $tiptapWrappers = [];
public ?array $options = null;
public string $type = 'tiptap';
public bool $limitHeight = false;
public bool $syntax = false;
public string $customTheme = 'github';
public ?array $customOptions = null;
public ?array $browserModules;
public ?array $classList;
public static function make(): static
{
return new self(
component: \A17\Twill\View\Components\Fields\Wysiwyg::class,
mandatoryProperties: ['name', 'label']
);
}
/**
* Hides the character counter.
*/
public function hideCounter(bool $hideCounter = true): static
{
$this->hideCounter = $hideCounter;
return $this;
}
/**
* Adds a edit source button.
*/
public function allowSource(bool $allowSource = true): static
{
$this->editSource = $allowSource;
return $this;
}
/**
* Allows you to set custom toolbar options. This depends on the editor used.
*/
public function toolbarOptions(array $toolbarOptions): static
{
$this->toolbarOptions = $toolbarOptions;
return $this;
}
/**
* Allows you to set editor options. This depends on the editor used.
*/
public function options(array $options): static
{
$this->options = $options;
return $this;
}
/**
* The type of editor to use, defaults to quill, options are: tiptap
*/
public function type(string $type): static
{
$this->type = $type;
return $this;
}
/**
* Limits the height of the editor, otherwise grows infinitely.
*/
public function limitHeight(bool $limitHeight = true): static
{
$this->limitHeight = $limitHeight;
return $this;
}
/**
* Enables syntax highlight.
*/
public function syntax(bool $syntax = true): static
{
$this->syntax = $syntax;
return $this;
}
/**
* Set a custom theme for the syntax highlighter.
*/
public function customTheme(string $customTheme): static
{
$this->customTheme = $customTheme;
return $this;
}
/**
* Additional custom options.
*/
public function customOptions(array $customOptions): static
{
$this->customOptions = $customOptions;
return $this;
}
/**
* Add wrappers that can be used in the editor.
*/
public function addTiptapWrapper(TiptapWrapper $wrapper): static
{
$this->tiptapWrappers[$wrapper->className] = $wrapper;
return $this;
}
protected function getToolbarOptions(): array
{
$base = $this->toolbarOptions;
if ($this->tiptapWrappers !== []) {
$wrapperList = [];
foreach ($this->tiptapWrappers as $wrapper) {
$wrapperList[] = $wrapper->toArray();
}
$base[] = [
'wrappers' => $wrapperList
];
}
return $base;
}
/**
* The browser module(s) that can be used to select existing content.
*/
public function browserModules(?array $modules = null): static
{
if (count($modules) === 1 && ! isset($modules[0])) {
$this->browserModules[] = [
'name' => getModuleNameByModel(array_pop($modules))
];
} else {
foreach ($modules as $module) {
if (isset($module['name'])) {
$this->browserModules[] = [
'name' => getModuleNameByModel($module['name']),
'label' => $module['label']
];
} else {
$this->browserModules[] = [
'name' => getModuleNameByModel($module),
];
}
}
}
return $this;
}
/**
* Assosiative array ['class' => 'Label'] of classes
* than will be available to editor in the form of checkboxes
* when creating new link in modal
*/
public function classList(array $list): static
{
$this->classList = array_map(fn($value, $label) => ['value' => $value, 'label' => $label, 'selected' => false], array_keys($list), $list);
return $this;
}
}