From 763219412b90e80726b0c781f43996918337ac7e Mon Sep 17 00:00:00 2001 From: "IETM_FIXE\\ietm6" Date: Sat, 17 Oct 2026 23:53:23 +0200 Subject: [PATCH] Mode plat : seulement quand l'arc et le chapitre uniques sont vides MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Une méthode guidée lancée dans une campagne vide crée un arc et un chapitre qui portent le contenu écrit (titre, accroche, enjeux, vérité cachée ou plan du méchant en notes MJ). Le mode plat les masquait : ce contenu devenait introuvable derrière le nœud « Scènes ». - Règle commune (flat-structure.ts) : un seul arc linéaire, un seul chapitre, et aucun contenu d'écriture dans l'un ou l'autre (les noms ne comptent pas, « Arc principal » / « Chapitre 1 » étant créés d'office). - Appliquée au volet de gauche, à la page de la campagne (cartes d'arc visibles dès qu'il y a du contenu) et au livret PDF (en-têtes d'arc et de chapitre conservés ; un arc HUB n'est plus jamais mis à plat). Vérifié : core clean verify (1289 tests, dont livret plat / non plat), web build + 860 vitest (règle du mode plat), lint sans erreur. À redéployer : core + web (aucune migration). --- .../transfer/pdf/PdfExportService.java | 32 ++++++++++-- .../transfer/pdf/PdfExportFlatModeTest.java | 49 +++++++++++++++++++ web/src/app/campaigns/campaign-tree.helper.ts | 12 +++-- .../campaign-detail.component.html | 4 +- .../campaign-detail.component.ts | 8 +++ web/src/app/campaigns/flat-structure.spec.ts | 29 +++++++++++ web/src/app/campaigns/flat-structure.ts | 36 ++++++++++++++ 7 files changed, 159 insertions(+), 11 deletions(-) create mode 100644 core/src/test/java/com/loremind/infrastructure/transfer/pdf/PdfExportFlatModeTest.java create mode 100644 web/src/app/campaigns/flat-structure.spec.ts create mode 100644 web/src/app/campaigns/flat-structure.ts diff --git a/core/src/main/java/com/loremind/infrastructure/transfer/pdf/PdfExportService.java b/core/src/main/java/com/loremind/infrastructure/transfer/pdf/PdfExportService.java index b7eec6c..1d02506 100644 --- a/core/src/main/java/com/loremind/infrastructure/transfer/pdf/PdfExportService.java +++ b/core/src/main/java/com/loremind/infrastructure/transfer/pdf/PdfExportService.java @@ -5,6 +5,7 @@ import com.loremind.domain.campaigncontext.bestiary.NpcStory; import com.loremind.domain.campaigncontext.quest.NodeType; import com.loremind.domain.campaigncontext.quest.Prerequisite; import com.loremind.domain.campaigncontext.quest.QuestNodeRef; +import com.loremind.domain.campaigncontext.structure.ArcType; import com.loremind.domain.campaigncontext.structure.Room; import com.loremind.domain.campaigncontext.structure.RoomBranch; import com.loremind.domain.campaigncontext.structure.SceneBranch; @@ -540,11 +541,12 @@ public class PdfExportService { List arcs = st.visibleArcs; if (arcs.isEmpty()) return; - // Mode plat (miroir de l'UI Niveau 0) : 1 arc d'UN SEUL chapitre → on masque les - // niveaux Arc/Chapitre et on présente les scènes À PLAT (cohérent avec la sidebar). - if (arcs.size() == 1) { + // Mode plat (miroir de l'UI Niveau 0) : 1 arc linéaire d'UN SEUL chapitre, tous deux + // VIDES (créés d'office) → on masque les niveaux Arc/Chapitre et on présente les + // scènes À PLAT. Un arc ou un chapitre écrit (méthode guidée…) garde ses en-têtes. + if (arcs.size() == 1 && arcs.get(0).getType() != ArcType.HUB) { List only = st.chaptersByArc.get(arcs.get(0).getId()); - if (only.size() == 1) { + if (only.size() == 1 && isBare(arcs.get(0)) && isBare(only.get(0))) { narrativeFlat(ctx, st, arcs.get(0), only.get(0)); return; } @@ -561,6 +563,28 @@ public class PdfExportService { ctx.endColumns(); } + /** Aucun contenu d'ecriture dans l'arc (son nom ne compte pas : « Arc principal » est cree d'office). */ + static boolean isBare(ArcJpaEntity arc) { + return allBlank(arc.getDescription(), arc.getThemes(), arc.getStakes(), arc.getRewards(), + arc.getResolution(), arc.getGmNotes(), arc.getHiddenTruth(), arc.getHooks()) + && isEmpty(arc.getIllustrationImageIds()); + } + + /** Aucun contenu d'ecriture dans le chapitre (son nom ne compte pas : « Chapitre 1 » est cree d'office). */ + static boolean isBare(ChapterJpaEntity ch) { + return allBlank(ch.getDescription(), ch.getPlayerObjectives(), ch.getNarrativeStakes(), ch.getGmNotes()) + && isEmpty(ch.getIllustrationImageIds()); + } + + private static boolean allBlank(String... values) { + for (String v : values) if (notBlank(v)) return false; + return true; + } + + private static boolean isEmpty(List list) { + return list == null || list.isEmpty(); + } + /** Arborescence des signets Arc -> Chapitre pour la partie narrative complete (mode non plat). */ private void narrativeBookmarks(Ctx ctx, Structure st, List arcs) { StringBuilder arcBms = new StringBuilder(); diff --git a/core/src/test/java/com/loremind/infrastructure/transfer/pdf/PdfExportFlatModeTest.java b/core/src/test/java/com/loremind/infrastructure/transfer/pdf/PdfExportFlatModeTest.java new file mode 100644 index 0000000..ec4e013 --- /dev/null +++ b/core/src/test/java/com/loremind/infrastructure/transfer/pdf/PdfExportFlatModeTest.java @@ -0,0 +1,49 @@ +package com.loremind.infrastructure.transfer.pdf; + +import com.loremind.infrastructure.persistence.entity.*; +import com.loremind.infrastructure.persistence.jpa.*; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Mode plat du livret : un seul arc d'un seul chapitre n'est présenté « à plat » que si + * ces deux conteneurs sont vides. Un chapitre écrit (méthode guidée…) garde ses en-têtes. + */ +@SpringBootTest +@Transactional +class PdfExportFlatModeTest { + + @Autowired private PdfExportService service; + @Autowired private CampaignJpaRepository campaignRepo; + @Autowired private ArcJpaRepository arcRepo; + @Autowired private ChapterJpaRepository chapterRepo; + @Autowired private SceneJpaRepository sceneRepo; + + private String campaign(String chapterGmNotes) { + CampaignJpaEntity camp = campaignRepo.save(CampaignJpaEntity.builder().name("C").arcsCount(1).build()); + ArcJpaEntity arc = arcRepo.save(ArcJpaEntity.builder().name("Arc principal").campaignId(camp.getId()).order(0).build()); + ChapterJpaEntity ch = chapterRepo.save(ChapterJpaEntity.builder().name("L'héritage empoisonné") + .arcId(arc.getId()).order(0).gmNotes(chapterGmNotes).build()); + sceneRepo.save(SceneJpaEntity.builder().name("L'arrestation").chapterId(ch.getId()).order(0).build()); + return String.valueOf(camp.getId()); + } + + @Test + void bareArcAndChapter_areFlattened() { + String x = service.xhtml(campaign(null), PdfExportOptions.defaults()); + assertTrue(x.contains("id=\"part-narrative\">Scènes")); + assertFalse(x.contains("L'héritage empoisonné"), "en-tête de chapitre masqué"); + } + + @Test + void writtenChapter_keepsItsHeaders() { + String x = service.xhtml(campaign("Le notaire est coupable."), PdfExportOptions.defaults()); + assertTrue(x.contains("id=\"part-narrative\">Structure narrative")); + assertTrue(x.contains("L'héritage empoisonné"), "le chapitre de la méthode guidée reste visible"); + assertTrue(x.contains("Le notaire est coupable.")); + } +} diff --git a/web/src/app/campaigns/campaign-tree.helper.ts b/web/src/app/campaigns/campaign-tree.helper.ts index 0317369..d87b433 100644 --- a/web/src/app/campaigns/campaign-tree.helper.ts +++ b/web/src/app/campaigns/campaign-tree.helper.ts @@ -12,6 +12,7 @@ import { Npc } from '../services/npc.model'; import { RandomTable } from '../services/random-table.model'; import { Enemy } from '../services/enemy.model'; import { CampaignReadinessAssessment, ReadinessGap, ReadinessSeverity, ReadinessEntityType } from '../services/readiness.model'; +import { flatContainer } from './flat-structure'; /** * Helper — charge l'arborescence complète d'une campagne (arcs -> chapitres -> scènes) @@ -365,12 +366,13 @@ export function buildCampaignTree( // des scènes et l'ajout restent identiques (c'est le nœud chapitre, relabellisé // et remonté à la racine). Dès qu'un 2e arc OU un 2e chapitre existe, la // structure complète réapparaît automatiquement. - const onlyArc = sortedArcs.length === 1 ? sortedArcs[0] : null; - const onlyArcChapters = onlyArc ? (data.chaptersByArc[onlyArc.id!] ?? []) : []; + // Mode plat seulement si l'arc et le chapitre uniques sont VIDES (cf. flatContainer) : + // un chapitre écrit par une méthode guidée doit rester visible. + const flat = flatContainer(sortedArcs, data.chaptersByArc); + const onlyArc = flat?.arc ?? null; let narrationNodes: TreeItem[] = arcNodes; - // Le mode plat ne s'applique PAS à un arc HUB (qui présente des quêtes, pas juste des scènes). - if (onlyArc && onlyArc.type !== 'HUB' && onlyArcChapters.length === 1) { - const ch = onlyArcChapters[0]; + if (flat && onlyArc) { + const ch = flat.chapter; const sortedScenes = [...(data.scenesByChapter[ch.id!] ?? [])].sort(byOrder); const sceneItems: TreeItem[] = sortedScenes.map(sc => ({ id: `scene-${sc.id}`, diff --git a/web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.html b/web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.html index 7e4bf7f..7fcab15 100644 --- a/web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.html +++ b/web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.html @@ -228,7 +228,7 @@ - @if (arcs.length > 0 && !showQuickAddScene) { + @if (arcs.length > 0 && !flatMode) {
@for (arc of arcs; track arc.id) { @@ -242,7 +242,7 @@ } - @if (arcs.length > 0 && showQuickAddScene) { + @if (arcs.length > 0 && flatMode) {

{{ 'campaignDetail.flatModeHint' | translate }}

diff --git a/web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.ts b/web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.ts index 7ee3ba9..8c9aac6 100644 --- a/web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.ts +++ b/web/src/app/campaigns/campaign/campaign-detail/campaign-detail.component.ts @@ -37,6 +37,7 @@ import { CampaignPlayBlockComponent } from '../campaign-play-block/campaign-play 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'; +import { flatContainer } from '../../flat-structure'; @Component({ selector: 'app-campaign-detail', @@ -212,6 +213,7 @@ export class CampaignDetailComponent implements OnInit, OnDestroy { // 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.flatMode = !!flatContainer(this.arcs, treeData.chaptersByArc); this.showLayout(allCampaigns, treeData); this.pageTitleService.set(campaign.name); this.openEditFromQueryParam(); @@ -348,6 +350,12 @@ export class CampaignDetailComponent implements OnInit, OnDestroy { // structure est simple (vide, ou un seul arc d'au plus un chapitre). quickSceneInFlight = false; + /** + * Mode plat (arc et chapitre uniques encore vides) : les cartes d'arc sont masquées, les + * scènes vivent dans le volet. Dès qu'il y a du contenu écrit, la structure reste visible. + */ + flatMode = 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; diff --git a/web/src/app/campaigns/flat-structure.spec.ts b/web/src/app/campaigns/flat-structure.spec.ts new file mode 100644 index 0000000..00dd917 --- /dev/null +++ b/web/src/app/campaigns/flat-structure.spec.ts @@ -0,0 +1,29 @@ +import { flatContainer } from './flat-structure'; +import { Arc, Chapter } from '../services/campaign.model'; + +describe('flatContainer (mode plat)', () => { + const arc = (extra: Partial = {}): Arc => ({ id: 'a1', name: 'Arc principal', campaignId: 'c', type: 'LINEAR', ...extra } as Arc); + const chapter = (extra: Partial = {}): Chapter => ({ id: 'ch1', name: 'Chapitre 1', arcId: 'a1', ...extra } as Chapter); + + it('flattens a single empty arc with a single empty chapter', () => { + expect(flatContainer([arc()], { a1: [chapter()] })?.chapter.id).toBe('ch1'); + }); + + it('keeps the structure once the chapter or the arc has written content', () => { + expect(flatContainer([arc()], { a1: [chapter({ gmNotes: 'Le notaire est coupable' })] })).toBeNull(); + expect(flatContainer([arc()], { a1: [chapter({ description: "L'affaire commence" })] })).toBeNull(); + expect(flatContainer([arc({ hooks: '- Un ami disparaît' })], { a1: [chapter()] })).toBeNull(); + expect(flatContainer([arc({ illustrationImageIds: ['img'] })], { a1: [chapter()] })).toBeNull(); + }); + + it('ignores names and blank text', () => { + expect(flatContainer([arc({ name: 'Ma campagne', stakes: ' ' })], { a1: [chapter({ name: 'Début' })] })).not.toBeNull(); + }); + + it('never flattens a HUB arc, several chapters or several arcs (system arc ignored)', () => { + expect(flatContainer([arc({ type: 'HUB' })], { a1: [chapter()] })).toBeNull(); + expect(flatContainer([arc()], { a1: [chapter(), chapter({ id: 'ch2' })] })).toBeNull(); + expect(flatContainer([arc(), arc({ id: 'a2' })], { a1: [chapter()], a2: [] })).toBeNull(); + expect(flatContainer([arc(), arc({ id: 'sys', type: 'SYSTEM' })], { a1: [chapter()] })).not.toBeNull(); + }); +}); diff --git a/web/src/app/campaigns/flat-structure.ts b/web/src/app/campaigns/flat-structure.ts new file mode 100644 index 0000000..cd47a74 --- /dev/null +++ b/web/src/app/campaigns/flat-structure.ts @@ -0,0 +1,36 @@ +import { Arc, Chapter } from '../services/campaign.model'; + +/** + * Mode « plat » (Niveau 0) : une campagne d'un seul arc linéaire et d'un seul chapitre + * présente directement ses scènes, sans les niveaux arc et chapitre. Seulement si ces deux + * conteneurs sont VIDES (créés d'office pour poser une première scène) : dès que l'auteur y + * a écrit quelque chose — à la main ou via une méthode guidée (titre, accroche, vérité + * cachée, plan du méchant…) — on les montre, sinon ce contenu deviendrait introuvable. + * Même règle dans le volet, sur la page de la campagne et dans le livret PDF (core). + */ +export function flatContainer(arcs: Arc[], chaptersByArc: Record): { arc: Arc; chapter: Chapter } | null { + const visible = arcs.filter(a => a.type !== 'SYSTEM'); + if (visible.length !== 1) return null; + const arc = visible[0]; + if (arc.type === 'HUB') return null; // un arc HUB présente des quêtes, pas une simple liste de scènes + const chapters = chaptersByArc[arc.id!] ?? []; + if (chapters.length !== 1) return null; + const chapter = chapters[0]; + return isBareArc(arc) && isBareChapter(chapter) ? { arc, chapter } : null; +} + +/** Aucun contenu d'écriture dans l'arc (le nom ne compte pas : « Arc principal » est créé d'office). */ +export function isBareArc(arc: Arc): boolean { + return [arc.description, arc.themes, arc.stakes, arc.rewards, arc.resolution, arc.gmNotes, arc.hiddenTruth, arc.hooks] + .every(blank) && !(arc.illustrationImageIds?.length); +} + +/** Aucun contenu d'écriture dans le chapitre (le nom ne compte pas : « Chapitre 1 » est créé d'office). */ +export function isBareChapter(chapter: Chapter): boolean { + return [chapter.description, chapter.playerObjectives, chapter.narrativeStakes, chapter.gmNotes] + .every(blank) && !(chapter.illustrationImageIds?.length); +} + +function blank(value: string | null | undefined): boolean { + return !value || !value.trim(); +}