Improve toolbox checklists, image browsing, and guide integration
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s

This commit is contained in:
Shinuwa 2026-09-27 22:26:39 +02:00
parent 00e73f0062
commit 386212041f
47 changed files with 744 additions and 112 deletions

View file

@ -0,0 +1,42 @@
// Rôle : affiche la collection d'un outil Images dans la galerie de la visionneuse.
import { useEffect, useRef } from "react";
export function ImageGallery({ images, activeIndex, focusIndex = -1, onSelect, textContent = {} }) {
const galleryRef = useRef(null);
useEffect(() => {
if (focusIndex < 0) return;
const frame = window.requestAnimationFrame(() => {
galleryRef.current?.querySelector(`[data-image-index="${focusIndex}"]`)?.focus();
});
return () => window.cancelAnimationFrame(frame);
}, [focusIndex]);
return (
<div ref={galleryRef} className="image-viewer-gallery themed-scrollbar">
{images.map((image, index) => {
const position = `${index + 1} / ${images.length}`;
const label = image.label || `${textContent.imageAlt || "Image"} ${index + 1}`;
return (
<button
className={`image-viewer-gallery-item nds-card ${index === activeIndex ? "is-active" : ""}`}
data-variant="interactive"
data-image-index={index}
type="button"
key={image.id || `${image.dataUrl}-${index}`}
onClick={() => onSelect(index)}
aria-label={`${textContent.galleryImageAriaLabel || "Afficher en grand"} : ${label}`}
>
<span className="image-viewer-gallery-frame">
<img src={image.dataUrl} alt="" loading="lazy" decoding="async" />
</span>
<span className="image-viewer-gallery-caption">
<strong>{label}</strong>
<small>{position}</small>
</span>
</button>
);
})}
</div>
);
}

View file

@ -1,10 +1,11 @@
// Rôle : affiche les images en modale, avec support des annotations.
// Rôle : affiche en modale une image annotable ou une collection navigable avec galerie.
import { useEffect, useRef, useState } from "react";
import { DrawingControls } from "../features/toolboxes/modules/DrawingControls.jsx";
import { DrawingOverlay } from "../features/toolboxes/modules/DrawingOverlay.jsx";
import { lockBodyScroll } from "../utils/bodyScrollLock.js";
import { DRAWING_COLORS, DRAWING_WIDTHS } from "../utils/drawingOptions.js";
import { Icon } from "./Icon.jsx";
import { ImageGallery } from "./ImageGallery.jsx";
import { Tooltip } from "./Tooltip.jsx";
function clampPercent(value) {
@ -32,27 +33,49 @@ function openImageInNewTab(dataUrl) {
}
}
function formatPositionLabel(template, current, total) {
return String(template || "Image {current} sur {total}")
.replace("{current}", current)
.replace("{total}", total);
}
export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.crypto?.randomUUID?.() || `marker-${Date.now()}` }) {
useEffect(() => lockBodyScroll(), []);
const canAnnotate = Boolean(image.canAnnotate && image.onChangeMarkers);
const canDraw = Boolean(canAnnotate && image.onChangeDrawings);
const [viewerMarkers, setViewerMarkers] = useState(() => Array.isArray(image.markers) ? image.markers : []);
const [viewerDrawings, setViewerDrawings] = useState(() => image.drawings?.strokes ? image.drawings : { strokes: [] });
const [drawingMode, setDrawingMode] = useState(image.drawingMode === "temporary" ? "temporary" : "permanent");
const isCollection = image.kind === "collection" && Array.isArray(image.images) && image.images.length > 0;
const collection = isCollection ? image.images : [image];
const initialIndex = isCollection ? Math.min(collection.length - 1, Math.max(0, Number(image.activeIndex) || 0)) : 0;
const initialView = isCollection && image.initialView === "gallery" ? "gallery" : "detail";
const [activeIndex, setActiveIndex] = useState(initialIndex);
const [view, setView] = useState(initialView);
const [galleryFocusIndex, setGalleryFocusIndex] = useState(-1);
const [previousRequest, setPreviousRequest] = useState(image);
if (previousRequest !== image) {
setPreviousRequest(image);
setActiveIndex(initialIndex);
setView(initialView);
setGalleryFocusIndex(-1);
}
const activeImage = collection[activeIndex] || collection[0] || image;
const textContent = isCollection ? image.textContent || {} : {};
const canAnnotate = Boolean(activeImage.canAnnotate && activeImage.onChangeMarkers);
const canDraw = Boolean(canAnnotate && activeImage.onChangeDrawings);
const [viewerMarkers, setViewerMarkers] = useState(() => Array.isArray(activeImage.markers) ? activeImage.markers : []);
const [viewerDrawings, setViewerDrawings] = useState(() => activeImage.drawings?.strokes ? activeImage.drawings : { strokes: [] });
const [drawingMode, setDrawingMode] = useState(activeImage.drawingMode === "temporary" ? "temporary" : "permanent");
const [drawingActive, setDrawingActive] = useState(false);
const [eraseActive, setEraseActive] = useState(false);
const [drawingColor, setDrawingColor] = useState("#f6c453");
const [drawingWidth, setDrawingWidth] = useState(4);
const [previousImage, setPreviousImage] = useState(image);
const [previousImage, setPreviousImage] = useState(activeImage);
const viewerRef = useRef(null);
const mediaRef = useRef(null);
const hasMarkers = viewerMarkers.length > 0;
if (previousImage !== image) {
setPreviousImage(image);
setViewerMarkers(Array.isArray(image.markers) ? image.markers : []);
setViewerDrawings(image.drawings?.strokes ? image.drawings : { strokes: [] });
setDrawingMode(image.drawingMode === "temporary" ? "temporary" : "permanent");
if (previousImage !== activeImage) {
setPreviousImage(activeImage);
setViewerMarkers(Array.isArray(activeImage.markers) ? activeImage.markers : []);
setViewerDrawings(activeImage.drawings?.strokes ? activeImage.drawings : { strokes: [] });
setDrawingMode(activeImage.drawingMode === "temporary" ? "temporary" : "permanent");
setDrawingActive(false);
setEraseActive(false);
}
@ -76,31 +99,43 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
observer.disconnect();
window.removeEventListener("resize", syncImageHeight);
};
}, [image.dataUrl, canAnnotate]);
}, [activeImage.dataUrl, canAnnotate, view]);
useEffect(() => {
const onKeyDown = (event) => {
if (event.key === "Escape") onClose();
if (event.key === "Escape") {
onClose();
return;
}
if (!isCollection || view !== "detail") return;
if (event.key === "ArrowLeft" && activeIndex > 0) {
event.preventDefault();
setActiveIndex((index) => index - 1);
}
if (event.key === "ArrowRight" && activeIndex < collection.length - 1) {
event.preventDefault();
setActiveIndex((index) => index + 1);
}
};
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
}, [onClose]);
}, [activeIndex, collection.length, isCollection, onClose, view]);
function updateViewerMarkers(nextMarkers) {
setViewerMarkers(nextMarkers);
image.onChangeMarkers?.(nextMarkers);
activeImage.onChangeMarkers?.(nextMarkers);
}
function updateViewerDrawings(nextStrokes) {
const nextDrawings = { strokes: nextStrokes };
setViewerDrawings(nextDrawings);
image.onChangeDrawings?.(nextDrawings, drawingMode);
activeImage.onChangeDrawings?.(nextDrawings, drawingMode);
}
function toggleDrawingMode() {
const nextMode = drawingMode === "permanent" ? "temporary" : "permanent";
setDrawingMode(nextMode);
image.onChangeDrawingMode?.(nextMode, viewerDrawings);
activeImage.onChangeDrawingMode?.(nextMode, viewerDrawings);
}
function getMediaSurfaceSize() {
@ -126,6 +161,19 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
updateViewerMarkers(nextMarkers);
}
function openGallery() {
setGalleryFocusIndex(activeIndex);
setView("gallery");
}
function openGalleryImage(index) {
setGalleryFocusIndex(-1);
setActiveIndex(index);
setView("detail");
}
const positionLabel = formatPositionLabel(textContent.positionLabel, activeIndex + 1, collection.length);
function updateViewerMarker(markerId, updater) {
updateViewerMarkers(viewerMarkers.map((marker) => marker.id === markerId ? updater(marker) : marker));
}
@ -137,17 +185,32 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
return (
<div className="image-viewer-root">
<div className="image-viewer-backdrop" onClick={onClose} />
<section ref={viewerRef} className="image-viewer" role="dialog" aria-modal="true" aria-label="Image">
<section ref={viewerRef} className={`image-viewer ${view === "gallery" ? "is-gallery" : ""}`} role="dialog" aria-modal="true" aria-label={view === "gallery" ? textContent.galleryTitle || "Galerie d'images" : "Image"}>
<header>
{image.label && <strong className="image-viewer-title">{image.label}</strong>}
<div className="image-viewer-heading">
{(view === "gallery" || isCollection || activeImage.label) && (
<strong className="image-viewer-title">{view === "gallery" ? textContent.galleryTitle || "Galerie d'images" : activeImage.label || textContent.imageAlt || "Image"}</strong>
)}
{isCollection && view === "detail" && <span className="image-viewer-position" aria-label={positionLabel} title={positionLabel} aria-live="polite">{activeIndex + 1} / {collection.length}</span>}
</div>
<div className="image-viewer-actions">
{!canAnnotate && !hasMarkers && (
<button className="image-viewer-button nds-icon-button" data-size="sm" onClick={() => openImageInNewTab(image.dataUrl)} aria-label="Ouvrir l'image en taille réelle" title="Taille réelle"><Icon name="zoom" /></button>
{isCollection && view === "detail" && (
<button className="image-viewer-button nds-icon-button" data-size="sm" onClick={openGallery} aria-label={textContent.returnGalleryTitle || "Revenir à la galerie"} title={textContent.returnGalleryTitle || "Revenir à la galerie"}><Icon name="picture" /></button>
)}
{view === "detail" && !canAnnotate && !hasMarkers && (
<button className="image-viewer-button nds-icon-button" data-size="sm" onClick={() => openImageInNewTab(activeImage.dataUrl)} aria-label="Ouvrir l'image en taille réelle" title="Taille réelle"><Icon name="zoom" /></button>
)}
<button className="image-viewer-button nds-icon-button" data-size="sm" onClick={onClose} aria-label="Fermer" title="Fermer"><Icon name="close" /></button>
</div>
</header>
<div className={`image-viewer-body ${canAnnotate ? "has-annotation-side" : ""}`}>
{view === "gallery" ? (
<ImageGallery images={collection} activeIndex={activeIndex} focusIndex={galleryFocusIndex} onSelect={openGalleryImage} textContent={textContent} />
) : <div className={`image-viewer-body ${canAnnotate ? "has-annotation-side" : ""} ${isCollection && collection.length > 1 ? "has-navigation" : ""}`}>
{isCollection && collection.length > 1 && (
<button className="image-viewer-nav image-viewer-nav-previous nds-icon-button" data-size="md" type="button" disabled={activeIndex === 0} onClick={() => setActiveIndex((index) => index - 1)} aria-label={textContent.previousImageTitle || "Image précédente"} title={textContent.previousImageTitle || "Image précédente"}>
<Icon name="chevron-down" />
</button>
)}
<div className={`image-viewer-media ${canAnnotate ? "can-annotate" : ""}`}>
<div
ref={mediaRef}
@ -155,9 +218,9 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
onClick={addViewerMarker}
role={canAnnotate ? "button" : undefined}
tabIndex={canAnnotate ? 0 : undefined}
aria-label={canAnnotate ? image.addMarkerAriaLabel || "Ajouter un marqueur sur l'image" : undefined}
aria-label={canAnnotate ? activeImage.addMarkerAriaLabel || "Ajouter un marqueur sur l'image" : undefined}
>
<img src={image.dataUrl} alt={image.alt || "Image"} />
<img src={activeImage.dataUrl} alt={activeImage.alt || activeImage.label || textContent.imageAlt || "Image"} />
<DrawingOverlay
active={drawingActive}
erase={eraseActive}
@ -166,12 +229,12 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
width={drawingWidth}
onChange={updateViewerDrawings}
getSurfaceSize={getMediaSurfaceSize}
createStrokeId={image.createStrokeId}
createStrokeId={activeImage.createStrokeId}
coordinateMode="percent"
/>
{canDraw && (
<DrawingControls
textContent={image}
textContent={activeImage}
active={drawingActive}
onToggleActive={() => setDrawingActive((value) => !value)}
eraseActive={eraseActive}
@ -193,7 +256,7 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
as="span"
className="annotation-marker image-viewer-marker"
key={marker.id || `${marker.x}-${marker.y}-${index}`}
content={marker.label || `${image.markerPrefix || "Marqueur"} ${index + 1}`}
content={marker.label || `${activeImage.markerPrefix || "Marqueur"} ${index + 1}`}
position="top"
style={{ "--marker-x": `${marker.x}%`, "--marker-y": `${marker.y}%` }}
>
@ -202,6 +265,11 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
))}
</div>
</div>
{isCollection && collection.length > 1 && (
<button className="image-viewer-nav image-viewer-nav-next nds-icon-button" data-size="md" type="button" disabled={activeIndex === collection.length - 1} onClick={() => setActiveIndex((index) => index + 1)} aria-label={textContent.nextImageTitle || "Image suivante"} title={textContent.nextImageTitle || "Image suivante"}>
<Icon name="chevron-down" />
</button>
)}
{canAnnotate && (
<aside className="image-viewer-side">
<strong>{viewerMarkers.length} {viewerMarkers.length > 1 ? "marqueurs" : "marqueur"}</strong>
@ -215,11 +283,11 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
data-size="sm"
type="text"
value={marker.label}
placeholder={image.markerPlaceholder || "Libellé du marqueur"}
placeholder={activeImage.markerPlaceholder || "Libellé du marqueur"}
onChange={(event) => updateViewerMarker(marker.id, (item) => ({ ...item, label: event.target.value }))}
aria-label={`Libellé du marqueur ${index + 1}`}
/>
<button className="tool-split-action-button nds-icon-button" data-size="sm" data-variant="danger" type="button" onClick={() => removeViewerMarker(marker.id)} aria-label={`${image.deleteMarkerTitle || "Supprimer"} ${marker.label || `${image.markerPrefix || "Marqueur"} ${index + 1}`}`} title={image.deleteMarkerTitle || "Supprimer"}>
<button className="tool-split-action-button nds-icon-button" data-size="sm" data-variant="danger" type="button" onClick={() => removeViewerMarker(marker.id)} aria-label={`${activeImage.deleteMarkerTitle || "Supprimer"} ${marker.label || `${activeImage.markerPrefix || "Marqueur"} ${index + 1}`}`} title={activeImage.deleteMarkerTitle || "Supprimer"}>
<Icon name="trash" />
</button>
</li>
@ -230,7 +298,7 @@ export function ImageViewer({ image, onClose, createMarkerId = () => globalThis.
)}
</aside>
)}
</div>
</div>}
</section>
</div>
);

View file

@ -3,12 +3,12 @@ import { Icon } from "./Icon.jsx";
import { ImportButton } from "./ImportButton.jsx";
import { Tooltip } from "./Tooltip.jsx";
export function Shell({ route, content, games, toolboxes, links, drawerGameId = "", actions, children }) {
export function Shell({ route, content, games, drawerGameId = "", actions, children }) {
const gameId = route.split("/")[1] === "games" ? route.split("/")[2] || "" : "";
const game = games.find((item) => item.id === gameId);
const linkedToolbox = game ? toolboxes.find((item) => item.id === links[game.id]) : null;
const topbarLabel = route === "/design-system" && import.meta.env.DEV ? "Design system" : route.startsWith("/toolbox") ? content.topbar.toolbox : route.startsWith("/games") ? content.topbar.games : route === "/library" ? content.topbar.library : content.topbar.dashboard;
const toolboxLabel = linkedToolbox ? `Ouvrir la toolbox ${linkedToolbox.name}` : "Associer une toolbox";
const toolboxOpen = Boolean(game && drawerGameId === game.id);
const toolboxLabel = toolboxOpen ? "Masquer la toolbox" : "Afficher la toolbox";
const compactSidebar = Boolean(drawerGameId);
return (
@ -48,15 +48,16 @@ export function Shell({ route, content, games, toolboxes, links, drawerGameId =
{game && (
<div className="topbar-actions">
<button
className={`drawer-button toolbox-icon-button nds-icon-button ${drawerGameId === game.id ? "active" : ""}`}
className={`drawer-button toolbox-button nds-button ${toolboxOpen ? "active" : ""}`}
data-size="sm"
data-variant="primary"
onClick={() => actions.setDrawerGameId(drawerGameId === game.id ? "" : game.id)}
onClick={() => actions.setDrawerGameId(toolboxOpen ? "" : game.id)}
aria-label={toolboxLabel}
aria-pressed={drawerGameId === game.id}
aria-pressed={toolboxOpen}
title={toolboxLabel}
>
<img src="/static/icons/toolbox.svg" alt="" aria-hidden="true" />
<span>{toolboxLabel}</span>
</button>
</div>
)}