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