mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
eae8480728
* feat(studio): widen the document, and stop a save from losing it The schema gains what the elements coming after it need: an icon element, a badge that can be restyled rather than only placed, a date and a pair of coordinates on a text binding, and the map's route, pin and road settings. Widening it turned up two ways a book could fail to come back. The mood badge writes its code into the document, and the schema said that code was exactly two characters, from a first draft where it was a country. The real codes are words, so every save was rejected and the read that followed parsed to an empty document: seventeen spreads in, nothing out. Widened to what the badge actually writes, and normalizeBookDocument now drops the elements it cannot read rather than the whole book, so one bad element costs a picture frame instead of a year of work. The other was the body limit. The parser allowed 100kb for everything, and a book with a routed map measures past that, so the save came back 413 and the editor sat on "not saved" with no way forward. Book writes get their own parser at 8mb; everything else keeps the limit it had. * feat(studio): a date, a pair of coordinates and an icon library Three things that had to be drawn by hand before. A journal entry knows when it happened and where, and neither could be put on a page: you read the date off the entry and typed it into a text box, where it then stayed while the entry moved. Both are chips now, next to the title and the story, and both follow their entry the way the others do. The formatting lives in one place so the chip, the template and the resolver cannot drift by a character — coordinates in degrees or decimals, the date in the reader's language. The icons are the whole Lucide set, searchable, placed as a real element with its own colour and stroke rather than as a character in a font. The stroke is deliberately not called strokeWidth: three separate places rescale a field of that name by page width, and an icon whose line thins as the book grows is not what anyone asked for. And the badges finally have their settings. A mood or weather badge could be placed and then only moved: the icon's colour was fixed, the icon could not be turned off, and there was no way to keep the icon and drop the words. All three are switches now, with the colour following the badge unless you say otherwise. * fix(studio): let a placed entry field follow its journal entry again The inspector says a bound field follows the journal entry and that editing it here breaks the link. The first half was not true: nothing ever re-read the entry, so renaming a title in Journey left the old words on the page for good, and the same went for every other bound field. The resolver runs where the journey is loaded and re-reads the fields that still point at an entry. What it will not do is as important as what it does. A field somebody has typed over stays typed over, an entry that is gone leaves its element alone rather than blanking it, and an entry that answers with nothing never wipes what is already on the page. Formatted fields — the date, the coordinates — are only re-read when the format itself changed, which is what keeps this from quietly rewriting books that already exist: an entry whose date was placed as a heading and then set in capitals keeps its capitals. Checked against the real database before it shipped, where the first version overwrote three of them. * feat(studio): start a new book empty, with six layouts to choose from A new journey opened onto a book that had already been laid out, which is the wrong first move: it decides the shape of somebody's book before they have seen it, and undoing it means deleting pages one at a time. A new book starts empty now, and the auto layout runs when it is asked to. The layouts themselves went from two to six, so choosing one is a real choice: what was there covered a photo-heavy spread and a text-heavy one and nothing in between. * fix(studio): hyphenate the export the way the editor does, and set the bleed Two things the press sees and the editor did not. A paragraph broke in different places in the PDF than on screen. The print document is written from scratch and never carried a language, so the browser hyphenated the editor's German and the export's nothing-in- particular. It takes the page's own language now. The wait for the sheet to be ready also covers the images inside SVG, with a ceiling, so a map that is slow to draw does not print half-finished. And the bleed and the safe margin are typeable. Both were baked into every page preset at what a photo-book vendor usually asks for, 3mm and 5mm, with no control anywhere. That is fine until a printer asks for 5 and 10, at which point the book cannot be made here at all: the export carries the wrong bleed and the crop marks sit in the wrong place, which nobody finds until the guillotine has run. * feat(studio): rebuild the inspector The panel on the right had grown by accretion: every element type had added its own controls in whatever shape suited it that day, so the same idea — a colour, a size, a toggle — looked different depending on what you had selected, and long panels were a wall of unlabelled fields. It is one set of controls now, used by every element: foldable sections, a line that holds a label and its control, radio choices, checkboxes, the app's own toggle, and a number field that holds a draft so typing "500" is not read as "5", clamped and written back under the cursor. Duplicated controls were merged rather than deleted; nothing an element could do before it cannot do now. The head carries the element's identity and its actions, so what you have selected is readable without counting fields, and the sections have room to breathe: what was there was correct and unreadable. * feat(studio): a route map worth printing The map in a book was a straight grey line between grey dots on a tile that was chosen for a screen. At print size those tiles are read at roughly four times the resolution they were fetched for, which is why it looked soft next to everything around it. The imagery comes from sources that hold up on paper: NASA's shaded relief and EOX's cloudless Sentinel-2, both licensed for it, picked at a zoom that matches the printer rather than the screen. Tiles are fetched at twice the density where the source offers it, and the seam between them scales with the tile instead of sitting at a fixed fraction of a millimetre, which under editor zoom fell below one device pixel and showed as white threads. The route is drawn rather than plotted. A leg that was never recorded bows, by an amount that grows with its length and turns away from the pole, and it dashes, because an inferred line should not claim to be a track — a recorded track never bows. Both strokes are laid down in two passes, casing then core, so the line stays legible over dark sea and bright desert alike. Legs can follow roads instead, routed leg by leg with the geometry thinned before it is stored, which is the setting a road trip wants and the one a flight cannot have. Stops carry their own first photograph, cut to a circle by a stencil that exists — the first one was written into a defs block that only appears on a map clipped to a coastline, so on an ordinary map it clipped nothing and the photographs sat square on the page. A stop with no photograph of its own is numbered rather than handed one from elsewhere in the journey: a marker at one place showing a picture taken at another is a caption that lies. Whatever the frame, the whole route is inside it. The window takes the frame's aspect, so the fit and the imagery agree instead of one containing what the other covers, and a map dragged narrow no longer cuts off its end. Each linked trip can carry its own map, and a map notices a photograph added to a stop afterwards rather than having to be placed again. * i18n(journey): the new Studio strings, in all 23 locales * test(journey): pin the coverage the map work brought with it The journey domain measures higher than its floor after this round, so the floor follows it up. Only journey moved; the generator's suggestion would have taken memories' functions down a point, and a ratchet that can fall is not a ratchet. * test(studio): cover the thinning that keeps a road route saveable The one path in roadRoute nothing exercised, and the one that matters most: a router answers a long drive with thousands of points at a precision no press can resolve, and all of it goes into the document. Keeping a leg whole is what pushed a real book past the body limit. Both ends have to survive so the line still meets its stops, and the spacing has to stay even, because cutting a run out of the middle would straighten exactly the bends somebody wanted to see.
238 lines
9.0 KiB
TypeScript
238 lines
9.0 KiB
TypeScript
import type { BookElement, BookPageSetup, BookSpread, JourneyStats } from '@trek/shared'
|
|
import type { SpreadTemplate } from './bookTemplates.data'
|
|
import { formatBookCoords, formatBookDate } from './entryText'
|
|
import { coordValue } from './resolveBindings'
|
|
|
|
/**
|
|
* Laying an entry out on a template somebody drew.
|
|
*
|
|
* ── Why this exists ──────────────────────────────────────────────────────
|
|
*
|
|
* The layouts written as code in autoLayout.ts are what a function can reason
|
|
* its way to: a picture here, words there, margins that add up. What they are
|
|
* not is *designed* — a panel bled off one edge, a shape running off the
|
|
* corner, a strip of pictures at two sizes with the coordinates set under them.
|
|
* Those decisions come from looking at a page, and the editor is where looking
|
|
* at a page happens.
|
|
*
|
|
* So the templates are drawn in Studio and read back out (see
|
|
* bookTemplates.data.ts). This is the other half: taking one of those spreads
|
|
* and putting an entry into it.
|
|
*
|
|
* ── What it fills, and what it leaves alone ──────────────────────────────
|
|
*
|
|
* Only what the entry can answer for:
|
|
*
|
|
* - text carrying a `binding` — the title, the story, a caption
|
|
* - empty photo frames, taking the entry's photographs in order
|
|
* - the day, coordinate, date and country marks
|
|
* - the stats and countries elements, from the journey's figures
|
|
*
|
|
* Everything else is the design and is copied as drawn. A panel stays where it
|
|
* was put; a decorative shape keeps its colour. That division is the whole
|
|
* contract between the person who draws a template and the code that uses it.
|
|
*
|
|
* ── Sizes ────────────────────────────────────────────────────────────────
|
|
*
|
|
* The template's numbers are fractions of the page it was drawn on, so they are
|
|
* multiplied back up here. A template built on a 210mm square lays out on an A4
|
|
* landscape book without being redrawn.
|
|
*/
|
|
|
|
export interface TemplateEntry {
|
|
id: number
|
|
title: string | null
|
|
story: string | null
|
|
location: string | null
|
|
date: string | null
|
|
photos: { photoId: number; width: number | null; height: number | null; caption?: string | null }[]
|
|
lat?: number | null
|
|
lng?: number | null
|
|
country?: string | null
|
|
dayNumber?: number | null
|
|
dayCount?: number | null
|
|
}
|
|
|
|
export interface TemplateContext {
|
|
page: BookPageSetup
|
|
locale: string
|
|
stats: JourneyStats | null
|
|
/** Already translated, because this module has no translation context. */
|
|
dayLabel: string
|
|
}
|
|
|
|
let seq = 0
|
|
const uid = (p: string) => `${p}-${(seq++).toString(36)}-${Math.floor(Math.random() * 1e6).toString(36)}`
|
|
|
|
/** How well a template suits an entry: more is better, -1 means unusable. */
|
|
export function templateFit(template: SpreadTemplate, entry: TemplateEntry): number {
|
|
const frames = template.elements.filter(e => e.kind === 'photo').length
|
|
const photos = entry.photos.length
|
|
const wantsStory = template.elements.some(e => e.kind === 'text' && e.binding?.source === 'entry.story')
|
|
const hasStory = !!(entry.story || '').trim()
|
|
|
|
/*
|
|
* A template is a template *for an entry* when it uses one.
|
|
*
|
|
* The distinction has to come out of the drawing rather than from a label
|
|
* somebody remembers to set: a summary spread is all figures and country
|
|
* outlines and reads perfectly well on its own, and dropping a day's
|
|
* photographs into it would be nonsense. Bindings are how a page says it
|
|
* expects an entry.
|
|
*/
|
|
const usesEntry = template.elements.some(
|
|
e => ('binding' in e && e.binding) || (e.kind === 'photo' && e.photoId == null),
|
|
)
|
|
if (!usesEntry) return -1
|
|
|
|
/*
|
|
* A template with three frames and an entry with one picture leaves two
|
|
* placeholders on a finished page, which is worse than a simpler layout.
|
|
* More pictures than frames is fine — the extra ones are parked.
|
|
*/
|
|
if (frames > photos + 1) return -1
|
|
if (wantsStory && !hasStory) return -1
|
|
|
|
// Closest match wins: a two-frame template for two pictures beats a one-frame
|
|
// one, and both beat a template that would leave a hole.
|
|
return 100 - Math.abs(frames - photos) * 10 - (wantsStory === hasStory ? 0 : 5)
|
|
}
|
|
|
|
/** Fill a template with an entry, and hand back the spread it makes. */
|
|
export function applyTemplate(
|
|
template: SpreadTemplate,
|
|
entry: TemplateEntry,
|
|
ctx: TemplateContext,
|
|
): BookSpread {
|
|
const { page } = ctx
|
|
const PW = page.pageWidth
|
|
const PH = page.pageHeight
|
|
|
|
let nextPhoto = 0
|
|
const story = (entry.story || '').trim()
|
|
const heading = entry.title || entry.location || ''
|
|
|
|
const elements = template.elements.map(el => {
|
|
const out = {
|
|
...el,
|
|
id: uid(el.kind[0]),
|
|
frame: {
|
|
x: round2(el.frame.x * PW),
|
|
y: round2(el.frame.y * PH),
|
|
w: round2(el.frame.w * PW),
|
|
h: round2(el.frame.h * PH),
|
|
},
|
|
} as BookElement
|
|
|
|
// Sizes came out as fractions of the page height, so they go back up.
|
|
if ('size' in out && typeof out.size === 'number') out.size = round2(out.size * PH)
|
|
if ('radius' in out && typeof out.radius === 'number') out.radius = round2(out.radius * PW)
|
|
if ('strokeWidth' in out && typeof out.strokeWidth === 'number') {
|
|
out.strokeWidth = round2(out.strokeWidth * PW)
|
|
}
|
|
|
|
if (out.kind === 'text' && out.binding) {
|
|
const source = out.binding.source
|
|
const format = out.binding.format
|
|
// What the words were made from, where they were made rather than copied.
|
|
let value: string | undefined
|
|
if (source === 'entry.title') out.text = heading
|
|
else if (source === 'entry.story') out.text = story
|
|
else if (source === 'entry.date') {
|
|
out.text = formatBookDate(entry.date, ctx.locale)
|
|
value = entry.date ?? undefined
|
|
}
|
|
// The place, as its name or as its point — see the `format` note in the
|
|
// contract. A stop the entry cannot answer keeps the words the template
|
|
// was drawn with, the same bargain fillBadge makes below.
|
|
else if (source === 'entry.location') {
|
|
if (format && entry.lat != null && entry.lng != null) {
|
|
out.text = formatBookCoords(entry.lat, entry.lng, format)
|
|
value = coordValue(entry.lat, entry.lng)
|
|
} else if (!format && entry.location) {
|
|
out.text = entry.location
|
|
}
|
|
}
|
|
out.binding = { ...out.binding, entryId: entry.id, ...(value ? { value } : {}) }
|
|
}
|
|
|
|
if (out.kind === 'photo' && out.photoId == null) {
|
|
const photo = entry.photos[nextPhoto++]
|
|
if (photo) out.photoId = photo.photoId
|
|
}
|
|
|
|
if (out.kind === 'badge') fillBadge(out, entry, ctx)
|
|
|
|
if (out.kind === 'countries' && ctx.stats) {
|
|
out.codes = ctx.stats.countries.map(c => c.code)
|
|
out.names = ctx.stats.countries.map(c => c.name)
|
|
}
|
|
|
|
// The figures live in one map on the element, keyed by metric — the same
|
|
// shape the panels write when a stats element is placed by hand.
|
|
if (out.kind === 'stats' && ctx.stats) {
|
|
out.values = {
|
|
distance: ctx.stats.distance,
|
|
days: ctx.stats.days,
|
|
steps: ctx.stats.steps,
|
|
photos: ctx.stats.photos,
|
|
places: ctx.stats.places,
|
|
countries: ctx.stats.countries.length,
|
|
furthest: ctx.stats.furthest,
|
|
}
|
|
}
|
|
|
|
return out
|
|
})
|
|
|
|
return {
|
|
id: uid('sp'),
|
|
role: 'inner',
|
|
background: template.background,
|
|
elements,
|
|
parked: [],
|
|
entryId: entry.id,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The small marks, filled from the stop.
|
|
*
|
|
* A badge whose value the entry cannot answer keeps the one it was drawn with:
|
|
* a template showing "DAY 1" on an entry with no date is a template that reads
|
|
* as a placeholder, which is exactly what it is until a date exists.
|
|
*/
|
|
function fillBadge(el: BookElement & { kind: 'badge' }, entry: TemplateEntry, ctx: TemplateContext) {
|
|
if (el.variant === 'day' && entry.dayNumber) {
|
|
el.text = `${ctx.dayLabel} ${entry.dayNumber}`
|
|
}
|
|
if (el.variant === 'coords' && entry.lat != null && entry.lng != null) {
|
|
el.text = formatBookCoords(entry.lat, entry.lng)
|
|
}
|
|
if ((el.variant === 'flag' || el.variant === 'country') && entry.country) {
|
|
el.code = entry.country
|
|
el.text = countryName(entry.country, ctx).toUpperCase()
|
|
}
|
|
if (el.variant === 'date' && entry.date) {
|
|
const d = new Date(`${entry.date}T00:00:00`)
|
|
if (!Number.isNaN(d.getTime())) {
|
|
el.text = String(d.getDate())
|
|
el.sub = d.toLocaleDateString(ctx.locale, { month: 'long' }).toUpperCase()
|
|
}
|
|
}
|
|
if (el.variant === 'distance' && ctx.stats?.distance) {
|
|
el.text = `${Math.round(ctx.stats.distance / 1000).toLocaleString('en-US')} km`
|
|
}
|
|
}
|
|
|
|
function countryName(code: string, ctx: TemplateContext): string {
|
|
const known = ctx.stats?.countries.find(c => c.code === code)
|
|
try {
|
|
return new Intl.DisplayNames([ctx.locale], { type: 'region' }).of(code) ?? known?.name ?? code
|
|
} catch {
|
|
return known?.name ?? code
|
|
}
|
|
}
|
|
|
|
const round2 = (n: number) => Math.round(n * 100) / 100
|