|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodeEditor for Craft CMS |
|
4
|
|
|
* |
|
5
|
|
|
* Provides a code editor field with Twig & Craft API autocomplete |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\codeeditor\base; |
|
12
|
|
|
|
|
13
|
|
|
use Craft; |
|
14
|
|
|
use craft\base\Model; |
|
15
|
|
|
use craft\helpers\ArrayHelper; |
|
16
|
|
|
use nystudio107\codeeditor\CodeEditor; |
|
17
|
|
|
use nystudio107\codeeditor\models\CompleteItem; |
|
18
|
|
|
use nystudio107\codeeditor\types\AutocompleteTypes; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
|
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
22
|
|
|
* @package CodeEditor |
|
|
|
|
|
|
23
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
|
|
25
|
|
|
abstract class Autocomplete extends Model implements AutocompleteInterface |
|
26
|
|
|
{ |
|
27
|
|
|
// Constants |
|
28
|
|
|
// ========================================================================= |
|
29
|
|
|
|
|
30
|
|
|
public const COMPLETION_KEY = '__completions'; |
|
31
|
|
|
|
|
32
|
|
|
// Public Properties |
|
33
|
|
|
// ========================================================================= |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* @var string The name of the autocomplete |
|
37
|
|
|
*/ |
|
38
|
|
|
public $name = 'BaseAutocomplete'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
|
|
|
|
|
41
|
|
|
* @var string The type of the autocomplete |
|
42
|
|
|
*/ |
|
43
|
|
|
public $type = AutocompleteTypes::TwigExpressionAutocomplete; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
|
|
|
|
|
46
|
|
|
* @var bool Whether the autocomplete should be parsed with `.`-delimited nested sub-properties |
|
47
|
|
|
*/ |
|
48
|
|
|
public $hasSubProperties = false; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
|
|
|
|
|
51
|
|
|
* @var string The field type passed down from the template to the autocomplete |
|
52
|
|
|
*/ |
|
53
|
|
|
public $fieldType = CodeEditor::DEFAULT_FIELD_TYPE; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
|
|
|
|
|
56
|
|
|
* @var array The CodeEditor options object passed down from the template to the autocomplete |
|
57
|
|
|
*/ |
|
58
|
|
|
public $codeEditorOptions = []; |
|
59
|
|
|
|
|
60
|
|
|
// Protected Properties |
|
61
|
|
|
// ========================================================================= |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
|
|
|
|
|
64
|
|
|
* @var array The accumulated complete items |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $completeItems = []; |
|
67
|
|
|
|
|
68
|
|
|
// Public Static Methods |
|
69
|
|
|
// ========================================================================= |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
|
|
|
|
|
72
|
|
|
* @inerhitDoc |
|
73
|
|
|
*/ |
|
|
|
|
|
|
74
|
|
|
public function generateCompleteItems(): void |
|
75
|
|
|
{ |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
|
|
|
|
|
79
|
|
|
* @inerhitDoc |
|
80
|
|
|
*/ |
|
|
|
|
|
|
81
|
|
|
public function addCompleteItem(CompleteItem $item, string $path = ''): void |
|
82
|
|
|
{ |
|
83
|
|
|
if (!$item->validate()) { |
|
84
|
|
|
Craft::debug(print_r($item->getErrors(), true), __METHOD__); |
|
|
|
|
|
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
if (empty($path)) { |
|
88
|
|
|
$path = $item->label; |
|
89
|
|
|
} |
|
90
|
|
|
ArrayHelper::setValue($this->completeItems, $path, [ |
|
|
|
|
|
|
91
|
|
|
self::COMPLETION_KEY => array_filter($item->toArray(), static function($v) { |
|
|
|
|
|
|
92
|
|
|
return !is_null($v); |
|
93
|
|
|
}), |
|
|
|
|
|
|
94
|
|
|
]); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
|
|
|
|
|
98
|
|
|
* @inerhitDoc |
|
99
|
|
|
*/ |
|
|
|
|
|
|
100
|
|
|
public function getCompleteItems(): array |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->completeItems; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|