Skip to content

Commit 0bd0291

Browse files
Copilotaustenstone
andcommitted
Fix code review issues in Color Palette Generator
Co-authored-by: austenstone <22425467+austenstone@users.noreply.github.com>
1 parent 8b2794a commit 0bd0291

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

webapp/index.html

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ <h2>💝 Saved Palettes</h2>
300300

301301
// Generate a random color
302302
function randomColor() {
303-
return '#' + Math.floor(Math.random()*16777215).toString(16).padStart(6, '0');
303+
const randomInt = Math.floor(Math.random() * 0x1000000);
304+
return '#' + randomInt.toString(16).padStart(6, '0');
304305
}
305306

306307
// Get a random color name
@@ -366,9 +367,34 @@ <h2>💝 Saved Palettes</h2>
366367

367368
// Copy color to clipboard
368369
function copyToClipboard(text) {
369-
navigator.clipboard.writeText(text).then(() => {
370+
if (navigator.clipboard && navigator.clipboard.writeText) {
371+
navigator.clipboard.writeText(text).then(() => {
372+
showCopyIndicator();
373+
}).catch((err) => {
374+
console.error('Failed to copy:', err);
375+
// Fallback for older browsers
376+
fallbackCopyToClipboard(text);
377+
});
378+
} else {
379+
fallbackCopyToClipboard(text);
380+
}
381+
}
382+
383+
// Fallback copy method for browsers without clipboard API
384+
function fallbackCopyToClipboard(text) {
385+
const textArea = document.createElement('textarea');
386+
textArea.value = text;
387+
textArea.style.position = 'fixed';
388+
textArea.style.left = '-999999px';
389+
document.body.appendChild(textArea);
390+
textArea.select();
391+
try {
392+
document.execCommand('copy');
370393
showCopyIndicator();
371-
});
394+
} catch (err) {
395+
console.error('Fallback copy failed:', err);
396+
}
397+
document.body.removeChild(textArea);
372398
}
373399

374400
// Show copy indicator
@@ -417,10 +443,13 @@ <h2>💝 Saved Palettes</h2>
417443
container.style.display = 'block';
418444
list.innerHTML = '';
419445

420-
saved.reverse().forEach((palette, index) => {
446+
// Display in reverse order (newest first) but keep original indices
447+
const reversedSaved = [...saved].reverse();
448+
reversedSaved.forEach((palette, index) => {
421449
const item = document.createElement('div');
422450
item.className = 'saved-palette-item';
423-
item.onclick = () => loadPalette(saved.length - 1 - index);
451+
const originalIndex = saved.length - 1 - index;
452+
item.onclick = () => loadPalette(originalIndex);
424453

425454
const colorsHTML = palette.colors.map(color =>
426455
`<div class="saved-color-preview" style="background-color: ${color}"></div>`

0 commit comments

Comments
 (0)