Skip to content

Commit 9981365

Browse files
committed
Start wiring up undo/redo.
1 parent a08c189 commit 9981365

1 file changed

Lines changed: 32 additions & 26 deletions

File tree

src/pg/inputPixelEditor/inputPixelEditor.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ type HistoryColorUpdateType = {
5555
index: number
5656
}
5757

58-
type History = {
58+
type HistoryItem = {
5959
type: HistoryType,
6060
data: HistoryGroupType | HistoryPixelType | HistoryColorUpdateType
6161
}
6262

63+
type History = HistoryItem[];
64+
6365
type Layer = {
6466
name: string,
6567
visible: boolean,
@@ -665,6 +667,18 @@ export default class PgInputPixelEditor extends HTMLElement {
665667
this.copyPngToClipboard(image, {});
666668
}
667669
break;
670+
case 'z':
671+
if (event.shiftKey) {
672+
// modern redo keybinding
673+
this.redo();
674+
} else {
675+
this.undo();
676+
}
677+
break;
678+
case 'y':
679+
// legacy redo keybinding
680+
this.redo();
681+
break;
668682
}
669683
}
670684
}
@@ -992,22 +1006,12 @@ export default class PgInputPixelEditor extends HTMLElement {
9921006
// ToDo: Code this
9931007
}
9941008

1009+
/**
1010+
* ToDo: Delete this method
1011+
*/
9951012
clear() {
9961013
const gridEmpty = fillGrid(this.width, this.height);
9971014
const diff = diffGrid(this.#data[this.#layer[0]], gridEmpty);
998-
this.#undoHistory.push({
999-
type: HistoryType.Group,
1000-
data: {
1001-
name: 'Clear'
1002-
}
1003-
});
1004-
this.#undoHistory.push({
1005-
type: HistoryType.Pixel,
1006-
data: {
1007-
pixels: [],
1008-
layer: this.#layer[0]
1009-
}
1010-
});
10111015
this.#data = [fillGrid(this.width, this.height)];
10121016
this.#export = fillGrid(this.width, this.height);
10131017
this.#selectionPreview = fillGrid(this.width, this.height);
@@ -1091,18 +1095,20 @@ export default class PgInputPixelEditor extends HTMLElement {
10911095

10921096
undo() {
10931097
// ToDo: Rewrite to use new history api
1094-
const revert = this.#undoHistory.pop();
1095-
if (!revert) { return; }
1096-
switch (revert.type) {
1097-
case HistoryType.Pixel:
1098-
this.#redoHistory.push(revert);
1099-
(revert.data as HistoryPixelType).pixels.forEach((item) => {
1100-
const [x, y] = item;
1101-
this.#data[this.#layer[0]][y][x] = item[2];
1102-
// redraw canvas
1103-
});
1104-
break;
1105-
}
1098+
const historyItems = this.#undoHistory.pop();
1099+
if (!historyItems) { return; }
1100+
this.#redoHistory.push(historyItems);
1101+
historyItems.forEach((historyItem) => {
1102+
switch (historyItem.type) {
1103+
case HistoryType.Pixel:
1104+
(historyItem.data as HistoryPixelType).pixels.forEach((item) => {
1105+
const [x, y] = item;
1106+
this.#data[this.#layer[0]][y][x] = item[2];
1107+
// redraw canvas
1108+
});
1109+
break;
1110+
}
1111+
});
11061112
}
11071113

11081114
redo() {

0 commit comments

Comments
 (0)