feat: Permettre à l'enseignant de rédiger avec un éditeur riche et joindre des fichiers

Les enseignants avaient besoin de consignes plus claires pour les élèves :
le champ description en texte brut ne permettait ni mise en forme ni
partage de documents. Cette limitation obligeait à décrire verbalement
les ressources au lieu de les joindre directement.

L'éditeur WYSIWYG (TipTap) remplace le textarea avec gras, italique,
listes et liens. Le contenu HTML est sanitisé côté backend via
symfony/html-sanitizer pour prévenir les injections XSS. Les pièces
jointes (PDF, JPEG, PNG, max 10 Mo) sont uploadées via une API dédiée
avec validation MIME côté domaine et protection path-traversal sur le
téléchargement. Les descriptions en texte brut existantes restent
lisibles sans migration de données.
This commit is contained in:
2026-03-24 16:08:23 +01:00
parent 93baeb1eaa
commit ab835e5c3d
26 changed files with 2655 additions and 33 deletions

View File

@@ -0,0 +1,276 @@
<script lang="ts">
const ACCEPTED_TYPES = ['application/pdf', 'image/jpeg', 'image/png'];
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 Mo
interface UploadedFile {
id: string;
filename: string;
fileSize: number;
mimeType: string;
}
let {
existingFiles = [],
onUpload,
onDelete,
disabled = false
}: {
existingFiles?: UploadedFile[];
onUpload: (file: File) => Promise<UploadedFile>;
onDelete: (fileId: string) => Promise<void>;
disabled?: boolean;
} = $props();
let files = $state<UploadedFile[]>(existingFiles);
let pendingFiles = $state<{ name: string; size: number }[]>([]);
let error = $state<string | null>(null);
let fileInput: HTMLInputElement;
$effect(() => {
files = existingFiles;
});
function formatFileSize(bytes: number): string {
if (bytes < 1024) return `${bytes} o`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} Ko`;
return `${(bytes / (1024 * 1024)).toFixed(1)} Mo`;
}
function getFileIcon(mimeType: string): string {
if (mimeType === 'application/pdf') return '📄';
if (mimeType.startsWith('image/')) return '🖼️';
return '📎';
}
function validateFile(file: File): string | null {
if (!ACCEPTED_TYPES.includes(file.type)) {
return `Type de fichier non accepté : ${file.type}. Types autorisés : PDF, JPEG, PNG.`;
}
if (file.size > MAX_FILE_SIZE) {
return `Le fichier dépasse la taille maximale de 10 Mo (${formatFileSize(file.size)}).`;
}
return null;
}
async function handleFileSelect(event: Event) {
const input = event.target as HTMLInputElement;
const selectedFiles = input.files;
if (!selectedFiles || selectedFiles.length === 0) return;
error = null;
for (const file of selectedFiles) {
const validationError = validateFile(file);
if (validationError) {
error = validationError;
continue;
}
pendingFiles = [...pendingFiles, { name: file.name, size: file.size }];
try {
const uploaded = await onUpload(file);
files = [...files, uploaded];
} catch {
error = `Erreur lors de l'envoi de "${file.name}".`;
} finally {
pendingFiles = pendingFiles.filter((p) => p.name !== file.name);
}
}
input.value = '';
}
async function handleDelete(fileId: string) {
error = null;
try {
await onDelete(fileId);
files = files.filter((f) => f.id !== fileId);
} catch {
error = 'Erreur lors de la suppression du fichier.';
}
}
</script>
<div class="file-upload" class:disabled>
{#if error}
<p class="upload-error" role="alert">{error}</p>
{/if}
{#if files.length > 0 || pendingFiles.length > 0}
<ul class="file-list">
{#each files as file}
<li class="file-item">
<span class="file-icon">{getFileIcon(file.mimeType)}</span>
<span class="file-name">{file.filename}</span>
<span class="file-size">{formatFileSize(file.fileSize)}</span>
{#if !disabled}
<button
type="button"
class="file-remove"
onclick={() => handleDelete(file.id)}
title="Supprimer {file.filename}"
aria-label="Supprimer {file.filename}"
>
</button>
{/if}
</li>
{/each}
{#each pendingFiles as pending}
<li class="file-item file-pending">
<span class="file-icon"></span>
<span class="file-name">{pending.name}</span>
<span class="file-size">{formatFileSize(pending.size)}</span>
<span class="file-uploading">Envoi...</span>
</li>
{/each}
</ul>
{/if}
{#if !disabled}
<button type="button" class="upload-btn" onclick={() => fileInput.click()}>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
<path d="M21.44 11.05l-9.19 9.19a6 6 0 01-8.49-8.49l9.19-9.19a4 4 0 015.66 5.66l-9.2 9.19a2 2 0 01-2.83-2.83l8.49-8.48" />
</svg>
Ajouter un fichier
</button>
<input
bind:this={fileInput}
type="file"
accept=".pdf,.jpg,.jpeg,.png"
onchange={handleFileSelect}
class="file-input-hidden"
aria-hidden="true"
tabindex="-1"
/>
<p class="upload-hint">PDF, JPEG ou PNG — 10 Mo max par fichier</p>
{/if}
</div>
<style>
.file-upload {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.file-upload.disabled {
opacity: 0.6;
}
.upload-error {
margin: 0;
padding: 0.5rem 0.75rem;
background: #fee2e2;
border-radius: 0.375rem;
color: #991b1b;
font-size: 0.8125rem;
}
.file-list {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
gap: 0.375rem;
}
.file-item {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 0.75rem;
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 0.375rem;
font-size: 0.875rem;
}
.file-pending {
opacity: 0.6;
}
.file-icon {
flex-shrink: 0;
}
.file-name {
flex: 1;
color: #374151;
font-weight: 500;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.file-size {
color: #9ca3af;
font-size: 0.75rem;
flex-shrink: 0;
}
.file-uploading {
color: #3b82f6;
font-size: 0.75rem;
flex-shrink: 0;
}
.file-remove {
display: flex;
align-items: center;
justify-content: center;
width: 1.5rem;
height: 1.5rem;
border: none;
border-radius: 50%;
background: #e5e7eb;
color: #6b7280;
font-size: 0.625rem;
cursor: pointer;
flex-shrink: 0;
transition: background-color 0.15s;
}
.file-remove:hover {
background: #fecaca;
color: #dc2626;
}
.upload-btn {
display: inline-flex;
align-items: center;
gap: 0.375rem;
padding: 0.5rem 0.75rem;
border: 1px dashed #d1d5db;
border-radius: 0.375rem;
background: white;
color: #374151;
font-size: 0.875rem;
cursor: pointer;
transition: border-color 0.15s, background-color 0.15s;
align-self: flex-start;
}
.upload-btn:hover {
border-color: #3b82f6;
background: #eff6ff;
color: #2563eb;
}
.file-input-hidden {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
opacity: 0;
}
.upload-hint {
margin: 0;
font-size: 0.75rem;
color: #9ca3af;
}
</style>