1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Code Field plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Provides a Code Field that has a full-featured code editor with syntax highlighting & autocomplete |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\codefield\fields; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
15
|
|
|
use craft\base\Field; |
16
|
|
|
use craft\base\PreviewableFieldInterface; |
17
|
|
|
use craft\helpers\Html; |
18
|
|
|
use craft\helpers\Json; |
19
|
|
|
use craft\validators\ArrayValidator; |
20
|
|
|
use GraphQL\Type\Definition\Type; |
21
|
|
|
use nystudio107\codefield\gql\types\generators\CodeDataGenerator; |
22
|
|
|
use nystudio107\codefield\models\CodeData; |
23
|
|
|
use nystudio107\codefield\validators\JsonValidator; |
24
|
|
|
use yii\db\Schema; |
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* @author nystudio107 |
|
|
|
|
28
|
|
|
* @package CodeField |
|
|
|
|
29
|
|
|
* @since 4.0.0 |
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
class Code extends Field implements PreviewableFieldInterface |
32
|
|
|
{ |
33
|
|
|
// Public Properties |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @var string The theme to use for the Code Editor field. |
38
|
|
|
*/ |
39
|
|
|
public string $theme = 'auto'; |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @var string The language to use for the Code Editor field. |
43
|
|
|
*/ |
44
|
|
|
public string $language = 'javascript'; |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @var bool Whether the Code Editor field display as a single line |
48
|
|
|
*/ |
49
|
|
|
public bool $singleLineEditor = false; |
50
|
|
|
|
51
|
|
|
/** |
|
|
|
|
52
|
|
|
* @var int The font size to use for the Code Editor field |
53
|
|
|
*/ |
54
|
|
|
public int $fontSize = 14; |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* @var bool Whether line numbers should be displayed in the Code Editor field |
58
|
|
|
*/ |
59
|
|
|
public bool $lineNumbers = false; |
60
|
|
|
|
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* @var bool Whether code folding should be used in the Code Editor field |
63
|
|
|
*/ |
64
|
|
|
public bool $codeFolding = false; |
65
|
|
|
|
66
|
|
|
/** |
|
|
|
|
67
|
|
|
* @var string The text that will be shown if the code field is empty. |
68
|
|
|
*/ |
69
|
|
|
public string $placeholder = ''; |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @var bool Whether the language selector dropdown menu should be displayed. |
73
|
|
|
*/ |
74
|
|
|
public bool $showLanguageDropdown = true; |
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* @var array The languages that should be listed in the language selector dropdown menu. |
78
|
|
|
*/ |
79
|
|
|
public array $availableLanguages = [ |
80
|
|
|
'css', |
81
|
|
|
'graphql', |
82
|
|
|
'html', |
83
|
|
|
'javascript', |
84
|
|
|
'json', |
85
|
|
|
'markdown', |
86
|
|
|
'mysql', |
87
|
|
|
'php', |
88
|
|
|
'shell', |
89
|
|
|
'twig', |
90
|
|
|
'typescript', |
91
|
|
|
'yaml', |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @var string JSON blob of Monaco [EditorOptions](https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IEditorOptions.html) that will override the default settings |
96
|
|
|
*/ |
97
|
|
|
public string $monacoEditorOptions = ''; |
98
|
|
|
|
99
|
|
|
// Static Methods |
100
|
|
|
// ========================================================================= |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
|
|
|
|
105
|
|
|
public static function displayName(): string |
106
|
|
|
{ |
107
|
|
|
return Craft::t('codefield', 'Code'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Public Methods |
111
|
|
|
// ========================================================================= |
112
|
|
|
|
113
|
|
|
/** |
|
|
|
|
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
|
|
|
|
116
|
|
|
public function normalizeValue($value, ElementInterface $element = null): mixed |
117
|
|
|
{ |
118
|
|
|
if ($value instanceof CodeData) { |
119
|
|
|
return $value; |
120
|
|
|
} |
121
|
|
|
// Default config |
122
|
|
|
$config = [ |
123
|
|
|
'value' => '', |
124
|
|
|
'language' => $this->language, |
125
|
|
|
]; |
126
|
|
|
// Handle incoming values potentially being JSON or an array |
127
|
|
|
if (!empty($value)) { |
128
|
|
|
// Handle JSON-encoded values coming in |
129
|
|
|
if (\is_string($value)) { |
130
|
|
|
$jsonValue = Json::decodeIfJson($value); |
131
|
|
|
// If this is still a string (meaning it's not valid JSON), treat it as the value |
132
|
|
|
if (\is_string($jsonValue)) { |
133
|
|
|
$config['value'] = $jsonValue; |
134
|
|
|
} else { |
135
|
|
|
$value = $jsonValue; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
if (\is_array($value)) { |
139
|
|
|
$config = array_merge($config, array_filter($value)); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
// Create and validate the model |
143
|
|
|
$codeData = new CodeData($config); |
144
|
|
|
if (!$codeData->validate()) { |
145
|
|
|
Craft::error( |
146
|
|
|
Craft::t('codefield', 'CodeData failed validation: ') |
147
|
|
|
. print_r($codeData->getErrors(), true), |
|
|
|
|
148
|
|
|
__METHOD__ |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $codeData; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
|
|
|
|
156
|
|
|
* @inheritdoc |
157
|
|
|
*/ |
|
|
|
|
158
|
|
|
public function getSettingsHtml(): ?string |
159
|
|
|
{ |
160
|
|
|
$monacoLanguages = require(__DIR__ . '/MonacoLanguages.php'); |
|
|
|
|
161
|
|
|
// Render the settings template |
162
|
|
|
return Craft::$app->getView()->renderTemplate( |
163
|
|
|
'codefield/_components/fields/Code_settings', |
164
|
|
|
[ |
165
|
|
|
'field' => $this, |
166
|
|
|
'monacoLanguages' => $monacoLanguages, |
167
|
|
|
] |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
|
|
|
|
172
|
|
|
* @inheritdoc |
173
|
|
|
*/ |
|
|
|
|
174
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
175
|
|
|
{ |
176
|
|
|
// Get our id and namespace |
177
|
|
|
$id = Html::id($this->handle); |
|
|
|
|
178
|
|
|
$namespacedId = Craft::$app->getView()->namespaceInputId($id); |
179
|
|
|
|
180
|
|
|
// Extract just the languages that have been selected for display |
181
|
|
|
$displayLanguages = []; |
182
|
|
|
if ($this->showLanguageDropdown) { |
183
|
|
|
$monacoLanguages = require(__DIR__ . '/MonacoLanguages.php'); |
|
|
|
|
184
|
|
|
$decomposedLanguages = array_column($monacoLanguages, 'label', 'value'); |
185
|
|
|
$displayLanguages = array_intersect_key($decomposedLanguages, array_flip($this->availableLanguages)); |
186
|
|
|
$displayLanguages = array_map(static function ($k, $v) { |
|
|
|
|
187
|
|
|
return ['value' => $k, 'label' => $v]; |
188
|
|
|
}, array_keys($displayLanguages), array_values($displayLanguages)); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
$monacoOptionsOverride = Json::decodeIfJson($this->monacoEditorOptions); |
191
|
|
|
if ($monacoOptionsOverride === null || is_string($monacoOptionsOverride)) { |
192
|
|
|
$monacoOptionsOverride = []; |
193
|
|
|
} |
194
|
|
|
// Render the input template |
195
|
|
|
return Craft::$app->getView()->renderTemplate( |
196
|
|
|
'codefield/_components/fields/Code_input', |
197
|
|
|
[ |
198
|
|
|
'name' => $this->handle, |
199
|
|
|
'value' => $value, |
200
|
|
|
'field' => $this, |
201
|
|
|
'orientation' => $this->getOrientation($element), |
202
|
|
|
'id' => $id, |
203
|
|
|
'namespacedId' => $namespacedId, |
204
|
|
|
'displayLanguages' => $displayLanguages, |
205
|
|
|
'monacoOptionsOverride' => $monacoOptionsOverride, |
206
|
|
|
] |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
|
|
|
|
211
|
|
|
* @inheritdoc |
212
|
|
|
*/ |
|
|
|
|
213
|
|
|
public function getContentGqlType(): Type|array |
214
|
|
|
{ |
215
|
|
|
$typeArray = CodeDataGenerator::generateTypes($this); |
216
|
|
|
|
217
|
|
|
return [ |
218
|
|
|
'name' => $this->handle, |
219
|
|
|
'description' => 'Code Editor field', |
220
|
|
|
'type' => array_shift($typeArray), |
221
|
|
|
]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
|
|
|
|
225
|
|
|
* @inheritdoc |
226
|
|
|
*/ |
|
|
|
|
227
|
|
|
public function rules(): array |
228
|
|
|
{ |
229
|
|
|
$rules = parent::rules(); |
230
|
|
|
return array_merge($rules, [ |
|
|
|
|
231
|
|
|
['theme', 'in', 'range' => ['auto', 'vs', 'vs-dark', 'hc-black']], |
232
|
|
|
['theme', 'default', 'value' => 'auto'], |
233
|
|
|
['language', 'string'], |
234
|
|
|
['language', 'default', 'value' => 'javascript'], |
235
|
|
|
[['singleLineEditor', 'showLanguageDropdown', 'lineNumbers', 'codeFolding'], 'boolean'], |
236
|
|
|
['placeholder', 'string'], |
237
|
|
|
['placeholder', 'default', 'value' => ''], |
238
|
|
|
['fontSize', 'integer'], |
239
|
|
|
['fontSize', 'default', 'value' => 14], |
240
|
|
|
['availableLanguages', ArrayValidator::class], |
241
|
|
|
['monacoEditorOptions', JsonValidator::class], |
242
|
|
|
]); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
|
|
|
|
246
|
|
|
* @inheritdoc |
247
|
|
|
*/ |
|
|
|
|
248
|
|
|
public function getContentColumnType(): string |
249
|
|
|
{ |
250
|
|
|
return Schema::TYPE_TEXT; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|