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 string The default value the Code Field will be populated with |
78
|
|
|
*/ |
79
|
|
|
public string $defaultValue = ''; |
80
|
|
|
|
81
|
|
|
/** |
|
|
|
|
82
|
|
|
* @var array The languages that should be listed in the language selector dropdown menu. |
83
|
|
|
*/ |
84
|
|
|
public array $availableLanguages = [ |
85
|
|
|
'css', |
86
|
|
|
'graphql', |
87
|
|
|
'html', |
88
|
|
|
'javascript', |
89
|
|
|
'json', |
90
|
|
|
'markdown', |
91
|
|
|
'mysql', |
92
|
|
|
'php', |
93
|
|
|
'shell', |
94
|
|
|
'twig', |
95
|
|
|
'typescript', |
96
|
|
|
'yaml', |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
/** |
|
|
|
|
100
|
|
|
* @var string|null The type of database column the field should have in the content table |
101
|
|
|
*/ |
102
|
|
|
public ?string $columnType = Schema::TYPE_TEXT; |
103
|
|
|
|
104
|
|
|
/** |
|
|
|
|
105
|
|
|
* @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 |
106
|
|
|
*/ |
107
|
|
|
public string $monacoEditorOptions = ''; |
108
|
|
|
|
109
|
|
|
// Static Methods |
110
|
|
|
// ========================================================================= |
111
|
|
|
|
112
|
|
|
/** |
|
|
|
|
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
|
|
|
|
115
|
|
|
public static function displayName(): string |
116
|
|
|
{ |
117
|
|
|
return Craft::t('codefield', 'Code'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Public Methods |
121
|
|
|
// ========================================================================= |
122
|
|
|
|
123
|
|
|
/** |
|
|
|
|
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
|
|
|
|
126
|
|
|
public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed |
127
|
|
|
{ |
128
|
|
|
if ($value instanceof CodeData) { |
129
|
|
|
return $value; |
130
|
|
|
} |
131
|
|
|
// Default config |
132
|
|
|
$config = [ |
133
|
|
|
'value' => $this->defaultValue, |
134
|
|
|
'language' => $this->language, |
135
|
|
|
]; |
136
|
|
|
// Handle incoming values potentially being JSON or an array |
137
|
|
|
if (!empty($value)) { |
138
|
|
|
// Handle JSON-encoded values coming in |
139
|
|
|
if (\is_string($value)) { |
140
|
|
|
$jsonValue = Json::decodeIfJson($value); |
141
|
|
|
// If this is still a string (meaning it's not valid JSON), treat it as the value |
142
|
|
|
if (\is_string($jsonValue)) { |
143
|
|
|
$config['value'] = $jsonValue; |
144
|
|
|
} |
145
|
|
|
if (\is_array($jsonValue)) { |
146
|
|
|
// Check to make sure the array returned is an encoded `CodeData` config, with exactly |
147
|
|
|
// the same expected key/value pairs |
148
|
|
|
if (!array_diff_key($config, $jsonValue) && !array_diff_key($jsonValue, $config)) { |
149
|
|
|
$value = $jsonValue; |
150
|
|
|
} else { |
151
|
|
|
// Otherwise treat it as JSON data |
152
|
|
|
$value = [ |
153
|
|
|
'value' => $value, |
154
|
|
|
'language' => 'json', |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
if (\is_array($value)) { |
160
|
|
|
$config = array_merge($config, array_filter($value)); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
// Create and validate the model |
164
|
|
|
$codeData = new CodeData($config); |
165
|
|
|
if (!$codeData->validate()) { |
166
|
|
|
Craft::error( |
167
|
|
|
Craft::t('codefield', 'CodeData failed validation: ') |
168
|
|
|
. print_r($codeData->getErrors(), true), |
|
|
|
|
169
|
|
|
__METHOD__ |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $codeData; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
|
|
|
|
177
|
|
|
* @inheritdoc |
178
|
|
|
*/ |
|
|
|
|
179
|
|
|
public function getSettingsHtml(): ?string |
180
|
|
|
{ |
181
|
|
|
$monacoLanguages = require(__DIR__ . '/MonacoLanguages.php'); |
|
|
|
|
182
|
|
|
$schemaFilePath = Craft::getAlias('@nystudio107/codefield/resources/IEditorOptionsSchema.json'); |
183
|
|
|
$optionsSchema = @file_get_contents($schemaFilePath) ?: ''; |
|
|
|
|
184
|
|
|
// Render the settings template |
185
|
|
|
return Craft::$app->getView()->renderTemplate( |
186
|
|
|
'codefield/_components/fields/Code_settings', |
187
|
|
|
[ |
188
|
|
|
'field' => $this, |
189
|
|
|
'monacoLanguages' => $monacoLanguages, |
190
|
|
|
'optionsSchema' => $optionsSchema, |
191
|
|
|
] |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
|
|
|
|
196
|
|
|
* @inheritdoc |
197
|
|
|
*/ |
|
|
|
|
198
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
199
|
|
|
{ |
200
|
|
|
$twigVariables = $this->getFieldRenderVariables($value, $element, true); |
|
|
|
|
201
|
|
|
// Render the input template |
202
|
|
|
return Craft::$app->getView()->renderTemplate( |
203
|
|
|
'codefield/_components/fields/Code_input', |
204
|
|
|
$twigVariables |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
|
|
|
|
209
|
|
|
* @inheritdoc |
210
|
|
|
*/ |
|
|
|
|
211
|
|
|
public function getStaticHtml(mixed $value, ElementInterface $element): string |
212
|
|
|
{ |
213
|
|
|
$twigVariables = $this->getFieldRenderVariables($value, $element, false); |
214
|
|
|
// Render the input template |
215
|
|
|
return Craft::$app->getView()->renderTemplate( |
216
|
|
|
'codefield/_components/fields/Code_input', |
217
|
|
|
$twigVariables |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
|
|
|
|
222
|
|
|
* @inheritdoc |
223
|
|
|
*/ |
|
|
|
|
224
|
|
|
public function getContentGqlType(): Type|array |
225
|
|
|
{ |
226
|
|
|
$typeArray = CodeDataGenerator::generateTypes($this); |
227
|
|
|
|
228
|
|
|
return [ |
229
|
|
|
'name' => $this->handle, |
230
|
|
|
'description' => 'Code Editor field', |
231
|
|
|
'type' => array_shift($typeArray), |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
|
|
|
|
236
|
|
|
* @inheritdoc |
237
|
|
|
*/ |
|
|
|
|
238
|
|
|
public function rules(): array |
239
|
|
|
{ |
240
|
|
|
$rules = parent::rules(); |
241
|
|
|
return array_merge($rules, [ |
|
|
|
|
242
|
|
|
['theme', 'in', 'range' => ['auto', 'vs', 'vs-dark', 'hc-black']], |
243
|
|
|
['theme', 'default', 'value' => 'auto'], |
244
|
|
|
['language', 'string'], |
245
|
|
|
['language', 'default', 'value' => 'javascript'], |
246
|
|
|
[['singleLineEditor', 'showLanguageDropdown', 'lineNumbers', 'codeFolding'], 'boolean'], |
247
|
|
|
['placeholder', 'string'], |
248
|
|
|
['placeholder', 'default', 'value' => ''], |
249
|
|
|
['fontSize', 'integer'], |
250
|
|
|
['fontSize', 'default', 'value' => 14], |
251
|
|
|
['availableLanguages', ArrayValidator::class], |
252
|
|
|
['monacoEditorOptions', JsonValidator::class], |
253
|
|
|
]); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
|
|
|
|
257
|
|
|
* @inheritdoc |
258
|
|
|
*/ |
|
|
|
|
259
|
|
|
public function getContentColumnType(): string |
260
|
|
|
{ |
261
|
|
|
if ($this->columnType) { |
262
|
|
|
return $this->columnType; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return Schema::TYPE_TEXT; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// Protected Methods |
269
|
|
|
// ========================================================================= |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Return the Twig variables for rendering the field |
273
|
|
|
* |
274
|
|
|
* @param $value |
|
|
|
|
275
|
|
|
* @param ElementInterface $element |
|
|
|
|
276
|
|
|
* @param bool $enabled Whether the field is enabled or not |
|
|
|
|
277
|
|
|
* @return array[] |
|
|
|
|
278
|
|
|
*/ |
279
|
|
|
protected function getFieldRenderVariables($value, ElementInterface $element, bool $enabled): array |
280
|
|
|
{ |
281
|
|
|
// Get our id and namespace |
282
|
|
|
$id = Html::id($this->handle); |
|
|
|
|
283
|
|
|
$namespacedId = Craft::$app->getView()->namespaceInputId($id); |
284
|
|
|
|
285
|
|
|
// Extract just the languages that have been selected for display |
286
|
|
|
$displayLanguages = []; |
287
|
|
|
if ($this->showLanguageDropdown) { |
288
|
|
|
$monacoLanguages = require(__DIR__ . '/MonacoLanguages.php'); |
|
|
|
|
289
|
|
|
$decomposedLanguages = array_column($monacoLanguages, 'label', 'value'); |
290
|
|
|
$displayLanguages = array_intersect_key($decomposedLanguages, array_flip($this->availableLanguages)); |
291
|
|
|
// Handle "all" checkbox |
292
|
|
|
if ($this->availableLanguages[0] === '*') { |
293
|
|
|
$displayLanguages = $decomposedLanguages; |
294
|
|
|
} |
295
|
|
|
$displayLanguages = array_map(static function ($k, $v) { |
|
|
|
|
296
|
|
|
return ['value' => $k, 'label' => $v]; |
297
|
|
|
}, array_keys($displayLanguages), array_values($displayLanguages)); |
|
|
|
|
298
|
|
|
} |
299
|
|
|
$monacoOptionsOverride = Json::decodeIfJson($this->monacoEditorOptions); |
300
|
|
|
if ($monacoOptionsOverride === null || is_string($monacoOptionsOverride)) { |
301
|
|
|
$monacoOptionsOverride = []; |
302
|
|
|
} |
303
|
|
|
// Disable the Monaco editor |
304
|
|
|
if (!$enabled) { |
305
|
|
|
$monacoOptionsOverride['domReadOnly'] = true; |
306
|
|
|
$monacoOptionsOverride['readOnly'] = true; |
307
|
|
|
} |
308
|
|
|
return [ |
|
|
|
|
309
|
|
|
'name' => $this->handle, |
310
|
|
|
'value' => $value, |
311
|
|
|
'field' => $this, |
312
|
|
|
'orientation' => $this->getOrientation($element), |
313
|
|
|
'id' => $id, |
314
|
|
|
'namespacedId' => $namespacedId, |
315
|
|
|
'displayLanguages' => $displayLanguages, |
316
|
|
|
'monacoOptionsOverride' => $monacoOptionsOverride, |
317
|
|
|
]; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|