openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
186 lines (185 loc) • 5.62 kB
JavaScript
//#region src/boards/board-layout.ts
const BOARD_SIZE_PRESETS = {
sm: {
sizeW: 3,
sizeH: 3
},
md: {
sizeW: 6,
sizeH: 4
},
lg: {
sizeW: 8,
sizeH: 6
},
xl: {
sizeW: 12,
sizeH: 8
},
full: {
sizeW: 12,
sizeH: 8
}
};
const BOARD_WIDGET_PROPS_MAX_BYTES = 8192;
var BoardValidationError = class extends Error {
constructor(e, t) {
super(t);
this.name = "BoardValidationError";
this.code = e;
}
};
function clampInteger(e, t, n) {
return Math.min(n, Math.max(t, Math.trunc(e)));
}
function comparePosition(e, t) {
return e.position - t.position;
}
function normalizeBoardLayout(e) {
return normalizeOwnedBoardLayout(structuredClone(e));
}
function normalizeOwnedBoardLayout(e) {
const t = e.tabs.sort(comparePosition);
t.forEach((e, t) => {
e.position = t;
});
const n = new Map(t.map((e) => [e.tabId, e.position]));
const r = e.widgets.sort((e, t) => {
return (n.get(e.tabId) ?? Number.MAX_SAFE_INTEGER) - (n.get(t.tabId) ?? Number.MAX_SAFE_INTEGER) || e.position - t.position;
});
const i = /* @__PURE__ */ new Map();
for (const e of r) {
const t = i.get(e.tabId) ?? 0;
e.position = t;
i.set(e.tabId, t + 1);
}
return {
tabs: t,
widgets: r
};
}
function requireTab(e, t) {
const n = e.tabs.find((e) => e.tabId === t);
if (!n) throw new BoardValidationError("not_found", `board tab not found: ${t}`);
return n;
}
function requireWidget(e, t) {
const n = e.widgets.find((e) => e.name === t);
if (!n) throw new BoardValidationError("not_found", `board widget not found: ${t}`);
return n;
}
function moveTab(e, t, n) {
const r = e.tabs.filter((e) => e !== t).sort(comparePosition);
r.splice(clampInteger(n, 0, r.length), 0, t);
r.forEach((e, t) => {
e.position = t;
});
e.tabs = r;
}
function moveWidget(e, t, n, r, i) {
requireTab(e, n);
if (r !== void 0 && i !== void 0) throw new BoardValidationError("invalid_operation", "widget_move accepts either position or after, not both");
const a = e.widgets.filter((e) => e.tabId === n && e !== t).sort(comparePosition);
let o = a.length;
if (i !== void 0) {
if (i === t.name) throw new BoardValidationError("invalid_operation", "widget cannot be placed after itself");
const e = a.findIndex((e) => e.name === i);
if (e < 0) throw new BoardValidationError("not_found", `board widget anchor not found on tab ${n}: ${i}`);
o = e + 1;
} else if (r !== void 0) o = clampInteger(r, 0, a.length);
t.tabId = n;
a.splice(o, 0, t);
a.forEach((e, t) => {
e.position = t;
});
e.widgets = [...e.widgets.filter((e) => e !== t && e.tabId !== n), ...a];
}
function applyBoardOp(e, t) {
switch (t.kind) {
case "tab_create":
if (e.tabs.some((e) => e.tabId === t.tabId)) throw new BoardValidationError("conflict", `board tab already exists: ${t.tabId}`);
e.tabs.push({
tabId: t.tabId,
title: t.title,
position: e.tabs.length,
chatDock: t.chatDock ?? "right"
});
return;
case "tab_update": {
const n = requireTab(e, t.tabId);
if (t.title === void 0 && t.chatDock === void 0 && t.position === void 0) throw new BoardValidationError("invalid_operation", "tab_update has no changes");
if (t.title !== void 0) n.title = t.title;
if (t.chatDock !== void 0) n.chatDock = t.chatDock;
if (t.position !== void 0) moveTab(e, n, t.position);
return;
}
case "tab_delete": {
const n = requireTab(e, t.tabId);
const r = e.tabs.filter((e) => e !== n).sort(comparePosition);
const i = e.widgets.filter((e) => e.tabId === n.tabId).sort(comparePosition);
if (r.length === 0 && i.length > 0) throw new BoardValidationError("invalid_operation", "cannot delete the last board tab while it contains widgets");
e.tabs = r;
if (i.length > 0) {
const e = r[0];
for (const t of i) {
t.tabId = e.tabId;
t.position = Number.MAX_SAFE_INTEGER;
}
}
return;
}
case "tabs_reorder": {
if (t.tabIds.length !== e.tabs.length || new Set(t.tabIds).size !== t.tabIds.length || t.tabIds.some((t) => !e.tabs.some((e) => e.tabId === t))) throw new BoardValidationError("invalid_operation", "tabs_reorder must contain every tab exactly once");
const n = new Map(e.tabs.map((e) => [e.tabId, e]));
e.tabs = t.tabIds.map((e, t) => {
const r = n.get(e);
r.position = t;
return r;
});
return;
}
case "widget_move": {
const n = requireWidget(e, t.name);
moveWidget(e, n, t.tabId ?? n.tabId, t.position, t.after);
return;
}
case "widget_resize": {
const n = requireWidget(e, t.name);
n.sizeW = clampInteger(t.sizeW, 1, 12);
n.sizeH = clampInteger(t.sizeH, 1, 20);
n.heightMode = t.heightMode ?? "fixed";
return;
}
case "widget_remove":
requireWidget(e, t.name);
e.widgets = e.widgets.filter((e) => e.name !== t.name);
}
}
function applyBoardOps(e, t) {
const n = structuredClone(e);
for (const e of t) {
applyBoardOp(n, e);
normalizeOwnedBoardLayout(n);
}
return t.length > 0 ? {
tabs: n.tabs,
widgets: n.widgets
} : normalizeOwnedBoardLayout(n);
}
function insertBoardWidget(e, t, n) {
const r = structuredClone(e);
const i = r.widgets.findIndex((e) => e.name === t.name);
const a = r.widgets[i];
const o = {
...structuredClone(t),
tabId: n.tabId
};
if (a) {
o.position = a.position;
r.widgets[i] = o;
} else r.widgets.push(o);
if (!a || n.move) moveWidget(r, o, n.tabId, void 0, n.after);
return normalizeOwnedBoardLayout(r);
}
//#endregion
export { insertBoardWidget as a, applyBoardOps as i, BOARD_WIDGET_PROPS_MAX_BYTES as n, normalizeBoardLayout as o, BoardValidationError as r, normalizeOwnedBoardLayout as s, BOARD_SIZE_PRESETS as t };