-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (66 loc) · 1.94 KB
/
Copy pathapp.js
File metadata and controls
77 lines (66 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Declare Vue utilities globally once
const { createApp, ref, computed, watch } = Vue;
// Helper Functions
function getRandomColor() {
return '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
}
function isValidHex(hex) {
return /^#[0-9A-Fa-f]{3}$|^#[0-9A-Fa-f]{6}$/.test(hex);
}
function expandShortHex(hex) {
if (hex.length === 4) {
const r = hex[1];
const g = hex[2];
const b = hex[3];
return `#${r}${r}${g}${g}${b}${b}`;
}
return hex;
}
// Main Application
const app = createApp({
setup() {
// Initialize with 6 random colors
const colors = ref(Array.from({ length: 6 }, getRandomColor));
// Add a new column
const addColumn = () => {
colors.value.push(getRandomColor());
};
// Update a color at a specific index
const updateColor = (index, newColor) => {
colors.value[index] = newColor;
};
// Delete a column at a specific index
const deleteColumn = (index) => {
colors.value.splice(index, 1);
};
// Two-way computed property for the color array string
const colorArrayString = computed({
get() {
return JSON.stringify(colors.value);
},
set(newValue) {
try {
const newColors = JSON.parse(newValue);
if (Array.isArray(newColors) && newColors.every(isValidHex)) {
colors.value = newColors;
} else {
throw new Error('Invalid array or hex colors');
}
} catch (e) {
console.error('Invalid input:', e.message);
}
}
});
return {
colors,
addColumn,
updateColor,
deleteColumn,
colorArrayString
};
}
});
// Register the ColorColumn component (defined in color-column.js)
app.component('color-column', ColorColumn);
// Mount the app
app.mount('#app');