|
| 1 | +import { parseEntry } from "@snk/action/outputsOptions"; |
1 | 2 | import { basePalettes } from "@snk/action/palettes"; |
2 | 3 | import { userContributionToGrid } from "@snk/action/userContributionToGrid"; |
3 | 4 | import { |
@@ -196,35 +197,63 @@ const createViewer = ({ |
196 | 197 | document.body.append(input); |
197 | 198 |
|
198 | 199 | // |
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); |
208 | 202 |
|
209 | 203 | svgString = createSvg(grid0, cells, chain, drawOptions, { |
210 | 204 | stepDurationMs: 100, |
211 | 205 | }); |
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)}`; |
214 | 207 |
|
215 | | - if (schemaSelect.value.includes("dark")) |
| 208 | + if ( |
| 209 | + Object.entries(basePalettes) |
| 210 | + .find(([_, o]) => o.colorEmpty === drawOptions.colorEmpty)?.[0] |
| 211 | + .includes("-dark") |
| 212 | + ) |
216 | 213 | document.body.parentElement?.classList.add("dark-mode"); |
217 | 214 | else document.body.parentElement?.classList.remove("dark-mode"); |
218 | 215 |
|
219 | 216 | 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"]) { |
222 | 224 | const option = document.createElement("option"); |
223 | 225 | option.value = name; |
224 | 226 | option.innerText = name; |
225 | | - schemaSelect.appendChild(option); |
| 227 | + paletteSelect.appendChild(option); |
226 | 228 | } |
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); |
228 | 257 |
|
229 | 258 | // |
230 | 259 | // dark mode |
|
0 commit comments