Skip to content
Open
24 changes: 10 additions & 14 deletions komga-webui/src/functions/book-spreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,36 @@ export function buildSpreads(pages: PageDtoWithUrl[], pageLayout: PagedReaderLay
if (pageLayout !== PagedReaderLayout.SINGLE_PAGE) {
const spreads = [] as PageDtoWithUrl[][]
const pagesClone = cloneDeep(pages)
let lastPages = undefined
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed lastPages, and the chunk handling it below as the loop part of the algorithm handles getting the last image in place

if (pageLayout === PagedReaderLayout.DOUBLE_PAGES) {
const firstPage = pagesClone.shift() as PageDtoWithUrl
if (isPageLandscape(firstPage))
spreads.push([firstPage] as PageDtoWithUrl[])
else
spreads.push([createEmptyPage(firstPage), firstPage] as PageDtoWithUrl[])
if (pagesClone.length > 0) {
const lastPage = pagesClone.pop() as PageDtoWithUrl
if(isPageLandscape(lastPage))
lastPages = [lastPage] as PageDtoWithUrl[]
else
lastPages = [lastPage, createEmptyPage(lastPage)] as PageDtoWithUrl[]
}
}
while (pagesClone.length > 0) {
const p = pagesClone.shift() as PageDtoWithUrl
if (isPageLandscape(p)) {
spreads.push([p])
} else {
if (pagesClone.length > 0) {
const p2 = pagesClone.shift() as PageDtoWithUrl
if (isPageLandscape(p2)) {
spreads.push([p, createEmptyPage(p)])
spreads.push([p2])
const [prevPage] = spreads.length > 0 ? (spreads[spreads.length - 1] as PageDtoWithUrl[]) : []
if (pageLayout === PagedReaderLayout.DOUBLE_NO_COVER && isPageLandscape(prevPage)){
spreads.push([createEmptyPage(p), p])
} else {
spreads.push([p, p2])
const p2 = pagesClone.shift() as PageDtoWithUrl
if (isPageLandscape(p2)) {
spreads.push([p, createEmptyPage(p)])
spreads.push([p2])
} else {
spreads.push([p, p2])
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically all the same logic, just if it's DBNC layout, then just push a empty page after the previous page if and only if it was a landscape page. This guarantees the layouts will be opposite if landscape pages existed, which is what throws off the flow.

}
} else {
spreads.push([p, createEmptyPage(p)])
}
}
}
if (lastPages) spreads.push(lastPages)
return spreads
} else {
return pages.map(p => [p])
Expand Down
11 changes: 4 additions & 7 deletions komga-webui/tests/unit/functions/book-spreads.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,20 @@ describe('Double Pages', () => {
] as PageDtoWithUrl[]

const spreads = buildSpreads(pages, pageLayout)

expect(spreads.length).toEqual(4)
// Expecting [E, 1], [2, 3], [4, 5]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked about this in more details with an example in the PR original post

expect(spreads.length).toEqual(3)

expect(spreads[0].length).toEqual(2)
expect(spreads[0][1].number).toEqual(1)
expect(spreads[0][0].number).toEqual(0) // empty page
expect(spreads[0][1].number).toEqual(1)

expect(spreads[1].length).toEqual(2)
expect(spreads[1][0].number).toEqual(2)
expect(spreads[1][1].number).toEqual(3)

expect(spreads[2].length).toEqual(2)
expect(spreads[2][0].number).toEqual(4)

expect(spreads[3].length).toEqual(2)
expect(spreads[3][0].number).toEqual(5)
expect(spreads[3][1].number).toEqual(0) // empty page
expect(spreads[2][1].number).toEqual(5)
})
})

Expand Down