Spaces:
Running
Running
Download script.js from medo00100/pdf-wizardry-toolkit: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/spaces/medo00100/pdf-wizardry-toolkit/resolve/main/script.js
- Command line
-
hf download hf://spaces/medo00100/pdf-wizardry-toolkit/script.js
-
curl -L -o script.js https://huggingface.co/spaces/medo00100/pdf-wizardry-toolkit/resolve/main/script.js
4.28 kB
| // Shared functionality across all pages | |
| document.addEventListener('DOMContentLoaded', () => { | |
| // Initialize dark mode from localStorage or preference | |
| if (localStorage.getItem('darkMode') === 'true' || | |
| (!localStorage.getItem('darkMode') && window.matchMedia('(prefers-color-scheme: dark)').matches)) { | |
| document.documentElement.classList.add('dark'); | |
| } | |
| // Add event listeners for dark mode toggle | |
| const darkModeToggle = document.getElementById('darkModeToggle'); | |
| if (darkModeToggle) { | |
| darkModeToggle.checked = document.documentElement.classList.contains('dark'); | |
| darkModeToggle.addEventListener('change', (e) => { | |
| if (e.target.checked) { | |
| document.documentElement.classList.add('dark'); | |
| localStorage.setItem('darkMode', 'true'); | |
| } else { | |
| document.documentElement.classList.remove('dark'); | |
| localStorage.setItem('darkMode', 'false'); | |
| } | |
| }); | |
| } | |
| // Mobile menu toggle | |
| const mobileMenuButton = document.getElementById('mobileMenuButton'); | |
| const mobileMenu = document.getElementById('mobileMenu'); | |
| if (mobileMenuButton && mobileMenu) { | |
| mobileMenuButton.addEventListener('click', () => { | |
| mobileMenu.classList.toggle('hidden'); | |
| }); | |
| } | |
| // Initialize all tooltips | |
| const tooltips = document.querySelectorAll('[data-tooltip]'); | |
| tooltips.forEach(tooltip => { | |
| new Tooltip(tooltip); | |
| }); | |
| }); | |
| // Tooltip class for UI elements | |
| class Tooltip { | |
| constructor(element) { | |
| this.element = element; | |
| this.tooltipText = element.getAttribute('data-tooltip'); | |
| this.tooltip = null; | |
| this.init(); | |
| } | |
| init() { | |
| this.element.addEventListener('mouseenter', this.show.bind(this)); | |
| this.element.addEventListener('mouseleave', this.hide.bind(this)); | |
| } | |
| show() { | |
| this.tooltip = document.createElement('div'); | |
| this.tooltip.className = 'absolute z-10 bg-gray-900 text-white text-xs rounded py-1 px-2 whitespace-nowrap'; | |
| this.tooltip.textContent = this.tooltipText; | |
| const rect = this.element.getBoundingClientRect(); | |
| this.tooltip.style.top = `${rect.bottom + window.scrollY + 5}px`; | |
| this.tooltip.style.left = `${rect.left + window.scrollX + rect.width / 2}px`; | |
| this.tooltip.style.transform = 'translateX(-50%)'; | |
| document.body.appendChild(this.tooltip); | |
| } | |
| hide() { | |
| if (this.tooltip) { | |
| document.body.removeChild(this.tooltip); | |
| this.tooltip = null; | |
| } | |
| } | |
| } | |
| // Shared file handling functions | |
| class FileHandler { | |
| static createFileList(files) { | |
| const fileList = document.createElement('div'); | |
| fileList.className = 'file-list'; | |
| files.forEach(file => { | |
| const fileItem = document.createElement('div'); | |
| fileItem.className = 'file-item'; | |
| const fileInfo = document.createElement('div'); | |
| fileInfo.innerHTML = ` | |
| <span class="font-medium">${file.name}</span> | |
| <span class="text-xs text-gray-500">${this.formatFileSize(file.size)}</span> | |
| `; | |
| const removeBtn = document.createElement('button'); | |
| removeBtn.className = 'text-red-500 hover:text-red-700'; | |
| removeBtn.innerHTML = '<i data-feather="x"></i>'; | |
| removeBtn.addEventListener('click', () => { | |
| fileItem.remove(); | |
| if (fileList.children.length === 0) { | |
| fileList.remove(); | |
| } | |
| }); | |
| fileItem.appendChild(fileInfo); | |
| fileItem.appendChild(removeBtn); | |
| fileList.appendChild(fileItem); | |
| }); | |
| feather.replace(); | |
| return fileList; | |
| } | |
| static formatFileSize(bytes) { | |
| if (bytes === 0) return '0 Bytes'; | |
| const k = 1024; | |
| const sizes = ['Bytes', 'KB', 'MB', 'GB']; | |
| const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
| return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | |
| } | |
| } | |
| // Export for use in tool pages | |
| window.FileHandler = FileHandler; |