aboutsummaryrefslogtreecommitdiff
path: root/build/js/ui/panels/history.js
blob: bc159a741237a9b1e52a7bd12989affa28dd7d6d (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
// History panel: click an entry to jump back/forward in time.
import { el } from '../../core/util.js';

export class HistoryPanel {
  constructor(app, container) {
    this.app = app;
    this.list = el('div', { class: 'history-list' });
    // Drag the grip above the list to trade space with the Layers panel.
    const grip = el('div', { class: 'panel-grip', title: 'Drag to resize' });
    this.el = el('div', { class: 'panel history-panel' }, grip, el('div', { class: 'panel-title' }, 'History'), this.list);
    container.appendChild(this.el);
    this.list.style.height = `${app.prefs.get('historyHeight', 96)}px`;
    grip.addEventListener('pointerdown', (e) => {
      grip.setPointerCapture(e.pointerId);
      const y0 = e.clientY, h0 = this.list.offsetHeight;
      const move = (ev) => { this.list.style.height = `${Math.max(48, Math.min(600, h0 - (ev.clientY - y0)))}px`; };
      const up = () => {
        grip.removeEventListener('pointermove', move);
        grip.removeEventListener('pointerup', up);
        app.prefs.set('historyHeight', this.list.offsetHeight);
      };
      grip.addEventListener('pointermove', move);
      grip.addEventListener('pointerup', up);
    });
    app.on('doc', () => this.update());
    app.on('history', () => this.update());
    this.update();
  }

  update() {
    const doc = this.app.doc;
    this.list.innerHTML = '';
    if (!doc) return;
    const h = doc.history;
    const row = (label, n) => {
      const r = el('div', { class: `history-row${n === h.index ? ' current' : ''}${n > h.index ? ' future' : ''}` }, label);
      r.addEventListener('click', () => {
        this.app.tool?.commit(); // like Undo: finish a pending transform or text edit first
        h.goto(n);
      });
      return r;
    };
    this.list.appendChild(row('Open', 0));
    h.entries.forEach((e, i) => this.list.appendChild(row(e.label, i + 1)));
    // keep the current entry visible without scrolling ancestor containers
    const cur = this.list.querySelector('.current');
    if (cur) {
      const top = cur.offsetTop; // .history-list is position: relative
      if (top < this.list.scrollTop) this.list.scrollTop = top;
      else if (top + cur.offsetHeight > this.list.scrollTop + this.list.clientHeight) this.list.scrollTop = top + cur.offsetHeight - this.list.clientHeight;
    }
  }
}