mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
refactor(shared): the travel looks are built in shared
The factories behind the Travel panel's cards (a summary as a poster, a ticket or a sign, a figure on a medal or an odometer, a map of the journey or of one country, a mark as a stamp or a tag, a country as a stamp, a visa or a postcard, each dressed in the book's theme) move to book/travel/book-travel-looks.ts, so the auto layout can build its framing pages from the same looks. Where an element lands stays the caller's: the panel still centres it on the spread on screen, through the same wrapper and the same API, and a designed page names its frame. A mark can now also take an entry's value rather than the journey's.
This commit is contained in:
@@ -1,100 +1,56 @@
|
||||
import type {
|
||||
BookBadgeStyle,
|
||||
BookBadgeVariant,
|
||||
BookColorToken,
|
||||
BookCountryLook,
|
||||
BookElement,
|
||||
BookMapElement,
|
||||
BookMapScope,
|
||||
BookMetric,
|
||||
BookPageSetup,
|
||||
BookStatsLook,
|
||||
BookTheme,
|
||||
BookTravelMapCard,
|
||||
BookTravelMarkVariant,
|
||||
JourneyStats,
|
||||
} from '@trek/shared';
|
||||
import {
|
||||
bookBadgeValue,
|
||||
bookCountryName,
|
||||
bookTravelBadge,
|
||||
bookTravelCountries,
|
||||
bookTravelMap,
|
||||
bookTravelSize,
|
||||
bookTravelStats,
|
||||
themedElement,
|
||||
BOOK_TRAVEL_COUNTRY_LOOKS,
|
||||
BOOK_TRAVEL_FIGURE_SHELVES,
|
||||
BOOK_TRAVEL_MARK_STYLES,
|
||||
BOOK_TRAVEL_MARK_VARIANTS,
|
||||
BOOK_TRAVEL_SUMMARY_LOOKS,
|
||||
bookTravelLooks,
|
||||
bookElementId as uid,
|
||||
} from '@trek/shared';
|
||||
import { studioTravelContext } from '../studioTravelContext';
|
||||
|
||||
/**
|
||||
* What the Travel panel offers, built: every card is the element its click
|
||||
* places, made by the book engine's own factories (book-travel.ts) so a card
|
||||
* and what the assistant places are one thing, then set in one of the newer
|
||||
* looks and dressed in the book's theme.
|
||||
* places, made by the book's own travel looks (book-travel-looks.ts in
|
||||
* shared), the same factory the auto layout builds its framing pages with,
|
||||
* so a card and a designed page are one thing.
|
||||
*
|
||||
* Pure, so the panel stays a list of shelves: it asks for an element and
|
||||
* renders it through the page's own renderer, and the preview and the result
|
||||
* are the same object.
|
||||
* are the same object. What is the panel's own here is where a new element
|
||||
* lands: centred on the spread on screen.
|
||||
*/
|
||||
|
||||
/** The figures a summary offers: how far, how long, where, and how much was kept. */
|
||||
const SUMMARY_METRICS: BookMetric[] = ['distance', 'days', 'countries', 'photos'];
|
||||
|
||||
/** The looks of a summary, in the order the panel offers them. */
|
||||
export const SUMMARY_LOOKS: BookStatsLook[] = [
|
||||
'poster',
|
||||
'ticket',
|
||||
'tiles',
|
||||
'sign',
|
||||
'odometer',
|
||||
'note',
|
||||
'sketch',
|
||||
'band',
|
||||
];
|
||||
export const SUMMARY_LOOKS: readonly BookStatsLook[] = BOOK_TRAVEL_SUMMARY_LOOKS;
|
||||
|
||||
/** The marks the journey fills, in the order a page is read: when, which day, how far, where. */
|
||||
export const MARK_VARIANTS = ['date', 'day', 'distance', 'coords', 'flag'] as const;
|
||||
export type MarkVariant = (typeof MARK_VARIANTS)[number];
|
||||
export const MARK_VARIANTS = BOOK_TRAVEL_MARK_VARIANTS;
|
||||
export type MarkVariant = BookTravelMarkVariant;
|
||||
|
||||
/** The newer styles a mark comes in, and which of them each mark is offered in. */
|
||||
export const MARK_STYLES: { style: BookBadgeStyle; variants: readonly MarkVariant[] }[] = [
|
||||
{ style: 'stamp', variants: ['date', 'day', 'flag'] },
|
||||
{ style: 'tag', variants: ['day', 'distance', 'coords', 'flag'] },
|
||||
{ style: 'pill', variants: ['date', 'day', 'distance', 'coords', 'flag'] },
|
||||
{ style: 'ribbon', variants: ['date', 'day', 'distance', 'flag'] },
|
||||
{ style: 'sticker', variants: ['date', 'day', 'distance', 'flag'] },
|
||||
{ style: 'patch', variants: ['day', 'distance', 'flag'] },
|
||||
{ style: 'sketch', variants: ['date', 'day', 'distance', 'coords', 'flag'] },
|
||||
];
|
||||
export const MARK_STYLES: readonly { style: BookBadgeStyle; variants: readonly MarkVariant[] }[] =
|
||||
BOOK_TRAVEL_MARK_STYLES;
|
||||
|
||||
/** The single figures, one shelf per look, with the figures each look suits. */
|
||||
export const FIGURE_SHELVES: { look: BookStatsLook; metrics: readonly BookMetric[] }[] = [
|
||||
{ look: 'medal', metrics: ['distance', 'days', 'steps', 'photos', 'countries', 'places', 'furthest'] },
|
||||
{ look: 'odometer', metrics: ['distance', 'furthest', 'days', 'photos'] },
|
||||
{ look: 'sketch', metrics: ['distance', 'days', 'countries', 'photos'] },
|
||||
];
|
||||
export const FIGURE_SHELVES: readonly { look: BookStatsLook; metrics: readonly BookMetric[] }[] =
|
||||
BOOK_TRAVEL_FIGURE_SHELVES;
|
||||
|
||||
/** The looks a country is offered in. */
|
||||
export const COUNTRY_LOOKS: BookCountryLook[] = ['stamp', 'visa', 'postcard', 'flag', 'pin', 'label', 'sketch'];
|
||||
|
||||
/** The looks written by hand take the theme's script face rather than its heading. */
|
||||
const HANDWRITTEN = new Set<string>(['note', 'sketch']);
|
||||
|
||||
/** Which of the theme's accents each country look takes, so a shelf of them is not one colour. */
|
||||
const COUNTRY_ACCENT: Partial<Record<BookCountryLook, BookColorToken>> = {
|
||||
stamp: 'accent2',
|
||||
visa: 'accent3',
|
||||
pin: 'accent',
|
||||
label: 'accent2',
|
||||
};
|
||||
|
||||
/** Which of the theme's accents each mark takes, so a page of them is not one colour. */
|
||||
const MARK_ACCENT: Record<MarkVariant, BookColorToken> = {
|
||||
date: 'accent',
|
||||
day: 'accent2',
|
||||
distance: 'accent3',
|
||||
coords: 'accent',
|
||||
flag: 'accent2',
|
||||
};
|
||||
export const COUNTRY_LOOKS: readonly BookCountryLook[] = BOOK_TRAVEL_COUNTRY_LOOKS;
|
||||
|
||||
/** The map cards: the photographed route, its stops numbered, its two ends marked, and the dark ground. */
|
||||
export const MAP_CARDS: {
|
||||
@@ -130,130 +86,19 @@ export function travelBuilders({ page, stats, path, locale, t, theme, single }:
|
||||
return { x: (W - size.w) / 2, y: (page.pageHeight - size.h) / 2, w: size.w, h: size.h };
|
||||
};
|
||||
|
||||
/**
|
||||
* An element dressed in the book's theme: its words in the theme's ink and
|
||||
* heading face, its colour one of the theme's accents, all three linked so
|
||||
* it follows when the theme changes. A book without a theme keeps the
|
||||
* engine's own colours.
|
||||
*/
|
||||
const dress = (el: BookElement, accent: BookColorToken = 'accent', hand = false): BookElement =>
|
||||
theme
|
||||
? themedElement(
|
||||
{ ...el, token: 'ink', accentToken: accent, fontRole: hand ? 'script' : 'heading' } as BookElement,
|
||||
theme
|
||||
)
|
||||
: el;
|
||||
const looks = bookTravelLooks({ page, stats, travel, theme, newId: uid, place: at });
|
||||
|
||||
/** A summary in one of the newer looks; a band is a strip, so it is set a third as tall. */
|
||||
const summaryEl = (look: BookStatsLook): BookElement => {
|
||||
const w = page.pageWidth * (look === 'note' || look === 'sign' ? 0.56 : 0.72);
|
||||
const h =
|
||||
look === 'band' ? page.pageHeight * 0.13 : look === 'ticket' ? w * 0.42 : look === 'note' ? w * 0.72 : w * 0.5;
|
||||
const el = { ...bookTravelStats(stats, { id: uid('st'), frame: at({ w, h }), metrics: SUMMARY_METRICS }), look };
|
||||
return dress(el, look === 'sign' ? 'accent3' : 'accent', HANDWRITTEN.has(look));
|
||||
return {
|
||||
summaryEl: (look: BookStatsLook) => looks.summaryEl(look),
|
||||
figureEl: (metric: BookMetric, look: BookStatsLook = 'medal') => looks.figureEl(metric, look),
|
||||
mapEl: (
|
||||
card: BookTravelMapCard,
|
||||
scope: BookMapScope = 'journey',
|
||||
source: 'vector' | 'relief' | 'satellite' = 'vector'
|
||||
) => looks.mapEl(card, scope, source),
|
||||
countryMapEl: (code: string, kind: 'shape' | 'satellite' | 'box') => looks.countryMapEl(code, kind),
|
||||
markEl: (variant: MarkVariant, style: BookBadgeStyle) => looks.markEl(variant, style),
|
||||
countryEl: (code: string, look: BookCountryLook) => looks.countryEl(code, look),
|
||||
countryName: looks.countryName,
|
||||
};
|
||||
|
||||
/**
|
||||
* One figure on its own, the mark to drop beside a photograph: as a medal, on
|
||||
* the drums of an odometer, or ringed by hand.
|
||||
*/
|
||||
const figureEl = (metric: BookMetric, look: BookStatsLook = 'medal'): BookElement => {
|
||||
const side = Math.min(page.pageWidth, page.pageHeight) * 0.3;
|
||||
const size = look === 'odometer' ? { w: side * 1.6, h: side * 0.62 } : { w: side, h: side };
|
||||
const el = { ...bookTravelStats(stats, { id: uid('st'), frame: at(size), metrics: [metric] }), look };
|
||||
return dress(el, 'accent', HANDWRITTEN.has(look));
|
||||
};
|
||||
|
||||
/**
|
||||
* A map as the engine places it, on the card's ground and with the card's
|
||||
* stops. The scope is the journey or one trip; the ground is the outlines
|
||||
* or one of the two imagery sources.
|
||||
*/
|
||||
const mapEl = (
|
||||
card: { style: BookMapElement['style']; pins?: BookMapElement['pinStyle'] },
|
||||
scope: BookMapScope = 'journey',
|
||||
source: 'vector' | 'relief' | 'satellite' = 'vector'
|
||||
): BookElement => {
|
||||
const el = bookTravelMap(travel, stats, { id: uid('mp'), frame: at(bookTravelSize('map', page)), scope, source });
|
||||
const shaped = { ...el, style: card.style, ...(card.pins ? { pinStyle: card.pins } : {}) };
|
||||
// Over imagery the line is white on purpose (book-travel.ts); only the outline maps take the theme.
|
||||
return source === 'vector' ? dress(shaped) : shaped;
|
||||
};
|
||||
|
||||
/**
|
||||
* A map of one country: its stops alone, without the way there. A journey's
|
||||
* recorded way often starts with the flight from home, and fitted to all of
|
||||
* it the country is a speck at the edge of a continent; the page a travel
|
||||
* book wants is the country, cut to its own shape on the paper or over the
|
||||
* satellite picture, or the country in a box.
|
||||
*/
|
||||
const countryMapEl = (code: string, kind: 'shape' | 'satellite' | 'box'): BookElement => {
|
||||
const own = {
|
||||
...stats,
|
||||
points: stats.points.filter((p) => p.country === code),
|
||||
countries: stats.countries.filter((c) => c.code === code),
|
||||
};
|
||||
const ctx = studioTravelContext(own, [], locale, t);
|
||||
const source = kind === 'satellite' ? 'satellite' : 'vector';
|
||||
const el = bookTravelMap(ctx, own, {
|
||||
id: uid('mp'),
|
||||
frame: at(bookTravelSize('map', page)),
|
||||
scope: 'journey',
|
||||
source,
|
||||
});
|
||||
// Cut to its shape on the paper, the land is dark, so the silhouette reads and the route in the accent stands on it.
|
||||
const style = kind === 'shape' ? 'dark' : 'minimal';
|
||||
const shaped = {
|
||||
...el,
|
||||
style,
|
||||
clip: kind === 'box' ? 'rect' : 'country',
|
||||
pinStyle: 'numbered',
|
||||
fitToCountries: true,
|
||||
} as const;
|
||||
return source === 'vector' ? dress(shaped) : shaped;
|
||||
};
|
||||
|
||||
/** A mark in one of the newer styles, at the size its variant suits; a stamp is set taller for its two rules. */
|
||||
const markEl = (variant: MarkVariant, style: BookBadgeStyle): BookElement | null => {
|
||||
const value = bookBadgeValue(travel, variant, null);
|
||||
if (!value) return null;
|
||||
const frame = at(markSize(style, value.text, value.sub));
|
||||
const el = { ...bookTravelBadge({ id: uid('bd'), frame, variant: variant as BookBadgeVariant, value }), style };
|
||||
return dress(el, MARK_ACCENT[variant], style === 'sketch');
|
||||
};
|
||||
|
||||
/**
|
||||
* How large a mark is set: as wide as its words need. A stamp stacks them in
|
||||
* a box with room for its two rules; a tag or a pill sets them in a line
|
||||
* after its eyelet or its disc, so its length follows the words.
|
||||
*/
|
||||
const markSize = (style: BookBadgeStyle, text: string, sub: string) => {
|
||||
const twoLines = !!text && !!sub && text.length > 14;
|
||||
const h = page.pageHeight * (style === 'stamp' ? 0.12 : twoLines ? 0.1 : 0.066);
|
||||
const main = text || sub;
|
||||
if (style === 'stamp') return { w: Math.max(h * 1.3, h * 0.2 * Math.max(main.length * 1.4, sub.length)), h };
|
||||
// Two lines are set smaller than one, so they need less width a letter.
|
||||
if (twoLines)
|
||||
return { w: Math.min(page.pageWidth * 0.6, h * (1.5 + 0.13 * Math.max(text.length, sub.length * 0.7))), h };
|
||||
const chars = main.length * (text ? 1 : 0.85) + (text ? sub.length * 0.5 : 0);
|
||||
return { w: Math.min(page.pageWidth * 0.6, h * (1.6 + 0.3 * chars)), h };
|
||||
};
|
||||
|
||||
/** One country as a square stamp, or as a card in the proportions of a postcard. */
|
||||
const countryEl = (code: string, look: BookCountryLook): BookElement => {
|
||||
const side = Math.min(page.pageWidth, page.pageHeight) * 0.36;
|
||||
const ratio = look === 'stamp' || look === 'pin' || look === 'sketch' ? 1 : look === 'postcard' ? 1.36 : 1.4;
|
||||
const frame = at({ w: side * ratio, h: side });
|
||||
const el = {
|
||||
...bookTravelCountries(travel, { id: uid('co'), frame, codes: [code] }),
|
||||
look,
|
||||
showFlag: look === 'pin' || look === 'postcard',
|
||||
};
|
||||
return dress(el, COUNTRY_ACCENT[look] ?? 'accent', look === 'sketch');
|
||||
};
|
||||
|
||||
/** A country's name in the reader's language, from the same CLDR data Atlas uses. */
|
||||
const countryName = (code: string): string => bookCountryName(code, locale, stats);
|
||||
|
||||
return { summaryEl, figureEl, mapEl, countryMapEl, markEl, countryEl, countryName };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
import type { AutoEntry } from '../auto/auto-types';
|
||||
import type { BookTravelContext } from '../book-travel';
|
||||
import { bookElementSchema, bookPageSetupSchema, type BookElement } from '../book.schema';
|
||||
import type { JourneyStats } from '../journey-stats.schema';
|
||||
import { bookThemeColor, bookThemeOf } from '../theme/book-theme';
|
||||
import {
|
||||
BOOK_TRAVEL_COUNTRY_LOOKS,
|
||||
BOOK_TRAVEL_FIGURE_SHELVES,
|
||||
BOOK_TRAVEL_MARK_STYLES,
|
||||
BOOK_TRAVEL_SUMMARY_LOOKS,
|
||||
bookTravelLooks,
|
||||
} from './book-travel-looks';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
/** The travel elements in their newer looks, as the Travel panel and the auto layout both build them. */
|
||||
|
||||
type Point = JourneyStats['points'][number];
|
||||
const pt = (label: string, at: [number, number], country: string, entryId: number): Point => ({
|
||||
lat: at[0],
|
||||
lng: at[1],
|
||||
label,
|
||||
date: null,
|
||||
country,
|
||||
tripId: 1,
|
||||
photoId: null,
|
||||
entryId,
|
||||
});
|
||||
|
||||
const stats: JourneyStats = {
|
||||
journeyId: 5,
|
||||
distance: 4_000_000,
|
||||
days: 21,
|
||||
steps: 6,
|
||||
photos: 40,
|
||||
places: 12,
|
||||
furthest: 3_000_000,
|
||||
countries: [
|
||||
{ code: 'IS', name: 'Iceland', places: 2, firstVisit: '2026-06-02' },
|
||||
{ code: 'PT', name: 'Portugal', places: 1, firstVisit: '2026-09-04' },
|
||||
],
|
||||
points: [
|
||||
pt('Reykjavík', [64.14, -21.94], 'IS', 1),
|
||||
pt('Akureyri', [65.68, -18.12], 'IS', 2),
|
||||
pt('Lisboa', [38.72, -9.14], 'PT', 3),
|
||||
],
|
||||
trips: [],
|
||||
excluded: [],
|
||||
start: '2026-06-02',
|
||||
end: '2026-09-12',
|
||||
};
|
||||
|
||||
const entry: AutoEntry = {
|
||||
id: 2,
|
||||
title: 'North',
|
||||
story: null,
|
||||
location: 'Akureyri',
|
||||
date: '2026-06-06',
|
||||
photos: [],
|
||||
lat: 65.68,
|
||||
lng: -18.12,
|
||||
country: 'IS',
|
||||
dayNumber: 5,
|
||||
dayCount: 21,
|
||||
};
|
||||
|
||||
const travel: BookTravelContext = {
|
||||
locale: 'en',
|
||||
stats,
|
||||
path: [],
|
||||
entries: [entry],
|
||||
labels: {
|
||||
day: 'DAY {day}',
|
||||
stations: 'Stops',
|
||||
summary: 'Summary',
|
||||
countries: 'Countries',
|
||||
distance: 'km',
|
||||
pros: '+',
|
||||
cons: '-',
|
||||
},
|
||||
};
|
||||
|
||||
const page = bookPageSetupSchema.parse({});
|
||||
const theme = bookThemeOf('riso');
|
||||
let n = 0;
|
||||
const newId = (p: string) => `${p}-${++n}`;
|
||||
const place = (size: { w: number; h: number }) => ({ x: 1, y: 2, w: size.w, h: size.h });
|
||||
const looks = bookTravelLooks({ page, stats, travel, theme, newId, place });
|
||||
const plain = bookTravelLooks({ page, stats, travel, theme: null, newId, place });
|
||||
|
||||
const valid = (el: BookElement | null) => {
|
||||
expect(el).not.toBeNull();
|
||||
expect(bookElementSchema.safeParse(el).success).toBe(true);
|
||||
};
|
||||
|
||||
/** Every element of every shelf, as the panel offers them. */
|
||||
function everyElement(of: typeof looks): BookElement[] {
|
||||
const out: BookElement[] = [];
|
||||
for (const look of BOOK_TRAVEL_SUMMARY_LOOKS) out.push(of.summaryEl(look));
|
||||
for (const shelf of BOOK_TRAVEL_FIGURE_SHELVES) for (const m of shelf.metrics) out.push(of.figureEl(m, shelf.look));
|
||||
for (const code of ['IS', 'PT']) for (const look of BOOK_TRAVEL_COUNTRY_LOOKS) out.push(of.countryEl(code, look));
|
||||
for (const kind of ['shape', 'satellite', 'box'] as const) out.push(of.countryMapEl('IS', kind));
|
||||
out.push(of.mapEl({ style: 'minimal' }), of.mapEl({ style: 'dark', pins: 'numbered' }));
|
||||
for (const { style, variants } of BOOK_TRAVEL_MARK_STYLES) {
|
||||
for (const v of variants)
|
||||
out.push(...[of.markEl(v, style), of.markEl(v, style, { entry })].flatMap((e) => (e ? [e] : [])));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
describe('the travel looks', () => {
|
||||
it('TRVLOOK-001: every factory answers an element the document takes, in every look it offers', () => {
|
||||
const all = everyElement(looks);
|
||||
expect(all.length).toBeGreaterThan(60);
|
||||
for (const el of all) valid(el);
|
||||
});
|
||||
|
||||
it('TRVLOOK-002: never in the classic look or a classic mark style', () => {
|
||||
for (const el of [...everyElement(looks), ...everyElement(plain)]) {
|
||||
if (el.kind === 'stats' || el.kind === 'countries') expect(el.look).toBeDefined();
|
||||
if (el.kind === 'stats' || el.kind === 'countries') expect(el.look).not.toBe('classic');
|
||||
if (el.kind === 'badge') expect(['plain', 'chip', 'outline', 'stacked']).not.toContain(el.style);
|
||||
}
|
||||
});
|
||||
|
||||
it("TRVLOOK-003: dressed in the book's theme, its links kept", () => {
|
||||
const stamp = looks.countryEl('IS', 'stamp');
|
||||
expect(stamp).toMatchObject({ token: 'ink', accentToken: 'accent2', fontRole: 'heading' });
|
||||
if (stamp.kind !== 'countries') throw new Error('a country element');
|
||||
expect(stamp.color).toBe(bookThemeColor(theme, 'ink'));
|
||||
expect(stamp.accent).toBe(bookThemeColor(theme, 'accent2'));
|
||||
expect(looks.summaryEl('note')).toMatchObject({ fontRole: 'script' });
|
||||
// Without a theme the engine's own colours stay, unlinked.
|
||||
expect(plain.countryEl('IS', 'stamp')).not.toHaveProperty('token');
|
||||
});
|
||||
|
||||
it('TRVLOOK-004: lands in the frame given, else where the caller places one of its size', () => {
|
||||
const frame = { x: 10, y: 20, w: 30, h: 40 };
|
||||
expect(looks.summaryEl('poster', frame).frame).toEqual(frame);
|
||||
expect(looks.countryEl('PT', 'visa', frame).frame).toEqual(frame);
|
||||
expect(looks.figureEl('distance', 'odometer', frame).frame).toEqual(frame);
|
||||
expect(looks.mapEl({ style: 'minimal' }, 'journey', 'vector', frame).frame).toEqual(frame);
|
||||
expect(looks.markEl('day', 'stamp', { entry, frame })?.frame).toEqual(frame);
|
||||
const placed = looks.summaryEl('ticket');
|
||||
expect(placed.frame).toMatchObject({ x: 1, y: 2, ...looks.summarySize('ticket') });
|
||||
});
|
||||
|
||||
it("TRVLOOK-005: a mark takes an entry's value when given one, the journey's when not", () => {
|
||||
const day = looks.markEl('day', 'stamp', { entry });
|
||||
const first = looks.markEl('day', 'stamp');
|
||||
expect(day).toMatchObject({ kind: 'badge', text: 'DAY 5' });
|
||||
expect(first).toMatchObject({ kind: 'badge', text: 'DAY 1' });
|
||||
expect(looks.markEl('flag', 'tag', { entry })).toMatchObject({ code: 'IS' });
|
||||
const lost = bookTravelLooks({ page, stats, travel: { ...travel, stats: null }, theme, newId, place });
|
||||
expect(lost.markEl('distance', 'pill')).toBeNull();
|
||||
});
|
||||
|
||||
it("TRVLOOK-006: a country's map holds that country's stops alone, cut to its shape", () => {
|
||||
const map = looks.countryMapEl('IS', 'shape');
|
||||
if (map.kind !== 'map') throw new Error('a map');
|
||||
expect(map.points).toHaveLength(2);
|
||||
expect(map).toMatchObject({ clip: 'country', style: 'dark', pinStyle: 'numbered' });
|
||||
expect(looks.countryMapEl('IS', 'box')).toMatchObject({ clip: 'rect', style: 'minimal' });
|
||||
expect(looks.countryName('PT')).toBe('Portugal');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,301 @@
|
||||
import type { AutoEntry } from '../auto/auto-types';
|
||||
import {
|
||||
bookBadgeValue,
|
||||
bookCountryName,
|
||||
bookTravelBadge,
|
||||
bookTravelCountries,
|
||||
bookTravelMap,
|
||||
bookTravelSize,
|
||||
bookTravelStats,
|
||||
type BookMapScope,
|
||||
type BookTravelContext,
|
||||
} from '../book-travel';
|
||||
import type {
|
||||
BookBadgeStyle,
|
||||
BookBadgeVariant,
|
||||
BookCountryLook,
|
||||
BookElement,
|
||||
BookFrame,
|
||||
BookMapElement,
|
||||
BookMetric,
|
||||
BookPageSetup,
|
||||
BookStatsLook,
|
||||
} from '../book.schema';
|
||||
import type { JourneyStats } from '../journey-stats.schema';
|
||||
import { themedElement } from '../theme/book-theme-apply';
|
||||
import type { BookColorToken, BookTheme } from '../theme/book-theme.schema';
|
||||
|
||||
/**
|
||||
* The travel elements in their newer looks, dressed in the book's theme: a
|
||||
* summary as a poster, a ticket or a sign, a single figure on a medal or an
|
||||
* odometer, a map of the journey or of one country, a mark as a stamp or a
|
||||
* tag, a country as a stamp, a visa or a postcard.
|
||||
*
|
||||
* One factory for the Studio's Travel panel, whose cards are the elements
|
||||
* their click places, and for the auto layout, which builds its framing pages
|
||||
* from the same looks. Both take the book engine's own factories
|
||||
* (book-travel.ts), so a card, a designed page and what the assistant places
|
||||
* are one thing. Where an element lands is the caller's: the panel centres it
|
||||
* on the spread on screen, a designed page names its frame.
|
||||
*/
|
||||
|
||||
/** The figures a summary offers: how far, how long, where, and how much was kept. */
|
||||
const SUMMARY_METRICS: readonly BookMetric[] = ['distance', 'days', 'countries', 'photos'];
|
||||
|
||||
/** The looks of a summary, in the order the panel offers them. */
|
||||
export const BOOK_TRAVEL_SUMMARY_LOOKS: readonly BookStatsLook[] = [
|
||||
'poster',
|
||||
'ticket',
|
||||
'tiles',
|
||||
'sign',
|
||||
'odometer',
|
||||
'note',
|
||||
'sketch',
|
||||
'band',
|
||||
];
|
||||
|
||||
/** The marks the journey fills, in the order a page is read: when, which day, how far, where. */
|
||||
export const BOOK_TRAVEL_MARK_VARIANTS = ['date', 'day', 'distance', 'coords', 'flag'] as const;
|
||||
export type BookTravelMarkVariant = (typeof BOOK_TRAVEL_MARK_VARIANTS)[number];
|
||||
|
||||
/** The newer styles a mark comes in, and which of them each mark is offered in. */
|
||||
export const BOOK_TRAVEL_MARK_STYLES: readonly {
|
||||
style: BookBadgeStyle;
|
||||
variants: readonly BookTravelMarkVariant[];
|
||||
}[] = [
|
||||
{ style: 'stamp', variants: ['date', 'day', 'flag'] },
|
||||
{ style: 'tag', variants: ['day', 'distance', 'coords', 'flag'] },
|
||||
{ style: 'pill', variants: ['date', 'day', 'distance', 'coords', 'flag'] },
|
||||
{ style: 'ribbon', variants: ['date', 'day', 'distance', 'flag'] },
|
||||
{ style: 'sticker', variants: ['date', 'day', 'distance', 'flag'] },
|
||||
{ style: 'patch', variants: ['day', 'distance', 'flag'] },
|
||||
{ style: 'sketch', variants: ['date', 'day', 'distance', 'coords', 'flag'] },
|
||||
];
|
||||
|
||||
/** The single figures, one shelf per look, with the figures each look suits. */
|
||||
export const BOOK_TRAVEL_FIGURE_SHELVES: readonly { look: BookStatsLook; metrics: readonly BookMetric[] }[] = [
|
||||
{ look: 'medal', metrics: ['distance', 'days', 'steps', 'photos', 'countries', 'places', 'furthest'] },
|
||||
{ look: 'odometer', metrics: ['distance', 'furthest', 'days', 'photos'] },
|
||||
{ look: 'sketch', metrics: ['distance', 'days', 'countries', 'photos'] },
|
||||
];
|
||||
|
||||
/** The looks a country is offered in. */
|
||||
export const BOOK_TRAVEL_COUNTRY_LOOKS: readonly BookCountryLook[] = [
|
||||
'stamp',
|
||||
'visa',
|
||||
'postcard',
|
||||
'flag',
|
||||
'pin',
|
||||
'label',
|
||||
'sketch',
|
||||
];
|
||||
|
||||
/** The looks written by hand take the theme's script face rather than its heading. */
|
||||
const HANDWRITTEN = new Set<string>(['note', 'sketch']);
|
||||
|
||||
/** Which of the theme's accents each country look takes, so a shelf of them is not one colour. */
|
||||
export const BOOK_TRAVEL_COUNTRY_ACCENT: Partial<Record<BookCountryLook, BookColorToken>> = {
|
||||
stamp: 'accent2',
|
||||
visa: 'accent3',
|
||||
pin: 'accent',
|
||||
label: 'accent2',
|
||||
};
|
||||
|
||||
/** Which of the theme's accents each mark takes, so a page of them is not one colour. */
|
||||
const MARK_ACCENT: Record<BookTravelMarkVariant, BookColorToken> = {
|
||||
date: 'accent',
|
||||
day: 'accent2',
|
||||
distance: 'accent3',
|
||||
coords: 'accent',
|
||||
flag: 'accent2',
|
||||
};
|
||||
|
||||
/** A map card: its ground and the pins it marks its stops with. */
|
||||
export interface BookTravelMapCard {
|
||||
style: BookMapElement['style'];
|
||||
pins?: BookMapElement['pinStyle'];
|
||||
}
|
||||
|
||||
export interface BookTravelLooksInput {
|
||||
page: BookPageSetup;
|
||||
stats: JourneyStats;
|
||||
/** The journey's figures, way and words, as the engine's factories read them. */
|
||||
travel: BookTravelContext;
|
||||
/** The book's theme; without one an element keeps the engine's own colours. */
|
||||
theme: BookTheme | null | undefined;
|
||||
newId: (prefix: string) => string;
|
||||
/** Where an element of this size lands when the caller names no frame of its own. */
|
||||
place: (size: { w: number; h: number }) => BookFrame;
|
||||
}
|
||||
|
||||
/** The travel elements of one book, each in the frame given or where `place` puts one of its size. */
|
||||
export function bookTravelLooks({ page, stats, travel, theme, newId, place }: BookTravelLooksInput) {
|
||||
const frameFor = (size: { w: number; h: number }, frame?: BookFrame) => frame ?? place(size);
|
||||
|
||||
/**
|
||||
* An element dressed in the book's theme: its words in the theme's ink and
|
||||
* heading face, its colour one of the theme's accents, all three linked so
|
||||
* it follows when the theme changes. A book without a theme keeps the
|
||||
* engine's own colours.
|
||||
*/
|
||||
const dress = (el: BookElement, accent: BookColorToken = 'accent', hand = false): BookElement =>
|
||||
theme
|
||||
? themedElement(
|
||||
{ ...el, token: 'ink', accentToken: accent, fontRole: hand ? 'script' : 'heading' } as BookElement,
|
||||
theme,
|
||||
)
|
||||
: el;
|
||||
|
||||
/** The size a summary is set at in a look; a band is a strip, so it is set a third as tall. */
|
||||
const summarySize = (look: BookStatsLook) => {
|
||||
const w = page.pageWidth * (look === 'note' || look === 'sign' ? 0.56 : 0.72);
|
||||
const h =
|
||||
look === 'band' ? page.pageHeight * 0.13 : look === 'ticket' ? w * 0.42 : look === 'note' ? w * 0.72 : w * 0.5;
|
||||
return { w, h };
|
||||
};
|
||||
|
||||
/** A summary in one of the newer looks, of the figures given or the four a summary offers. */
|
||||
const summaryEl = (look: BookStatsLook, frame?: BookFrame, metrics: readonly BookMetric[] = SUMMARY_METRICS) => {
|
||||
const at = frameFor(summarySize(look), frame);
|
||||
const el = { ...bookTravelStats(stats, { id: newId('st'), frame: at, metrics }), look };
|
||||
return dress(el, look === 'sign' ? 'accent3' : 'accent', HANDWRITTEN.has(look));
|
||||
};
|
||||
|
||||
/** The size a single figure is set at: a square, or the drums of an odometer. */
|
||||
const figureSize = (look: BookStatsLook) => {
|
||||
const side = Math.min(page.pageWidth, page.pageHeight) * 0.3;
|
||||
return look === 'odometer' ? { w: side * 1.6, h: side * 0.62 } : { w: side, h: side };
|
||||
};
|
||||
|
||||
/**
|
||||
* One figure on its own, the mark to drop beside a photograph: as a medal, on
|
||||
* the drums of an odometer, or ringed by hand.
|
||||
*/
|
||||
const figureEl = (metric: BookMetric, look: BookStatsLook = 'medal', frame?: BookFrame): BookElement => {
|
||||
const at = frameFor(figureSize(look), frame);
|
||||
const el = { ...bookTravelStats(stats, { id: newId('st'), frame: at, metrics: [metric] }), look };
|
||||
return dress(el, 'accent', HANDWRITTEN.has(look));
|
||||
};
|
||||
|
||||
/**
|
||||
* A map as the engine places it, on the card's ground and with the card's
|
||||
* stops. The scope is the journey or one trip; the ground is the outlines
|
||||
* or one of the two imagery sources.
|
||||
*/
|
||||
const mapEl = (
|
||||
card: BookTravelMapCard,
|
||||
scope: BookMapScope = 'journey',
|
||||
source: 'vector' | 'relief' | 'satellite' = 'vector',
|
||||
frame?: BookFrame,
|
||||
): BookElement => {
|
||||
const at = frameFor(bookTravelSize('map', page), frame);
|
||||
const el = bookTravelMap(travel, stats, { id: newId('mp'), frame: at, scope, source });
|
||||
const shaped = { ...el, style: card.style, ...(card.pins ? { pinStyle: card.pins } : {}) };
|
||||
// Over imagery the line is white on purpose (book-travel.ts); only the outline maps take the theme.
|
||||
return source === 'vector' ? dress(shaped) : shaped;
|
||||
};
|
||||
|
||||
/**
|
||||
* A map of one country: its stops alone, without the way there. A journey's
|
||||
* recorded way often starts with the flight from home, and fitted to all of
|
||||
* it the country is a speck at the edge of a continent; the page a travel
|
||||
* book wants is the country, cut to its own shape on the paper or over the
|
||||
* satellite picture, or the country in a box.
|
||||
*/
|
||||
const countryMapEl = (code: string, kind: 'shape' | 'satellite' | 'box', frame?: BookFrame): BookElement => {
|
||||
const own: JourneyStats = {
|
||||
...stats,
|
||||
points: stats.points.filter((p) => p.country === code),
|
||||
countries: stats.countries.filter((c) => c.code === code),
|
||||
};
|
||||
const ctx: BookTravelContext = { ...travel, stats: own, path: [], entries: [] };
|
||||
const source = kind === 'satellite' ? 'satellite' : 'vector';
|
||||
const at = frameFor(bookTravelSize('map', page), frame);
|
||||
const el = bookTravelMap(ctx, own, { id: newId('mp'), frame: at, scope: 'journey', source });
|
||||
// Cut to its shape on the paper, the land is dark, so the silhouette reads and the route in the accent stands on it.
|
||||
const style = kind === 'shape' ? 'dark' : 'minimal';
|
||||
const shaped = {
|
||||
...el,
|
||||
style,
|
||||
clip: kind === 'box' ? 'rect' : 'country',
|
||||
pinStyle: 'numbered',
|
||||
fitToCountries: true,
|
||||
} as const;
|
||||
return source === 'vector' ? dress(shaped) : shaped;
|
||||
};
|
||||
|
||||
/**
|
||||
* How large a mark is set: as wide as its words need. A stamp stacks them in
|
||||
* a box with room for its two rules; a tag or a pill sets them in a line
|
||||
* after its eyelet or its disc, so its length follows the words.
|
||||
*/
|
||||
const markSize = (style: BookBadgeStyle, text: string, sub: string) => {
|
||||
const twoLines = !!text && !!sub && text.length > 14;
|
||||
const h = page.pageHeight * (style === 'stamp' ? 0.12 : twoLines ? 0.1 : 0.066);
|
||||
const main = text || sub;
|
||||
if (style === 'stamp') return { w: Math.max(h * 1.3, h * 0.2 * Math.max(main.length * 1.4, sub.length)), h };
|
||||
// Two lines are set smaller than one, so they need less width a letter.
|
||||
if (twoLines)
|
||||
return { w: Math.min(page.pageWidth * 0.6, h * (1.5 + 0.13 * Math.max(text.length, sub.length * 0.7))), h };
|
||||
const chars = main.length * (text ? 1 : 0.85) + (text ? sub.length * 0.5 : 0);
|
||||
return { w: Math.min(page.pageWidth * 0.6, h * (1.6 + 0.3 * chars)), h };
|
||||
};
|
||||
|
||||
/**
|
||||
* A mark in one of the newer styles, at the size its variant suits; a stamp
|
||||
* is set taller for its two rules. The journey's value, or an entry's when
|
||||
* one is given; null when nothing says what it would show.
|
||||
*/
|
||||
const markEl = (
|
||||
variant: BookTravelMarkVariant,
|
||||
style: BookBadgeStyle,
|
||||
of: { entry?: AutoEntry | null; frame?: BookFrame } = {},
|
||||
): BookElement | null => {
|
||||
const value = bookBadgeValue(travel, variant, of.entry ?? null);
|
||||
if (!value) return null;
|
||||
const at = frameFor(markSize(style, value.text, value.sub), of.frame);
|
||||
const el = {
|
||||
...bookTravelBadge({ id: newId('bd'), frame: at, variant: variant as BookBadgeVariant, value }),
|
||||
style,
|
||||
};
|
||||
return dress(el, MARK_ACCENT[variant], style === 'sketch');
|
||||
};
|
||||
|
||||
/** The size a country is set at: a square stamp, or a card in the proportions of a postcard. */
|
||||
const countrySize = (look: BookCountryLook) => {
|
||||
const side = Math.min(page.pageWidth, page.pageHeight) * 0.36;
|
||||
const ratio = look === 'stamp' || look === 'pin' || look === 'sketch' ? 1 : look === 'postcard' ? 1.36 : 1.4;
|
||||
return { w: side * ratio, h: side };
|
||||
};
|
||||
|
||||
/** One country as a square stamp, or as a card in the proportions of a postcard. */
|
||||
const countryEl = (code: string, look: BookCountryLook, frame?: BookFrame): BookElement => {
|
||||
const at = frameFor(countrySize(look), frame);
|
||||
const el = {
|
||||
...bookTravelCountries(travel, { id: newId('co'), frame: at, codes: [code] }),
|
||||
look,
|
||||
showFlag: look === 'pin' || look === 'postcard',
|
||||
};
|
||||
return dress(el, BOOK_TRAVEL_COUNTRY_ACCENT[look] ?? 'accent', look === 'sketch');
|
||||
};
|
||||
|
||||
/** A country's name in the reader's language, from the same CLDR data Atlas uses. */
|
||||
const countryName = (code: string): string => bookCountryName(code, travel.locale, stats);
|
||||
|
||||
return {
|
||||
summaryEl,
|
||||
summarySize,
|
||||
figureEl,
|
||||
figureSize,
|
||||
mapEl,
|
||||
countryMapEl,
|
||||
markEl,
|
||||
markSize,
|
||||
countryEl,
|
||||
countrySize,
|
||||
countryName,
|
||||
};
|
||||
}
|
||||
|
||||
/** The travel elements one book builds: what `bookTravelLooks` answers. */
|
||||
export type BookTravelLooks = ReturnType<typeof bookTravelLooks>;
|
||||
@@ -163,6 +163,7 @@ export * from './book/photo-pick';
|
||||
export * from './book/book-element-ops.schema';
|
||||
export * from './book/book-doc-ops';
|
||||
export * from './book/book-travel';
|
||||
export * from './book/travel/book-travel-looks';
|
||||
export * from './book/book-text';
|
||||
export * from './book/book-path';
|
||||
export * from './book/book-icons';
|
||||
|
||||
Reference in New Issue
Block a user