Add library tools to local toolboxes
This commit is contained in:
parent
daa5eba8bd
commit
ec7a4a4c1d
15 changed files with 650 additions and 9 deletions
|
|
@ -476,6 +476,10 @@ function remapModuleDataForExport(type, data, nextId, moduleIdMap) {
|
|||
return compact;
|
||||
}
|
||||
|
||||
export function copyModuleDataWithFreshIds(type, data, moduleIdMap = new Map()) {
|
||||
return remapModuleDataForExport(type, data, uid, moduleIdMap);
|
||||
}
|
||||
|
||||
export function createToolboxExportPayload(toolbox, moduleData) {
|
||||
const source = normalizeToolbox(toolbox);
|
||||
const nextId = createExportIdFactory();
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ export const TOOLBOX_MODULE_DEFINITIONS = {
|
|||
icon: "memos",
|
||||
editable: true,
|
||||
scrollable: true,
|
||||
copyDependency: { type: "taskPlanner", dataField: "plannerLink" },
|
||||
libraryCategory: "notesTracking"
|
||||
},
|
||||
checklist: {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
// Rôle : regroupe les actions métier de création, import/export, lien et édition toolbox.
|
||||
import {
|
||||
compactModuleDataForStorage,
|
||||
copyModuleDataWithFreshIds,
|
||||
createGlobalExportPayload,
|
||||
createToolboxExportPayload,
|
||||
globalModuleKey,
|
||||
|
|
@ -55,8 +56,7 @@ export function useToolboxActions({ store, notify, creationTemplate, importConte
|
|||
});
|
||||
}
|
||||
|
||||
function createToolbox(name, gameId = "", options = {}) {
|
||||
if (!store.ready) return null;
|
||||
function buildToolboxCreation(name, options = {}) {
|
||||
const now = new Date().toISOString();
|
||||
const memosModuleId = uid("mod");
|
||||
const linksModuleId = uid("mod");
|
||||
|
|
@ -78,16 +78,101 @@ export function useToolboxActions({ store, notify, creationTemplate, importConte
|
|||
if (options.tutorial === true) toolbox.tutorial = true;
|
||||
const memos = compactModuleDataForStorage("memos", { items: [{ id: uid("memo"), text: welcomeText, color: "#f6c453", updatedAt: now }] });
|
||||
const links = compactModuleDataForStorage("links", { links: [libraryLink] });
|
||||
void store.persistToolboxSnapshot((current) => ({
|
||||
toolboxes: [toolbox, ...current.toolboxes],
|
||||
links: gameId ? { ...current.links, [gameId]: toolbox.id } : current.links,
|
||||
return {
|
||||
toolbox,
|
||||
moduleData: {
|
||||
...current.moduleData,
|
||||
[globalModuleKey(toolbox.id, memosModuleId)]: memos,
|
||||
[globalModuleKey(toolbox.id, linksModuleId)]: links
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function createToolbox(name, gameId = "", options = {}) {
|
||||
if (!store.ready) return null;
|
||||
const creation = buildToolboxCreation(name, options);
|
||||
void store.persistToolboxSnapshot((current) => ({
|
||||
toolboxes: [creation.toolbox, ...current.toolboxes],
|
||||
links: gameId ? { ...current.links, [gameId]: creation.toolbox.id } : current.links,
|
||||
moduleData: { ...current.moduleData, ...creation.moduleData }
|
||||
}));
|
||||
return toolbox;
|
||||
return creation.toolbox;
|
||||
}
|
||||
|
||||
async function addLibraryToolToToolbox({
|
||||
sourceModule,
|
||||
sourceData,
|
||||
dependencyModules = [],
|
||||
copyData = true,
|
||||
toolboxId = "",
|
||||
spaceId = "",
|
||||
newToolboxName = ""
|
||||
}) {
|
||||
if (!store.ready || !sourceModule?.type) return null;
|
||||
|
||||
const sources = [sourceModule, ...dependencyModules]
|
||||
.filter((module, index, modules) => module?.id && module?.type && modules.findIndex((item) => item.id === module.id) === index);
|
||||
const moduleIdMap = new Map(sources.map((module) => [module.id, uid("mod")]));
|
||||
if (copyData && sourceModule.type === "memos") {
|
||||
const plannerLink = String(sourceData?.plannerLink || "");
|
||||
if (plannerLink && plannerLink !== "all" && !moduleIdMap.has(plannerLink)) return null;
|
||||
}
|
||||
|
||||
const modulesToAdd = sources.map((module) => {
|
||||
const copiedModule = { id: moduleIdMap.get(module.id), type: module.type };
|
||||
if (module.title) copiedModule.title = module.title;
|
||||
return copiedModule;
|
||||
});
|
||||
const copiedData = copyData
|
||||
? Object.fromEntries(sources.map((module) => [
|
||||
moduleIdMap.get(module.id),
|
||||
copyModuleDataWithFreshIds(module.type, module.data, moduleIdMap)
|
||||
]).filter(([, data]) => data != null))
|
||||
: {};
|
||||
const requestedToolboxId = String(toolboxId || "");
|
||||
const requestedSpaceId = String(spaceId || "");
|
||||
const createNew = !requestedToolboxId && Boolean(String(newToolboxName || "").trim());
|
||||
const creation = createNew ? buildToolboxCreation(newToolboxName) : null;
|
||||
let result = null;
|
||||
|
||||
try {
|
||||
const saved = await store.persistToolboxSnapshot((current) => {
|
||||
const destination = creation?.toolbox || current.toolboxes.find((toolbox) => toolbox.id === requestedToolboxId);
|
||||
if (!destination) return current;
|
||||
const destinationSpaceId = creation
|
||||
? destination.defaultSpaceId
|
||||
: destination.spaces.some((space) => space.id === requestedSpaceId) ? requestedSpaceId : "";
|
||||
if (!destinationSpaceId) return current;
|
||||
|
||||
const updatedAt = new Date().toISOString();
|
||||
const updatedToolbox = {
|
||||
...modulesToAdd.reduce(
|
||||
(toolbox, module) => addModuleToToolboxSpace(toolbox, module, destinationSpaceId),
|
||||
destination
|
||||
),
|
||||
activeSpaceId: destinationSpaceId
|
||||
};
|
||||
const nextModuleData = { ...(creation?.moduleData || {}) };
|
||||
Object.entries(copiedData).forEach(([moduleId, data]) => {
|
||||
nextModuleData[globalModuleKey(destination.id, moduleId)] = data;
|
||||
});
|
||||
result = {
|
||||
toolboxId: destination.id,
|
||||
toolboxName: destination.name,
|
||||
spaceId: destinationSpaceId,
|
||||
moduleIds: modulesToAdd.map((module) => module.id)
|
||||
};
|
||||
return {
|
||||
...current,
|
||||
toolboxes: creation
|
||||
? [{ ...updatedToolbox, updatedAt }, ...current.toolboxes]
|
||||
: current.toolboxes.map((toolbox) => toolbox.id === destination.id ? { ...updatedToolbox, updatedAt } : toolbox),
|
||||
moduleData: { ...current.moduleData, ...nextModuleData }
|
||||
};
|
||||
});
|
||||
return saved ? result : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function updateToolbox(nextToolbox) {
|
||||
|
|
@ -301,6 +386,7 @@ export function useToolboxActions({ store, notify, creationTemplate, importConte
|
|||
|
||||
return {
|
||||
createToolbox,
|
||||
addLibraryToolToToolbox,
|
||||
updateToolbox,
|
||||
deleteToolbox,
|
||||
linkToolboxToGame,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue