mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
feat(studio): a book saved by a newer Studio opens read-only instead of losing what it cannot read
- A book carries schemaRev. The reader that meets a newer one opens the book read-only with a reload banner and saves nothing, where it used to drop the elements it could not read on the next save. - The server refuses a save, a restore or a tool edit that would write an older revision over a newer book or one newer than itself (409 book_schema_newer), on REST and through every book tool alike. - normalizeBookDocument moves to book-normalize.ts. - Print waits for photos eagerly and up to 15 s for an export of more than 40 photos, and skips images without a source.
This commit is contained in:
@@ -936,6 +936,8 @@ export type BookLayout = z.infer<typeof bookLayoutSchema>;
|
||||
export const bookDocumentSchema = z.object({
|
||||
/** In the document, not in a column: a format bump must not need a migration. */
|
||||
version: z.literal(1).catch(1).default(1),
|
||||
/** The format revision that wrote it (book-schema-rev.ts); absent, or unreadable and so dropped, reads as 1. */
|
||||
schemaRev: z.number().int().min(1).optional().catch(undefined),
|
||||
title: z.string().max(MAX_BOOK_TITLE).default(''),
|
||||
page: bookPageSetupSchema.default(() => bookPageSetupSchema.parse({})),
|
||||
spreads: z.array(bookSpreadSchema).max(MAX_SPREADS).default([]),
|
||||
@@ -943,63 +945,3 @@ export const bookDocumentSchema = z.object({
|
||||
layouts: z.array(bookLayoutSchema).max(MAX_BOOK_LAYOUTS).optional(),
|
||||
});
|
||||
export type BookDocument = z.infer<typeof bookDocumentSchema>;
|
||||
|
||||
/**
|
||||
* Read a stored document without ever throwing.
|
||||
*
|
||||
* Same shape as `normalizeAppearance`: a book that cannot be parsed must still
|
||||
* open — an editor that refuses to load someone's work because one field drifted
|
||||
* is worse than one that drops the field.
|
||||
*
|
||||
* ── Why there are two attempts ───────────────────────────────────────────
|
||||
*
|
||||
* Dropping *the field* was the intent; dropping the whole book was what the
|
||||
* code did. `elements` is an array of a discriminated union, so one element the
|
||||
* contract cannot read fails its spread, fails the array, fails the document —
|
||||
* and this function then handed back an empty book. It is not hypothetical: a
|
||||
* mood mark carried the journal's key in a field capped at two characters, so
|
||||
* every book with one on a page refused to save, and would have opened blank.
|
||||
* Both the client's load seam and the server's read *and* write path run
|
||||
* through here, which means the empty book was also what got written down.
|
||||
*
|
||||
* So a document that fails whole is tried again with the unreadable elements
|
||||
* removed, and only a document that fails even then is given up on. Losing one
|
||||
* decoration to a version skew is a shrug; losing the book is not.
|
||||
*/
|
||||
export function normalizeBookDocument(raw: unknown): BookDocument {
|
||||
const parsed = bookDocumentSchema.safeParse(raw);
|
||||
if (parsed.success) return parsed.data;
|
||||
|
||||
const salvaged = bookDocumentSchema.safeParse(withoutUnreadableElements(raw));
|
||||
if (salvaged.success) return salvaged.data;
|
||||
|
||||
return bookDocumentSchema.parse({});
|
||||
}
|
||||
|
||||
function withoutUnreadableElements(raw: unknown): unknown {
|
||||
if (!raw || typeof raw !== 'object') return raw;
|
||||
const doc = raw as { spreads?: unknown };
|
||||
if (!Array.isArray(doc.spreads)) return raw;
|
||||
|
||||
const readable = (list: unknown) =>
|
||||
Array.isArray(list)
|
||||
? // Trimmed to the cap as well as filtered: a spread that grew past 60
|
||||
// fails the array the same way an unreadable element fails the union, and
|
||||
// losing the sixty-first decoration beats losing the book.
|
||||
list.filter((el) => bookElementSchema.safeParse(el).success).slice(0, MAX_SPREAD_ELEMENTS)
|
||||
: list;
|
||||
|
||||
const layouts = (doc as { layouts?: unknown }).layouts;
|
||||
return {
|
||||
...doc,
|
||||
// A saved layout is a convenience: one that no longer reads is dropped, not the book.
|
||||
...(Array.isArray(layouts)
|
||||
? { layouts: layouts.filter((l) => bookLayoutSchema.safeParse(l).success).slice(0, MAX_BOOK_LAYOUTS) }
|
||||
: {}),
|
||||
spreads: doc.spreads.map((sp) => {
|
||||
if (!sp || typeof sp !== 'object') return sp;
|
||||
const spread = sp as { elements?: unknown; parked?: unknown };
|
||||
return { ...spread, elements: readable(spread.elements), parked: readable(spread.parked) };
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user