Skip to content

Commit 7e73876

Browse files
feat: Add button for exporting a theme from the demo site (#176)
Co-authored-by: Jonah Lawrence <[email protected]>
1 parent ad1b59e commit 7e73876

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

src/demo/css/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,10 @@ input:focus:invalid {
294294
.btn.tooltip:disabled:before {
295295
content: "You must first input a valid username.";
296296
}
297+
298+
textarea#exportedPhp {
299+
margin-top: 10px;
300+
width: 100%;
301+
resize: vertical;
302+
height: 100px;
303+
}

src/demo/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ function gtag() {
9292
</select>
9393
<button class="plus btn" onclick="return preview.addProperty();">+</button>
9494
</div>
95+
<button class="btn" type="button" onclick='return exportPhp()'>Export to PHP</button>
96+
<textarea id="exportedPhp" hidden></textarea>
97+
9598
</details>
9699

97100
<input class="btn" type="submit" value="Submit">

src/demo/js/script.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,9 @@ let preview = {
77
// update the preview
88
update: function () {
99
// get parameter values from all .param elements
10-
const params = Array.from(document.querySelectorAll(".param")).reduce(
11-
(acc, next) => {
12-
let obj = { ...acc };
13-
let value = next.value;
1410

15-
if (value.indexOf('#') >= 0) {
16-
// if the value is colour, remove the hash sign
17-
value = value.replace(/#/g, "");
18-
if (value.length > 6) {
19-
// if the value is in hexa and opacity is 1, remove FF
20-
value = value.replace(/(F|f){2}$/, "");
21-
}
22-
}
23-
obj[next.id] = value;
24-
return obj;
25-
},
26-
{}
27-
);
11+
const params = objectFromElements(document.querySelectorAll(".param"))
12+
2813
// convert parameters to query string
2914
const encode = encodeURIComponent;
3015
const query = Object.keys(params)
@@ -72,9 +57,9 @@ let preview = {
7257
label.setAttribute("data-property", property);
7358
// color picker
7459
const jscolorConfig = {
75-
format: 'hexa',
76-
onChange: 'pickerChange(this, "'+property+'")',
77-
onInput: 'pickerChange(this, "'+property+'")'
60+
format: "hexa",
61+
onChange: 'pickerChange(this, "' + property + '")',
62+
onInput: 'pickerChange(this, "' + property + '")',
7863
};
7964
const input = document.createElement("input");
8065
input.className = "param jscolor";
@@ -179,8 +164,39 @@ window.addEventListener(
179164
},
180165
false
181166
);
167+
function objectFromElements(elements)
168+
{
169+
// create a key value mapping of parameter values from all elements in a Node list
170+
return Array.from(elements).reduce((acc, next) => {
171+
let obj = { ...acc };
172+
let value = next.value;
173+
if (value.indexOf("#") >= 0) {
174+
// if the value is colour, remove the hash sign
175+
value = value.replace(/#/g, "");
176+
if (value.length > 6) {
177+
// if the value is in hexa and opacity is 1, remove FF
178+
value = value.replace(/(F|f){2}$/, "");
179+
}
180+
}
181+
obj[next.id] = value;
182+
return obj;
183+
}, {});
184+
}
185+
function exportPhp() {
186+
let params = objectFromElements(document.querySelectorAll(".advanced .param.jscolor"))
187+
const output =
188+
"[\n" +
189+
Object.keys(params)
190+
.map((key) => ` "${key}" => "#${params[key]}",\n`)
191+
.join("") +
192+
"]";
193+
194+
let textarea = document.getElementById('exportedPhp');
195+
textarea.value = output;
196+
textarea.hidden = false;
197+
}
182198

183-
function checkColor(color, input){
199+
function checkColor(color, input) {
184200
if (color.length == 9 && color.slice(-2) == "FF") {
185201
// if color has hex alpha value -> remove it
186202
document.getElementById(input).value = color.slice(0, -2);

0 commit comments

Comments
 (0)