mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
fix(studio): a restore brings in no photo from outside the journey's gallery, letter fills counted
restore_book_layout took any layout the caller sent, so a layout naming a photo of another journey was kept, and laying a page out on it put that photo into this book while place_photo refused the same id. The layout is now held to the rule a page restore states: every photo it names, in a frame, on a map marker or in the letters of a text, is in the gallery or on a page of the book already, or the call is refused with photo_not_in_gallery. restore_book_page and the design apply read the photos of a page in one place now (book-photo-refs.ts), which counts the photo a text is filled with. A page put back with letters filled with a photo from outside the gallery is refused as a frame of it would be, and a designed page whose letter fill photo left the gallery since is skipped as photo_gone; on a new book's cover the letters keep their colour instead, as its frame is emptied. A saved layout keeps no photo of the page it was taken from in its letters, as it keeps none in its frames, so laying another page out on it no longer carries that photo along. A layout kept before this fills such letters with the first photo of the page it is laid on, or leaves them in their colour. The style operation checks a letter fill photo only when the change sets the fill, so a title whose photo left the gallery can still be resized or recoloured, as the text operation already allowed.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
bookSpreadPhotoIds,
|
||||
compareCodeUnits,
|
||||
MAX_SPREADS,
|
||||
type BookDesignPlanRow,
|
||||
@@ -53,15 +54,6 @@ export function canonical(value: unknown): string {
|
||||
const samePage = (a: BookSpread | undefined, b: BookSpread | undefined) =>
|
||||
a !== undefined && b !== undefined && canonical(a) === canonical(b);
|
||||
|
||||
/** The photos a page shows: its frames, placed or parked, and the pictures its maps mark stops with. */
|
||||
function photosOf(spread: BookSpread): number[] {
|
||||
return [...spread.elements, ...spread.parked].flatMap((el) => {
|
||||
if (el.kind === 'photo') return el.photoId === null ? [] : [el.photoId];
|
||||
if (el.kind === 'map') return el.points.flatMap((p) => (p.photoId === null ? [] : [p.photoId]));
|
||||
return [];
|
||||
});
|
||||
}
|
||||
|
||||
/** Where a new page goes in `spreads`: after the nearest plan page before it that is there, inside the covers. */
|
||||
function insertAt(spreads: readonly BookSpread[], plan: readonly BookDesignPlanRow[], row: number, page: BookSpread) {
|
||||
/** Where the first page of one of these roles stands; the end of the book when there is none. */
|
||||
@@ -86,17 +78,21 @@ function insertAt(spreads: readonly BookSpread[], plan: readonly BookDesignPlanR
|
||||
|
||||
/**
|
||||
* A framing page of a new book without the photos that left the gallery: the
|
||||
* book needs its cover and its back, so their frames are emptied (and a map's
|
||||
* stops lose their picture) rather than the page left out.
|
||||
* book needs its cover and its back, so their frames are emptied (a map's
|
||||
* stops lose their picture, and letters filled with one keep their colour)
|
||||
* rather than the page left out.
|
||||
*/
|
||||
function withoutGonePhotos(spread: BookSpread, gallery: ReadonlySet<number>): BookSpread {
|
||||
const gone = (id: number | null) => id !== null && !gallery.has(id);
|
||||
if (!photosOf(spread).some(gone)) return spread;
|
||||
if (!bookSpreadPhotoIds(spread).some(gone)) return spread;
|
||||
const clean = (el: BookSpread['elements'][number]): BookSpread['elements'][number] => {
|
||||
if (el.kind === 'photo') return gone(el.photoId) ? { ...el, photoId: null } : el;
|
||||
if (el.kind === 'map') {
|
||||
return { ...el, points: el.points.map((p) => (gone(p.photoId) ? { ...p, photoId: null } : p)) };
|
||||
}
|
||||
if (el.kind === 'text' && el.fillPaint?.type === 'photo' && gone(el.fillPaint.photoId)) {
|
||||
return { ...el, fillPaint: null };
|
||||
}
|
||||
return el;
|
||||
};
|
||||
return { ...spread, elements: spread.elements.map(clean), parked: spread.parked.map(clean) };
|
||||
@@ -116,7 +112,7 @@ function newBook(draft: DraftToApply, gallery: ReadonlySet<number>): AppliedDesi
|
||||
skipped.push({ spreadId: spread.id, reason: 'excluded' });
|
||||
return [];
|
||||
}
|
||||
if (!photosOf(spread).every((id) => gallery.has(id))) {
|
||||
if (!bookSpreadPhotoIds(spread).every((id) => gallery.has(id))) {
|
||||
skipped.push({ spreadId: spread.id, reason: 'photo_gone' });
|
||||
return [];
|
||||
}
|
||||
@@ -175,7 +171,7 @@ export function applyDesignDraft(
|
||||
}
|
||||
const page = designed.get(row.spreadId);
|
||||
if (!page) return;
|
||||
if (!photosOf(page).every((id) => gallery.has(id))) return skip(row.spreadId, 'photo_gone');
|
||||
if (!bookSpreadPhotoIds(page).every((id) => gallery.has(id))) return skip(row.spreadId, 'photo_gone');
|
||||
if (row.action === 'replace') {
|
||||
if (at < 0 || !samePage(spreads[at], base.get(row.spreadId))) return skip(row.spreadId, 'changed_since');
|
||||
spreads[at] = page;
|
||||
|
||||
@@ -381,10 +381,11 @@ export class JourneyBookEditService {
|
||||
/**
|
||||
* `restore_book_layout`, the assistant's undo of `remove_book_layout`: the
|
||||
* layout it answered put back, only while the book is at `expectedVersion`.
|
||||
* The layout comes as JSON and is parsed again like any argument.
|
||||
* The layout comes as JSON and is parsed again like any argument; a photo it
|
||||
* names is the gallery's or the book's already, as for a page put back.
|
||||
*/
|
||||
async restoreLayout(journeyId: number, userId: number, args: RestoreBookLayoutRequest): Promise<BookWriteAnswer> {
|
||||
return this.write(journeyId, userId, (doc) => restoreBookLayout(stored(doc), parsedJson(args.layout)), {
|
||||
return this.write(journeyId, userId, (doc, ctx) => restoreBookLayout(stored(doc), parsedJson(args.layout), ctx), {
|
||||
expectedVersion: args.expectedVersion,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user