163 lines
7 KiB
JavaScript
163 lines
7 KiB
JavaScript
// Rôle : applique les créations, suppressions et déplacements d'outils entre espaces toolbox.
|
|
import { MAX_TOOLBOX_SPACES, normalizeModuleOrder, uid } from "./storage/toolboxStorage.js";
|
|
|
|
export const SPACE_GROUP_SEPARATOR = "::";
|
|
|
|
export function getActiveToolboxSpace(toolbox) {
|
|
return toolbox.spaces.find((space) => space.id === toolbox.activeSpaceId) || toolbox.spaces[0];
|
|
}
|
|
|
|
export function getModuleSpace(toolbox, moduleId) {
|
|
return toolbox.spaces.find((space) => space.moduleOrder.one.includes(moduleId)) || toolbox.spaces[0];
|
|
}
|
|
|
|
export function encodeSpaceColumn(spaceId, column = "one") {
|
|
return `${spaceId}${SPACE_GROUP_SEPARATOR}${column}`;
|
|
}
|
|
|
|
export function decodeSpaceColumn(value) {
|
|
const [spaceId = "", column = "one"] = String(value || "").split(SPACE_GROUP_SEPARATOR);
|
|
return { spaceId, column };
|
|
}
|
|
|
|
function removeId(order, moduleId) {
|
|
return {
|
|
one: order.one.filter((id) => id !== moduleId),
|
|
two: order.two.map((column) => column.filter((id) => id !== moduleId))
|
|
};
|
|
}
|
|
|
|
function insertAround(ids, moduleId, targetId, placement) {
|
|
const next = ids.filter((id) => id !== moduleId);
|
|
const targetIndex = next.indexOf(targetId);
|
|
if (targetIndex < 0) return [...next, moduleId];
|
|
next.splice(placement === "after" ? targetIndex + 1 : targetIndex, 0, moduleId);
|
|
return next;
|
|
}
|
|
|
|
export function addModuleToToolboxSpace(toolbox, module, spaceId = toolbox.activeSpaceId) {
|
|
const modules = [...toolbox.modules, module];
|
|
const targetId = toolbox.spaces.some((space) => space.id === spaceId) ? spaceId : toolbox.defaultSpaceId;
|
|
return {
|
|
...toolbox,
|
|
modules,
|
|
spaces: toolbox.spaces.map((space) => {
|
|
if (space.id !== targetId) return space;
|
|
const order = normalizeModuleOrder(space.moduleOrder, modules.filter((item) => space.moduleOrder.one.includes(item.id)));
|
|
const two = order.two.map((column) => column.filter((id) => id !== module.id));
|
|
const targetColumn = two[0].length <= two[1].length ? 0 : 1;
|
|
two[targetColumn].push(module.id);
|
|
return { ...space, moduleOrder: { one: [...order.one.filter((id) => id !== module.id), module.id], two } };
|
|
})
|
|
};
|
|
}
|
|
|
|
export function removeModuleFromToolboxSpaces(toolbox, moduleId) {
|
|
return {
|
|
...toolbox,
|
|
modules: toolbox.modules.filter((module) => module.id !== moduleId),
|
|
spaces: toolbox.spaces.map((space) => ({ ...space, moduleOrder: removeId(space.moduleOrder, moduleId) }))
|
|
};
|
|
}
|
|
|
|
export function createToolboxSpace(toolbox, name) {
|
|
if (toolbox.spaces.length >= MAX_TOOLBOX_SPACES || !String(name || "").trim()) return toolbox;
|
|
const id = uid("space");
|
|
return {
|
|
...toolbox,
|
|
spaces: [...toolbox.spaces, { id, name: String(name || "").trim(), moduleOrder: { one: [], two: [[], []] } }],
|
|
activeSpaceId: id
|
|
};
|
|
}
|
|
|
|
export function deleteToolboxSpace(toolbox, spaceId) {
|
|
if (spaceId === toolbox.defaultSpaceId) return toolbox;
|
|
const removed = toolbox.spaces.find((space) => space.id === spaceId);
|
|
const fallback = toolbox.spaces.find((space) => space.id === toolbox.defaultSpaceId);
|
|
if (!removed || !fallback) return toolbox;
|
|
const mergedOrder = {
|
|
one: [...fallback.moduleOrder.one, ...removed.moduleOrder.one],
|
|
two: [
|
|
[...fallback.moduleOrder.two[0], ...removed.moduleOrder.two[0]],
|
|
[...fallback.moduleOrder.two[1], ...removed.moduleOrder.two[1]]
|
|
]
|
|
};
|
|
return {
|
|
...toolbox,
|
|
spaces: toolbox.spaces.filter((space) => space.id !== spaceId).map((space) => (
|
|
space.id === fallback.id ? { ...space, moduleOrder: mergedOrder } : space
|
|
)),
|
|
activeSpaceId: fallback.id
|
|
};
|
|
}
|
|
|
|
export function reorderToolboxSpaces(toolbox, sourceId, targetId, placement = "before") {
|
|
if (sourceId === toolbox.defaultSpaceId || sourceId === targetId) return toolbox;
|
|
const secondary = toolbox.spaces.filter((space) => space.id !== toolbox.defaultSpaceId && space.id !== sourceId);
|
|
const moving = toolbox.spaces.find((space) => space.id === sourceId);
|
|
if (!moving) return toolbox;
|
|
let targetIndex = secondary.findIndex((space) => space.id === targetId);
|
|
if (targetId === toolbox.defaultSpaceId) targetIndex = 0;
|
|
else if (placement === "after" && targetIndex >= 0) targetIndex += 1;
|
|
if (targetIndex < 0) targetIndex = secondary.length;
|
|
secondary.splice(targetIndex, 0, moving);
|
|
const fallback = toolbox.spaces.find((space) => space.id === toolbox.defaultSpaceId);
|
|
return { ...toolbox, spaces: [fallback, ...secondary] };
|
|
}
|
|
|
|
export function moveToolboxModule(toolbox, moduleId, {
|
|
targetSpaceId,
|
|
targetModuleId = "",
|
|
targetColumn = "",
|
|
placement = "after",
|
|
moduleColumns = toolbox.moduleColumns
|
|
}) {
|
|
const sourceSpace = getModuleSpace(toolbox, moduleId);
|
|
const destination = toolbox.spaces.find((space) => space.id === targetSpaceId);
|
|
if (!sourceSpace || !destination) return toolbox;
|
|
const sourceColumnIndex = sourceSpace.moduleOrder.two[1].includes(moduleId) ? 1 : 0;
|
|
|
|
if (sourceSpace.id === destination.id) {
|
|
return {
|
|
...toolbox,
|
|
spaces: toolbox.spaces.map((space) => {
|
|
if (space.id !== destination.id) return space;
|
|
const next = { one: [...space.moduleOrder.one], two: space.moduleOrder.two.map((column) => [...column]) };
|
|
if (Number(moduleColumns) === 1) {
|
|
next.one = insertAround(next.one, moduleId, targetModuleId, placement);
|
|
} else {
|
|
next.two = next.two.map((column) => column.filter((id) => id !== moduleId));
|
|
const foundColumnIndex = next.two.findIndex((column) => column.includes(targetModuleId));
|
|
const columnIndex = targetColumn === "right" ? 1 : targetColumn === "left" ? 0 : foundColumnIndex >= 0 ? foundColumnIndex : sourceColumnIndex;
|
|
next.two[columnIndex] = targetModuleId
|
|
? insertAround(next.two[columnIndex], moduleId, targetModuleId, placement)
|
|
: placement === "before" ? [moduleId, ...next.two[columnIndex]] : [...next.two[columnIndex], moduleId];
|
|
}
|
|
return { ...space, moduleOrder: next };
|
|
})
|
|
};
|
|
}
|
|
|
|
const spaces = toolbox.spaces.map((space) => ({ ...space, moduleOrder: removeId(space.moduleOrder, moduleId) }));
|
|
return {
|
|
...toolbox,
|
|
spaces: spaces.map((space) => {
|
|
if (space.id !== destination.id) return space;
|
|
const next = { one: [...space.moduleOrder.one], two: space.moduleOrder.two.map((column) => [...column]) };
|
|
const targetColumnIndex = targetColumn === "right"
|
|
? 1
|
|
: targetColumn === "left"
|
|
? 0
|
|
: Number(moduleColumns) === 2 ? next.two.findIndex((column) => column.includes(targetModuleId)) : -1;
|
|
|
|
next.one = targetModuleId && Number(moduleColumns) === 1
|
|
? insertAround(next.one, moduleId, targetModuleId, placement)
|
|
: [...next.one, moduleId];
|
|
const columnIndex = targetColumnIndex >= 0 ? targetColumnIndex : sourceColumnIndex;
|
|
next.two[columnIndex] = targetModuleId && Number(moduleColumns) === 2
|
|
? insertAround(next.two[columnIndex], moduleId, targetModuleId, placement)
|
|
: targetColumn && placement === "before" ? [moduleId, ...next.two[columnIndex]] : [...next.two[columnIndex], moduleId];
|
|
return { ...space, moduleOrder: next };
|
|
})
|
|
};
|
|
}
|