/
Gabryelf
/
Icons-Customize-Gen
Обзор
Документация
Войти
/
Gabryelf
/
Icons-Customize-Gen
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/models/Project.js
80 строк
3 KB
Valeev Serj
running_start_version
10 июл 2026, 22:15
10 июл 2026, 22:15
5234bd4
Код
Авторство
О чём код?
import { Layer } from '../core/Layer.js'; export class Project { constructor() { this.layers = []; this.width = 600; this.height = 600; this.background = '#ffffff'; this.name = 'Новый проект'; this._selectedId = null; this._nextZIndex = 0; } get selectedId() { return this._selectedId; } set selectedId(id) { this._selectedId = id; } get selectedLayer() { return this.layers.find(l => l.id === this._selectedId) || null; } get visibleLayers() { return this.layers.filter(l => l.visible); } addLayer(data) { const layer = data instanceof Layer ? data : new Layer(data); layer.zIndex = this._nextZIndex++; this.layers.push(layer); this.selectedId = layer.id; return layer; } removeLayer(id) { const idx = this.layers.findIndex(l => l.id === id); if (idx === -1) return false; this.layers.splice(idx, 1); if (this.selectedId === id) { this.selectedId = this.layers.length > 0 ? this.layers[0].id : null; } return true; } moveLayer(id, delta) { const idx = this.layers.findIndex(l => l.id === id); if (idx === -1) return; const newIdx = idx + delta; if (newIdx < 0 || newIdx >= this.layers.length) return; const [layer] = this.layers.splice(idx, 1); this.layers.splice(newIdx, 0, layer); this.layers.forEach((l, i) => l.zIndex = i); } toJSON() { return { name: this.name, width: this.width, height: this.height, background: this.background, selectedId: this.selectedId, nextZIndex: this._nextZIndex, nextLayerId: Layer._nextId, layers: this.layers.map(l => l.toJSON()) }; } static fromJSON(data) { const project = new Project(); project.name = data.name || 'Новый проект'; project.width = data.width || 600; project.height = data.height || 600; project.background = data.background || '#ffffff'; project._nextZIndex = data.nextZIndex || 0; if (data.nextLayerId) Layer._nextId = data.nextLayerId; project.layers = (data.layers || []).map(raw => Layer.fromJSON(raw)); project.selectedId = data.selectedId || (project.layers.length > 0 ? project.layers[0].id : null); project.layers.forEach((l, i) => l.zIndex = i); if (project.layers.length > 0) project._nextZIndex = project.layers.length; return project; } }