-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
133 lines (110 loc) · 3.94 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Supernote File</title>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.3/gh-fork-ribbon.min.css"/>
<style>
#images {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
padding: 10px;
}
.image-container {
position: relative;
min-height: 200px;
/*width: 200px; !* Default width *!*/
border: 1px solid #ccc;
padding: 10px;
display: flex;
flex-direction: column;
align-items: center;
}
.image-container img {
max-width: 100%;
height: auto;
}
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.github-fork-ribbon::before {
background-color: #333;
}
</style>
</head>
<body>
<h1>Display a Supernote File</h1>
<form id="uploadForm">
<input type="file" id="noteInput" accept=".note" required/>
<label for="widthSlider">width </label>
<input type="range" id="widthSlider" min="150" max="1600">
</form>
<div id="images"></div>
<script type="module">
import { SupernoteX } from 'supernote-typescript'
import MyWorker from './src/worker?worker'
class SupernoteWorker {
constructor () {
this.worker = new MyWorker()
}
process (note, pageIndex) {
this.worker.postMessage({ note, pageIndex })
}
onMessage (callback) {
this.worker.onmessage = (e) => callback(e.data)
}
terminate () {
this.worker.terminate()
}
}
const hardwareConcurrency = navigator.hardwareConcurrency || 3 // Default to 3 if not available
const MAX_WORKERS = Math.min(hardwareConcurrency, 8)
document.getElementById('noteInput').addEventListener('change', async (event) => {
const file = event.target.files[0]
if (!file) return
const arrayBuffer = await file.arrayBuffer()
const note = new SupernoteX(new Uint8Array(arrayBuffer))
const totalPages = note.pages.length
const workers = Array(MAX_WORKERS).fill(null).map(() => new SupernoteWorker())
const processQueue = Array.from({ length: totalPages }, (_, i) => i + 1)
// Initialize page containers
document.getElementById('images').innerHTML = processQueue.map(i => `<div id="container-${i}" class="image-container">
<div class="loading">Processing page ${i}...</div>
</div>`).join('')
workers.forEach(worker => {
worker.onMessage(async ({ pageIndex, imageData, status, error }) => {
if (status === 'success') {
const base64Image = btoa(String.fromCharCode(...new Uint8Array(imageData)))
document.getElementById(`container-${pageIndex}`).innerHTML =
`<img src="data:image/png;base64,${base64Image}" alt="Page ${pageIndex}">`
}
const nextPage = processQueue.shift()
if (nextPage) {
worker.process(note, nextPage)
}
})
})
// Start initial batch
workers.forEach(worker => {
const pageIndex = processQueue.shift()
if (pageIndex) worker.process(note, pageIndex)
})
})
document.getElementById('widthSlider').addEventListener('input', (event) => {
const width = event.target.value + 'px'
document.querySelectorAll('.image-container').forEach(container => {
container.style.width = width
})
document.getElementById('images').style.gridTemplateColumns = `repeat(auto-fit, minmax(${width}, 1fr))`
})
</script>
<a class="github-fork-ribbon" href="https://github.com/cristianvasquez/supernote-web-viewer"
data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>
</body>
</html>