mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
feat(studio): follow-ups from using Studio on a real journey (#1984)
* 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.
This commit is contained in:
@@ -111,10 +111,31 @@ export async function buildApp(): Promise<INestApplication> {
|
||||
const rawBodyKeeper = (req: Request, _res: Response, buffer: Buffer) => {
|
||||
if (Buffer.isBuffer(buffer)) (req as Request & { rawBody?: Buffer }).rawBody = buffer;
|
||||
};
|
||||
/*
|
||||
* ── Why one route gets a larger body than the rest ───────────────────
|
||||
*
|
||||
* A hundred kilobytes is the right ceiling for an API of forms and ids, and
|
||||
* it is the wrong one for a photo book. A Studio document is sent WHOLE on
|
||||
* every autosave — deliberately, see book-store.schema.ts, because a patch
|
||||
* protocol would need an ordering guarantee and a merge rule per field — and
|
||||
* the contract already caps it at 150 spreads of 60 elements. A real book of
|
||||
* a fortnight's journey is well past a hundred kilobytes before anybody asks
|
||||
* for road geometry, and what the ceiling produces is not an error message
|
||||
* but a save that quietly fails and an editor that says "not saved" without
|
||||
* saying why.
|
||||
*
|
||||
* So the book route, and only the book route, is measured against the size a
|
||||
* book can actually be. Everything else keeps the tighter limit.
|
||||
*/
|
||||
const bookBody = express.json({ limit: '8mb', verify: rawBodyKeeper });
|
||||
const json = express.json({ limit: '100kb', verify: rawBodyKeeper });
|
||||
const urlencoded = express.urlencoded({ limit: '100kb', extended: true, verify: rawBodyKeeper });
|
||||
const isMcp = (req: Request) => req.path === '/mcp' || req.path === '/mcp/';
|
||||
const isBookWrite = (req: Request) =>
|
||||
req.method === 'PUT' && /^\/api\/journeys\/\d+\/book$/.test(req.path);
|
||||
|
||||
instance.use(function jsonParser(req: Request, res: Response, next: NextFunction) {
|
||||
if (isBookWrite(req)) return bookBody(req, res, next);
|
||||
return isMcp(req) ? next() : json(req, res, next);
|
||||
});
|
||||
instance.use(function urlencodedParser(req: Request, res: Response, next: NextFunction) {
|
||||
|
||||
@@ -889,20 +889,32 @@ export class JourneyDomainService {
|
||||
if (!this.canAccessJourney(journeyId, userId)) return null;
|
||||
|
||||
const entryRows = this.db.prepare(`
|
||||
SELECT id, title, location_name, location_lat, location_lng, entry_date
|
||||
SELECT id, title, location_name, location_lat, location_lng, entry_date, source_trip_id
|
||||
FROM journey_entries
|
||||
WHERE journey_id = ?
|
||||
ORDER BY entry_date ASC, sort_order ASC, id ASC
|
||||
`).all(journeyId) as {
|
||||
id: number; title: string | null; location_name: string | null;
|
||||
location_lat: number | null; location_lng: number | null; entry_date: string | null;
|
||||
source_trip_id: number | null;
|
||||
}[];
|
||||
|
||||
const tripDates = this.db.prepare(`
|
||||
SELECT t.start_date AS start, t.end_date AS end
|
||||
/*
|
||||
* The trips themselves, named and dated.
|
||||
*
|
||||
* Ordered by the trip's own start date: `journey_trips` had a `sort_order`
|
||||
* for one migration and has not had one since 87, so the link row carries
|
||||
* no order to read. Undated trips sort last rather than first, where an
|
||||
* empty string would otherwise put them.
|
||||
*/
|
||||
const tripRows = this.db.prepare(`
|
||||
SELECT t.id, t.title, t.start_date AS start, t.end_date AS end
|
||||
FROM journey_trips jt JOIN trips t ON t.id = jt.trip_id
|
||||
WHERE jt.journey_id = ?
|
||||
`).all(journeyId) as { start: string | null; end: string | null }[];
|
||||
ORDER BY t.start_date IS NULL, t.start_date ASC, t.id ASC
|
||||
`).all(journeyId) as { id: number; title: string | null; start: string | null; end: string | null }[];
|
||||
|
||||
const tripDates = tripRows.map(t => ({ start: t.start, end: t.end }));
|
||||
|
||||
// Ordered the way the trip is walked: by day, then by the order within it.
|
||||
// A place can be assigned to more than one day (a hotel across three nights
|
||||
@@ -910,7 +922,7 @@ export class JourneyDomainService {
|
||||
// one row per place at its earliest day — otherwise the route would visit
|
||||
// the hotel three times and the distance would count those legs.
|
||||
const placeRows = this.db.prepare(`
|
||||
SELECT p.id, p.name, p.lat, p.lng,
|
||||
SELECT p.id, p.name, p.lat, p.lng, p.trip_id AS tripId,
|
||||
MIN(d.date) AS day,
|
||||
MIN(da.order_index) AS ord
|
||||
FROM journey_trips jt
|
||||
@@ -921,7 +933,7 @@ export class JourneyDomainService {
|
||||
GROUP BY p.id
|
||||
ORDER BY day IS NULL, day ASC, ord ASC, p.id ASC
|
||||
`).all(journeyId) as {
|
||||
id: number; name: string | null; lat: number | null; lng: number | null;
|
||||
id: number; name: string | null; lat: number | null; lng: number | null; tripId: number | null;
|
||||
day: string | null; ord: number | null;
|
||||
}[];
|
||||
|
||||
@@ -935,6 +947,41 @@ export class JourneyDomainService {
|
||||
.prepare('SELECT COUNT(*) AS n FROM journey_photos WHERE journey_id = ?')
|
||||
.get(journeyId) as { n: number };
|
||||
|
||||
/*
|
||||
* ── A photograph per stop, for a map that marks them with pictures ───
|
||||
*
|
||||
* The truthful source is the junction: the photos somebody actually
|
||||
* attached to that entry, earliest first, videos excluded because a video
|
||||
* poster inside a four-millimetre circle is not a photograph.
|
||||
*/
|
||||
const entryPhotoRows = this.db.prepare(`
|
||||
SELECT jep.entry_id AS entryId, gp.photo_id AS photoId
|
||||
FROM journey_entry_photos jep
|
||||
JOIN journey_photos gp ON gp.id = jep.journey_photo_id
|
||||
JOIN trek_photos tp ON tp.id = gp.photo_id
|
||||
WHERE gp.journey_id = ?
|
||||
AND (tp.media_type IS NULL OR tp.media_type = 'image')
|
||||
ORDER BY jep.entry_id ASC, jep.sort_order ASC, gp.sort_order ASC, gp.id ASC
|
||||
`).all(journeyId) as { entryId: number; photoId: number }[];
|
||||
|
||||
const photoByEntry = new Map<number, number>();
|
||||
for (const r of entryPhotoRows) if (!photoByEntry.has(r.entryId)) photoByEntry.set(r.entryId, r.photoId);
|
||||
|
||||
/*
|
||||
* ── And no second tier, deliberately ─────────────────────────────────
|
||||
*
|
||||
* The obvious fallback is to hand the journey's gallery out in blocks, the
|
||||
* way the auto layout hands photographs to pages, so that a journey with
|
||||
* fifty-seven pictures and an empty junction still gets pictures on its
|
||||
* map. It was written that way first and it was wrong: a marker at
|
||||
* Akureyri showing a photograph taken in Vík is a caption that lies, and a
|
||||
* map is exactly where a reader trusts that a picture is OF the place it
|
||||
* sits on.
|
||||
*
|
||||
* So a stop shows its own first photograph or it shows a number. The
|
||||
* number is not a degraded state; it is the honest one.
|
||||
*/
|
||||
|
||||
// The cached country per place, for whichever of them Atlas has seen.
|
||||
const cachedCountry = new Map<number, string>();
|
||||
const placeIds = placeRows.map(p => p.id);
|
||||
@@ -963,6 +1010,8 @@ export class JourneyDomainService {
|
||||
label: e.title || e.location_name || '',
|
||||
date: e.entry_date ?? null,
|
||||
country: countryAt(e.location_lat as number, e.location_lng as number),
|
||||
tripId: e.source_trip_id ?? null,
|
||||
photoId: photoByEntry.get(e.id) ?? null,
|
||||
}));
|
||||
|
||||
const points: StatsInputPoint[] = fromEntries.length
|
||||
@@ -975,8 +1024,26 @@ export class JourneyDomainService {
|
||||
label: p.name || '',
|
||||
date: p.day ?? null,
|
||||
country: countryAt(p.lat as number, p.lng as number, p.id),
|
||||
tripId: p.tripId ?? null,
|
||||
// A trip place carries `image_url`, which is a provider photo behind
|
||||
// its own attribution rather than a trek_photos id. It must not be
|
||||
// smuggled into a field the book will print as one of the journey's
|
||||
// own pictures.
|
||||
photoId: null,
|
||||
}));
|
||||
|
||||
/*
|
||||
* How many of the route's stops each trip owns.
|
||||
*
|
||||
* Counted rather than assumed: a linked trip whose entries nobody wrote,
|
||||
* or whose places carry no coordinates, contributes nothing to the line and
|
||||
* offering a map of it would produce an empty frame.
|
||||
*/
|
||||
const perTrip = new Map<number, number>();
|
||||
for (const p of points) {
|
||||
if (p.tripId != null) perTrip.set(p.tripId, (perTrip.get(p.tripId) ?? 0) + 1);
|
||||
}
|
||||
|
||||
return computeJourneyStats({
|
||||
journeyId,
|
||||
points,
|
||||
@@ -985,6 +1052,13 @@ export class JourneyDomainService {
|
||||
places: placeCount?.n ?? 0,
|
||||
tripDates,
|
||||
countryNames: countryNamesFor(points),
|
||||
trips: tripRows.map(t => ({
|
||||
id: t.id,
|
||||
title: t.title || '',
|
||||
start: t.start,
|
||||
end: t.end,
|
||||
points: perTrip.get(t.id) ?? 0,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { JourneyStats, JourneyStatsCountry, JourneyStatsPoint } from '@trek/shared';
|
||||
import type { JourneyStats, JourneyStatsCountry, JourneyStatsPoint, JourneyStatsTrip } from '@trek/shared';
|
||||
|
||||
/**
|
||||
* What a journey adds up to — the arithmetic, with no database and no Nest.
|
||||
@@ -23,6 +23,10 @@ export interface StatsInputPoint {
|
||||
/** ISO date, when the stop has one. */
|
||||
date: string | null;
|
||||
country: string | null;
|
||||
/** The trip this stop belongs to, when the journey knows. */
|
||||
tripId?: number | null;
|
||||
/** One photograph from this stop, for a map that marks it with a picture. */
|
||||
photoId?: number | null;
|
||||
}
|
||||
|
||||
const rad = (deg: number) => (deg * Math.PI) / 180;
|
||||
@@ -160,6 +164,8 @@ export interface StatsInput {
|
||||
/** Dates from the trips, used when the entries carry none. */
|
||||
tripDates: { start: string | null; end: string | null }[];
|
||||
countryNames: Record<string, string>;
|
||||
/** The trips themselves, so a book can print a map of one of them. */
|
||||
trips?: JourneyStatsTrip[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,6 +204,20 @@ export function computeJourneyStats(input: StatsInput): JourneyStats {
|
||||
label: p.label,
|
||||
date: p.date,
|
||||
country: p.country,
|
||||
tripId: p.tripId ?? null,
|
||||
photoId: p.photoId ?? null,
|
||||
})),
|
||||
/*
|
||||
* Counted after thinning, not before.
|
||||
*
|
||||
* `thinRoute` drops every n-th stop on a long route, so a trip that had
|
||||
* forty stops and kept nine has nine on the map. A panel offering "Iceland
|
||||
* (40)" and then drawing nine would be counting something the reader
|
||||
* cannot see.
|
||||
*/
|
||||
trips: (input.trips ?? []).map(t => ({
|
||||
...t,
|
||||
points: points.filter(p => p.tripId === t.id).length,
|
||||
})),
|
||||
start,
|
||||
end,
|
||||
|
||||
Reference in New Issue
Block a user