-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.html
More file actions
538 lines (464 loc) · 19.7 KB
/
reader.html
File metadata and controls
538 lines (464 loc) · 19.7 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chế độ đọc sách - BrickDocs</title>
<meta name="description" content="Chế độ đọc sách hướng dẫn lắp ráp PDF trực tuyến trên BrickDocs. Trải nghiệm đọc tài liệu sinh động với hiệu ứng lật trang độc đáo.">
<meta name="keywords" content="BrickDocs, đọc sách online, hướng dẫn lắp ráp, PDF viewer, Lego, Qman, Keeppley, Sluban">
<meta name="author" content="Dat">
<meta property="og:title" content="Chế độ đọc sách - BrickDocs">
<meta property="og:description" content="Trải nghiệm đọc hướng dẫn lắp ráp PDF với hiệu ứng lật trang sinh động trên BrickDocs.">
<meta property="og:type" content="article">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/@phosphor-icons/web"></script>
<!-- PDF.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
<!-- Page Flip Lib -->
<script src="https://unpkg.com/page-flip/dist/js/page-flip.browser.js"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:wght@300;400;500;600;700&display=swap');
body {
font-family: 'Be Vietnam Pro', sans-serif;
background-color: #1a1a1a;
color: #e2e8f0;
margin: 0;
overflow: hidden;
}
.reader-container {
height: 100vh;
display: flex;
flex-direction: column;
}
.flip-book {
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.5);
margin: auto;
visibility: hidden;
/* Hidden until loaded */
}
.page {
background-color: white;
overflow: hidden;
}
/* Spine Shadow Effect */
.page::after {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: 8%;
pointer-events: none;
z-index: 5;
opacity: 0.8;
}
/* Mình đã tinh chỉnh lại bóng gáy sách dựa trên thông số của TDTU */
.page-left::after {
right: 0;
background: linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.05) 70%, rgba(0, 0, 0, 0.2) 100%);
width: 60px;
/* Độ rộng bóng đổ gáy */
}
.page-right::after {
left: 0;
background: linear-gradient(to left, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.05) 70%, rgba(0, 0, 0, 0.2) 100%);
width: 60px;
}
.page-content {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
canvas {
max-width: 100%;
max-height: 100%;
object-fit: contain;
}
/* Loading UI */
.loader-overlay {
position: fixed;
inset: 0;
background: #1a1a1a;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 100;
transition: opacity 0.5s ease;
}
.progress-bar-container {
width: 200px;
height: 4px;
background: #334155;
border-radius: 2px;
margin-top: 1rem;
overflow: hidden;
}
.progress-bar-fill {
height: 100%;
background: #dc2626;
width: 0%;
transition: width 0.3s ease;
}
/* Controls */
.controls {
position: fixed;
bottom: 1.5rem;
left: 50%;
transform: translate(-50%, 0);
background: rgba(30, 41, 59, 0.85);
backdrop-filter: blur(12px);
padding: 0.5rem 1rem;
border-radius: 9999px;
display: flex;
align-items: center;
gap: 0.75rem;
border: 1px solid rgba(255, 255, 255, 0.1);
z-index: 50;
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.5);
}
@media (min-width: 768px) {
.controls {
bottom: 2rem;
padding: 0.75rem 1.5rem;
gap: 1.5rem;
}
}
.control-btn {
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: rgba(255, 255, 255, 0.05);
transition: all 0.2s;
cursor: pointer;
}
@media (min-width: 768px) {
.control-btn {
width: 40px;
height: 40px;
}
}
.control-btn:hover {
background: rgba(220, 38, 38, 0.2);
color: #ef4444;
}
/* Scale Controls */
.zoom-badge {
background: rgba(255, 255, 255, 0.1);
color: #e2e8f0;
font-size: 10px;
padding: 2px 6px;
border-radius: 4px;
font-weight: 500;
}
.flip-book {
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.5);
margin: auto;
visibility: hidden;
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
transform-origin: center center;
}
.control-btn:active {
transform: scale(0.9);
}
.exit-btn {
position: fixed;
top: 1rem;
left: 1rem;
z-index: 50;
}
@media (min-width: 768px) {
.exit-btn {
top: 1.5rem;
left: 1.5rem;
}
}
/* Book wood texture background effect */
.bg-pattern {
position: fixed;
inset: 0;
background: radial-gradient(circle at center, #2d3748 0%, #1a202c 100%);
opacity: 0.6;
pointer-events: none;
}
.wide-mode {
padding: 0.5rem !important;
}
</style>
</head>
<body>
<div class="bg-pattern"></div>
<div class="reader-container">
<!-- Exit -->
<div class="exit-btn">
<button onclick="closeReader();"
class="flex items-center gap-1.5 md:gap-2 px-3 py-1.5 md:px-4 md:py-2 bg-white/10 hover:bg-white/20 rounded-full text-white text-sm md:text-base transition backdrop-blur-sm border border-white/10">
<i class="ph ph-arrow-left"></i> Thoát
</button>
</div>
<!-- Loader -->
<div id="loader" class="loader-overlay">
<div
class="w-16 h-16 bg-red-600 rounded-2xl flex items-center justify-center text-white shadow-lg shadow-red-500/20 mb-6 animate-bounce">
<i class="ph-fill ph-book-open text-3xl"></i>
</div>
<h2 class="text-xl font-bold text-white mb-2">Đang chuẩn bị sách...</h2>
<p id="loadingStatus" class="text-slate-400 text-sm">Đang tải tài liệu</p>
<div class="progress-bar-container">
<div id="progressBar" class="progress-bar-fill"></div>
</div>
</div>
<!-- Book -->
<div id="mainArea" class="flex-1 flex items-center justify-center p-4 md:p-10">
<div id="bookContainer" class="flip-book">
<!-- Pages will be injected here -->
</div>
</div>
<!-- Controls -->
<div class="controls transition-all duration-500 opacity-0" id="uiControls" style="transform: translate(-50%, 40px);">
<button class="control-btn" id="prevBtn" title="Trang trước">
<i class="ph-bold ph-caret-left text-xl"></i>
</button>
<div class="flex items-center gap-1.5 md:gap-2 border-x border-white/10 px-2 md:px-4">
<span id="currentPage" class="font-bold text-red-400 text-xs md:text-base min-w-[32px] md:min-w-[48px] text-center whitespace-nowrap">1</span>
<span class="text-slate-500 text-xs md:text-sm">/</span>
<span id="totalPages" class="text-slate-300 text-xs md:text-sm min-w-[12px] md:min-w-[20px] text-center">?</span>
</div>
<button class="control-btn" id="nextBtn" title="Trang sau">
<i class="ph-bold ph-caret-right text-xl"></i>
</button>
<div class="flex items-center gap-2 border-l border-white/10 pl-4">
<button class="control-btn" onclick="adjustZoom(-0.1)" title="Thu nhỏ">
<i class="ph ph-minus"></i>
</button>
<div class="zoom-badge" id="zoomLevel">100%</div>
<button class="control-btn" onclick="adjustZoom(0.1)" title="Phóng to">
<i class="ph ph-plus"></i>
</button>
<button class="control-btn ml-1" id="wideModeBtn" onclick="toggleWideMode()" title="Chế độ tràn viền">
<i class="ph ph-arrows-out-simple"></i>
</button>
</div>
</div>
</div>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
let pageFlip = null;
let pdfDoc = null;
const renderedPages = new Set();
const bookContainer = document.getElementById('bookContainer');
const progressBar = document.getElementById('progressBar');
const loadingStatus = document.getElementById('loadingStatus');
let currentZoom = 1;
function updateZoomDisplay() {
document.getElementById('zoomLevel').innerText = `${Math.round(currentZoom * 100)}%`;
bookContainer.style.transform = `scale(${currentZoom})`;
}
function adjustZoom(delta) {
currentZoom = Math.min(Math.max(0.5, currentZoom + delta), 2);
// If zoomed, allow scroll on the parent
const parent = bookContainer.parentElement;
if (currentZoom > 1.1) {
parent.classList.add('overflow-auto');
parent.classList.remove('items-center');
parent.style.padding = '8rem 2rem';
} else {
parent.classList.remove('overflow-auto');
parent.classList.add('items-center');
parent.style.padding = '';
}
updateZoomDisplay();
}
function toggleWideMode() {
const parent = document.getElementById('mainArea');
const isWide = parent.classList.contains('wide-mode');
if (isWide) {
parent.classList.remove('wide-mode');
document.getElementById('wideModeBtn').innerHTML = '<i class="ph ph-arrows-out-simple"></i>';
} else {
parent.classList.add('wide-mode');
document.getElementById('wideModeBtn').innerHTML = '<i class="ph ph-arrows-in-simple"></i>';
}
}
function closeReader() {
const params = new URLSearchParams(window.location.search);
const subjectId = params.get('id');
if (window.opener || window.history.length > 1) {
if (window.opener) {
window.close();
} else {
window.history.back();
}
} else {
// If subjectId exists, return to the set page, otherwise home
window.location.href = subjectId ? `set.html?id=${subjectId}` : 'index.html';
}
}
async function initReader() {
const params = new URLSearchParams(window.location.search);
const fileUrl = params.get('file');
if (!fileUrl) {
alert('Không tìm thấy file tài liệu!');
closeReader();
return;
}
try {
// 1. Load PDF
const loadingTask = pdfjsLib.getDocument(fileUrl);
const pdf = await loadingTask.promise;
const numPages = pdf.numPages;
document.getElementById('totalPages').innerText = numPages;
// 2. Create Placeholder Pages (Immediately)
loadingStatus.innerText = `Đang khởi tạo ${numPages} trang...`;
for (let i = 1; i <= numPages; i++) {
const pageDiv = document.createElement('div');
pageDiv.className = `page ${i % 2 === 0 ? 'page-left' : 'page-right'}`;
pageDiv.id = `page-${i}`;
// Cover pages use hard density
if (i === 1 || i === numPages) {
pageDiv.setAttribute('data-density', 'hard');
} else {
pageDiv.setAttribute('data-density', 'soft');
}
const contentDiv = document.createElement('div');
contentDiv.className = 'page-content';
contentDiv.id = `page-content-${i}`;
// Add a tiny spinner placeholder
contentDiv.innerHTML = `
<div class="flex flex-col items-center gap-2 text-slate-300">
<i class="ph ph-spinner animate-spin text-2xl"></i>
<span class="text-[10px]">Trang ${i}</span>
</div>
`;
pageDiv.appendChild(contentDiv);
bookContainer.appendChild(pageDiv);
}
// 3. Initialize PageFlip (With placeholders)
// We need dimensions of the first page to initialize correctly
const firstPage = await pdf.getPage(1);
const baseViewport = firstPage.getViewport({ scale: 1.5 });
const bookWidth = Math.round(baseViewport.width);
const bookHeight = Math.round(baseViewport.height);
initPageFlip(bookWidth, bookHeight);
pdfDoc = pdf; // Save for lazy loading
// 4. Initial Render (Cover + First few pages)
await Promise.all([
renderPage(1),
renderPage(2),
renderPage(3),
renderPage(4)
]);
// 5. Hide Loader
setTimeout(() => {
document.getElementById('loader').style.opacity = '0';
setTimeout(() => {
document.getElementById('loader').remove();
const controls = document.getElementById('uiControls');
controls.classList.remove('opacity-0');
controls.style.transform = 'translate(-50%, 0)';
bookContainer.style.visibility = 'visible';
}, 500);
}, 300);
// Initial Buffer
managePageBuffer(0);
} catch (err) {
console.error('Error loading PDF:', err);
alert('Lỗi khi tải hoặc hiển thị tài liệu. Vui lòng thử lại!');
}
}
function initPageFlip(width, height) {
// Adjust width/height based on screen
const isMobile = window.innerWidth < 768;
pageFlip = new St.PageFlip(bookContainer, {
width: width,
height: height,
size: "stretch",
minWidth: 315,
maxWidth: 1000,
minHeight: 420,
maxHeight: 1350,
maxShadowOpacity: 0.5,
showCover: true,
mobileScrollSupport: false // set to false to prevent page scrolling on mobile devices
});
pageFlip.loadFromHTML(document.querySelectorAll('.page'));
// Events
document.getElementById('prevBtn').onclick = () => pageFlip.flipPrev();
document.getElementById('nextBtn').onclick = () => pageFlip.flipNext();
pageFlip.on('flip', (e) => {
const index = e.data; // Current page index (0-indexed)
const total = pdfDoc.numPages;
const orientation = pageFlip.getOrientation();
let displayStr = "";
if (orientation === 'portrait' || index === 0) {
displayStr = index + 1;
} else if (index === total - 1 && total % 2 === 0) {
// Last page in a book with even pages is single on the left?
// No, usually it's the back cover.
displayStr = total;
} else {
// Spread mode: Page N and N+1
const p1 = index + 1;
const p2 = index + 2;
displayStr = (p2 <= total) ? `${p1}-${p2}` : p1;
}
document.getElementById('currentPage').innerText = displayStr;
managePageBuffer(index);
});
}
async function renderPage(pageNum) {
if (renderedPages.has(pageNum) || !pdfDoc || pageNum < 1 || pageNum > pdfDoc.numPages) return;
renderedPages.add(pageNum);
const contentDiv = document.getElementById(`page-content-${pageNum}`);
try {
const page = await pdfDoc.getPage(pageNum);
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.style.width = '100%';
canvas.style.height = '100%';
canvas.style.objectFit = 'contain';
const renderViewport = page.getViewport({ scale: 1.5 });
canvas.height = renderViewport.height;
canvas.width = renderViewport.width;
await page.render({ canvasContext: context, viewport: renderViewport }).promise;
contentDiv.innerHTML = '';
contentDiv.appendChild(canvas);
} catch (err) {
console.error(`Error rendering page ${pageNum}:`, err);
renderedPages.delete(pageNum);
}
}
function managePageBuffer(currentStepIndex) {
// Buffer: Render +/- 5 pages
const current = currentStepIndex + 1;
const start = Math.max(1, current - 5);
const end = Math.min(pdfDoc.numPages, current + 5);
for (let i = start; i <= end; i++) {
renderPage(i);
}
// Cleanup: Remote distant canvases (> 15 pages) to save memory
if (renderedPages.size > 30) {
for (const pageNum of renderedPages) {
if (pageNum < current - 15 || pageNum > current + 15) {
const contentDiv = document.getElementById(`page-content-${pageNum}`);
if (contentDiv) {
contentDiv.innerHTML = `
<div class="flex flex-col items-center gap-2 text-slate-300">
<i class="ph ph-spinner animate-spin text-2xl"></i>
<span class="text-[10px]">Trang ${pageNum}</span>
</div>
`;
renderedPages.delete(pageNum);
}
}
}
}
}
window.onload = initReader;
</script>
</body>
</html>