Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md (setup and step-by-step recipes) and a README in each main folder, with an index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to JSDoc @file blocks, define a three-level header template in website/AGENTS.md and check the header in tests/file-headers.test.mjs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
8.1 KiB
JavaScript
133 lines
8.1 KiB
JavaScript
/**
|
|
* @file Affiche et gère les onglets d'espaces persistants d'une toolbox.
|
|
*/
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { Icon } from "../../components/Icon.jsx";
|
|
import { Modal } from "../../components/Modal.jsx";
|
|
import { useGroupedReorder } from "../../hooks/useGroupedReorder.js";
|
|
import { MAX_TOOLBOX_SPACES } from "./storage/toolboxStorage.js";
|
|
import { createToolboxSpace, deleteToolboxSpace, reorderToolboxSpaces } from "./toolboxSpaceUtils.js";
|
|
|
|
const SPACE_HOVER_DELAY_MS = 600;
|
|
|
|
function SpaceNameModal({ initialName = "", title, text, onClose }) {
|
|
const [name, setName] = useState(initialName);
|
|
return (
|
|
<Modal dialogClassName="confirm-modal nds-modal toolbox-space-modal" labelledBy="toolbox-space-modal-title" onRequestClose={() => onClose("")}>
|
|
<header><h2 id="toolbox-space-modal-title">{title}</h2></header>
|
|
<form onSubmit={(event) => {
|
|
event.preventDefault();
|
|
if (name.trim()) onClose(name.trim());
|
|
}}>
|
|
<label className="field"><span>{text.nameLabel}</span><input className="nds-input" data-size="sm" value={name} onChange={(event) => setName(event.target.value)} autoComplete="off" data-modal-initial-focus="" required /></label>
|
|
<footer className="nds-modal-footer">
|
|
<button className="nds-button" data-size="sm" type="button" onClick={() => onClose("")}>{text.cancelLabel}</button>
|
|
<button className="nds-button" data-size="sm" data-variant="primary" type="submit">{text.saveLabel}</button>
|
|
</footer>
|
|
</form>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
export function ToolboxSpaces({ toolbox, text, moduleReorder, onUpdateToolbox, onHoverActivate, onConfirm }) {
|
|
const [nameModal, setNameModal] = useState(null);
|
|
const activeSpace = toolbox.spaces.find((space) => space.id === toolbox.activeSpaceId) || toolbox.spaces[0];
|
|
const atLimit = toolbox.spaces.length >= MAX_TOOLBOX_SPACES;
|
|
const hoveredSpaceId = toolbox.spaces.find((space) => space.id !== activeSpace.id && moduleReorder.isGroupDropTarget(space.id, toolbox.id))?.id || "";
|
|
const spaceReorder = useGroupedReorder({
|
|
namespace: `toolbox-spaces-${toolbox.id}`,
|
|
items: toolbox.spaces,
|
|
getItemId: (space) => space.id,
|
|
orientation: "horizontal",
|
|
canMoveItem: (operation) => operation.sourceId !== toolbox.defaultSpaceId,
|
|
onItemMove: (operation) => onUpdateToolbox(reorderToolboxSpaces(toolbox, operation.sourceId, operation.targetId, operation.placement))
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!hoveredSpaceId || !moduleReorder.itemReorder.draggingId) return undefined;
|
|
const timeoutId = window.setTimeout(() => onHoverActivate(hoveredSpaceId), SPACE_HOVER_DELAY_MS);
|
|
return () => window.clearTimeout(timeoutId);
|
|
}, [hoveredSpaceId, moduleReorder.itemReorder.draggingId, onHoverActivate]);
|
|
|
|
function saveName(name) {
|
|
if (!name || !nameModal) return setNameModal(null);
|
|
if (nameModal.mode === "create") onUpdateToolbox(createToolboxSpace(toolbox, name));
|
|
else onUpdateToolbox({
|
|
...toolbox,
|
|
spaces: toolbox.spaces.map((space) => space.id === nameModal.spaceId ? { ...space, name } : space)
|
|
});
|
|
setNameModal(null);
|
|
}
|
|
|
|
function requestDelete() {
|
|
const count = activeSpace.moduleOrder.one.length;
|
|
onConfirm({
|
|
title: text.deleteTitle,
|
|
message: count ? text.deleteWithTools.replace("{count}", count) : text.deleteEmpty,
|
|
confirmLabel: text.deleteLabel,
|
|
danger: true,
|
|
onResolve: (confirmed) => {
|
|
if (confirmed) onUpdateToolbox(deleteToolboxSpace(toolbox, activeSpace.id));
|
|
}
|
|
});
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<section className={`toolbox-spaces-toolbar ${toolbox.spaces.length === 1 ? "is-single" : ""}`} aria-label={text.toolbarLabel}>
|
|
{toolbox.spaces.length > 1 && (
|
|
<div className="toolbox-space-tabs-row legacy-scrollbar">
|
|
<div className="toolbox-space-tabs" role="tablist" aria-label={text.tabsLabel}>
|
|
{toolbox.spaces.map((space) => {
|
|
const active = space.id === activeSpace.id;
|
|
const isDefault = space.id === toolbox.defaultSpaceId;
|
|
const isModuleDropTarget = moduleReorder.isGroupDropTarget(space.id, toolbox.id);
|
|
const dropAfter = spaceReorder.getDropPlacement("item", space.id) === "after";
|
|
return (
|
|
<div
|
|
className={`toolbox-space-tab-wrap ${active ? "active" : ""} ${isModuleDropTarget ? "is-module-drop-target" : ""} ${spaceReorder.isItemDragging(space.id) ? "is-dragging" : ""} ${spaceReorder.isItemDropTarget(space.id) ? "is-drop-target" : ""} ${dropAfter ? "drop-after" : ""}`}
|
|
key={space.id}
|
|
role="presentation"
|
|
{...spaceReorder.getItemProps({ itemId: space.id })}
|
|
>
|
|
{!isDefault && <button className="toolbox-space-drag-handle nds-icon-button" data-size="xs" type="button" onPointerDown={(event) => spaceReorder.itemReorder.startDrag(event, space.id)} aria-label={`${text.reorderLabel} ${space.name}`} title={text.reorderLabel}><Icon name="drag" /></button>}
|
|
<button
|
|
className={`toolbox-space-tab ${active ? "active" : ""}`}
|
|
type="button"
|
|
role="tab"
|
|
aria-selected={active}
|
|
tabIndex={active ? 0 : -1}
|
|
onClick={() => onUpdateToolbox({ ...toolbox, activeSpaceId: space.id })}
|
|
onKeyDown={(event) => {
|
|
const index = toolbox.spaces.findIndex((item) => item.id === space.id);
|
|
let nextIndex;
|
|
if (event.key === "ArrowRight") nextIndex = (index + 1) % toolbox.spaces.length;
|
|
else if (event.key === "ArrowLeft") nextIndex = (index - 1 + toolbox.spaces.length) % toolbox.spaces.length;
|
|
else if (event.key === "Home") nextIndex = 0;
|
|
else if (event.key === "End") nextIndex = toolbox.spaces.length - 1;
|
|
else return;
|
|
event.preventDefault();
|
|
const nextSpace = toolbox.spaces[nextIndex];
|
|
onUpdateToolbox({ ...toolbox, activeSpaceId: nextSpace.id });
|
|
event.currentTarget.parentElement?.parentElement?.querySelectorAll('[role="tab"]')[nextIndex]?.focus();
|
|
}}
|
|
{...moduleReorder.getGroupProps({ groupId: space.id, parentId: toolbox.id })}
|
|
>{space.name}</button>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
<button className="toolbox-space-add-compact nds-icon-button" data-size="sm" type="button" disabled={atLimit} onClick={() => !atLimit && setNameModal({ mode: "create" })} aria-label={atLimit ? text.limitReached : text.addLabel} title={atLimit ? text.limitReached : text.addLabel}><Icon name="add" /></button>
|
|
</div>
|
|
)}
|
|
<div className="toolbox-space-actions nds-toolbar">
|
|
{toolbox.spaces.length > 1 && <button className="nds-icon-button" data-size="sm" type="button" onClick={() => setNameModal({ mode: "rename", spaceId: activeSpace.id })} aria-label={text.renameLabel} title={text.renameLabel}><Icon name="edit" /></button>}
|
|
{toolbox.spaces.length > 1 && activeSpace.id !== toolbox.defaultSpaceId && <button className="nds-icon-button" data-size="sm" data-variant="danger" type="button" onClick={requestDelete} aria-label={text.deleteLabel} title={text.deleteLabel}><span className="ui-icon ui-icon-trash" aria-hidden="true" /></button>}
|
|
{toolbox.spaces.length === 1 && <button className="nds-button toolbox-space-add" data-size="sm" data-variant="dashed" type="button" disabled={atLimit} onClick={() => !atLimit && setNameModal({ mode: "create" })} aria-label={atLimit ? text.limitReached : text.addLabel} title={atLimit ? text.limitReached : text.addLabel}><Icon name="add" />{text.addLabel}</button>}
|
|
</div>
|
|
</section>
|
|
{nameModal && <SpaceNameModal initialName={nameModal.mode === "rename" ? activeSpace.name : ""} title={nameModal.mode === "create" ? text.createTitle : text.renameTitle} text={text} onClose={saveName} />}
|
|
</>
|
|
);
|
|
}
|