feat(shared): the Studio's auto layout entry points

auto/auto-designed.ts holds what the Studio's Auto layout menu calls.
autoDesignedBook lays the whole book on the designed templates in the
book's own theme object (a recoloured one of its own included, the calm
fabric-classic theme for a book without one), over the stored book, its
page format kept and every page holding a locked element kept; another
seed lays another book. autoRelayoutSpread lays one page again around its
locks, its id kept: an entry's page on the best template other than the
one on screen and its neighbours', avoiding one that would cut its story
where it can; the cover on the next cover template of its archetype and
then of its kin; the summary and the countries in their next look, an old
classic summary included. Pages the engine did not make are left alone,
as before. autoRelayoutKind says which pages those are.

designBook's templated path is split so the relayout reads the same
setting (templatedSetting) as the whole book, and the page layer is
shared (layEntryPage); the parity digests are unchanged by it. buildBook
and relayoutSpread stay exported and unchanged.
This commit is contained in:
Maurice
2026-10-24 20:01:49 +02:00
parent 9729b0b575
commit 0879b5aecc
8 changed files with 528 additions and 42 deletions
+54 -10
View File
@@ -28,7 +28,13 @@ import { isLivelyBeat } from './design-lively-pacing';
import { keepsStored, mergeDesign, type BookDesignPlanRow, type DesignedSpread } from './design-merge';
import { applyPageOverrides } from './design-overrides';
import type { BookDesignBeat } from './design-pacing';
import { TEMPLATED_ENTRY_PHOTOS, choosePhotos, shareGalleryByDay, type BookDesignLeftOut } from './design-photos';
import {
TEMPLATED_ENTRY_PHOTOS,
choosePhotos,
shareGalleryByDay,
type BookDesignLeftOut,
type DesignPhotoChoice,
} from './design-photos';
import { repairSpread } from './design-repair';
import { designGrid, designLook, paperBackground, type BookDesignLook } from './design-style';
import { archetypeOfTheme } from './design-template-pick';
@@ -171,7 +177,7 @@ function travelOf(input: BookDesignInput, entries: readonly AutoEntry[]): BookTr
}
/** The beat a page of the Auto layout plays, read from what it holds. */
export function classicBeat(spread: BookSpread): BookDesignBeat {
function classicBeat(spread: BookSpread): BookDesignBeat {
if (spread.role !== 'inner') return spread.role;
if (spread.entryId != null) return 'opener';
const has = (kind: BookSpread['elements'][number]['kind']) => spread.elements.some((e) => e.kind === kind);
@@ -243,7 +249,7 @@ export function designBook(
const directives = withBriefTheme(bookDesignDirectivesSchema.parse(raw));
const entries = inScope(input.entries, directives.scope);
if (classicOnly(directives)) return classicDesign(doc, input, directives, entries);
if (directives.layouts === 'templates') return templatedDesign(doc, input, directives, entries);
if (directives.layouts === 'templates') return templatedDesign(doc, input, raw);
const newId = input.newId ?? bookElementId;
const page = pageFor(doc, input, directives);
@@ -466,12 +472,33 @@ function templatedPages(
* travel looks. The rest is the composer's: the scope, the photo picker, the
* page budget, the stored pages kept, the repair and the findings.
*/
function templatedDesign(
/** What a book on the designed templates is laid with: its context, archetype, entries and photos. */
export interface TemplatedSetting {
c: ComposeCtx;
archetype: BookCoverArchetype;
/** The book counts as calm for template choice. */
calm: boolean;
/** The entries in date order, the gallery shared out to them. */
placed: AutoEntry[];
/** Loose photos no entry had room for. */
rest: AutoPhoto[];
choice: DesignPhotoChoice;
/** A page finished as the design finishes it: a templated one repaired, a composed one linked to the theme too. */
finish: (spread: BookSpread, templated?: boolean) => { spread: BookSpread; findings: BookFinding[] };
}
/**
* The setting of a book laid on the designed templates, read from the
* stored book, the journey and the directives: what the whole book is laid
* with, and what laying one of its pages again needs.
*/
export function templatedSetting(
doc: BookDocument | null,
input: BookDesignInput,
directives: BookDesignDirectives,
entries: AutoEntry[],
): BookDesignResult {
raw: BookDesignDirectivesInput,
): TemplatedSetting {
const directives = withBriefTheme(bookDesignDirectivesSchema.parse(raw));
const entries = inScope(input.entries, directives.scope);
const newId = input.newId ?? bookElementId;
const page = pageFor(doc, input, directives);
const shared = shareGalleryByDay(byDate(entries), input.gallery ?? [], input.candidates);
@@ -497,9 +524,6 @@ function templatedDesign(
const archetype = directives.cover?.archetype ?? archetypeOfTheme(look.theme);
// A rich theme designed without a brief is lively for its templates, so a poster book gets its loud pages at all.
const calm = art.energy === 'calm' && !(look.theme.decoration === 'rich' && !art.briefed);
const needs: FramingNeeds = { room: roomFor(doc, directives.keep), perStations: stationsPerSpread(c) };
const inner = composeTemplatedInner(c, placed, choice, input, needs, { archetype, calm, rest: shared.rest });
const designed = templatedPages(c, input, { placed, usedPhotos, inner: inner.spreads, archetype });
const repairCtx = { page, photos: input.photos, look, newId };
// A templated page has its colours linked to the theme by its template, so it is repaired, not relinked.
const finish = (spread: BookSpread, templated = false) => {
@@ -507,6 +531,26 @@ function templatedDesign(
const weighted = { ...fixed.spread, elements: fixed.spread.elements.map(withRealWeight) };
return { spread: templated ? weighted : unlinkDrifted(weighted, look.theme), findings: fixed.findings };
};
return { c, archetype, calm, placed, rest: shared.rest, choice, finish };
}
/**
* The book laid on the Studio's designed templates (`layouts: 'templates'`):
* the cover on a cover template of the theme's archetype, every chapter on
* its spreads, the summary, the countries and the back cover in the newer
* travel looks. The rest is the composer's: the scope, the photo picker, the
* page budget, the stored pages kept, the repair and the findings.
*/
function templatedDesign(
doc: BookDocument | null,
input: BookDesignInput,
raw: BookDesignDirectivesInput,
): BookDesignResult {
const { c, archetype, calm, placed, rest, choice, finish } = templatedSetting(doc, input, raw);
const needs: FramingNeeds = { room: roomFor(doc, c.directives.keep), perStations: stationsPerSpread(c) };
const inner = composeTemplatedInner(c, placed, choice, input, needs, { archetype, calm, rest });
const usedPhotos = [...choice.byEntry.values()].flat();
const designed = templatedPages(c, input, { placed, usedPhotos, inner: inner.spreads, archetype });
const done = designed.map((d): DesignedSpread => ({ ...d, ...finish(d.spread, !!d.template) }));
const leftOut = [...choice.leftOut, ...inner.leftOut];
return templatedResult(doc, input, { c, done, finish, placed, leftOut, notes: inner.notes });
@@ -83,7 +83,7 @@ export function archetypeOfTheme(theme: BookTheme | null | undefined): BookCover
}
/** How loud a template is, from its tags: a loud poster, a lively collage, a graphic grid, a calm page. */
export type TemplateEnergy = 'loud' | 'lively' | 'medium' | 'calm';
type TemplateEnergy = 'loud' | 'lively' | 'medium' | 'calm';
/** A template's energy. Graphic pages, and the few drawn without a word for it, count as medium. */
export function templateEnergy(tpl: Pick<BookDecoratedTemplate, 'tags'>): TemplateEnergy {
@@ -150,7 +150,7 @@ export function bodyCapacity(tpl: BookDecoratedTemplate, page: BookPageSetup, th
}
/** The parts a page of photos is cut into: how many photos each takes, the first one carrying the words. */
export interface PhotoChunking {
interface PhotoChunking {
sizes: number[];
/** Photos that find no frame: noted and parked, never lost. */
parked: number;
@@ -346,7 +346,7 @@ export function pickInnerTemplate(chunk: TemplateChunk, ctx: TemplatePickCtx): B
}
/** What a cover may be laid with: how many photos there are, and whether the cover photo stands tall. */
export interface CoverPick {
interface CoverPick {
archetype: BookCoverArchetype;
photos: number;
portrait: boolean;
@@ -34,7 +34,7 @@ import { pickCoverTemplate } from './design-template-pick';
*/
/** The travel looks of this book, an element of a size centred on the right page unless a frame is named. */
export function looksOf(c: ComposeCtx): BookTravelLooks | null {
function looksOf(c: ComposeCtx): BookTravelLooks | null {
const stats = c.travel.stats;
if (!stats) return null;
const box = liveBox(c, 'right');
@@ -119,9 +119,19 @@ export function composeTemplatedCover(
avoid,
});
if (!tpl) return { spread: composeArchetypeCover(c, archetype, words, material), template: null };
return { spread: coverOnTemplate(c, words, material, tpl), template: tpl.id };
}
/** The cover laid on this cover template, its title and subtitle bound to the journey's. */
export function coverOnTemplate(
c: ComposeCtx,
words: CoverWords,
material: CoverMaterial,
tpl: BookDecoratedTemplate,
): BookSpread {
const laid = pourTemplate(c, coverSeed(c, words, material, material.photos.length), tpl, material.entries);
// The photos the cover has no frame for are the book's own pages', not the cover's to park.
return { spread: { ...laid, parked: [] }, template: tpl.id };
return { ...laid, parked: [] };
}
// ── the journey at a glance ───────────────────────────────────────────────
@@ -136,7 +146,7 @@ export function summaryLookFor(archetype: BookCoverArchetype): BookStatsLook {
export const SUMMARY_CYCLE: readonly BookStatsLook[] = ['poster', 'tiles', 'sign'];
/** Whether the journey adds up to a summary worth a spread: a way of two stops at least. */
export const hasSummary = (stats: JourneyStats | null): stats is JourneyStats => !!stats && stats.points.length >= 2;
const hasSummary = (stats: JourneyStats | null): stats is JourneyStats => !!stats && stats.points.length >= 2;
/**
* The journey at a glance: its map with a photo at every stop on the left
+54 -25
View File
@@ -46,14 +46,14 @@ import {
* found no entry with room close the book on pages of more moments.
*/
export interface TemplatedInnerResult {
interface TemplatedInnerResult {
spreads: DesignedSpread[];
leftOut: BookDesignLeftOut[];
notes: string[];
}
/** What the inner pages are laid with beside the composer's context. */
export interface TemplatedPlan {
interface TemplatedPlan {
archetype: BookCoverArchetype;
/** The book counts as calm for template choice: its busy pages stay few. */
calm: boolean;
@@ -92,7 +92,7 @@ function storyOf(c: ComposeCtx, entry: AutoEntry | undefined): string {
* asked of each photo, and the photos in order. The title, place and date the
* template takes from the journal itself, bound to the entry.
*/
export function entrySeedSpread(
function entrySeedSpread(
c: ComposeCtx,
entry: AutoEntry,
photos: readonly AutoPhoto[],
@@ -131,9 +131,9 @@ function cutToFrame(el: BookTextElement, words: string): string {
const isStory = (e: BookElement): e is BookTextElement => e.kind === 'text' && e.binding?.source === 'entry.story';
/**
* A laid page with its story cut to its frame where it runs over: no longer
* bound to the journal and said in the notes, the rest handed back for the
* next page. A story that fits stays as it is.
* A laid page with its story cut to its frame where it runs over: marked as
* no longer following the journal and said in the notes, the rest handed
* back for the next page. A story that fits stays as it is.
*/
function fittedStory(c: ComposeCtx, spread: BookSpread, entry: AutoEntry): { spread: BookSpread; rest: string } {
const story = spread.elements.find(isStory);
@@ -169,36 +169,64 @@ interface Laying {
out: DesignedSpread[];
}
/** One page laid on a template, or composed where none takes it; its story's rest handed back. */
/** A story to set on a page: its words, and whether they are the whole of the entry's. */
export type PageStory = { words: string; whole: boolean } | null;
/** One page as it is laid: the spread, the template it was laid on (none for a composed one) and the story's rest. */
export interface LaidPage {
spread: BookSpread;
template: BookDecoratedTemplate | null;
rest: string;
}
/**
* One page of an entry laid on the template that suits it best, in the
* book's theme, or composed as a page of words where none takes it; the
* story cut to its frame, its rest handed back for the next page.
*/
export function layEntryPage(
c: ComposeCtx,
entry: AutoEntry,
page: { photos: readonly AutoPhoto[]; story: PageStory; entries: readonly AutoEntry[] },
pick: TemplatePickCtx,
): LaidPage {
const mappable = (entry.lat != null && entry.lng != null) || !!c.travel.stats?.points.length;
const chunk = { photos: page.photos, story: page.story?.words ?? '', mappable };
const tpl = pickInnerTemplate(chunk, pick);
if (!tpl) {
const fitted = fittedStory(c, composeWordsPage(c, entry, wordsColumn(c, entry, page.story)), entry);
return { ...fitted, template: null };
}
const laid = tidied(pourTemplate(c, entrySeedSpread(c, entry, page.photos, page.story), tpl, page.entries));
return { ...fittedStory(c, laid, entry), template: tpl };
}
/** One page of the book laid and added; the rest of its story handed back. */
function layPage(
l: Laying,
entry: AutoEntry,
photos: readonly AutoPhoto[],
story: { words: string; whole: boolean } | null,
story: PageStory,
chapter: string | null,
): string {
const { c } = l;
const mappable = (entry.lat != null && entry.lng != null) || !!c.travel.stats?.points.length;
const chunk = { photos, story: story?.words ?? '', mappable };
const key = `e${entry.id}:${l.out.length}`;
const tpl = pickInnerTemplate(chunk, { ...l.base, history: l.history, key });
if (!tpl) {
const fitted = fittedStory(c, composeWordsPage(c, entry, wordsColumn(c, entry, story)), entry);
l.out.push({ spread: fitted.spread, beat: 'story', chapter, findings: [] });
return fitted.rest;
}
const laid = tidied(pourTemplate(c, entrySeedSpread(c, entry, photos, story), tpl, l.entries));
const fitted = fittedStory(c, laid, entry);
l.history.push(tpl.id);
l.out.push({ spread: fitted.spread, beat: beatOf(tpl, photos.length), chapter, findings: [], template: tpl.id });
return fitted.rest;
const laid = layEntryPage(l.c, entry, { photos, story, entries: l.entries }, { ...l.base, history: l.history, key });
if (laid.template) l.history.push(laid.template.id);
l.out.push({
spread: laid.spread,
beat: laid.template ? beatOf(laid.template, photos.length) : 'story',
chapter,
findings: [],
...(laid.template ? { template: laid.template.id } : {}),
});
return laid.rest;
}
/**
* A story set in a column down the left page, where no template takes it:
* bound to the entry while it is the whole of it, for `fittedStory` to cut.
*/
function wordsColumn(c: ComposeCtx, entry: AutoEntry, story: { words: string; whole: boolean } | null) {
function wordsColumn(c: ComposeCtx, entry: AutoEntry, story: PageStory) {
if (!story?.words) return null;
const binding = { source: 'entry.story' as const, entryId: entry.id };
const el = textEl(c, liveBox(c, 'left'), story.words, 'body', { binding, overridden: !story.whole });
@@ -260,7 +288,8 @@ function chapterPages(l: Laying, chapter: DesignChapter, notes: string[]) {
...last.spread,
parked: [...last.spread.parked, ...parked.map((p) => photoEl(l.c, p, liveBox(l.c, 'right')))],
};
notes.push(`${parked.length} photos of entry ${entry.id} found no frame and wait with its last page`);
const what = parked.length === 1 ? '1 photo' : `${parked.length} photos`;
notes.push(`${what} of entry ${entry.id} found no frame and wait with its last page`);
}
for (let n = 0; rest && n < MOST_CONTINUED; n++) rest = layPage(l, entry, [], { words: rest, whole: false }, name);
if (rest) notes.push(`part of the story of entry ${entry.id} found no room on its pages and is left out`);
@@ -313,7 +342,7 @@ function withinCap(body: DesignedSpread[], keep: number, leftOut: BookDesignLeft
if (body.length <= keep) return body;
const cut = body.slice(Math.max(0, keep));
for (const d of cut) {
for (const e of d.spread.elements) {
for (const e of [...d.spread.elements, ...d.spread.parked]) {
if (e.kind === 'photo' && e.photoId != null) leftOut.push({ photoId: e.photoId, reason: 'page_budget' });
}
}