-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
book-spreads.ts
65 lines (59 loc) · 1.97 KB
/
book-spreads.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {PageDtoWithUrl} from '@/types/komga-books'
import {PagedReaderLayout} from '@/types/enum-reader'
import {isPageLandscape} from '@/functions/page'
import {cloneDeep} from 'lodash'
export function buildSpreads(pages: PageDtoWithUrl[], pageLayout: PagedReaderLayout): PageDtoWithUrl[][] {
if (pages.length === 0) return []
if (pageLayout !== PagedReaderLayout.SINGLE_PAGE) {
const spreads = [] as PageDtoWithUrl[][]
const pagesClone = cloneDeep(pages)
let lastPages = undefined
if (pageLayout === PagedReaderLayout.DOUBLE_PAGES) {
const firstPage = pagesClone.shift() as PageDtoWithUrl
spreads.push([createEmptyPage(firstPage), firstPage] as PageDtoWithUrl[])
if (pagesClone.length > 0) {
const lastPage = pagesClone.pop() as PageDtoWithUrl
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])
} else {
spreads.push([p, p2])
}
} else {
spreads.push([p, createEmptyPage(p)])
}
}
}
if (lastPages) spreads.push(lastPages)
return spreads
} else {
return pages.map(p => [p])
}
}
function createEmptyPage(page: PageDtoWithUrl): PageDtoWithUrl {
return {
url: createTransparentDataUrl(page?.width || 20, page?.height || 30),
number: 0,
} as PageDtoWithUrl
}
function createTransparentDataUrl(w: number, h: number): string {
const canvas = document.createElement('canvas')
canvas.width = w
canvas.height = h
const ctx = canvas.getContext('2d')
if (ctx) {
ctx.fillStyle = 'rgb(0,0,0,0)'
ctx.fillRect(0, 0, w, h)
}
return canvas.toDataURL()
}