Translsis commited on
Commit
2c37878
·
verified ·
1 Parent(s): cf6481c

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +75 -1
index.html CHANGED
@@ -401,10 +401,52 @@
401
  let pdfDocs = [];
402
  let currentPages = [];
403
  let zoomLevels = [];
 
404
  let activeIndex = -1;
405
  let renderQueue = [];
406
  let isRendering = false;
407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
  // File input
409
  fileInput.addEventListener('change', (e) => {
410
  handleFiles(e.target.files);
@@ -461,13 +503,24 @@
461
 
462
  newFiles.forEach((file, idx) => {
463
  const fileIndex = startIndex + idx;
464
- zoomLevels[fileIndex] = 1.5;
 
 
 
 
 
 
 
 
 
465
  addTab(file, fileIndex);
466
  });
467
 
468
  if (activeIndex === -1) {
469
  switchTab(startIndex);
470
  }
 
 
471
  }
472
 
473
  function addTab(file, index) {
@@ -538,13 +591,24 @@
538
  // Setup scroll tracking with throttle
539
  let scrollTimeout;
540
  viewerContainer.addEventListener('scroll', () => {
 
 
 
541
  clearTimeout(scrollTimeout);
542
  scrollTimeout = setTimeout(() => {
543
  updateCurrentPage(index, viewerContainer);
544
  renderVisiblePages(index, viewerContainer);
 
545
  }, 100);
546
  });
547
 
 
 
 
 
 
 
 
548
  // Initial render of visible pages
549
  renderVisiblePages(index, viewerContainer);
550
 
@@ -632,6 +696,7 @@
632
 
633
  currentPages[index] = closestPage;
634
  currentPageEl.textContent = closestPage;
 
635
  }
636
 
637
  function scrollToPrevPage() {
@@ -664,6 +729,8 @@
664
  zoomLevels[activeIndex] = Math.max(0.5, Math.min(3, zoomLevels[activeIndex] + delta));
665
  zoomLevelEl.textContent = Math.round(zoomLevels[activeIndex] * 100) + '%';
666
 
 
 
667
  // Re-render current PDF with new zoom
668
  const tabContent = contentContainer.querySelector(`.tab-content[data-index="${activeIndex}"]`);
669
  const file = pdfFiles[activeIndex];
@@ -674,6 +741,7 @@
674
  const container = tabContent.querySelector('.pdf-viewer-container');
675
  if (container) {
676
  container.scrollTop = currentScroll * (zoomLevels[activeIndex] / (zoomLevels[activeIndex] - delta));
 
677
  }
678
  });
679
  }
@@ -708,6 +776,8 @@
708
  setTimeout(() => renderVisiblePages(index, container), 100);
709
  }
710
  }
 
 
711
  }
712
 
713
  function closeTab(index) {
@@ -715,6 +785,7 @@
715
  pdfDocs.splice(index, 1);
716
  currentPages.splice(index, 1);
717
  zoomLevels.splice(index, 1);
 
718
 
719
  const tab = tabsContainer.querySelector(`[data-index="${index}"]`);
720
  if (tab) tab.remove();
@@ -759,12 +830,15 @@
759
  <p>Nhấn "Mở file" hoặc kéo thả file PDF vào đây</p>
760
  </div>
761
  `;
 
762
  } else {
763
  fileCountEl.textContent = `${pdfFiles.length} file`;
764
  if (activeIndex >= pdfFiles.length || activeIndex === index) {
765
  switchTab(Math.max(0, Math.min(index, pdfFiles.length - 1)));
766
  }
767
  }
 
 
768
  }
769
 
770
  // Keyboard shortcuts
 
401
  let pdfDocs = [];
402
  let currentPages = [];
403
  let zoomLevels = [];
404
+ let scrollPositions = [];
405
  let activeIndex = -1;
406
  let renderQueue = [];
407
  let isRendering = false;
408
 
409
+ // Load saved state from localStorage
410
+ function loadSavedState() {
411
+ try {
412
+ const savedState = localStorage.getItem('pdfViewerState');
413
+ if (savedState) {
414
+ const state = JSON.parse(savedState);
415
+ currentPages = state.currentPages || [];
416
+ zoomLevels = state.zoomLevels || [];
417
+ scrollPositions = state.scrollPositions || [];
418
+ activeIndex = state.activeIndex || -1;
419
+ }
420
+ } catch (e) {
421
+ console.log('No saved state found');
422
+ }
423
+ }
424
+
425
+ // Save state to localStorage
426
+ function saveState() {
427
+ try {
428
+ const state = {
429
+ currentPages,
430
+ zoomLevels,
431
+ scrollPositions,
432
+ activeIndex,
433
+ fileNames: pdfFiles.map(f => f.name)
434
+ };
435
+ localStorage.setItem('pdfViewerState', JSON.stringify(state));
436
+ } catch (e) {
437
+ console.error('Failed to save state:', e);
438
+ }
439
+ }
440
+
441
+ // Auto-save state periodically
442
+ setInterval(saveState, 2000);
443
+
444
+ // Save state before page unload
445
+ window.addEventListener('beforeunload', saveState);
446
+
447
+ // Load saved state on page load
448
+ loadSavedState();
449
+
450
  // File input
451
  fileInput.addEventListener('change', (e) => {
452
  handleFiles(e.target.files);
 
503
 
504
  newFiles.forEach((file, idx) => {
505
  const fileIndex = startIndex + idx;
506
+ // Use saved zoom or default
507
+ if (!zoomLevels[fileIndex]) {
508
+ zoomLevels[fileIndex] = 1.5;
509
+ }
510
+ if (!currentPages[fileIndex]) {
511
+ currentPages[fileIndex] = 1;
512
+ }
513
+ if (!scrollPositions[fileIndex]) {
514
+ scrollPositions[fileIndex] = 0;
515
+ }
516
  addTab(file, fileIndex);
517
  });
518
 
519
  if (activeIndex === -1) {
520
  switchTab(startIndex);
521
  }
522
+
523
+ saveState();
524
  }
525
 
526
  function addTab(file, index) {
 
591
  // Setup scroll tracking with throttle
592
  let scrollTimeout;
593
  viewerContainer.addEventListener('scroll', () => {
594
+ // Save scroll position
595
+ scrollPositions[index] = viewerContainer.scrollTop;
596
+
597
  clearTimeout(scrollTimeout);
598
  scrollTimeout = setTimeout(() => {
599
  updateCurrentPage(index, viewerContainer);
600
  renderVisiblePages(index, viewerContainer);
601
+ saveState();
602
  }, 100);
603
  });
604
 
605
+ // Restore scroll position
606
+ if (scrollPositions[index]) {
607
+ setTimeout(() => {
608
+ viewerContainer.scrollTop = scrollPositions[index];
609
+ }, 100);
610
+ }
611
+
612
  // Initial render of visible pages
613
  renderVisiblePages(index, viewerContainer);
614
 
 
696
 
697
  currentPages[index] = closestPage;
698
  currentPageEl.textContent = closestPage;
699
+ saveState();
700
  }
701
 
702
  function scrollToPrevPage() {
 
729
  zoomLevels[activeIndex] = Math.max(0.5, Math.min(3, zoomLevels[activeIndex] + delta));
730
  zoomLevelEl.textContent = Math.round(zoomLevels[activeIndex] * 100) + '%';
731
 
732
+ saveState();
733
+
734
  // Re-render current PDF with new zoom
735
  const tabContent = contentContainer.querySelector(`.tab-content[data-index="${activeIndex}"]`);
736
  const file = pdfFiles[activeIndex];
 
741
  const container = tabContent.querySelector('.pdf-viewer-container');
742
  if (container) {
743
  container.scrollTop = currentScroll * (zoomLevels[activeIndex] / (zoomLevels[activeIndex] - delta));
744
+ scrollPositions[activeIndex] = container.scrollTop;
745
  }
746
  });
747
  }
 
776
  setTimeout(() => renderVisiblePages(index, container), 100);
777
  }
778
  }
779
+
780
+ saveState();
781
  }
782
 
783
  function closeTab(index) {
 
785
  pdfDocs.splice(index, 1);
786
  currentPages.splice(index, 1);
787
  zoomLevels.splice(index, 1);
788
+ scrollPositions.splice(index, 1);
789
 
790
  const tab = tabsContainer.querySelector(`[data-index="${index}"]`);
791
  if (tab) tab.remove();
 
830
  <p>Nhấn "Mở file" hoặc kéo thả file PDF vào đây</p>
831
  </div>
832
  `;
833
+ localStorage.removeItem('pdfViewerState');
834
  } else {
835
  fileCountEl.textContent = `${pdfFiles.length} file`;
836
  if (activeIndex >= pdfFiles.length || activeIndex === index) {
837
  switchTab(Math.max(0, Math.min(index, pdfFiles.length - 1)));
838
  }
839
  }
840
+
841
+ saveState();
842
  }
843
 
844
  // Keyboard shortcuts