Qualité & Sécurité / Web (ESLint · angular-eslint + sonarjs) (push) Successful in 44s
Qualité & Sécurité / Trivy (CVE dépendances Maven / pip / npm) (push) Failing after 57s
Qualité & Sécurité / MegaLinter (PMD · Ruff · Bandit · gitleaks · hadolint) (push) Successful in 1m41s
- Correctifs du livret : le résumé des scènes est enfin exporté, libellés
accentués (Thèmes, Récompenses, Résolution, Choix et conséquences), stats
Foundry en français (PV, CA, FOR, FP…), encadrés et longues pages de lore
coupables entre deux pages (fini les grands blancs), plus de date d'export
en couverture, titre/auteur/sujet/langue dans les propriétés du PDF.
- Fiche Publication de la campagne (écran /campaigns/:id/publication) :
sous-titre, auteur, version, illustration de couverture, page technique
(système, niveaux, joueurs, durée), argument, avertissements de contenu,
contexte caché et accroches pour le MJ, crédits et licence. Stockée en JSON
(migration V26), API GET/PUT /api/campaigns/{id}/publication, conservée à
l'export/import de campagne (couverture remappée).
- Dans le PDF : couverture illustrée, page de crédits, introduction (page
technique, l'aventure en bref, contexte MJ, accroches, structure). Fiche
vide : livret de campagne inchangé.
- Options d'export (modale au clic sur « Exporter en PDF ») : version MJ ou
joueurs (aucun secret : notes MJ, conséquences, sorties, combats, contexte),
format A4 / A5 / US Letter, mode impression sans aplats, choix des parties,
lore complet ou seulement les pages liées à la campagne.
- Cinq nouvelles fiches du coach d'écriture sur l'écran Publication
(argument, avertissements, contexte caché, accroches, crédits).
Vérifié : core clean verify (1247 tests, dont contenu du PDF par option,
version joueurs sans secret, propriétés du fichier, format Letter, remap de
la couverture à l'import), web build + 357 vitest, lint sans erreur, essai
dans le navigateur (migration V26 sur une base existante, enregistrement de
la fiche, export joueurs A5).
À redéployer : core + web (migration V26).
671 lines
28 KiB
TypeScript
671 lines
28 KiB
TypeScript
import { Component, OnInit, OnDestroy, DestroyRef } from '@angular/core';
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
import { DataSyncService } from '../../../services/data-sync.service';
|
|
import { CampaignSidebarService } from '../../../services/campaign-sidebar.service';
|
|
|
|
import { ActivatedRoute } from '@angular/router';
|
|
import { FormsModule } from '@angular/forms';
|
|
import { CdkDropList, CdkDrag, CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
|
|
import { LucideAngularModule, Swords, Plus, Globe, Pencil, Trash2, Dices, Drama, Check, Play, Upload, Sparkles, Download, FileText, ChevronDown, ChevronRight, X, Network } from 'lucide-angular';
|
|
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
|
|
import { Router, RouterLink } from '@angular/router';
|
|
import { forkJoin, of, Observable } from 'rxjs';
|
|
import { catchError, switchMap, filter, map } from 'rxjs/operators';
|
|
import { CampaignService, FoundryExportOptions, PdfExportOptions } from '../../../services/campaign.service';
|
|
import { LoreService } from '../../../services/lore.service';
|
|
import { GameSystemService } from '../../../services/game-system.service';
|
|
import { GameSystem } from '../../../services/game-system.model';
|
|
import { NpcService } from '../../../services/npc.service';
|
|
import { RandomTableService } from '../../../services/random-table.service';
|
|
import { EnemyService } from '../../../services/enemy.service';
|
|
import { SessionService } from '../../../services/session.service';
|
|
import { PlaythroughService } from '../../../services/playthrough.service';
|
|
import { Playthrough } from '../../../services/campaign.model';
|
|
import { Session } from '../../../services/session.model';
|
|
import { Npc } from '../../../services/npc.model';
|
|
import { LayoutService } from '../../../services/layout.service';
|
|
import { PageTitleService } from '../../../services/page-title.service';
|
|
import { Campaign, Arc } from '../../../services/campaign.model';
|
|
import { Lore } from '../../../services/lore.model';
|
|
import { loadCampaignTreeData, buildCampaignSidebarConfig, CampaignTreeData } from '../../campaign-tree.helper';
|
|
import { ConfirmDialogService } from '../../../shared/confirm-dialog/confirm-dialog.service';
|
|
import { FolderGroup, groupByFolder, byOrder } from '../../../shared/folder-grouping.util';
|
|
import { CampaignReadinessAssessment } from '../../../services/readiness.model';
|
|
import { ReadinessPanelComponent } from '../../../shared/readiness-panel/readiness-panel.component';
|
|
import { WritingReviewPanelComponent } from '../../../shared/writing-review/writing-review-panel.component';
|
|
import { CampaignPlayBlockComponent } from '../campaign-play-block/campaign-play-block.component';
|
|
import { FoundryExportDialogComponent } from '../foundry-export-dialog/foundry-export-dialog.component';
|
|
import { PdfExportDialogComponent } from '../pdf-export-dialog/pdf-export-dialog.component';
|
|
import { downloadBlob, safeFileName } from '../../../shared/download.util';
|
|
|
|
@Component({
|
|
selector: 'app-campaign-detail',
|
|
imports: [FormsModule, LucideAngularModule, RouterLink, TranslatePipe, CdkDropList, CdkDrag, ReadinessPanelComponent, WritingReviewPanelComponent, CampaignPlayBlockComponent, FoundryExportDialogComponent, PdfExportDialogComponent],
|
|
templateUrl: './campaign-detail.component.html',
|
|
styleUrls: ['./campaign-detail.component.scss']
|
|
})
|
|
export class CampaignDetailComponent implements OnInit, OnDestroy {
|
|
readonly Swords = Swords;
|
|
readonly Plus = Plus;
|
|
readonly Globe = Globe;
|
|
readonly Pencil = Pencil;
|
|
readonly Trash2 = Trash2;
|
|
readonly Dices = Dices;
|
|
readonly Drama = Drama;
|
|
readonly Check = Check;
|
|
readonly Play = Play;
|
|
readonly Upload = Upload;
|
|
readonly Sparkles = Sparkles;
|
|
readonly Download = Download;
|
|
readonly FileText = FileText;
|
|
readonly ChevronDown = ChevronDown;
|
|
readonly ChevronRight = ChevronRight;
|
|
readonly X = X;
|
|
readonly Network = Network;
|
|
|
|
/** Export Foundry en cours (anti double-clic). */
|
|
exportingFoundry = false;
|
|
/** Modale de périmètre de l'export Foundry. */
|
|
foundryDialogOpen = false;
|
|
/** Export PDF en cours (anti double-clic). */
|
|
exportingPdf = false;
|
|
/** Modale des options du livret PDF. */
|
|
pdfDialogOpen = false;
|
|
/** Fiche Publication remplie (sinon la modale propose de la compléter). */
|
|
hasPublication = true;
|
|
|
|
campaign: Campaign | null = null;
|
|
arcs: Arc[] = [];
|
|
/** Nombre de chapitres par arc — alimente le compteur des cartes. */
|
|
chapterCountByArc: Record<string, number> = {};
|
|
/** Lore associé si `campaign.loreId` est renseigné ; sinon null. */
|
|
linkedLore: Lore | null = null;
|
|
/** Lores disponibles pour changer l'association en mode édition. */
|
|
availableLores: Lore[] = [];
|
|
/** GameSystems disponibles pour changer l'association en mode édition. */
|
|
availableGameSystems: GameSystem[] = [];
|
|
/** GameSystem associé si `campaign.gameSystemId` est renseigné ; sinon null. */
|
|
linkedGameSystem: GameSystem | null = null;
|
|
/** Fiches de personnages non-joueurs (PNJ) de la campagne. */
|
|
npcs: Npc[] = [];
|
|
/** PNJ groupés par dossier (dossiers alpha, ordre manuel dans chacun) — comme la sidebar. */
|
|
npcGroups: FolderGroup<Npc>[] = [];
|
|
|
|
/** En-tête : description repliée par défaut (clamp), dépliable via « Voir plus ». */
|
|
descExpanded = false;
|
|
|
|
/** Dossiers de PNJ repliés (section Personnages) — par nom ('' = « Sans dossier »). */
|
|
collapsedNpcFolders = new Set<string>();
|
|
|
|
/** Renommage inline d'une partie : id en cours d'édition + valeur saisie. */
|
|
editingPlaythroughId: string | null = null;
|
|
editPlaythroughName = '';
|
|
/** Sessions de jeu (passées et en cours) liées à cette campagne. */
|
|
sessions: Session[] = [];
|
|
|
|
/** Parties (Playthroughs) de cette campagne. */
|
|
playthroughs: Playthrough[] = [];
|
|
|
|
/** Bilan de préparation (Pilier B — guidage) : alimente le panneau + les pastilles. */
|
|
readiness: CampaignReadinessAssessment | null = null;
|
|
|
|
/** Mode édition inline. */
|
|
editing = false;
|
|
editName = '';
|
|
editDescription = '';
|
|
editLoreId = '';
|
|
editGameSystemId = '';
|
|
|
|
/** Valeur sentinelle de l'option "Creer un systeme" dans le <select>. */
|
|
readonly CREATE_GAMESYSTEM_SENTINEL = '__create__';
|
|
/** Mode creation inline d'un GameSystem depuis le dropdown d'edition. */
|
|
creatingGameSystem = false;
|
|
newGameSystemName = '';
|
|
creatingGameSystemInFlight = false;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private campaignService: CampaignService,
|
|
private loreService: LoreService,
|
|
private gameSystemService: GameSystemService,
|
|
private npcService: NpcService,
|
|
private randomTableService: RandomTableService,
|
|
private enemyService: EnemyService,
|
|
private sessionService: SessionService,
|
|
private playthroughService: PlaythroughService,
|
|
private layoutService: LayoutService,
|
|
private pageTitleService: PageTitleService,
|
|
private confirmDialog: ConfirmDialogService,
|
|
private translate: TranslateService,
|
|
private dataSync: DataSyncService,
|
|
private campaignSidebar: CampaignSidebarService,
|
|
private destroyRef: DestroyRef
|
|
) {}
|
|
|
|
ngOnInit(): void {
|
|
// Synchro temps réel : si l'ordre des arcs change ailleurs (arbre de la sidebar),
|
|
// on recharge les cartes pour rester cohérent sans rafraîchir la page.
|
|
this.dataSync.onChange(this.destroyRef, () => {
|
|
if (this.campaign?.id) {
|
|
this.campaignService.getArcs(this.campaign.id).subscribe(a =>
|
|
this.arcs = [...a].sort(byOrder).filter(x => x.type !== 'SYSTEM'));
|
|
this.loadNpcs(this.campaign.id); // PNJ regroupés/ordonnés
|
|
this.campaignSidebar.show(this.campaign.id); // recharge l'arbre aussi
|
|
}
|
|
});
|
|
|
|
// switchMap annule automatiquement le load précédent si l'utilisateur
|
|
// change de campagne avant que le forkJoin ne réponde — évite qu'une
|
|
// réponse en retard écrase des données plus récentes (race condition).
|
|
this.route.paramMap.pipe(
|
|
map(pm => pm.get('id')),
|
|
filter((id): id is string => !!id && id !== this.campaign?.id),
|
|
switchMap(id => this.loadCampaignBundle(id)),
|
|
takeUntilDestroyed(this.destroyRef)
|
|
).subscribe(result => this.applyCampaignBundle(result));
|
|
}
|
|
|
|
private computeChapterCounts(data: CampaignTreeData): Record<string, number> {
|
|
const counts: Record<string, number> = {};
|
|
for (const arcId of Object.keys(data.chaptersByArc)) {
|
|
counts[arcId] = data.chaptersByArc[arcId].length;
|
|
}
|
|
return counts;
|
|
}
|
|
|
|
/**
|
|
* Recharge explicitement après une mise à jour locale (ex: saveEdit).
|
|
* Contrairement au flux ngOnInit, on bypass le filter sur l'ID puisqu'on
|
|
* veut rafraîchir même si l'ID n'a pas changé.
|
|
*/
|
|
private reload(id: string): void {
|
|
this.loadCampaignBundle(id).subscribe(result => this.applyCampaignBundle(result));
|
|
}
|
|
|
|
/** Charge en un seul forkJoin : la campagne, ses voisines, l'arbre et les parties. */
|
|
private loadCampaignBundle(id: string) {
|
|
return forkJoin({
|
|
campaign: this.campaignService.getCampaignById(id),
|
|
allCampaigns: this.campaignService.getAllCampaigns(),
|
|
treeData: loadCampaignTreeData(this.campaignService, id, this.npcService, this.randomTableService, this.enemyService).pipe(
|
|
catchError(() => of({ arcs: [], chaptersByArc: {}, scenesByChapter: {}, npcs: [], randomTables: [], enemies: [] } as CampaignTreeData))
|
|
),
|
|
playthroughs: this.playthroughService.listByCampaign(id).pipe(catchError(() => of([] as Playthrough[])))
|
|
});
|
|
}
|
|
|
|
/** Applique le bundle chargé à l'état du composant (commun à ngOnInit et reload). */
|
|
private applyCampaignBundle(
|
|
{ campaign, allCampaigns, treeData, playthroughs }:
|
|
{ campaign: Campaign; allCampaigns: Campaign[]; treeData: CampaignTreeData; playthroughs: Playthrough[] }
|
|
): void {
|
|
this.campaign = campaign;
|
|
this.editing = false;
|
|
this.playthroughs = playthroughs;
|
|
// Le bilan complet arrive avec l'arbre agrégé — plus de fetch séparé.
|
|
this.readiness = treeData.readiness ?? null;
|
|
this.loadLinkedLore(campaign);
|
|
this.loadLinkedGameSystem(campaign);
|
|
this.loadNpcs(campaign.id!);
|
|
this.loadSessions(campaign.id!);
|
|
// L'arc SYSTEM (« Quêtes libres ») est de la plomberie : pas de carte d'arc pour lui.
|
|
this.arcs = [...treeData.arcs].sort(byOrder).filter(a => a.type !== 'SYSTEM');
|
|
this.chapterCountByArc = this.computeChapterCounts(treeData);
|
|
this.showLayout(allCampaigns, treeData);
|
|
this.pageTitleService.set(campaign.name);
|
|
this.openEditFromQueryParam();
|
|
}
|
|
|
|
/**
|
|
* `?edit=system` (lien « Choisir le système » de l'onglet Règles en séance) : ouvre
|
|
* directement l'édition inline, puis retire le paramètre pour qu'un rechargement
|
|
* ne la rouvre pas.
|
|
*/
|
|
private openEditFromQueryParam(): void {
|
|
if (!this.route.snapshot.queryParamMap.has('edit')) return;
|
|
this.startEdit();
|
|
this.router.navigate([], { relativeTo: this.route, queryParams: { edit: null }, queryParamsHandling: 'merge', replaceUrl: true });
|
|
}
|
|
|
|
/**
|
|
* Charge le Lore associé (si loreId présent). On swallow l'erreur :
|
|
* si le Lore a été supprimé entre-temps, on affiche simplement "Univers introuvable".
|
|
*/
|
|
private loadLinkedLore(campaign: Campaign): void {
|
|
if (!campaign.loreId) {
|
|
this.linkedLore = null;
|
|
return;
|
|
}
|
|
this.loreService.getLoreById(campaign.loreId).pipe(
|
|
catchError(() => of(null))
|
|
).subscribe(lore => this.linkedLore = lore);
|
|
}
|
|
|
|
/** Même logique pour le GameSystem associé : dégradation si supprimé. */
|
|
private loadLinkedGameSystem(campaign: Campaign): void {
|
|
if (!campaign.gameSystemId) {
|
|
this.linkedGameSystem = null;
|
|
return;
|
|
}
|
|
this.gameSystemService.getById(campaign.gameSystemId).pipe(
|
|
catchError(() => of(null))
|
|
).subscribe(gs => this.linkedGameSystem = gs);
|
|
}
|
|
|
|
/** Charge les PNJ de la campagne (les PJ vivent désormais dans une Partie). */
|
|
private loadNpcs(campaignId: string): void {
|
|
this.npcService.getByCampaign(campaignId).pipe(
|
|
catchError(() => of([] as Npc[]))
|
|
).subscribe(list => {
|
|
this.npcs = list;
|
|
this.npcGroups = groupByFolder(list);
|
|
});
|
|
}
|
|
|
|
// Sessions retirées de cette vue (vivent dans Playthrough Detail).
|
|
private loadSessions(_campaignId: string): void { this.sessions = []; }
|
|
|
|
// ─────────────── Playthroughs (Parties) ───────────────
|
|
|
|
openPlaythrough(p: Playthrough): void {
|
|
this.router.navigate(['/campaigns', this.campaign?.id, 'playthroughs', p.id]);
|
|
}
|
|
|
|
/** Démarre le renommage inline d'une partie (sans déclencher l'ouverture de la carte). */
|
|
startRenamePlaythrough(p: Playthrough, event: Event): void {
|
|
event.stopPropagation();
|
|
this.editingPlaythroughId = p.id ?? null;
|
|
this.editPlaythroughName = p.name;
|
|
}
|
|
|
|
savePlaythroughRename(p: Playthrough): void {
|
|
const name = this.editPlaythroughName.trim();
|
|
if (!name || !p.id) { this.editingPlaythroughId = null; return; }
|
|
this.playthroughService.update(p.id, { ...p, name }).subscribe({
|
|
next: updated => { p.name = updated.name; this.editingPlaythroughId = null; },
|
|
error: () => { this.editingPlaythroughId = null; }
|
|
});
|
|
}
|
|
|
|
cancelPlaythroughRename(): void {
|
|
this.editingPlaythroughId = null;
|
|
}
|
|
|
|
/** Replie / déplie un dossier de PNJ (section Personnages). */
|
|
toggleNpcFolder(folder: string): void {
|
|
if (this.collapsedNpcFolders.has(folder)) this.collapsedNpcFolders.delete(folder);
|
|
else this.collapsedNpcFolders.add(folder);
|
|
}
|
|
|
|
isNpcFolderCollapsed(folder: string): boolean {
|
|
return this.collapsedNpcFolders.has(folder);
|
|
}
|
|
|
|
/** Crée une nouvelle Partie avec un nom par défaut, puis y navigue. */
|
|
newPlaythroughInFlight = false;
|
|
createPlaythrough(): void {
|
|
if (!this.campaign || this.newPlaythroughInFlight) return;
|
|
this.newPlaythroughInFlight = true;
|
|
const defaultName = this.translate.instant('campaignDetail.defaultPlaythroughName', { n: this.playthroughs.length + 1 });
|
|
this.playthroughService.create({
|
|
campaignId: this.campaign.id!,
|
|
name: defaultName
|
|
}).subscribe({
|
|
next: created => {
|
|
this.newPlaythroughInFlight = false;
|
|
this.router.navigate(['/campaigns', this.campaign?.id, 'playthroughs', created.id]);
|
|
},
|
|
error: () => { this.newPlaythroughInFlight = false; }
|
|
});
|
|
}
|
|
|
|
createNpc(): void {
|
|
if (!this.campaign) return;
|
|
this.router.navigate(['/campaigns', this.campaign.id, 'npcs', 'create']);
|
|
}
|
|
|
|
editNpc(npc: Npc): void {
|
|
if (!this.campaign || !npc.id) return;
|
|
this.router.navigate(['/campaigns', this.campaign.id, 'npcs', npc.id, 'edit']);
|
|
}
|
|
|
|
viewNpc(npc: Npc): void {
|
|
if (!this.campaign || !npc.id) return;
|
|
this.router.navigate(['/campaigns', this.campaign.id, 'npcs', npc.id]);
|
|
}
|
|
|
|
createArc(): void {
|
|
if (!this.campaign) return;
|
|
this.router.navigate(['/campaigns', this.campaign.id, 'arcs', 'create']);
|
|
}
|
|
|
|
// ─────────────── Ajout rapide d'une scène (conteneur par défaut) ───────────────
|
|
// Niveau 0 : on ne force plus à créer un arc puis un chapitre avant de poser une
|
|
// scène. Si la campagne n'a pas encore de structure, on provisionne un
|
|
// « Arc principal / Chapitre 1 » implicite (masqué dans l'arbre tant qu'il est
|
|
// seul), puis on ouvre la création de scène. Visible seulement quand la
|
|
// structure est simple (vide, ou un seul arc d'au plus un chapitre).
|
|
quickSceneInFlight = false;
|
|
|
|
get showQuickAddScene(): boolean {
|
|
if (this.arcs.length === 0) return true;
|
|
if (this.arcs.length === 1) return (this.chapterCountByArc[this.arcs[0].id!] ?? 0) <= 1;
|
|
return false;
|
|
}
|
|
|
|
quickAddScene(): void {
|
|
if (!this.campaign || this.quickSceneInFlight) return;
|
|
this.quickSceneInFlight = true;
|
|
const campaignId = this.campaign.id!;
|
|
this.ensureDefaultChapter(campaignId).subscribe({
|
|
next: ({ arcId, chapterId }) => {
|
|
this.quickSceneInFlight = false;
|
|
this.router.navigate(['/campaigns', campaignId, 'arcs', arcId, 'chapters', chapterId, 'scenes', 'create']);
|
|
},
|
|
error: () => {
|
|
this.quickSceneInFlight = false;
|
|
console.error('Erreur lors de la préparation de la scène rapide');
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Garantit l'existence d'un chapitre cible pour une scène : réutilise le premier
|
|
* arc / chapitre s'ils existent, sinon crée « Arc principal » puis « Chapitre 1 ».
|
|
*/
|
|
private ensureDefaultChapter(campaignId: string): Observable<{ arcId: string; chapterId: string }> {
|
|
const newChapter = (arcId: string) => this.campaignService.createChapter({
|
|
name: this.translate.instant('campaignDetail.defaultChapterName'),
|
|
description: '', arcId, order: 1, icon: null
|
|
}).pipe(map(ch => ({ arcId, chapterId: ch.id! })));
|
|
|
|
const firstArc = this.arcs[0];
|
|
if (!firstArc) {
|
|
return this.campaignService.createArc({
|
|
name: this.translate.instant('campaignDetail.defaultArcName'),
|
|
description: '', campaignId, order: 1, type: 'LINEAR', icon: null
|
|
}).pipe(switchMap(arc => newChapter(arc.id!)));
|
|
}
|
|
return this.campaignService.getChapters(firstArc.id!).pipe(
|
|
switchMap(chs => chs.length
|
|
? of({ arcId: firstArc.id!, chapterId: chs[0].id! })
|
|
: newChapter(firstArc.id!))
|
|
);
|
|
}
|
|
|
|
/** Ouvre la modale de choix du périmètre de l'export Foundry. */
|
|
exportFoundry(): void {
|
|
if (!this.campaign?.id || this.exportingFoundry) return;
|
|
this.foundryDialogOpen = true;
|
|
}
|
|
|
|
/** Télécharge le bundle Foundry (périmètre choisi) via un lien temporaire. */
|
|
onFoundryExportConfirmed(opts: FoundryExportOptions): void {
|
|
if (!this.campaign?.id || this.exportingFoundry) return;
|
|
this.exportingFoundry = true;
|
|
this.campaignService.exportFoundry(this.campaign.id, opts).subscribe({
|
|
next: (blob) => {
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = 'foundry-export.zip';
|
|
// Certains navigateurs ignorent un click() sur un <a> détaché : on l'attache
|
|
// au DOM le temps du déclenchement, puis on nettoie.
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
// Révocation différée : libère l'URL sans annuler le téléchargement en cours.
|
|
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
this.exportingFoundry = false;
|
|
this.foundryDialogOpen = false;
|
|
},
|
|
error: (err) => {
|
|
// Ne pas avaler l'erreur en silence : visible en console pour diagnostic.
|
|
console.error('Échec de l\'export Foundry', err);
|
|
this.exportingFoundry = false;
|
|
this.foundryDialogOpen = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Ouvre le graphe des liens : PNJ de la campagne reliés aux pages de son Lore. */
|
|
openGraph(): void {
|
|
if (this.campaign?.id) {
|
|
this.router.navigate(['/campaigns', this.campaign.id, 'graph']);
|
|
}
|
|
}
|
|
|
|
/** Ouvre la modale d'options du livret PDF (en vérifiant si la fiche Publication est remplie). */
|
|
exportPdf(): void {
|
|
if (!this.campaign?.id || this.exportingPdf) return;
|
|
this.pdfDialogOpen = true;
|
|
this.campaignService.getPublication(this.campaign.id).subscribe({
|
|
next: (p) => this.hasPublication = Object.values(p ?? {}).some(v => typeof v === 'string' && v.trim().length > 0),
|
|
error: () => this.hasPublication = true // inconnu : pas d'incitation à tort
|
|
});
|
|
}
|
|
|
|
/** Génère et télécharge le livret PDF selon les options choisies. */
|
|
onPdfExportConfirmed(opts: PdfExportOptions): void {
|
|
if (!this.campaign?.id || this.exportingPdf) return;
|
|
this.exportingPdf = true;
|
|
const name = this.campaign.name;
|
|
this.campaignService.exportPdf(this.campaign.id, opts).subscribe({
|
|
next: (blob) => {
|
|
const suffix = opts.edition === 'player' ? '_joueurs' : '';
|
|
downloadBlob(blob, `${safeFileName(name, 'campagne')}${suffix}.pdf`);
|
|
this.exportingPdf = false;
|
|
this.pdfDialogOpen = false;
|
|
},
|
|
error: (err) => {
|
|
console.error('Échec de l\'export PDF', err);
|
|
this.exportingPdf = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Depuis la modale PDF : compléter la fiche Publication (couverture, crédits…). */
|
|
openPublication(): void {
|
|
if (!this.campaign?.id) return;
|
|
this.pdfDialogOpen = false;
|
|
this.router.navigate(['/campaigns', this.campaign.id, 'publication']);
|
|
}
|
|
|
|
/** Réordonne les arcs par glisser-déposer et persiste le nouvel ordre. */
|
|
dropArc(event: CdkDragDrop<Arc[]>): void {
|
|
if (event.previousIndex === event.currentIndex) return;
|
|
moveItemInArray(this.arcs, event.previousIndex, event.currentIndex);
|
|
this.campaignService.reorderArcs(this.arcs.map(a => a.id!)).subscribe({
|
|
next: () => this.dataSync.notify(),
|
|
error: () => {
|
|
if (this.campaign?.id) {
|
|
this.campaignService.getArcs(this.campaign.id).subscribe(
|
|
a => this.arcs = [...a].sort((x, y) => (x.order ?? 0) - (y.order ?? 0)));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
openArc(arc: Arc): void {
|
|
if (!this.campaign || !arc.id) return;
|
|
this.router.navigate(['/campaigns', this.campaign.id, 'arcs', arc.id]);
|
|
}
|
|
|
|
/**
|
|
* Extrait une ligne de resume pour la fiche PJ/PNJ — 1re valeur de template
|
|
* non-vide (apres refonte 2026-04-30, remplace l'ancien parsing markdown).
|
|
*/
|
|
personaSnippet(p: { values?: Record<string, string> }): string {
|
|
const values = p.values ?? {};
|
|
for (const v of Object.values(values)) {
|
|
if (!v) continue;
|
|
const firstMeaningful = v
|
|
.split('\n')
|
|
.map(l => l.trim())
|
|
.find(l => l && !l.startsWith('#'));
|
|
if (!firstMeaningful) continue;
|
|
return firstMeaningful.length > 80
|
|
? firstMeaningful.substring(0, 77) + '…'
|
|
: firstMeaningful;
|
|
}
|
|
return '(Fiche vide)';
|
|
}
|
|
|
|
private showLayout(allCampaigns: Campaign[], data: CampaignTreeData): void {
|
|
this.layoutService.show(buildCampaignSidebarConfig(
|
|
this.campaign!, allCampaigns, data, this.campaign!.id!, this.translate, this.readiness?.gaps ?? []));
|
|
}
|
|
|
|
// ─────────────── Édition / suppression de la Campagne ───────────────
|
|
|
|
startEdit(): void {
|
|
if (!this.campaign) return;
|
|
this.editName = this.campaign.name;
|
|
this.editDescription = this.campaign.description ?? '';
|
|
this.editLoreId = this.campaign.loreId ?? '';
|
|
this.editGameSystemId = this.campaign.gameSystemId ?? '';
|
|
// On charge les Lores et GameSystems disponibles uniquement à l'entrée en mode édition.
|
|
this.loreService.getAllLores().subscribe({
|
|
next: (lores) => this.availableLores = lores,
|
|
error: () => this.availableLores = []
|
|
});
|
|
this.gameSystemService.getAll().subscribe({
|
|
next: (gs) => this.availableGameSystems = gs,
|
|
error: () => this.availableGameSystems = []
|
|
});
|
|
this.editing = true;
|
|
}
|
|
|
|
cancelEdit(): void {
|
|
this.editing = false;
|
|
this.creatingGameSystem = false;
|
|
this.newGameSystemName = '';
|
|
}
|
|
|
|
/** Detecte la selection de l'option sentinelle dans le <select> GameSystem. */
|
|
onEditGameSystemChange(value: string): void {
|
|
if (value === this.CREATE_GAMESYSTEM_SENTINEL) {
|
|
this.editGameSystemId = '';
|
|
this.startCreateGameSystem();
|
|
}
|
|
}
|
|
|
|
startCreateGameSystem(): void {
|
|
this.creatingGameSystem = true;
|
|
this.newGameSystemName = '';
|
|
}
|
|
|
|
cancelCreateGameSystem(): void {
|
|
this.creatingGameSystem = false;
|
|
this.newGameSystemName = '';
|
|
}
|
|
|
|
submitCreateGameSystem(): void {
|
|
const name = this.newGameSystemName.trim();
|
|
if (!name || this.creatingGameSystemInFlight) return;
|
|
this.creatingGameSystemInFlight = true;
|
|
this.gameSystemService.create({ name, isPublic: false }).subscribe({
|
|
next: (created) => {
|
|
this.creatingGameSystemInFlight = false;
|
|
this.availableGameSystems = [...this.availableGameSystems, created];
|
|
if (created.id) {
|
|
this.editGameSystemId = created.id;
|
|
}
|
|
this.creatingGameSystem = false;
|
|
this.newGameSystemName = '';
|
|
},
|
|
error: () => {
|
|
this.creatingGameSystemInFlight = false;
|
|
console.error('Erreur lors de la creation du systeme de jeu');
|
|
}
|
|
});
|
|
}
|
|
|
|
saveEdit(): void {
|
|
if (!this.campaign || !this.editName.trim()) return;
|
|
const newGameSystemId = this.editGameSystemId ? this.editGameSystemId : null;
|
|
const currentGameSystemId = this.campaign.gameSystemId ?? null;
|
|
const gameSystemChanged = newGameSystemId !== currentGameSystemId;
|
|
const hasSheets = this.npcs.length > 0;
|
|
if (gameSystemChanged && hasSheets) {
|
|
const count = this.npcs.length;
|
|
this.confirmDialog.confirm({
|
|
title: this.translate.instant('campaignDetail.gameSystemChange.title'),
|
|
message: this.translate.instant('campaignDetail.gameSystemChange.message'),
|
|
details: [
|
|
this.translate.instant('campaignDetail.gameSystemChange.detailSheets', { n: count }),
|
|
this.translate.instant('campaignDetail.gameSystemChange.detailFields'),
|
|
this.translate.instant('campaignDetail.gameSystemChange.detailStored')
|
|
],
|
|
confirmLabel: this.translate.instant('campaignDetail.gameSystemChange.confirm'),
|
|
variant: 'warning'
|
|
}).then(ok => { if (ok) this.persistEdit(newGameSystemId); });
|
|
return;
|
|
}
|
|
this.persistEdit(newGameSystemId);
|
|
}
|
|
|
|
private persistEdit(newGameSystemId: string | null): void {
|
|
if (!this.campaign) return;
|
|
this.campaignService.updateCampaign(this.campaign.id!, {
|
|
name: this.editName.trim(),
|
|
description: this.editDescription,
|
|
playerCount: this.campaign.playerCount ?? 0,
|
|
loreId: this.editLoreId ? this.editLoreId : null,
|
|
gameSystemId: newGameSystemId
|
|
}).subscribe({
|
|
next: (updated) => {
|
|
this.campaign = updated;
|
|
this.editing = false;
|
|
// Recharge pour actualiser le badge "Univers" et le titre sidebar.
|
|
this.reload(updated.id!);
|
|
},
|
|
error: () => console.error('Erreur lors de la mise à jour de la campagne')
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Suppression en cascade : récupère d'abord le détail de ce qui sera effacé
|
|
* (arcs / chapitres / scènes / personnages), affiche le récapitulatif dans
|
|
* la confirmation, puis supprime. Le cascade est orchestré côté backend dans
|
|
* une seule transaction.
|
|
*/
|
|
deleteCampaign(): void {
|
|
if (!this.campaign) return;
|
|
const campaign = this.campaign;
|
|
this.campaignService.getCampaignDeletionImpact(campaign.id!).subscribe({
|
|
next: impact => {
|
|
const parts: string[] = [];
|
|
if (impact.arcs > 0) parts.push(this.translate.instant(impact.arcs > 1 ? 'campaignDetail.delete.arcsPlural' : 'campaignDetail.delete.arcs', { n: impact.arcs }));
|
|
if (impact.chapters > 0) parts.push(this.translate.instant(impact.chapters > 1 ? 'campaignDetail.delete.chaptersPlural' : 'campaignDetail.delete.chapters', { n: impact.chapters }));
|
|
if (impact.scenes > 0) parts.push(this.translate.instant(impact.scenes > 1 ? 'campaignDetail.delete.scenesPlural' : 'campaignDetail.delete.scenes', { n: impact.scenes }));
|
|
if (impact.playthroughs > 0) parts.push(this.translate.instant(impact.playthroughs > 1 ? 'campaignDetail.delete.playthroughsPlural' : 'campaignDetail.delete.playthroughs', { n: impact.playthroughs }));
|
|
|
|
const details: string[] = [];
|
|
if (parts.length) details.push(this.translate.instant('campaignDetail.delete.alsoDeletes', { items: parts.join(', ') }));
|
|
details.push(this.translate.instant('campaignDetail.delete.irreversible'));
|
|
|
|
this.confirmDialog.confirm({
|
|
title: this.translate.instant('campaignDetail.delete.title'),
|
|
message: this.translate.instant('campaignDetail.delete.message', { name: campaign.name }),
|
|
confirmLabel: this.translate.instant('common.delete'),
|
|
details,
|
|
variant: 'danger'
|
|
}).then(ok => {
|
|
if (!ok) return;
|
|
this.campaignService.deleteCampaign(campaign.id!).subscribe({
|
|
next: () => this.router.navigate(['/campaigns']),
|
|
error: () => console.error('Erreur lors de la suppression de la campagne')
|
|
});
|
|
});
|
|
},
|
|
error: () => console.error('Impossible de récupérer les dépendances de la campagne')
|
|
});
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.layoutService.hide();
|
|
}
|
|
}
|