synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
221 lines (220 loc) • 5.92 kB
JavaScript
import { jsxs as u, Fragment as M, jsx as p } from "react/jsx-runtime";
import x from "../components/GridMenuButton/GridMenuButton.js";
import z from "@mui/icons-material/KeyboardArrowDown";
import { Menu as L, MenuItem as T } from "@mui/material";
import { useState as V, useCallback as m, useEffect as J } from "react";
import { convertActionToModelChange as w } from "../utils/convertActionToModelChange.js";
import { useStack as _ } from "./useStack.js";
function Q(c) {
return {
type: "COMPOSITE",
actions: c
};
}
const W = 100, Y = 100;
function co(c) {
const n = _([], W), r = _([], Y), [A, S] = V(null), [I, R] = V(null), U = !!A, P = !!I, v = (t) => {
S(t.currentTarget);
}, B = (t) => {
R(t.currentTarget);
}, b = () => S(null), K = () => R(null), G = m(() => {
if (n.isEmpty()) return null;
const t = n.peek();
let o, s = 0;
if (t.type === "COMPOSITE") {
o = "COMPOSITE";
for (let e = n.stack.length - 1; e >= 0 && n.stack[e].type === "COMPOSITE"; e--)
s++;
} else {
o = t.type;
for (let e = n.stack.length - 1; e >= 0 && n.stack[e].type === o; e--)
s++;
}
return {
lastType: o,
sameTypeCount: s,
totalActions: n.size
};
}, [n]), N = m(() => {
if (r.isEmpty()) return null;
const t = r.peek();
let o, s = 0;
if (t.type === "COMPOSITE")
o = "COMPOSITE", s = 1;
else {
o = t.type;
for (let e = r.stack.length - 1; e >= 0 && r.stack[e].type === o; e--)
s++;
}
return {
lastType: o,
sameTypeCount: s,
totalActions: r.size
};
}, [r]), i = G(), l = N(), h = m(() => {
if (n.isEmpty()) return;
const t = n.pop();
if (t)
if (t.type === "COMPOSITE") {
for (let o = t.actions.length - 1; o >= 0; o--) {
const s = t.actions[o], e = w(s, "undo");
e && c(e);
}
r.push(t);
} else {
const o = w(
t,
"undo"
);
if (!o) return;
c(o), r.push(t);
}
b();
}, [c, n, r]), y = m(() => {
if (r.isEmpty()) return;
const t = r.pop();
if (t)
if (t.type === "COMPOSITE") {
for (let o = 0; o < t.actions.length; o++) {
const s = t.actions[o], e = w(s, "redo");
e && c(e);
}
n.push(t);
} else {
const o = w(
t,
"redo"
);
if (!o) return;
c(o), n.push(t);
}
K();
}, [r, n, c]);
J(() => {
const t = (o) => {
o.ctrlKey && o.key === "z" && !o.shiftKey || o.metaKey && o.key === "z" && !o.shiftKey ? (o.preventDefault(), h()) : (o.ctrlKey && o.key === "y" || o.ctrlKey && o.shiftKey && o.key === "z" || o.metaKey && o.shiftKey && o.key === "z") && (o.preventDefault(), y());
};
return window.addEventListener("keydown", t), () => {
window.removeEventListener("keydown", t);
};
}, [h, y]);
const O = (t) => {
n.push(t);
}, j = () => n.clear(), g = () => r.clear(), X = (t, o, s) => {
g();
const e = [];
for (const f of t) {
const d = f.fromRowIndex, E = f.toRowIndex;
if (f.type === "CREATE")
for (let a = d; a < E; a++) {
const k = s[a];
e.push({
type: "CREATE",
rowIndex: a,
newValue: k
});
}
if (f.type === "UPDATE") {
const a = o.slice(d, E);
s.slice(d, E).forEach((C, D) => {
const q = a[D], H = d + D;
e.push({
type: "UPDATE",
rowIndex: H,
previousValue: q,
newValue: C
});
});
}
f.type === "DELETE" && o.slice(d, E).forEach((k, C) => {
e.push({
type: "DELETE",
rowIndex: d + C,
previousValue: k
});
});
}
e.length === 1 ? O(e[0]) : e.length > 1 && O(Q(e));
}, $ = /* @__PURE__ */ u(M, { children: [
/* @__PURE__ */ u(
x,
{
variant: "outlined",
"aria-controls": U ? "undo-menu" : void 0,
"aria-haspopup": "true",
onClick: v,
disabled: !i,
endIcon: /* @__PURE__ */ p(z, {}),
children: [
"Undo ",
i && `(${i.totalActions})`
]
}
),
/* @__PURE__ */ p(
L,
{
id: "undo-menu",
anchorEl: A,
open: U,
onClose: b,
children: i ? /* @__PURE__ */ u(T, { onClick: h, children: [
"Undo last ",
i.lastType.toLowerCase(),
" action"
] }, "lastAction") : /* @__PURE__ */ p(T, { disabled: !0, children: "No actions to undo" })
}
)
] }), F = /* @__PURE__ */ u(M, { children: [
/* @__PURE__ */ u(
x,
{
variant: "outlined",
"aria-controls": P ? "redo-menu" : void 0,
"aria-haspopup": "true",
onClick: B,
disabled: !l,
endIcon: /* @__PURE__ */ p(z, {}),
children: [
"Redo ",
l && `(${l.totalActions})`
]
}
),
/* @__PURE__ */ p(
L,
{
id: "redo-menu",
anchorEl: I,
open: P,
onClose: K,
children: l ? /* @__PURE__ */ u(T, { onClick: y, children: [
"Redo last ",
l.lastType.toLowerCase(),
" action"
] }, "lastAction") : /* @__PURE__ */ p(T, { disabled: !0, children: "No actions to redo" })
}
)
] });
return {
// Undo
undoPreview: i,
handleUndo: h,
addToUndoStack: O,
clearUndoStack: j,
addOperationsToUndoStack: X,
undoUI: $,
undoTotalActions: i ? i.totalActions : 0,
// Redo
redoPreview: l,
redoUI: F,
redoTotalActions: l ? l.totalActions : 0,
handleRedo: y,
clearRedoStack: g
};
}
export {
Q as batchGridActions,
co as useGridUndoRedo
};
//# sourceMappingURL=useGridUndoRedo.js.map