Skip to content

Commit b360b42

Browse files
committed
✨ add custom palette support in the demo
1 parent d47b6d7 commit b360b42

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

packages/demo/interactive/index.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseEntry } from "@snk/action/outputsOptions";
12
import { basePalettes } from "@snk/action/palettes";
23
import { userContributionToGrid } from "@snk/action/userContributionToGrid";
34
import {
@@ -196,35 +197,63 @@ const createViewer = ({
196197
document.body.append(input);
197198

198199
//
199-
const schemaSelect = document.createElement("select");
200-
schemaSelect.style.margin = "10px";
201-
schemaSelect.style.alignSelf = "flex-start";
202-
schemaSelect.value = "github-light";
203-
schemaSelect.addEventListener("change", () => {
204-
Object.assign(
205-
drawOptions,
206-
basePalettes[schemaSelect.value as keyof typeof basePalettes],
207-
);
200+
const applyDrawOptions = (options: Partial<DrawOptions>) => {
201+
Object.assign(drawOptions, options);
208202

209203
svgString = createSvg(grid0, cells, chain, drawOptions, {
210204
stepDurationMs: 100,
211205
});
212-
const svgImageUri = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`;
213-
svgLink.href = svgImageUri;
206+
svgLink.href = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`;
214207

215-
if (schemaSelect.value.includes("dark"))
208+
if (
209+
Object.entries(basePalettes)
210+
.find(([_, o]) => o.colorEmpty === drawOptions.colorEmpty)?.[0]
211+
.includes("-dark")
212+
)
216213
document.body.parentElement?.classList.add("dark-mode");
217214
else document.body.parentElement?.classList.remove("dark-mode");
218215

219216
loop();
220-
});
221-
for (const name of Object.keys(basePalettes)) {
217+
};
218+
219+
const paletteSelect = document.createElement("select");
220+
paletteSelect.style.margin = "10px";
221+
paletteSelect.style.alignSelf = "flex-start";
222+
paletteSelect.value = "github-light";
223+
for (const name of [...Object.keys(basePalettes), "custom"]) {
222224
const option = document.createElement("option");
223225
option.value = name;
224226
option.innerText = name;
225-
schemaSelect.appendChild(option);
227+
paletteSelect.appendChild(option);
226228
}
227-
document.body.append(schemaSelect);
229+
230+
const customInput = document.createElement("input");
231+
customInput.value =
232+
"color_snake=orange&color_dots=#bfd6f6,#8dbdff,#64a1f4,#4b91f1,#3c7dd9";
233+
customInput.style.margin = "10px";
234+
customInput.style.padding = "10px";
235+
customInput.style.width = "min( 800px , 100% - 32px )";
236+
customInput.style.alignSelf = "flex-start";
237+
customInput.style.display = "none";
238+
customInput.addEventListener("input", () => {
239+
const v = customInput.value.trim();
240+
const entry = v.startsWith("{") ? `x.svg ${v}` : `x.svg?${v}`;
241+
const result = parseEntry(entry);
242+
if (result) applyDrawOptions(result.drawOptions);
243+
});
244+
245+
paletteSelect.addEventListener("change", () => {
246+
const isCustom = paletteSelect.value === "custom";
247+
customInput.style.display = isCustom ? "" : "none";
248+
if (isCustom) customInput.dispatchEvent(new Event("input"));
249+
else
250+
applyDrawOptions(
251+
basePalettes[paletteSelect.value as keyof typeof basePalettes],
252+
);
253+
});
254+
255+
document.body.append(paletteSelect);
256+
document.body.append(customInput);
228257

229258
//
230259
// dark mode

0 commit comments

Comments
 (0)