Skip to content

Add button for exporting a theme from the demo site #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
"phpunit/phpunit": "^9"
},
"scripts": {
"start": "php -S localhost:8000 -t src",
"start": [
"Composer\\Config::disableProcessTimeout",
"php -S localhost:8000 -t src"
],
"test": "./vendor/bin/phpunit --testdox tests"
}
}
2 changes: 2 additions & 0 deletions src/demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ function gtag() { dataLayer.push(arguments); }
<button class="plus btn" onclick="return preview.addProperty();">+</button>
</div>
</details>
<button class="btn" type="button" onclick='return exportPhp()'>button</button>
<textarea id="test"></textarea>

<input class="btn" type="submit" value="Submit">
</form>
Expand Down
50 changes: 48 additions & 2 deletions src/demo/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let preview = {
let obj = { ...acc };
let value = next.value;

if (value.indexOf('#') >= 0) {
if (value.indexOf("#") >= 0) {
// if the value is colour, remove the hash sign
value = value.replace(/#/g, "");
if (value.length > 6) {
Expand Down Expand Up @@ -180,7 +180,53 @@ window.addEventListener(
false
);

function checkColor(color, input){
function exportPhp() {
let properties = [
"border",
"stroke",
"ring",
"fire",
"currStreakNum",
"sideNums",
"currStreakLabel",
"sideLabels",
"dates",
"background",
];
let array = Array.from(document.querySelectorAll(".param")).reduce(
(acc, next) => {
let obj = { ...acc };
let value = next.value;
if (value.indexOf("#") >= 0) {
// if the value is colour, remove the hash sign
value = value.replace(/#/g, "");
if (value.length > 6) {
// if the value is in hexa and opacity is 1, remove FF
value = value.replace(/(F|f){2}$/, "");
}
}
obj[next.id] = value;
return obj;
},
{}
);
var exportPhp = {};

for (const [key, val] of Object.entries(array)) {
if (properties.includes(key) > 0) {
exportPhp[key] = val;
}
}

fetch("/demo/json_decode.php", {
method: "POST",
body: JSON.stringify({ json: exportPhp }),
})
.then((response) => response.text())
.then((html) => (document.getElementById("test").value = html));
}

function checkColor(color, input) {
if (color.length == 9 && color.slice(-2) == "FF") {
// if color has hex alpha value -> remove it
document.getElementById(input).value = color.slice(0, -2);
Expand Down
4 changes: 4 additions & 0 deletions src/demo/json_decode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php declare (strict_types = 1);
$_POST = json_decode(file_get_contents("php://input"), true);
var_export($_POST['json'],false);
?>
Comment on lines +1 to +4
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no need for fetching any webpages to output the export data.

All it needs to do is create a string in the required format, which can be done with JavaScript code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that, but don't we have to format the string to PHP format? Like change the : to =>

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but there's no reason you can't use JavaScript to make the string have => in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do we want to export just like JSON? "{"property":"#123123"} Is that ok?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the following code:

const props = {"property":"#123123", "property2":"#123456"}
const output = "[\n" + Object.keys(props).map((key) => `\t"${key}" => "${props[key]}",\n`).join("") + "]";
console.log(output);

You may want to change it up a bit, but basically in a way like this, you can convert JSON into a string that resembles PHP array format.