/
Gabryelf
/
Icons-Customize-Gen
Обзор
Документация
Войти
/
Gabryelf
/
Icons-Customize-Gen
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/core/HistoryController.js
68 строк
2 KB
Valeev Serj
running_start_version
10 июл 2026, 22:15
10 июл 2026, 22:15
5234bd4
Код
Авторство
О чём код?
import { Layer } from './Layer.js'; import { Project } from '../models/Project.js'; export class HistoryController { constructor(project, eventBus, maxHistory = 30) { this.project = project; this.events = eventBus; this.maxHistory = maxHistory; this._history = []; this._index = -1; this._isRestoring = false; this.events.on('project:changed', () => { if (!this._isRestoring) this._pushState(); }); this._pushState(); } _pushState() { const state = JSON.stringify(this.project.toJSON()); if (this._index < this._history.length - 1) { this._history = this._history.slice(0, this._index + 1); } const last = this._history[this._index]; if (last === state) return; this._history.push(state); this._index = this._history.length - 1; if (this._history.length > this.maxHistory) { this._history.shift(); this._index--; } } _restoreState(index) { if (index < 0 || index >= this._history.length) return; if (index === this._index) return; this._isRestoring = true; const state = this._history[index]; const data = JSON.parse(state); const restored = Project.fromJSON(data); this.project.layers = restored.layers; this.project.selectedId = restored.selectedId; this.project._nextZIndex = restored._nextZIndex; this.project.background = restored.background; this.project.width = restored.width; this.project.height = restored.height; this.project.name = restored.name; if (data.nextLayerId) Layer._nextId = data.nextLayerId; this._index = index; this._isRestoring = false; this.events.emit('project:restored'); this.events.emit('project:changed'); } undo() { if (this._index > 0) this._restoreState(this._index - 1); } redo() { if (this._index < this._history.length - 1) this._restoreState(this._index + 1); } }