undoRedo.js 4.21 KB
Newer Older
pythongosssss's avatar
pythongosssss committed
1
import { app } from "../../scripts/app.js";
pythongosssss's avatar
pythongosssss committed
2
import { api } from "../../scripts/api.js"
pythongosssss's avatar
pythongosssss committed
3
4
5
6
7
8
9
10
11
12
13

const MAX_HISTORY = 50;

let undo = [];
let redo = [];
let activeState = null;
let isOurLoad = false;
function checkState() {
	const currentState = app.graph.serialize();
	if (!graphEqual(activeState, currentState)) {
		undo.push(activeState);
pythongosssss's avatar
pythongosssss committed
14
15
16
		if (undo.length > MAX_HISTORY) {
			undo.shift();
		}
pythongosssss's avatar
pythongosssss committed
17
18
		activeState = clone(currentState);
		redo.length = 0;
pythongosssss's avatar
pythongosssss committed
19
		api.dispatchEvent(new CustomEvent("graphChanged", { detail: activeState }));
pythongosssss's avatar
pythongosssss committed
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
	}
}

const loadGraphData = app.loadGraphData;
app.loadGraphData = async function () {
	const v = await loadGraphData.apply(this, arguments);
	if (isOurLoad) {
		isOurLoad = false;
	} else {
		checkState();
	}
	return v;
};

function clone(obj) {
	try {
		if (typeof structuredClone !== "undefined") {
			return structuredClone(obj);
		}
	} catch (error) {
		// structuredClone is stricter than using JSON.parse/stringify so fallback to that
	}

	return JSON.parse(JSON.stringify(obj));
}

function graphEqual(a, b, root = true) {
	if (a === b) return true;

	if (typeof a == "object" && a && typeof b == "object" && b) {
		const keys = Object.getOwnPropertyNames(a);

		if (keys.length != Object.getOwnPropertyNames(b).length) {
			return false;
		}

		for (const key of keys) {
			let av = a[key];
			let bv = b[key];
			if (root && key === "nodes") {
				// Nodes need to be sorted as the order changes when selecting nodes
				av = [...av].sort((a, b) => a.id - b.id);
				bv = [...bv].sort((a, b) => a.id - b.id);
			}
			if (!graphEqual(av, bv, false)) {
				return false;
			}
		}

		return true;
	}

	return false;
}

const undoRedo = async (e) => {
76
77
78
79
80
81
82
83
84
	const updateState = async (source, target) => {
		const prevState = source.pop();
		if (prevState) {
			target.push(activeState);
			isOurLoad = true;
			await app.loadGraphData(prevState, false);
			activeState = prevState;
		}
	}
pythongosssss's avatar
pythongosssss committed
85
86
	if (e.ctrlKey || e.metaKey) {
		if (e.key === "y") {
87
			updateState(redo, undo);
pythongosssss's avatar
pythongosssss committed
88
89
			return true;
		} else if (e.key === "z") {
90
			updateState(undo, redo);
pythongosssss's avatar
pythongosssss committed
91
92
93
94
95
96
			return true;
		}
	}
};

const bindInput = (activeEl) => {
pythongosssss's avatar
pythongosssss committed
97
	if (activeEl && activeEl.tagName !== "CANVAS" && activeEl.tagName !== "BODY") {
pythongosssss's avatar
pythongosssss committed
98
99
100
101
102
103
104
105
106
107
108
109
110
		for (const evt of ["change", "input", "blur"]) {
			if (`on${evt}` in activeEl) {
				const listener = () => {
					checkState();
					activeEl.removeEventListener(evt, listener);
				};
				activeEl.addEventListener(evt, listener);
				return true;
			}
		}
	}
};

111
let keyIgnored = false;
pythongosssss's avatar
pythongosssss committed
112
113
114
115
window.addEventListener(
	"keydown",
	(e) => {
		requestAnimationFrame(async () => {
pythongosssss's avatar
pythongosssss committed
116
117
118
119
120
121
122
123
			let activeEl;
			// If we are auto queue in change mode then we do want to trigger on inputs
			if (!app.ui.autoQueueEnabled || app.ui.autoQueueMode === "instant") {
				activeEl = document.activeElement;
				if (activeEl?.tagName === "INPUT" || activeEl?.type === "textarea") {
					// Ignore events on inputs, they have their native history
					return;
				}
pythongosssss's avatar
pythongosssss committed
124
			}
pythongosssss's avatar
pythongosssss committed
125
		
126
127
128
			keyIgnored = e.key === "Control" || e.key === "Shift" || e.key === "Alt" || e.key === "Meta";
			if (keyIgnored) return;

pythongosssss's avatar
pythongosssss committed
129
130
131
132
133
134
135
136
137
138
139
			// Check if this is a ctrl+z ctrl+y
			if (await undoRedo(e)) return;

			// If our active element is some type of input then handle changes after they're done
			if (bindInput(activeEl)) return;
			checkState();
		});
	},
	true
);

140
141
142
143
144
145
146
window.addEventListener("keyup", (e) => {
	if (keyIgnored) {
		keyIgnored = false;
		checkState();
	}
});

pythongosssss's avatar
pythongosssss committed
147
148
149
150
151
// Handle clicking DOM elements (e.g. widgets)
window.addEventListener("mouseup", () => {
	checkState();
});

pythongosssss's avatar
pythongosssss committed
152
153
154
155
156
// Handle prompt queue event for dynamic widget changes
api.addEventListener("promptQueued", () => {
	checkState();
});

pythongosssss's avatar
pythongosssss committed
157
158
159
160
161
162
163
164
165
166
167
168
169
// Handle litegraph clicks
const processMouseUp = LGraphCanvas.prototype.processMouseUp;
LGraphCanvas.prototype.processMouseUp = function (e) {
	const v = processMouseUp.apply(this, arguments);
	checkState();
	return v;
};
const processMouseDown = LGraphCanvas.prototype.processMouseDown;
LGraphCanvas.prototype.processMouseDown = function (e) {
	const v = processMouseDown.apply(this, arguments);
	checkState();
	return v;
};
pythongosssss's avatar
pythongosssss committed
170
171
172
173
174
175
176
177

// Handle litegraph context menu for COMBO widgets
const close = LiteGraph.ContextMenu.prototype.close;
LiteGraph.ContextMenu.prototype.close = function(e) {
	const v = close.apply(this, arguments);
	checkState();
	return v;
}