-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
73 lines (60 loc) · 2.06 KB
/
Copy pathstorage.js
File metadata and controls
73 lines (60 loc) · 2.06 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
const STORAGE_PREFIX = 'pixel-pint-';
const LIST_KEY = `${STORAGE_PREFIX}projects-list`;
const CUSTOM_PALETTES_KEY = `${STORAGE_PREFIX}custom-palettes`;
export const getProjects = () => {
const list = localStorage.getItem(LIST_KEY);
return list ? JSON.parse(list) : [];
};
export const saveProjectList = (projects) => {
localStorage.setItem(LIST_KEY, JSON.stringify(projects));
};
export const saveProjectData = (id, data) => {
localStorage.setItem(`${STORAGE_PREFIX}project-${id}`, JSON.stringify(data));
};
export const loadProjectData = (id) => {
const data = localStorage.getItem(`${STORAGE_PREFIX}project-${id}`);
return data ? JSON.parse(data) : null;
};
export const createProject = (name, width, height, palette, paletteName = null) => {
const id = Date.now().toString();
const newProject = {
id,
name,
width: parseInt(width),
height: parseInt(height),
palette: palette || ['#000000', '#FFFFFF'],
paletteName: paletteName || null,
lastModified: Date.now(),
};
const projects = getProjects();
projects.unshift(newProject);
saveProjectList(projects);
// Initialize empty data
saveProjectData(id, { foreground: null, background: null });
return newProject;
};
export const getCustomPalettes = () => {
const data = localStorage.getItem(CUSTOM_PALETTES_KEY);
return data ? JSON.parse(data) : {};
};
export const saveCustomPalette = (name, colors) => {
const palettes = getCustomPalettes();
palettes[name] = colors;
localStorage.setItem(CUSTOM_PALETTES_KEY, JSON.stringify(palettes));
};
export const deleteProject = (id) => {
const projects = getProjects().filter(p => p.id !== id);
saveProjectList(projects);
localStorage.removeItem(`${STORAGE_PREFIX}project-${id}`);
return projects;
};
export const updateProjectMeta = (id, updates) => {
const projects = getProjects();
const index = projects.findIndex(p => p.id === id);
if (index !== -1) {
projects[index] = { ...projects[index], ...updates, lastModified: Date.now() };
saveProjectList(projects);
return projects;
}
return projects;
};