524 lines
20 KiB
JavaScript
524 lines
20 KiB
JavaScript
// Rôle : normalise, compacte et sérialise les données toolbox persistées.
|
|
import { getDefaultModuleTitle } from "../toolboxModuleRegistry.js";
|
|
import { MODULE_DATA_COMPACTORS } from "./modules/index.js";
|
|
import { ID_PREFIXES, uid } from "./modules/shared.js";
|
|
|
|
export { normalizeCalculatorData } from "./modules/calculator.js";
|
|
export { normalizeCalendarData } from "./modules/calendar.js";
|
|
export { clampQty, normalizeChecklistData } from "./modules/checklist.js";
|
|
export { normalizeCombosData } from "./modules/combos.js";
|
|
export { normalizeCountersData } from "./modules/counters.js";
|
|
export { normalizeDrawingData } from "./modules/drawing.js";
|
|
export { normalizeEquipmentPlannerData, summarizeEquipmentPlannerData } from "./modules/equipmentPlanner.js";
|
|
export { normalizeGoalTreeData } from "./modules/goalTree.js";
|
|
export { labelFromFileName, normalizeImageAnnotationData } from "./modules/imageAnnotation.js";
|
|
export { hostnameFromUrl, normalizeLinksData, normalizeUrl } from "./modules/links.js";
|
|
export { normalizeMemosData } from "./modules/memos.js";
|
|
export { normalizeNotepadData, sanitizeNotepadHtml } from "./modules/notepad.js";
|
|
export { normalizeRandomizerData } from "./modules/randomizer.js";
|
|
export { normalizeRngSimulatorData } from "./modules/rngSimulator.js";
|
|
export { uid } from "./modules/shared.js";
|
|
export { normalizeTableData } from "./modules/table.js";
|
|
export { normalizeTaskPlannerData } from "./modules/taskPlanner.js";
|
|
export { normalizeTimerData } from "./modules/timer.js";
|
|
|
|
const TOOLBOX_ICON_BASE = "/static/img/toolbox-icons/";
|
|
const TOOLBOX_ICON_FILES = [
|
|
"toolbox.png",
|
|
"ball.png",
|
|
"capture.png",
|
|
"card.png",
|
|
"city.png",
|
|
"compass.png",
|
|
"crash.png",
|
|
"detective.png",
|
|
"fire.png",
|
|
"horror.png",
|
|
"jump.png",
|
|
"mining.png",
|
|
"parachute.png",
|
|
"puzzle.png",
|
|
"reflexion.png",
|
|
"shield.png",
|
|
"sword.png",
|
|
"target.png",
|
|
"tower.png",
|
|
"wheel.png"
|
|
];
|
|
export const TOOLBOX_ICONS = TOOLBOX_ICON_FILES.map((file) => `${TOOLBOX_ICON_BASE}${file}`);
|
|
export const DEFAULT_TOOLBOX_ICON = `${TOOLBOX_ICON_BASE}toolbox.png`;
|
|
export const MAX_TOOLBOX_SPACES = 5;
|
|
const LEGACY_TOOLBOX_UPDATED_AT = "1970-01-01T00:00:00.000Z";
|
|
|
|
export function normalizeToolboxIcon(icon) {
|
|
const value = String(icon || "").trim();
|
|
const file = value.replace(TOOLBOX_ICON_BASE, "").split("/").pop();
|
|
return TOOLBOX_ICON_FILES.includes(file) ? `${TOOLBOX_ICON_BASE}${file}` : DEFAULT_TOOLBOX_ICON;
|
|
}
|
|
|
|
export function normalizeToolboxCustomCover(customCover) {
|
|
const value = String(customCover || "").trim();
|
|
return /^data:image\/(?:jpeg|jpg|png|webp);base64,[a-z0-9+/]+=*$/i.test(value) ? value : "";
|
|
}
|
|
|
|
function normalizeToolboxModule(module) {
|
|
if (!module || typeof module !== "object" || !module.type) return null;
|
|
const title = String(module.title || "").trim();
|
|
const normalized = { id: module.id || uid("mod"), type: module.type };
|
|
if (title && title !== getDefaultModuleTitle(module.type)) normalized.title = title;
|
|
if (module.scrollable === true) normalized.scrollable = true;
|
|
if (module.collapsed === true) normalized.collapsed = true;
|
|
return normalized;
|
|
}
|
|
|
|
function uniqueKnownIds(ids, knownIds) {
|
|
const known = new Set(knownIds);
|
|
const seen = new Set();
|
|
return (Array.isArray(ids) ? ids : [])
|
|
.map((id) => String(id || ""))
|
|
.filter((id) => {
|
|
if (!known.has(id) || seen.has(id)) return false;
|
|
seen.add(id);
|
|
return true;
|
|
});
|
|
}
|
|
|
|
function completeModuleOrder(ids, knownIds) {
|
|
const ordered = uniqueKnownIds(ids, knownIds);
|
|
const used = new Set(ordered);
|
|
return [...ordered, ...knownIds.filter((id) => !used.has(id))];
|
|
}
|
|
|
|
function splitModuleOrder(ids) {
|
|
const splitIndex = Math.ceil(ids.length / 2);
|
|
return [ids.slice(0, splitIndex), ids.slice(splitIndex)];
|
|
}
|
|
|
|
export function normalizeModuleOrder(moduleOrder, modules) {
|
|
const moduleIds = modules.map((module) => module.id);
|
|
const one = completeModuleOrder(moduleOrder?.one, moduleIds);
|
|
const hasTwoColumnOrder = Array.isArray(moduleOrder?.two) && Array.isArray(moduleOrder.two[0]) && Array.isArray(moduleOrder.two[1]);
|
|
if (!hasTwoColumnOrder) return { one, two: splitModuleOrder(one) };
|
|
|
|
const firstColumn = uniqueKnownIds(moduleOrder.two[0], moduleIds);
|
|
const firstColumnIds = new Set(firstColumn);
|
|
const secondColumn = uniqueKnownIds(moduleOrder.two[1], moduleIds).filter((id) => !firstColumnIds.has(id));
|
|
const used = new Set([...firstColumn, ...secondColumn]);
|
|
const missing = moduleIds.filter((id) => !used.has(id));
|
|
const two = [firstColumn, secondColumn];
|
|
missing.forEach((id) => {
|
|
const targetIndex = two[0].length <= two[1].length ? 0 : 1;
|
|
two[targetIndex].push(id);
|
|
});
|
|
return { one, two };
|
|
}
|
|
|
|
export function remapModuleOrderIds(moduleOrder, moduleIdMap) {
|
|
if (!moduleOrder) return null;
|
|
const remapIds = (ids) => (Array.isArray(ids) ? ids.map((id) => moduleIdMap.get(id)).filter(Boolean) : []);
|
|
return {
|
|
one: remapIds(moduleOrder.one),
|
|
two: [
|
|
remapIds(moduleOrder.two?.[0]),
|
|
remapIds(moduleOrder.two?.[1])
|
|
]
|
|
};
|
|
}
|
|
|
|
function getSpaceModuleIds(space, knownModuleIds) {
|
|
const candidates = [
|
|
...(Array.isArray(space?.moduleOrder?.one) ? space.moduleOrder.one : []),
|
|
...(Array.isArray(space?.moduleOrder?.two?.[0]) ? space.moduleOrder.two[0] : []),
|
|
...(Array.isArray(space?.moduleOrder?.two?.[1]) ? space.moduleOrder.two[1] : [])
|
|
];
|
|
return uniqueKnownIds(candidates, knownModuleIds);
|
|
}
|
|
|
|
export function normalizeToolboxSpaces(toolbox, modules) {
|
|
const moduleIds = modules.map((module) => module.id);
|
|
const rawSpaces = Array.isArray(toolbox?.spaces) ? toolbox.spaces.filter((space) => space && typeof space === "object") : [];
|
|
if (!rawSpaces.length) {
|
|
// L'identifiant stable permet de comparer une ancienne toolbox avec sa version
|
|
// normalisée sans fabriquer un faux conflit entre deux lectures IndexedDB.
|
|
const id = "default";
|
|
return {
|
|
spaces: [{ id, name: "Défaut", moduleOrder: normalizeModuleOrder(toolbox?.moduleOrder, modules) }],
|
|
defaultSpaceId: id,
|
|
activeSpaceId: id
|
|
};
|
|
}
|
|
|
|
const seenSpaceIds = new Set();
|
|
const prepared = rawSpaces.slice(0, MAX_TOOLBOX_SPACES).map((space, index) => {
|
|
let id = String(space.id || "").trim() || uid("space");
|
|
while (seenSpaceIds.has(id)) id = uid("space");
|
|
seenSpaceIds.add(id);
|
|
return {
|
|
id,
|
|
name: String(space.name || "").trim() || (index === 0 ? "Défaut" : `Espace ${index + 1}`),
|
|
source: space
|
|
};
|
|
});
|
|
const requestedDefaultId = String(toolbox?.defaultSpaceId || "");
|
|
const defaultIndex = prepared.findIndex((space) => space.id === requestedDefaultId);
|
|
if (defaultIndex > 0) prepared.unshift(prepared.splice(defaultIndex, 1)[0]);
|
|
|
|
const assigned = new Set();
|
|
const memberships = new Map();
|
|
prepared.forEach((space) => {
|
|
const ids = getSpaceModuleIds(space.source, moduleIds).filter((id) => {
|
|
if (assigned.has(id)) return false;
|
|
assigned.add(id);
|
|
return true;
|
|
});
|
|
memberships.set(space.id, ids);
|
|
});
|
|
const defaultId = prepared[0].id;
|
|
memberships.set(defaultId, [...memberships.get(defaultId), ...moduleIds.filter((id) => !assigned.has(id))]);
|
|
|
|
const spaces = prepared.map((space) => {
|
|
const memberIds = memberships.get(space.id);
|
|
const memberSet = new Set(memberIds);
|
|
const spaceModules = modules.filter((module) => memberSet.has(module.id));
|
|
return {
|
|
id: space.id,
|
|
name: space.name,
|
|
moduleOrder: normalizeModuleOrder(space.source.moduleOrder, spaceModules)
|
|
};
|
|
});
|
|
const requestedActiveId = String(toolbox?.activeSpaceId || "");
|
|
const activeSpaceId = spaces.some((space) => space.id === requestedActiveId) ? requestedActiveId : defaultId;
|
|
return { spaces, defaultSpaceId: defaultId, activeSpaceId };
|
|
}
|
|
|
|
export function normalizeToolbox(toolbox) {
|
|
if (!toolbox || typeof toolbox !== "object") return null;
|
|
const modules = (Array.isArray(toolbox.modules) ? toolbox.modules : []).map(normalizeToolboxModule).filter(Boolean);
|
|
const spaceState = normalizeToolboxSpaces(toolbox, modules);
|
|
const normalized = {
|
|
id: toolbox.id || uid("tbx"),
|
|
name: String(toolbox.name || "Nouvelle toolbox").trim() || "Nouvelle toolbox",
|
|
icon: normalizeToolboxIcon(toolbox.icon),
|
|
customCover: normalizeToolboxCustomCover(toolbox.customCover),
|
|
moduleColumns: Number(toolbox.moduleColumns) === 1 ? 1 : 2,
|
|
modules,
|
|
...spaceState,
|
|
updatedAt: toolbox.updatedAt || LEGACY_TOOLBOX_UPDATED_AT
|
|
};
|
|
if (toolbox.tutorial === true) normalized.tutorial = true;
|
|
return normalized;
|
|
}
|
|
|
|
export function compactToolboxForStorage(toolbox) {
|
|
const normalized = normalizeToolbox(toolbox);
|
|
if (!normalized) return null;
|
|
const compact = {
|
|
id: normalized.id,
|
|
name: normalized.name,
|
|
modules: normalized.modules,
|
|
spaces: normalized.spaces,
|
|
defaultSpaceId: normalized.defaultSpaceId,
|
|
activeSpaceId: normalized.activeSpaceId,
|
|
updatedAt: normalized.updatedAt
|
|
};
|
|
if (normalized.moduleColumns === 1) compact.moduleColumns = 1;
|
|
if (normalized.icon !== DEFAULT_TOOLBOX_ICON) compact.icon = normalized.icon;
|
|
if (normalized.customCover) compact.customCover = normalized.customCover;
|
|
if (normalized.tutorial === true) compact.tutorial = true;
|
|
return compact;
|
|
}
|
|
|
|
export function compactToolboxesForStorage(toolboxes) {
|
|
return (Array.isArray(toolboxes) ? toolboxes : []).map(compactToolboxForStorage).filter(Boolean);
|
|
}
|
|
|
|
export function moduleStorageKey(toolboxId, moduleId) {
|
|
return `${toolboxId}:${moduleId}`;
|
|
}
|
|
|
|
export function globalModuleKey(toolboxId, moduleId) {
|
|
return `${toolboxId}:${moduleId}`;
|
|
}
|
|
|
|
export function compactModuleDataForStorage(type, value) {
|
|
const compactor = MODULE_DATA_COMPACTORS[type];
|
|
return compactor ? compactor(value) : value;
|
|
}
|
|
|
|
export function remapMemosPlannerLink(data, resolveModuleId) {
|
|
if (!data || typeof data !== "object" || !data.plannerLink || data.plannerLink === "all") return data;
|
|
return { ...data, plannerLink: resolveModuleId(data.plannerLink) || "" };
|
|
}
|
|
|
|
export function prepareModuleDataForStorage(toolboxes, toolboxId, moduleId, value, moduleType = "") {
|
|
const module = toolboxes.find((toolbox) => toolbox.id === toolboxId)?.modules.find((item) => item.id === moduleId);
|
|
return compactModuleDataForStorage(moduleType || module?.type, value);
|
|
}
|
|
|
|
function createExportIdFactory() {
|
|
const counts = {};
|
|
return (prefix) => {
|
|
counts[prefix] = (counts[prefix] || 0) + 1;
|
|
return `${ID_PREFIXES[prefix] || "x"}${counts[prefix].toString(36)}`;
|
|
};
|
|
}
|
|
|
|
function remapModuleDataForExport(type, data, nextId, moduleIdMap) {
|
|
const compact = compactModuleDataForStorage(type, data);
|
|
if (!compact) return null;
|
|
if (type === "checklist") {
|
|
const { sections, items, ...settings } = compact;
|
|
if (compact.sections) {
|
|
return {
|
|
...settings,
|
|
sections: sections.map((section) => ({
|
|
...section,
|
|
id: nextId("section"),
|
|
items: section.items.map((item) => ({ ...item, id: nextId("item") }))
|
|
}))
|
|
};
|
|
}
|
|
if (items) return { ...settings, items: items.map((item) => ({ ...item, id: nextId("item") })) };
|
|
return settings;
|
|
}
|
|
if (type === "images") {
|
|
return { images: compact.images.map((image) => ({ ...image, id: nextId("image") })) };
|
|
}
|
|
|
|
if (type === "imageAnnotation") {
|
|
const remapped = {
|
|
...compact,
|
|
markers: compact.markers.map((marker) => ({ ...marker, id: nextId("marker") }))
|
|
};
|
|
if (compact.drawings?.strokes) {
|
|
remapped.drawings = {
|
|
strokes: compact.drawings.strokes.map((stroke) => ({ ...stroke, id: nextId("stroke") }))
|
|
};
|
|
}
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "links") {
|
|
return { links: compact.links.map((link) => ({ ...link, id: nextId("link") })) };
|
|
}
|
|
|
|
if (type === "counters") {
|
|
return { counters: compact.counters.map((counter) => ({ ...counter, id: nextId("counter") })) };
|
|
}
|
|
|
|
if (type === "randomizer") {
|
|
const remapped = { ...compact };
|
|
if (compact.wheel?.entries) remapped.wheel = { ...compact.wheel, entries: compact.wheel.entries.map((entry) => ({ ...entry, id: nextId("randomizer") })) };
|
|
if (compact.teams?.players) {
|
|
const playerIdMap = new Map(compact.teams.players.map((player) => [player.id, nextId("randomizer")]));
|
|
remapped.teams = {
|
|
...compact.teams,
|
|
players: compact.teams.players.map((player) => ({ ...player, id: playerIdMap.get(player.id) })),
|
|
exclusions: (compact.teams.exclusions || []).map((exclusion) => ({
|
|
...exclusion,
|
|
id: nextId("randomizer"),
|
|
playerAId: playerIdMap.get(exclusion.playerAId),
|
|
playerBIds: (exclusion.playerBIds || [exclusion.playerBId])
|
|
.map((playerId) => playerIdMap.get(playerId))
|
|
.filter(Boolean)
|
|
})).filter((exclusion) => exclusion.playerAId && exclusion.playerBIds.length)
|
|
};
|
|
}
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "rngSimulator") {
|
|
const remapped = { ...compact };
|
|
if (compact.rates) remapped.rates = compact.rates.map((rate) => ({ ...rate, id: nextId("rng") }));
|
|
if (compact.activeRateId && compact.rates) {
|
|
const activeIndex = compact.rates.findIndex((rate) => rate.id === compact.activeRateId);
|
|
remapped.activeRateId = activeIndex >= 0 ? remapped.rates[activeIndex].id : "";
|
|
}
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "memos") {
|
|
const remapped = {};
|
|
if (compact.tickerEnabled === false) remapped.tickerEnabled = false;
|
|
if (compact.plannerLink === "all") remapped.plannerLink = "all";
|
|
else if (compact.plannerLink && moduleIdMap.has(compact.plannerLink)) remapped.plannerLink = moduleIdMap.get(compact.plannerLink);
|
|
if (compact.items) remapped.items = compact.items.map((memo) => ({ ...memo, id: nextId("memo") }));
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "notepad") {
|
|
const remapped = {
|
|
pages: compact.pages.map((page) => ({
|
|
...page,
|
|
id: nextId("page"),
|
|
drawings: page.drawings?.strokes?.length
|
|
? { strokes: page.drawings.strokes.map((stroke) => ({ ...stroke, id: nextId("stroke") })) }
|
|
: page.drawings
|
|
}))
|
|
};
|
|
const activeIndex = compact.pages.findIndex((page) => page.id === compact.activePageId);
|
|
if (activeIndex > 0) remapped.activePageId = remapped.pages[activeIndex].id;
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "combos") {
|
|
const remapped = {};
|
|
if (compact.device) remapped.device = compact.device;
|
|
if (compact.combos) remapped.combos = compact.combos.map((combo) => ({ ...combo, id: nextId("combo") }));
|
|
if (compact.collapsedCategories) remapped.collapsedCategories = compact.collapsedCategories;
|
|
if (compact.categoryOrder) remapped.categoryOrder = compact.categoryOrder;
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "calculator") {
|
|
const entryIdMap = new Map();
|
|
compact.entries.forEach((entry) => entryIdMap.set(entry.id, nextId("calc")));
|
|
const remapped = {
|
|
entries: compact.entries.map((entry) => {
|
|
const remapped = { ...entry, id: entryIdMap.get(entry.id) };
|
|
if (entry.parentId) remapped.parentId = entryIdMap.get(entry.parentId) || "";
|
|
return remapped;
|
|
})
|
|
};
|
|
if (compact.scrollResults) remapped.scrollResults = true;
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "timer") {
|
|
const remapped = { ...compact };
|
|
if (compact.stopwatch?.laps) {
|
|
remapped.stopwatch = {
|
|
...compact.stopwatch,
|
|
laps: compact.stopwatch.laps.map((lap) => ({ ...lap, id: nextId("timer") }))
|
|
};
|
|
}
|
|
if (compact.countdowns) {
|
|
remapped.countdowns = compact.countdowns.map((countdown) => ({ ...countdown, id: nextId("timer") }));
|
|
}
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "calendar") {
|
|
const remapped = { ...compact };
|
|
if (compact.events) remapped.events = compact.events.map((event) => ({ ...event, id: nextId("calendar") }));
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "taskPlanner") {
|
|
const taskIdMap = new Map();
|
|
compact.tasks?.forEach((task) => taskIdMap.set(task.id, nextId("task")));
|
|
const remapped = { ...compact };
|
|
if (compact.tasks) remapped.tasks = compact.tasks.map((task) => ({ ...task, id: taskIdMap.get(task.id) }));
|
|
if (compact.relations) {
|
|
remapped.relations = compact.relations
|
|
.map((relation) => ({
|
|
...relation,
|
|
id: nextId("relation"),
|
|
fromTaskId: taskIdMap.get(relation.fromTaskId),
|
|
toTaskId: taskIdMap.get(relation.toTaskId)
|
|
}))
|
|
.filter((relation) => relation.fromTaskId && relation.toTaskId);
|
|
}
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "goalTree") {
|
|
const nodeIdMap = new Map();
|
|
compact.nodes?.forEach((node) => nodeIdMap.set(node.id, nextId("goal")));
|
|
const remapped = { ...compact };
|
|
if (compact.nodes) {
|
|
remapped.nodes = compact.nodes.map((node) => {
|
|
const nextNode = { ...node, id: nodeIdMap.get(node.id) };
|
|
if (node.parentId) nextNode.parentId = nodeIdMap.get(node.parentId) || "";
|
|
if (node.parentIds) nextNode.parentIds = node.parentIds.map((parentId) => nodeIdMap.get(parentId)).filter(Boolean);
|
|
if (node.items) nextNode.items = node.items.map((item) => ({ ...item, id: nextId("goal") }));
|
|
return nextNode;
|
|
});
|
|
}
|
|
return remapped;
|
|
}
|
|
|
|
if (type === "equipmentPlanner") {
|
|
const typeIdMap = new Map();
|
|
const equipmentIdMap = new Map();
|
|
compact.types?.forEach((typeEntry) => typeIdMap.set(typeEntry.id, nextId("equipmentType")));
|
|
compact.equipments?.forEach((equipment) => equipmentIdMap.set(equipment.id, nextId("equipment")));
|
|
const remapped = {
|
|
...compact,
|
|
types: (compact.types || []).map((typeEntry) => ({ ...typeEntry, id: typeIdMap.get(typeEntry.id) })),
|
|
equipments: (compact.equipments || []).map((equipment) => {
|
|
const socketItemIdMap = new Map();
|
|
equipment.socketItems?.forEach((socketItem) => socketItemIdMap.set(socketItem.id, nextId("socketItem")));
|
|
return {
|
|
...equipment,
|
|
id: equipmentIdMap.get(equipment.id),
|
|
typeId: typeIdMap.get(equipment.typeId) || "",
|
|
characteristics: equipment.characteristics?.map((trait) => ({ ...trait, id: nextId("trait") })),
|
|
materials: equipment.materials?.map((material) => ({ ...material, id: nextId("material") })),
|
|
socketItems: equipment.socketItems?.map((socketItem) => ({
|
|
...socketItem,
|
|
id: socketItemIdMap.get(socketItem.id),
|
|
bonuses: socketItem.bonuses?.map((bonus) => ({ ...bonus, id: nextId("socketBonus") }))
|
|
})),
|
|
socketLinks: equipment.socketLinks?.map((link) => ({
|
|
...link,
|
|
id: nextId("socketLink"),
|
|
fromSocketItemId: socketItemIdMap.get(link.fromSocketItemId),
|
|
toSocketItemId: socketItemIdMap.get(link.toSocketItemId)
|
|
})).filter((link) => link.fromSocketItemId && link.toSocketItemId)
|
|
};
|
|
}).filter((equipment) => equipment.typeId)
|
|
};
|
|
if (compact.typeOrder) remapped.typeOrder = compact.typeOrder.map((typeId) => typeIdMap.get(typeId)).filter(Boolean);
|
|
return remapped;
|
|
}
|
|
|
|
return compact;
|
|
}
|
|
|
|
export function createToolboxExportPayload(toolbox, moduleData) {
|
|
const source = normalizeToolbox(toolbox);
|
|
const nextId = createExportIdFactory();
|
|
const moduleIdMap = new Map();
|
|
const spaceIdMap = new Map(source.spaces.map((space) => [space.id, nextId("space")]));
|
|
const exportedToolbox = compactToolboxForStorage({
|
|
...source,
|
|
id: nextId("tbx"),
|
|
modules: source.modules.map((module) => {
|
|
const id = nextId("mod");
|
|
moduleIdMap.set(module.id, id);
|
|
return { ...module, id };
|
|
}),
|
|
spaces: source.spaces.map((space) => ({
|
|
...space,
|
|
id: spaceIdMap.get(space.id),
|
|
moduleOrder: remapModuleOrderIds(space.moduleOrder, moduleIdMap)
|
|
})),
|
|
defaultSpaceId: spaceIdMap.get(source.defaultSpaceId),
|
|
activeSpaceId: spaceIdMap.get(source.activeSpaceId)
|
|
});
|
|
const modules = Object.fromEntries(source.modules
|
|
.map((module) => [
|
|
moduleIdMap.get(module.id),
|
|
remapModuleDataForExport(module.type, moduleData[moduleStorageKey(source.id, module.id)] || null, nextId, moduleIdMap)
|
|
])
|
|
.filter(([, data]) => data != null));
|
|
return { version: 2, toolbox: exportedToolbox, modules };
|
|
}
|
|
|
|
export function createGlobalExportPayload(toolboxes, links, moduleData) {
|
|
const modules = {};
|
|
toolboxes.forEach((toolbox) => {
|
|
toolbox.modules.forEach((module) => {
|
|
const data = compactModuleDataForStorage(module.type, moduleData[moduleStorageKey(toolbox.id, module.id)] || null);
|
|
if (data) modules[globalModuleKey(toolbox.id, module.id)] = data;
|
|
});
|
|
});
|
|
return {
|
|
version: 2,
|
|
exportedAt: new Date().toISOString(),
|
|
toolboxes: compactToolboxesForStorage(toolboxes),
|
|
modules,
|
|
links
|
|
};
|
|
}
|