Skip to content

Commit adaadc6

Browse files
authored
feat(demo): add png/json preview to demo site and refactor (DenverCoder1#457)
1 parent fd370d2 commit adaadc6

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

src/demo/css/style.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,13 @@ input:focus:invalid {
205205

206206
.advanced .parameters {
207207
margin-top: 8px;
208+
}
209+
210+
.advanced .color-properties {
208211
grid-template-columns: auto 1fr auto;
209212
}
210213

211-
.advanced .parameters label:first-of-type {
214+
.advanced .color-properties label:first-of-type {
212215
font-weight: bold;
213216
}
214217

@@ -239,7 +242,8 @@ input:focus:invalid {
239242
border-left: 4px solid var(--yellow);
240243
}
241244

242-
.output .md {
245+
.output .md,
246+
.output .json {
243247
background: var(--border);
244248
border-radius: 6px;
245249
padding: 12px 16px;

src/demo/index.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ function gtag() {
4747
</script>
4848
<title>GitHub Readme Streak Stats Demo</title>
4949
<link href="https://css.gg/css?=|moon|sun" rel="stylesheet">
50-
<link rel="stylesheet" href="./css/style.css">
51-
<link rel="stylesheet" href="./css/toggle-dark.css">
50+
<link rel="stylesheet" href="./css/style.css?v=<?= filemtime("./css/style.css") ?>">
51+
<link rel="stylesheet" href="./css/toggle-dark.css?v=<?= filemtime("./css/toggle-dark.css") ?>">
5252

5353
<!-- Favicons -->
5454
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
5555
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
5656
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
5757
<link rel="mask-icon" href="icon.svg" color="#fb8c00">
5858

59-
<script type="text/javascript" src="./js/script.js" defer></script>
60-
<script type="text/javascript" src="./js/accordion.js" defer></script>
61-
<script type="text/javascript" src="./js/toggle-dark.js" defer></script>
62-
<script type="text/javascript" src="./js/jscolor.min.js" defer></script>
59+
<script type="text/javascript" src="./js/script.js?v=<?= filemtime("./js/script.js") ?>" defer></script>
60+
<script type="text/javascript" src="./js/accordion.js?v=<?= filemtime("./js/accordion.js") ?>" defer></script>
61+
<script type="text/javascript" src="./js/toggle-dark.js?v=<?= filemtime("./js/toggle-dark.js") ?>" defer></script>
62+
<script type="text/javascript" src="./js/jscolor.min.js?v=<?= filemtime("./js/jscolor.min.js") ?>" defer></script>
6363
<script async defer src="https://buttons.github.io/buttons.js"></script>
6464
</head>
6565

@@ -137,15 +137,16 @@ function gtag() {
137137
<option value="weekly">Weekly</option>
138138
</select>
139139

140-
<label for="disable_animations">Disable Animations</label>
141-
<select class="param" id="hide-border" name="disable_animations">
142-
<option>false</option>
143-
<option>true</option>
140+
<label for="type">Output Type</label>
141+
<select class="param" id="type" name="type">
142+
<option value="svg">SVG</option>
143+
<option value="png">PNG</option>
144+
<option value="json">JSON</option>
144145
</select>
145146

146147
<details class="advanced">
147148
<summary>⚙ Advanced Options</summary>
148-
<div class="content parameters">
149+
<div class="content color-properties parameters">
149150
<label for="theme">Add Property</label>
150151
<select id="properties" name="properties">
151152
<?php foreach ($THEMES["default"] as $option => $color): ?>
@@ -167,6 +168,9 @@ function gtag() {
167168
<div class="top">
168169
<h2>Preview</h2>
169170
<img alt="GitHub Readme Streak Stats" src="preview.php?user=" />
171+
<div class="json" style="display: none;">
172+
<pre></pre>
173+
</div>
170174
<p class="warning">
171175
Note: The stats above are just examples and not from your GitHub profile.
172176
</p>

src/demo/js/script.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const preview = {
1212
locale: "en",
1313
border_radius: "4.5",
1414
mode: "daily",
15-
disable_animations: "false",
15+
type: "svg",
1616
},
1717

1818
/**
@@ -29,12 +29,23 @@ const preview = {
2929
// generate links and markdown
3030
const imageURL = `${window.___location.origin}?${query}`;
3131
const demoImageURL = `preview.php?${query}`;
32-
const repoLink = "https://git.io/streak-stats";
33-
const md = `[![GitHub Streak](${imageURL})](${repoLink})`;
34-
// update image preview
35-
document.querySelector(".output img").src = demoImageURL;
36-
// update markdown
37-
document.querySelector(".md code").innerText = md;
32+
// update preview
33+
if (params.type !== "json") {
34+
const repoLink = "https://git.io/streak-stats";
35+
const md = `[![GitHub Streak](${imageURL})](${repoLink})`;
36+
document.querySelector(".output img").src = demoImageURL;
37+
document.querySelector(".md code").innerText = md;
38+
document.querySelector(".output img").style.display = "block";
39+
document.querySelector(".output .json").style.display = "none";
40+
} else {
41+
document.querySelector(".output img").style.display = "none";
42+
document.querySelector(".output .json").style.display = "block";
43+
fetch(demoImageURL)
44+
.then((response) => response.json())
45+
.then((data) => (document.querySelector(".output .json pre").innerText = JSON.stringify(data, null, 2)))
46+
.catch(console.error);
47+
document.querySelector(".md code").innerText = imageURL;
48+
}
3849
// disable copy button if username is invalid
3950
const copyButton = document.querySelector(".copy-button");
4051
copyButton.disabled = Boolean(document.querySelector("#user:invalid") || !document.querySelector("#user").value);
@@ -87,12 +98,12 @@ const preview = {
8798
minus.innerText = "−";
8899
minus.setAttribute("data-property", propertyName);
89100
// add elements
90-
const parent = document.querySelector(".advanced .parameters");
101+
const parent = document.querySelector(".advanced .color-properties");
91102
parent.appendChild(label);
92103
parent.appendChild(input);
93104
parent.appendChild(minus);
94105

95-
//initialise jscolor on element
106+
// initialise jscolor on element
96107
jscolor.install(parent);
97108

98109
// check initial color value
@@ -108,7 +119,7 @@ const preview = {
108119
* @param {string} property - the name of the property to remove
109120
*/
110121
removeProperty(property) {
111-
const parent = document.querySelector(".advanced .parameters");
122+
const parent = document.querySelector(".advanced .color-properties");
112123
const selectElement = document.querySelector("#properties");
113124
// remove all elements for given property
114125
parent.querySelectorAll(`[data-property="${property}"]`).forEach((x) => parent.removeChild(x));
@@ -124,7 +135,7 @@ const preview = {
124135
* Removes all properties from the advanced section
125136
*/
126137
removeAllProperties() {
127-
const parent = document.querySelector(".advanced .parameters");
138+
const parent = document.querySelector(".advanced .color-properties");
128139
const activeProperties = parent.querySelectorAll("[data-property]");
129140
// select active and unique property names
130141
const propertyNames = Array.prototype.map
@@ -199,6 +210,8 @@ const preview = {
199210
pickerChange(picker, input) {
200211
// color was changed by picker - check it
201212
this.checkColor(picker.toHEXAString(), input);
213+
// update preview
214+
this.update();
202215
},
203216
};
204217

@@ -242,7 +255,6 @@ window.addEventListener(
242255
// refresh preview on interactions with the page
243256
const refresh = () => preview.update();
244257
document.addEventListener("keyup", refresh, false);
245-
document.addEventListener("click", refresh, false);
246258
[...document.querySelectorAll("select:not(#properties)")].forEach((element) => {
247259
element.addEventListener("change", refresh, false);
248260
});

0 commit comments

Comments
 (0)