Files
dmtools/src/Controller/StorylineController.php
T
FrankandClaude Sonnet 5 b77e5eaa4f Storyline: unfoldable section list per chapter in the sidebar
Each chapter in the right-hand menu gets a caret that unfolds its
numbered sections (e.g. the prologue's 28 scenes); clicking one jumps
straight to it. The chapter you're reading starts unfolded, other
chapters' sections link across (/storyline/overview#section-…), and the
section at the top of the pane is highlighted as you scroll.

- ChapterOutline service parses each rendered chapter for its
  <h2 class="storyline-section"> headings, adds anchor ids and returns the
  outline; the controller renders all chapters once to feed the sidebar.
- storyline-toc Stimulus controller: fold/unfold, in-pane jump, scroll-spy
  (works in the split view and in the small-screen window-scroll fallback).
- Tests for the outline parser.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-21 18:44:54 +02:00

103 lines
4.2 KiB
PHP

<?php
namespace App\Controller;
use App\Form\ChapterNoteType;
use App\Repository\ChapterNoteRepository;
use App\Storyline\ChapterOutline;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[IsGranted('IS_AUTHENTICATED_FULLY')]
class StorylineController extends AbstractController
{
/**
* The storyline is a set of hand-written chapters. Each entry points at a
* Twig partial under templates/storyline/chapters/. Add a chapter by adding
* a row here and the matching template; order in this array is reading order.
*
* - eyebrow: the kicker shown above the chapter title ("Overview", "Prologue",
* "Session 1", ...)
* - tag: 1-2 characters for the little badge in the chapter list / sidebar
*
* @var array<string, array{title: string, eyebrow: string, tag: string, summary: string, template: string}>
*/
private const CHAPTERS = [
'overview' => [
'title' => 'Campaign Overview',
'eyebrow' => 'Overview',
'tag' => 'Ov',
'summary' => 'The whole shape of the Brackenford campaign, from the sealing of Kyuss four centuries ago to the final crisis — the truth behind the blood, the red herring around Sir Aldren, and how it can resolve.',
'template' => 'storyline/chapters/overview.html.twig',
],
'prologue-the-long-road' => [
'title' => 'Prologue — The Long Road to Brackenford',
'eyebrow' => 'Prologue',
'tag' => 'Pr',
'summary' => "The courier job that carries the party to Brackenford: hired in Bellhaven to deliver Edric Vale's estate to his nephew Eamon, twenty days on the road, and one night in a guest cottage that won't let anyone sleep. Levels 1 to 2, about 2.5 hours.",
'template' => 'storyline/chapters/prologue-the-long-road.html.twig',
],
];
#[Route('/storyline', name: 'app_storyline', methods: ['GET'])]
public function index(): Response
{
return $this->render('storyline/index.html.twig', [
'chapters' => self::CHAPTERS,
]);
}
#[Route('/storyline/{slug}', name: 'app_storyline_chapter', requirements: ['slug' => '[a-z0-9-]+'], methods: ['GET', 'POST'])]
public function chapter(string $slug, Request $request, ChapterNoteRepository $notes, EntityManagerInterface $em, ChapterOutline $outline): Response
{
if (!isset(self::CHAPTERS[$slug])) {
throw $this->createNotFoundException('No such storyline chapter.');
}
$note = $notes->findOrCreate($slug);
$form = $this->createForm(ChapterNoteType::class, $note);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (null === $note->getId()) {
$em->persist($note);
}
$em->flush();
$this->addFlash('success', 'Chapter notes saved.');
return $this->redirectToRoute('app_storyline_chapter', ['slug' => $slug]);
}
// Render every chapter once: the current one is shown (with anchors),
// the rest only feed the sidebar's per-section jump lists.
$sections = [];
$body = '';
foreach (self::CHAPTERS as $s => $c) {
$processed = $outline->process($this->renderView($c['template']));
$sections[$s] = $processed['sections'];
if ($s === $slug) {
$body = $processed['html'];
}
}
$slugs = array_keys(self::CHAPTERS);
$pos = array_search($slug, $slugs, true);
return $this->render('storyline/chapter.html.twig', [
'chapters' => self::CHAPTERS,
'slug' => $slug,
'chapter' => self::CHAPTERS[$slug],
'body' => $body,
'sections' => $sections,
'note' => $note,
'form' => $form,
'prev' => $pos > 0 ? $slugs[$pos - 1] : null,
'next' => $pos < \count($slugs) - 1 ? $slugs[$pos + 1] : null,
]);
}
}