taggart

Simple golang tagging filesystem webapp
Log | Files | Refs

cbz-viewer.js (1319B)


      1 // CBZ Keyboard Navigation
      2 
      3 // Scroll to viewer on page load
      4 window.addEventListener('DOMContentLoaded', function() {
      5 	const viewer = document.querySelector('.cbz-viewer');
      6 	if (viewer) {
      7 		viewer.scrollIntoView({ behavior: 'instant', block: 'start' });
      8 	}
      9 });
     10 
     11 document.addEventListener('keydown', function(e) {
     12 	// Don't intercept keys if user is typing in an input/textarea
     13 	const activeElement = document.activeElement;
     14 	if (activeElement.tagName === 'INPUT' ||
     15 	    activeElement.tagName === 'TEXTAREA' ||
     16 	    activeElement.isContentEditable) {
     17 		return;
     18 	}
     19 
     20 	// Get data from the .cbz-viewer element
     21 	const viewer = document.querySelector('.cbz-viewer');
     22 	if (!viewer) return;
     23 
     24 	const currentIndex = parseInt(viewer.dataset.currentIndex);
     25 	const totalImages = parseInt(viewer.dataset.totalImages);
     26 	const fileID = viewer.dataset.fileId;
     27 
     28 	switch(e.key) {
     29 		case 'ArrowLeft':
     30 		case 'a':
     31 			if (currentIndex > 0) {
     32 				window.location.href = `/cbz/${fileID}/${currentIndex - 1}`;
     33 			}
     34 			break;
     35 		case 'ArrowRight':
     36 		case 'd':
     37 			if (currentIndex < totalImages - 1) {
     38 				window.location.href = `/cbz/${fileID}/${currentIndex + 1}`;
     39 			}
     40 			break;
     41 		case 'Home':
     42 			window.location.href = `/cbz/${fileID}/0`;
     43 			break;
     44 		case 'End':
     45 			window.location.href = `/cbz/${fileID}/${totalImages - 1}`;
     46 			break;
     47 	}
     48 });