-
Notifications
You must be signed in to change notification settings - Fork 14
/
helper.php
166 lines (138 loc) · 3.35 KB
/
helper.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
<?php
\defined('JPATH_PLATFORM') || die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
class TplLightningHelper
{
/**
* Ajax wrapper for saveCss()
*/
public static function saveCssAjax()
{
$json = Factory::getApplication()->input->json->getArray();
$helper = new TplLightningHelper();
return $helper->saveCss($json);
}
/**
* Method saving custom CSS properties.
*/
private function saveCss($json)
{
$css = $json['css'];
$response = '';
try
{
// Add the default variables
$string = ":root {\n";
foreach ($css['default'] as $key => $value)
{
$string .= ' ' . $key . ': ' . $value . ";\n";
}
$string .= "}\n\n";
// Add the dark variables
$string .= ":root.is-dark {\n";
foreach ($css['dark'] as $key => $value)
{
$string .= ' ' . $key . ': ' . $value . ";\n";
}
$string .= '}';
// Write to file
file_put_contents(JPATH_SITE . '/templates/lightning/css/custom-variables.css', $string);
$response = Text::_('CSS variables saved');
}
catch (Exception $e)
{
$response = $e->getMessage();
}
return $response;
}
/**
* Parse the CSS file string and return an array of CSS variables
*/
private function parseCss($file)
{
$variables = [
'default' => [],
'dark' => [],
];
$colourScheme = 'default';
$lines = explode("\n", $file);
foreach ($lines as $line)
{
// Trim the line
$trimmed = trim($line);
// Check if we're dealing with a blank line
if ($trimmed !== '')
{
// If the string contains ":root.is-dark", then we've started parsing the dark variables
if (strpos($trimmed, ':root.is-dark') !== false)
{
$colourScheme = 'dark';
}
// Get the variable name and value
preg_match('/^(--.*?):(.*?);$/', $trimmed, $match);
if (isset($match[1]) && isset($match[2]))
{
$variables[$colourScheme][trim($match[1])] = trim($match[2]);
}
}
}
return $variables;
}
/**
* Get the source CSS variables file
*/
public function getCssVariables()
{
$templateVariables = $this->parseCss(
file_get_contents(JPATH_SITE . '/templates/lightning/src/css/variables.css')
);
$overrideVariables = [
'default' => [],
'dark' => [],
];
// Check if the custom variable file exists
if (file_exists(JPATH_SITE . '/templates/lightning/css/custom-variables.css'))
{
$overrideVariables = $this->parseCss(
file_get_contents(JPATH_SITE . '/templates/lightning/css/custom-variables.css')
);
}
$defaultVariables = array_merge_recursive($templateVariables['default'], $overrideVariables['default']);
$darkVariables = array_merge_recursive($templateVariables['dark'], $overrideVariables['dark']);
return [
'default' => $defaultVariables,
'dark' => $darkVariables
];
}
/**
* Detect the type of CSS variable based on the value
*/
public function detectCssVariableType($variable)
{
$type = 'text';
if (substr($variable, 0, 3) === 'hsl'
|| substr($variable, 0, 1) === '#'
|| substr($variable, 0, 3) === 'rgb'
|| substr($variable, 0, 4) === 'rgba'
)
{
$type = 'color';
}
else if (substr($variable, 0, 4) === 'var(')
{
$type = 'mapped';
}
return $type;
}
/**
* Trim the "var()" wrapper
* E.g var(--xx) becomes --xx
*/
public function trimVariable($variable)
{
$trimmed = substr($variable, 0, -1);
$trimmed = substr($trimmed, 4);
return $trimmed;
}
}