// Undo/redo. Entries restore exactly what they changed: // - PixelEntry: a rectangle of one layer canvas (content or mask) // - StateEntry: document structure (layer list, props, canvas references, selection, size) // Operations that touch many layers (resize, crop...) replace canvases rather than // mutating them, so StateEntry can hold plain references. import { cloneCanvas, cropCanvas, Rect } from './util.js'; export class PixelEntry { constructor(label, layer, key, rect, before, after) { Object.assign(this, { label, layer, key, rect, before, after }); } _put(doc, src) { const target = this.layer[this.key]; if (!target) return; const ctx = target.getContext('2d'); ctx.save(); ctx.globalCompositeOperation = 'copy'; ctx.beginPath(); ctx.rect(this.rect.x, this.rect.y, this.rect.w, this.rect.h); ctx.clip(); ctx.drawImage(src, this.rect.x, this.rect.y); ctx.restore(); if (this.key === 'mask') this.layer.touchMask(); else this.layer.touch(); doc.activeLayerId = this.layer.id; } undo(doc) { this._put(doc, this.before); } redo(doc) { this._put(doc, this.after); } get cost() { return this.rect.w * this.rect.h * 8; } } export class StateEntry { constructor(label, before, after, coalesceKey = null) { Object.assign(this, { label, before, after, coalesceKey, time: performance.now() }); } undo(doc) { doc.restoreState(this.before); } redo(doc) { doc.restoreState(this.after); } get cost() { return 1024; } } export class CompoundEntry { constructor(label, entries) { this.label = label; this.entries = entries; } undo(doc) { for (let i = this.entries.length - 1; i >= 0; i--) this.entries[i].undo(doc); } redo(doc) { for (const e of this.entries) e.redo(doc); } get cost() { return this.entries.reduce((s, e) => s + e.cost, 0); } } /** Captures "before" pixels of one layer canvas; commit() records the change. */ export class PixelEdit { constructor(doc, layer, key = 'canvas') { this.doc = doc; this.layer = layer; this.key = key; this.snapshot = cloneCanvas(layer[key]); } /** Build the entry without pushing it (for compound edits). */ entry(label, rect = null) { const target = this.layer[this.key]; let r = rect ? Rect.clampTo(Rect.outset(rect, 1), target.width, target.height) : { x: 0, y: 0, w: target.width, h: target.height }; if (!r) return null; const before = cropCanvas(this.snapshot, r.x, r.y, r.w, r.h); const after = cropCanvas(target, r.x, r.y, r.w, r.h); this.snapshot = null; return new PixelEntry(label, this.layer, this.key, r, before, after); } commit(label, rect = null) { const e = this.entry(label, rect); if (e) this.doc.history.push(e); if (this.key === 'mask') this.layer.touchMask(); else this.layer.touch(); this.doc.markDirty(); return e; } /** Restore the snapshot (abort). */ cancel() { const target = this.layer[this.key]; const ctx = target.getContext('2d'); ctx.save(); ctx.globalCompositeOperation = 'copy'; ctx.drawImage(this.snapshot, 0, 0); ctx.restore(); if (this.key === 'mask') this.layer.touchMask(); else this.layer.touch(); this.doc.markDirty(); } } // Limits shared by every document; Preferences can change the step count. export const historyLimits = { entries: 80, cost: 600 * 1024 * 1024 }; // ~600MB of pixel snapshots export class History { constructor(doc) { this.doc = doc; this.entries = []; this.index = 0; // number of applied entries this.listeners = new Set(); this.savedIndex = 0; } onChange(fn) { this.listeners.add(fn); return () => this.listeners.delete(fn); } _emit() { for (const fn of this.listeners) fn(this); } push(entry) { this.entries.length = this.index; if (this.savedIndex > this.index) this.savedIndex = -1; this.entries.push(entry); this.index++; this._trim(); this._emit(); } /** Merge into the last entry if it has the same coalesce key (e.g. slider drags). */ pushCoalesced(entry) { const last = this.entries[this.index - 1]; if (last && entry.coalesceKey && last.coalesceKey === entry.coalesceKey && this.index === this.entries.length && entry.time - last.time < 1500 && this.savedIndex !== this.index) { last.after = entry.after; last.time = entry.time; this._emit(); return; } this.push(entry); } _trim() { let total = this.entries.reduce((s, e) => s + e.cost, 0); while (this.entries.length > 1 && (this.entries.length > historyLimits.entries || total > historyLimits.cost)) { total -= this.entries.shift().cost; this.index--; this.savedIndex--; } } get canUndo() { return this.index > 0; } get canRedo() { return this.index < this.entries.length; } undo() { if (!this.canUndo) return false; this.index--; this.entries[this.index].undo(this.doc); this.doc.markDirty(true); this._emit(); return true; } redo() { if (!this.canRedo) return false; this.entries[this.index].redo(this.doc); this.index++; this.doc.markDirty(true); this._emit(); return true; } /** Jump to a state: n = number of applied entries. */ goto(n) { while (this.index > n && this.undo()); while (this.index < n && this.redo()); } markSaved() { this.savedIndex = this.index; this._emit(); } get isModified() { return this.savedIndex !== this.index; } clear() { this.entries = []; this.index = 0; this.savedIndex = 0; this._emit(); } }