mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
feat(studio): give a book its first and its last page (#2317)
A new book was a cover, a spread and a back cover, so it opened straight onto pages 2 and 3. A bound book does not: the inside of the cover is blank, and the first thing facing the reader is a single right-hand page, page 1. The same at the end, where a single left-hand page sits before the back cover. Both are laid down now, empty, in a new book and in a laid-out one, and they are fixed the way the covers are: an inner spread only ever goes between them. The page numbers follow. They were counted from a spread's position in the array, which assumed the spread after the cover was the first, and three places did that sum on their own. One function now counts along the book, one page for a single leaf and two for a spread, so the sheet on screen and the label in the print view and the folio on the page all say the same number. A book with a first page starts at 1; a book from before the first page existed has none and keeps the numbers it always had.
This commit is contained in:
@@ -110,6 +110,7 @@ export function sheetsFor(doc: BookDocument, mode: SheetMode): Sheet[] {
|
||||
doc.spreads.forEach((spread, spreadIndex) => {
|
||||
const single = spread.role !== 'inner'
|
||||
const spreadWidth = single ? pageWidth : pageWidth * 2
|
||||
const folios = foliosOf(doc.spreads, spreadIndex, pageNumbers.startAt)
|
||||
|
||||
if (single || mode === 'spreads') {
|
||||
out.push({
|
||||
@@ -120,7 +121,7 @@ export function sheetsFor(doc: BookDocument, mode: SheetMode): Sheet[] {
|
||||
height: pageHeight,
|
||||
spreadWidth,
|
||||
single,
|
||||
label: single ? '' : folioRange(spreadIndex, pageNumbers.startAt),
|
||||
label: folioLabel(folios),
|
||||
})
|
||||
return;
|
||||
}
|
||||
@@ -134,7 +135,7 @@ export function sheetsFor(doc: BookDocument, mode: SheetMode): Sheet[] {
|
||||
height: pageHeight,
|
||||
spreadWidth,
|
||||
single: true,
|
||||
label: String(folio(spreadIndex, pageNumbers.startAt) + half),
|
||||
label: String(folios[half]),
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -142,18 +143,40 @@ export function sheetsFor(doc: BookDocument, mode: SheetMode): Sheet[] {
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* The number the left-hand page of a spread carries.
|
||||
*
|
||||
* Mirrors PageNumbers.tsx: the first inner spread opens at `startAt`, and the
|
||||
* cover is a separate sheet, which is why the index is offset rather than used
|
||||
* raw. It is only ever a label here — nothing is printed from it.
|
||||
*/
|
||||
function folio(spreadIndex: number, startAt: number): number {
|
||||
return startAt + (spreadIndex - 1) * 2
|
||||
/** Which of a book's leaves carry a page number: everything inside the covers. */
|
||||
export function isNumbered(role: BookSpread['role']): boolean {
|
||||
return role !== 'cover' && role !== 'back'
|
||||
}
|
||||
|
||||
function folioRange(spreadIndex: number, startAt: number): string {
|
||||
const left = folio(spreadIndex, startAt)
|
||||
return `${left} – ${left + 1}`
|
||||
/**
|
||||
* The page numbers a spread carries, in reading order.
|
||||
*
|
||||
* Counted along the book rather than from the index: the covers carry none,
|
||||
* a first or last page carries one, an inner spread two, and the first
|
||||
* numbered page is `startAt`. A book from before the single first page
|
||||
* existed has `startAt` 2 and no such page, so its first spread still reads
|
||||
* 2 – 3; a book laid out since has a first page and starts at 1, so that page
|
||||
* is 1 and the same spread behind it is 2 – 3 (#2317). Either way the number
|
||||
* follows what is in the book, not where the spread sits in the array.
|
||||
*
|
||||
* The renderer and the print labels both come through here, so a number seen
|
||||
* on screen is the one that is printed.
|
||||
*
|
||||
* Empty for a cover, and for an index past the end.
|
||||
*/
|
||||
export function foliosOf(spreads: readonly BookSpread[], spreadIndex: number, startAt: number): number[] {
|
||||
const spread = spreads[spreadIndex]
|
||||
if (!spread || !isNumbered(spread.role)) return []
|
||||
let next = startAt
|
||||
for (let i = 0; i < spreadIndex; i++) {
|
||||
const role = spreads[i].role
|
||||
if (!isNumbered(role)) continue
|
||||
next += role === 'inner' ? 2 : 1
|
||||
}
|
||||
return spread.role === 'inner' ? [next, next + 1] : [next]
|
||||
}
|
||||
|
||||
/** "3", or "2 – 3" for a spread; nothing for a cover. */
|
||||
export function folioLabel(folios: readonly number[]): string {
|
||||
return folios.length === 2 ? `${folios[0]} – ${folios[1]}` : folios.length === 1 ? String(folios[0]) : ''
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user