This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
android-interface.js
157 lines (131 loc) · 4.78 KB
/
android-interface.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var view, reader
document.addEventListener('DOMContentLoaded', async () => {
view = document.createElement('foliate-view')
reader = window.createReader()
bookLoaded = false
const onLoad = async ({ detail: { doc } }) => {
if (bookLoaded) return
const { book } = view
const metadata = book.metadata
const data = {
title: metadata?.title,
subtitle: metadata?.subtitle,
author: metadata?.author,
description: metadata?.description,
identifier: metadata?.identifier,
language: metadata?.language,
publisher: metadata?.publisher,
contributor: metadata?.contributor,
published: metadata?.published,
modified: metadata?.modified,
subject: metadata?.subject,
rights: metadata?.rights,
toc: book.toc,
}
bookLoaded = true
AndroidInterface.onBookLoaded(JSON.stringify(data))
}
lastLocation = { cfi:null, fraction: null }
const onRelocate = ({ detail }) => {
if (detail.cfi == lastLocation.cfi || detail.fraction == lastLocation.fraction) return
lastLocation = Object.assign(detail)
const data = {
cfi: detail.cfi,
fraction: detail.fraction,
tocItem: detail.tocItem,
currentLocation: detail.location.current,
nextLocation: detail.location.next,
totalLocations: detail.location.total,
}
if(bookLoaded) AndroidInterface.onRelocated(JSON.stringify(data))
}
view.addEventListener('load', onLoad)
view.addEventListener('relocate', onRelocate)
const params = new URLSearchParams(location.search)
const url = params.get('url')
try {
const response = await fetch(url)
const blob = await response.blob()
const fileName = new URL(url).pathname
reader.open(view, new File([blob], fileName))
} catch (error) {
console.error(error)
AndroidInterface.onBookLoadFailed(JSON.stringify(error, Object.getOwnPropertyNames(error)))
}
})
const next = () => view.next()
const prev = () => view.prev()
const goto = (locator) => view.goTo(locator)
const gotoFraction = (fraction) => view.goToFraction(fraction)
const getTocFractions = () => {
const tocFractions = []
const sizes = view.book.sections.filter(s => s.linear !== 'no').map(s => s.size)
if (sizes.length < 100) {
const total = sizes.reduce((a, b) => a + b, 0)
let sum = 0
for (const size of sizes.slice(0, -1)) {
sum += size
const fraction = sum / total
tocFractions.push(fraction)
}
}
return tocFractions
}
const getAppearance = () => {
const style = reader.style
const layout = reader.layout
const appearance = {};
appearance.lineHeight = style.lineHeight
appearance.justify = style.justify
appearance.hypenate = style.hypenate
appearance.themeName = style.theme.name
appearance.lightFg = style.theme.light.fg
appearance.lightBg = style.theme.light.bg
appearance.lightLink = style.theme.light.link
appearance.darkFg = style.theme.dark.fg
appearance.darkBg = style.theme.dark.bg
appearance.darkLink = style.theme.dark.link
appearance.useDark = style.isDark
appearance.gap = layout.gap
appearance.maxInlineSize = layout.maxInlineSize
appearance.maxBlockSize = layout.maxBlockSize
appearance.maxColumnCount = layout.maxColumnCount
appearance.flow = layout.flow
return appearance
}
const setAppearance = (appearance) => {
const style = { theme: { light: {}, dark: {} } }
style.lineHeight = appearance.lineHeight
style.justify = appearance.justify
style.hypenate = appearance.hypenate
style.theme.name = appearance.themeName
style.theme.light.fg = appearance.lightFg
style.theme.light.bg = appearance.lightBg
style.theme.light.link = appearance.lightLink
style.theme.dark.fg = appearance.darkFg
style.theme.dark.bg = appearance.darkBg
style.theme.dark.link = appearance.darkLink
style.isDark = appearance.useDark
const layout = {}
layout.gap = appearance.gap
layout.maxInlineSize = appearance.maxInlineSize
layout.maxBlockSize = appearance.maxBlockSize
layout.maxColumnCount = appearance.maxColumnCount
layout.flow = appearance.flow
reader.setAppearance(style, layout)
}
function getBlobAsBase64(blobUrl, callback) {
fetch(blobUrl)
.then(response => response.blob())
.then(blobData => {
const reader = new FileReader()
reader.onload = () => {
const base64String = reader.result
callback(base64String)
}
reader.readAsDataURL(blobData)
})
.catch(error => {
console.error('Error fetching the blob:', error)
})
}