aboutsummaryrefslogtreecommitdiff
path: root/build/js/core/history.js
blob: 7d4b904eadde4b95f2583072fe17880dcb1871b0 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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(); }
}