Skip to content

Commit c9133c3

Browse files
authored
fix(demo): fill missing php export colors from selected theme (DenverCoder1#253)
* feat(demo): fill missing php export colors from selected theme * style: Formatted code with Prettier * use same var for consistency Co-authored-by: DenverCoder1 <[email protected]>
1 parent 73b1e02 commit c9133c3

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/demo/index.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
$TRANSLATIONS = include "../translations.php";
55
// Get the keys of the first value in the translations array
66
$LOCALES = array_keys($TRANSLATIONS);
7+
8+
/**
9+
* Convert a camelCase string to a skewer-case string
10+
* @param string $str The camelCase string
11+
* @return string The skewer-case string
12+
*/
13+
function camel_to_skewer(string $str): string
14+
{
15+
return preg_replace_callback(
16+
"/([A-Z])/",
17+
function ($matches) {
18+
return "-" . strtolower($matches[0]);
19+
},
20+
$str
21+
);
22+
}
723
?>
824

925
<!DOCTYPE html>
@@ -65,7 +81,18 @@ function gtag() {
6581
<label for="theme">Theme</label>
6682
<select class="param" id="theme" name="theme" placeholder="default">
6783
<?php foreach ($THEMES as $theme => $options): ?>
68-
<option><?php echo $theme; ?></option>
84+
<?php
85+
$dataAttrs = "";
86+
foreach ($options as $key => $value) {
87+
// convert key from camelCase to skewer-case
88+
$key = camel_to_skewer($key);
89+
// remove '#' from hex color value
90+
$value = preg_replace("/^\#/", "", $value);
91+
// add data attribute
92+
$dataAttrs .= "data-" . $key . "=\"" . $value . "\" ";
93+
}
94+
?>
95+
<option value="<?php echo $theme; ?>" <?php echo $dataAttrs; ?>><?php echo $theme; ?></option>
6996
<?php endforeach; ?>
7097
</select>
7198

src/demo/js/script.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,20 @@ const preview = {
144144
* Export the advanced parameters to PHP code for creating a new theme
145145
*/
146146
exportPhp() {
147-
const params = this.objectFromElements(document.querySelectorAll(".advanced .param.jscolor"));
147+
// get default values from the currently selected theme
148+
const themeSelect = document.querySelector("#theme");
149+
const selectedOption = themeSelect.options[themeSelect.selectedIndex];
150+
const defaultParams = selectedOption.dataset;
151+
// get parameters with the advanced options
152+
const advancedParams = this.objectFromElements(document.querySelectorAll(".advanced .param.jscolor"));
153+
// update default values with the advanced options
154+
const params = { ...defaultParams, ...advancedParams };
155+
// convert parameters to PHP code
148156
const mappings = Object.keys(params)
149157
.map((key) => ` "${key}" => "#${params[key]}",`)
150158
.join("\n");
151159
const output = `[\n${mappings}\n]`;
152-
160+
// set the textarea value to the output
153161
const textarea = document.getElementById("exportedPhp");
154162
textarea.value = output;
155163
textarea.hidden = false;

0 commit comments

Comments
 (0)