diff --git a/shared/src/book/auto/auto-elements.ts b/shared/src/book/auto/auto-elements.ts index 92a58c70f..72f55f763 100644 --- a/shared/src/book/auto/auto-elements.ts +++ b/shared/src/book/auto/auto-elements.ts @@ -182,6 +182,11 @@ export function mapEl(newId: NewId, frame: BookFrame, stats: JourneyStats, opts: fitPadding: 0.5, fitToCountries: true, clip: 'rect', + // A map of the whole journey, said in so many words (revision 12): without them it reads as a map saved before, + // and one of a journey that later reaches a second country would be taken for a map of the first. + country: null, + dayDate: null, + stop: null, ...opts, } as BookElement; } diff --git a/shared/src/book/auto/auto-layout.parity.json b/shared/src/book/auto/auto-layout.parity.json index e2c389855..b4bb14147 100644 --- a/shared/src/book/auto/auto-layout.parity.json +++ b/shared/src/book/auto/auto-layout.parity.json @@ -4,7 +4,7 @@ "A/full/head": "e030d61f8d2aa9c5", "A/full/spread/0": "6036ca6b48d6e248", "A/full/spread/1": "bf4b62404c36aa0e", - "A/full/spread/2": "688f4ce239f0eb65", + "A/full/spread/2": "9b59b4cb758c2f63", "A/full/spread/3": "3bc5ecd3ad02ad83", "A/full/spread/4": "6fdb7d865a3534c5", "A/full/spread/5": "79c58347ba49449c", @@ -33,7 +33,7 @@ "A/plain/head": "e030d61f8d2aa9c5", "A/plain/spread/0": "6036ca6b48d6e248", "A/plain/spread/1": "bf4b62404c36aa0e", - "A/plain/spread/2": "688f4ce239f0eb65", + "A/plain/spread/2": "9b59b4cb758c2f63", "A/plain/spread/3": "3bc5ecd3ad02ad83", "A/plain/spread/4": "6fdb7d865a3534c5", "A/plain/spread/5": "637abb25cc53282f", diff --git a/shared/src/book/book-travel.spec.ts b/shared/src/book/book-travel.spec.ts index 2ade149fb..01f17ce18 100644 --- a/shared/src/book/book-travel.spec.ts +++ b/shared/src/book/book-travel.spec.ts @@ -359,16 +359,23 @@ describe('the scope of a map', () => { const iceland = bookTravelMap(ctx, stats, { id: 'mp-c', frame, scope: { country: 'is' } }); expect(valid(iceland)).toBe(true); expect(iceland).toMatchObject({ country: 'IS', countries: ['IS'], tripId: null, path: [] }); - expect(iceland).not.toHaveProperty('stop'); + expect(iceland).toMatchObject({ stop: null, dayDate: null }); expect(refreshPatch(iceland, stats)).toMatchObject({ countries: ['IS'] }); expect(refreshPatch(iceland, stats)).not.toHaveProperty('country'); const day = bookTravelMap(ctx, dated, { id: 'mp-d', frame, scope: { dayDate: '2026-06-06', tripId: 11 } }); expect(day).toMatchObject({ dayDate: '2026-06-06', tripId: 11, countries: ['IS'] }); expect(day.points.map((p) => p.label)).toEqual(['Akureyri']); expect(isStale(day, dated)).toBe(false); - // A map of the whole journey or of a trip writes nothing new: what it wrote before revision 12. + // A map of the whole journey or of a trip says so too, so it is never read as a map saved before revision 12. const whole = bookTravelMap(ctx, stats, { id: 'mp-w', frame, scope: 'journey' }); - for (const key of ['stop', 'country', 'dayDate', 'view', 'coastDetail']) expect(whole).not.toHaveProperty(key); + expect(whole).toMatchObject({ stop: null, country: null, dayDate: null, tripId: null }); + for (const key of ['view', 'coastDetail']) expect(whole).not.toHaveProperty(key); + expect(bookTravelMap(ctx, stats, { id: 'mp-t', frame, scope: { tripId: 11 } })).toMatchObject({ + tripId: 11, + stop: null, + country: null, + dayDate: null, + }); }); it('BOOKTRV-017: widens a map to the journey only when it is told it is of the journey', () => { @@ -379,6 +386,38 @@ describe('the scope of a map', () => { expect(routeFor(stats, null)).toContainEqual(lisboa); }); + it('BOOKTRV-019: keeps a new map of a one-country journey the journey once it reaches a second country', () => { + const iceland = { ...stats, countries: stats.countries.slice(0, 1), points: stats.points.slice(0, 2) }; + const placed = bookTravelMap({ ...ctx, path: [] }, iceland, { id: 'mp-w', frame, scope: 'journey' }); + expect(placed.path).toEqual([]); + // Read back from the store, as the next session gets it. + const saved = bookMapElementSchema.parse(JSON.parse(JSON.stringify(placed))); + expect(bookMapScopeOf(saved, stats)).toMatchObject({ country: null, stop: null, inferred: false }); + expect(isStale(saved, stats)).toBe(true); + expect((refreshPatch(saved, stats) as { countries: string[] }).countries).toEqual(['IS', 'PT']); + // One stop at first is no different. + const first = { ...iceland, points: stats.points.slice(0, 1) }; + const single = bookTravelMap({ ...ctx, path: [] }, first, { id: 'mp-1', frame, scope: 'journey' }); + expect((refreshPatch(single, stats) as { points: unknown[] }).points).toHaveLength(3); + }); + + it('BOOKTRV-020: writes down a plain scope of an old map, and leaves an ambiguous one to be read', () => { + // A trip map and a tracked journey map from before revision 12 settle as what they are. + const oldTrip = asMap({ tripId: 11, countries: ['IS'], points: [point(reykjavik), point(akureyri)] }); + expect(refreshPatch(oldTrip, stats)).toMatchObject({ country: null, dayDate: null, stop: null }); + const tracked = asMap({ + countries: ['IS', 'PT'], + points: [point(reykjavik), point(akureyri), point(lisboa)], + path: [[[64, -21]]], + }); + expect(refreshPatch(tracked, stats)).toMatchObject({ country: null, dayDate: null, stop: null }); + // An old country map is read as its country and says so from then on. + expect(refreshPatch(oldCountryMap, stats)).toMatchObject({ country: 'IS', dayDate: null, stop: null }); + // Two stops in one country on a one-country journey could be the country's map or the journey's: not settled. + const oneCountry = { ...stats, countries: stats.countries.slice(0, 1) }; + expect(refreshPatch(oldCountryMap, oneCountry)).not.toHaveProperty('country'); + }); + it('BOOKTRV-018: finds the stop of an entry on the route, else where the entry says, else none', () => { expect(bookMapStopFor(stats, [], 3)).toEqual({ entryId: 3, lat: 38.72, lng: -9.14 }); expect(bookMapStopFor(null, [entry], 2)).toEqual({ entryId: 2, lat: 65.68, lng: -18.12 }); diff --git a/shared/src/book/book-travel.ts b/shared/src/book/book-travel.ts index e55aa2690..747f02150 100644 --- a/shared/src/book/book-travel.ts +++ b/shared/src/book/book-travel.ts @@ -195,13 +195,28 @@ function mapCountries(stats: JourneyStats, whole: boolean, route: readonly Journ const mapPoints = (route: readonly JourneyStatsPoint[]) => route.map((p) => ({ lat: p.lat, lng: p.lng, label: p.label, photoId: p.photoId ?? null })); -/** What a map of these parts writes down beside its stops: only the parts that narrow it. */ -function scopeFields(parts: BookMapScopeParts): Pick, 'country' | 'dayDate' | 'stop'> { - return { - ...(parts.country ? { country: parts.country } : {}), - ...(parts.dayDate ? { dayDate: parts.dayDate } : {}), - ...(parts.stop ? { stop: parts.stop } : {}), - }; +/** + * What a map of these parts writes down beside its stops: every part, null + * where it does not narrow the map. A map of the whole journey says so too, + * since a map without the fields reads as one saved before revision 12 and + * has its scope guessed off what it holds (`bookMapScopeOf`). + */ +function bookMapScopeFields( + parts: Pick, +): Pick { + return { country: parts.country ?? null, dayDate: parts.dayDate ?? null, stop: parts.stop ?? null }; +} + +/** + * Whether a refresh writes an old map's scope down: when it was read off the + * map, and when the map plainly is of its trip or of the journey (several + * stops across several countries, or a recorded track). A map that could be + * either a country's or a one-country journey's keeps being read afresh. + */ +function settlesScope(el: BookMapElement, scope: BookMapScopeParts & { inferred: boolean }): boolean { + if (!fromBeforeScopes(el)) return false; + if (scope.inferred || scope.tripId != null) return true; + return el.points.length > 1 && (el.countries.length > 1 || el.path.length > 0); } /** A map of one stop brought up to date: the stop where it is now, and its picture where it had one. */ @@ -246,7 +261,7 @@ export function refreshPatch(el: BookElement, stats: JourneyStats): Partial { const map = out.elements.find((e) => e.kind === 'map'); expect(map).toMatchObject({ countries: ['PT'], style: 'minimal' }); expect(map?.kind === 'map' && map.points[0]?.label).toBe('Lisbon'); + // What the page's map is of and the ground it was framed to go into the template with its stops. + const view = { west: -10, south: 38, east: -8, north: 40 }; + const lisbon = bookMapElementSchema.parse({ ...own, country: 'PT', dayDate: null, stop: null, view }); + const poured = lay(page('inner', [lisbon, photo('ph1', 51)]), 'map-tickets').elements.find((e) => e.kind === 'map'); + expect(poured).toMatchObject({ country: 'PT', dayDate: null, stop: null, view, points: [{ label: 'Lisbon' }] }); const mapless = lay( page( 'inner', diff --git a/shared/src/book/templates/template-apply.ts b/shared/src/book/templates/template-apply.ts index 150e43ee8..7bbe68e79 100644 --- a/shared/src/book/templates/template-apply.ts +++ b/shared/src/book/templates/template-apply.ts @@ -74,7 +74,30 @@ function pouredMap( (travel?.stats ? bookTravelMap(travel, travel.stats, { id: slot.id, frame: slot.frame, scope: 'journey' }) : null); if (!from) return null; const { countries, points, path, roads, tripId, source, tileUrl, attribution, zoom } = from; - return { ...slot, countries, points, path, roads, tripId, source, tileUrl, attribution, zoom }; + /* + * What the map is of and the ground it was framed to travel with its stops + * (revision 12), or a country map poured into the template would come out + * as a map of nothing in particular, and the first refresh would widen it. + * A field the map never had stays absent, so an older map is still read off + * what it holds. + */ + const { country, dayDate, stop, view } = from; + return { + ...slot, + countries, + points, + path, + roads, + tripId, + source, + tileUrl, + attribution, + zoom, + country, + dayDate, + stop, + view, + }; } /** A role's text in the template's look, set in the page's words and keeping their link to the journal. */ diff --git a/shared/src/book/templates/template-extract.spec.ts b/shared/src/book/templates/template-extract.spec.ts index 7126b2c08..f60bd7816 100644 --- a/shared/src/book/templates/template-extract.spec.ts +++ b/shared/src/book/templates/template-extract.spec.ts @@ -102,7 +102,15 @@ describe('a drawn page as a template', () => { fillPaint: { type: 'photo', photoId: 5 }, }), bookTextElementSchema.parse({ id: 'c', kind: 'text', frame, text: '“' }), - bookMapElementSchema.parse({ id: 'd', kind: 'map', frame, countries: ['PT'], tileUrl: 'https://tiles' }), + bookMapElementSchema.parse({ + id: 'd', + kind: 'map', + frame, + countries: ['PT'], + tileUrl: 'https://tiles', + stop: { entryId: 4, lat: 38.7, lng: -9.1 }, + view: { west: -10, south: 38, east: -8, north: 40 }, + }), bookShapeElementSchema.parse({ id: 'e', kind: 'shape', frame, hidden: true }), ]; const tpl = bookTemplateFromSpread(spreadOf('cover', els), square, meta); @@ -112,6 +120,8 @@ describe('a drawn page as a template', () => { expect(title).toMatchObject({ kind: 'text', text: '', binding: null, fillPaint: null, fillPhoto: 'photo:1' }); expect(quote).toMatchObject({ kind: 'text', text: '“' }); expect(map).toMatchObject({ kind: 'map', countries: [], tileUrl: '', role: 'map' }); + // Where the map was and what of are the journey's, not the template's. + for (const key of ['stop', 'country', 'dayDate', 'view']) expect(map).not.toHaveProperty(key); expect(tpl.kind).toBe('single'); expect(tpl.labelKey).toBe('studio.templates.name.drawnOne'); }); diff --git a/shared/src/book/templates/template-extract.ts b/shared/src/book/templates/template-extract.ts index 8e40a73a4..b155332cf 100644 --- a/shared/src/book/templates/template-extract.ts +++ b/shared/src/book/templates/template-extract.ts @@ -165,8 +165,11 @@ function frameToFractions(el: BookElement, keep: boolean, m: Measures) { /** The content taken out of an element that takes a role: its picture, its words, its journey. */ function withoutContent(out: Loose, role: BookTemplateRole | undefined): void { if (out.kind === 'photo') out.photoId = null; - if (out.kind === 'map') + if (out.kind === 'map') { Object.assign(out, { countries: [], points: [], path: [], roads: [], tileUrl: '', attribution: '' }); + // What the map was of and where it was framed are the journey's too: a stop's place, a country, a day. + for (const key of ['stop', 'country', 'dayDate', 'view']) delete out[key]; + } if (out.kind === 'text' && role && (BOOK_TEMPLATE_TEXT_ROLES as readonly string[]).includes(role)) { Object.assign(out, { text: '', binding: null, overridden: false }); }