2023-11-27 14:00:15 +00:00
|
|
|
import { app } from "../../scripts/app.js";
|
2024-01-16 13:27:40 +00:00
|
|
|
import { api } from "../../scripts/api.js"
|
2023-11-27 14:00:15 +00:00
|
|
|
|
|
|
|
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);
|
2023-11-27 14:02:50 +00:00
|
|
|
if (undo.length > MAX_HISTORY) {
|
|
|
|
undo.shift();
|
|
|
|
}
|
2023-11-27 14:00:15 +00:00
|
|
|
activeState = clone(currentState);
|
|
|
|
redo.length = 0;
|
2024-01-16 13:27:40 +00:00
|
|
|
api.dispatchEvent(new CustomEvent("graphChanged", { detail: activeState }));
|
2023-11-27 14:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
2023-12-11 17:33:35 +00:00
|
|
|
const updateState = async (source, target) => {
|
|
|
|
const prevState = source.pop();
|
|
|
|
if (prevState) {
|
|
|
|
target.push(activeState);
|
|
|
|
isOurLoad = true;
|
|
|
|
await app.loadGraphData(prevState, false);
|
|
|
|
activeState = prevState;
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 14:00:15 +00:00
|
|
|
if (e.ctrlKey || e.metaKey) {
|
|
|
|
if (e.key === "y") {
|
2023-12-11 17:33:35 +00:00
|
|
|
updateState(redo, undo);
|
2023-11-27 14:00:15 +00:00
|
|
|
return true;
|
|
|
|
} else if (e.key === "z") {
|
2023-12-11 17:33:35 +00:00
|
|
|
updateState(undo, redo);
|
2023-11-27 14:00:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const bindInput = (activeEl) => {
|
2024-01-16 13:27:40 +00:00
|
|
|
if (activeEl && activeEl.tagName !== "CANVAS" && activeEl.tagName !== "BODY") {
|
2023-11-27 14:00:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-13 17:00:30 +00:00
|
|
|
let keyIgnored = false;
|
2023-11-27 14:00:15 +00:00
|
|
|
window.addEventListener(
|
|
|
|
"keydown",
|
|
|
|
(e) => {
|
|
|
|
requestAnimationFrame(async () => {
|
2024-01-16 13:27:40 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-11-27 14:00:15 +00:00
|
|
|
}
|
2024-01-16 13:27:40 +00:00
|
|
|
|
2024-01-13 17:00:30 +00:00
|
|
|
keyIgnored = e.key === "Control" || e.key === "Shift" || e.key === "Alt" || e.key === "Meta";
|
|
|
|
if (keyIgnored) return;
|
|
|
|
|
2023-11-27 14:00:15 +00:00
|
|
|
// 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
|
|
|
|
);
|
|
|
|
|
2024-01-13 17:00:30 +00:00
|
|
|
window.addEventListener("keyup", (e) => {
|
|
|
|
if (keyIgnored) {
|
|
|
|
keyIgnored = false;
|
|
|
|
checkState();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-27 14:00:15 +00:00
|
|
|
// Handle clicking DOM elements (e.g. widgets)
|
|
|
|
window.addEventListener("mouseup", () => {
|
|
|
|
checkState();
|
|
|
|
});
|
|
|
|
|
2024-01-16 13:27:40 +00:00
|
|
|
// Handle prompt queue event for dynamic widget changes
|
|
|
|
api.addEventListener("promptQueued", () => {
|
|
|
|
checkState();
|
|
|
|
});
|
|
|
|
|
2023-11-27 14:00:15 +00:00
|
|
|
// 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;
|
|
|
|
};
|
2024-01-16 13:27:40 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|