Spaces:
Sleeping
Sleeping
Commit ·
af270a2
1
Parent(s): 05f2e3b
Fix file options menu and starred sync behavior
Browse filesFiles changed (4)
- js/main.js+43-12
- js/state/stateManager.js+32
- js/ui/uiRenderer.js+34-5
- styles.css+8
239 unmodified lines24024124224324424510 unmodified lines25625725825926026126213 unmodified lines276277278279280281282123 unmodified lines40640740840941041141241341441541641741841942042142242342442542642742841 unmodified lines470471472473474475476477478479480239 unmodified linesreturn parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];}render() {const browseMode = this.state.currentBrowse;let displayFiles = [];10 unmodified linesdisplayFiles = this.state.recent;document.getElementById('breadcrumbs').innerHTML = '<span class="breadcrumb-item active">Recent</span>';} else if (browseMode === 'starred') {displayFiles = this.state.cachedFiles.filter(f => this.state.starred.includes(f.path));document.getElementById('breadcrumbs').innerHTML = '<span class="breadcrumb-item active">Starred</span>';}13 unmodified linesonPreview: (file) => this.openPreview(file),onDownload: (url, name) => this.downloadFile(url, name),onRename: (path, name) => this.openRenameModal(path, name),onStar: (path) => this.state.toggleStar(path),onDelete: (path, name) => this.openDeleteModal(path, name),onHistory: (path, name) => this.openHistory(path, name),getUrl: (path) => getFileUrl(this.hf.apiBase || '/api', path)123 unmodified linesif (!this.pendingDelete) return;const path = this.pendingDelete;const btn = document.getElementById('confirmDeleteBtn');// Check if item still exists in local set to avoid stale deletesconst exists = this.state.cachedFiles.some(f => f.path === path) || this.cachedFolders.some(f => f.path === path);if (!exists) {this.ui.showToast('Item no longer exists', 'warning');this.pendingDelete = null;document.getElementById('deleteModal').classList.remove('active');return;}if (btn) btn.classList.add('loading');try {const isFolder = this.cachedFolders.some(f => f.path === path);if (isFolder) {await this.hf.deleteFolder(path);} else {await this.hf.deleteFile(path);}this.ui.showToast('Deleted successfully', 'success');document.getElementById('deleteModal').classList.remove('active');41 unmodified linesconst path = this.pendingRename.path;const oldName = this.pendingRename.originalName;btn.classList.add('loading');try {const res = await this.hf.renameItem(path, newName);if (res.success) {this.ui.showToast(`Renamed "${oldName}" to "${newName}"`, 'success');document.getElementById('renameModal').classList.remove('active');this.pendingRename = null;239 unmodified lines24024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227310 unmodified lines28428528628728828929013 unmodified lines304305306307308309310311312313314123 unmodified lines43843944044144244344444544644744844945045145245345445545641 unmodified lines498499500501502503504505506507508509510511239 unmodified linesreturn parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];}buildStarredDisplayFiles() {const cachedByPath = new Map(this.state.cachedFiles.map(file => [file.path, file]));const recentByPath = new Map(this.state.recent.map(file => [file.path, file]));return this.state.starred.map((path) => {const cached = cachedByPath.get(path);if (cached) return cached;const recent = recentByPath.get(path);if (recent) {return {path,name: recent.name || path.split('/').pop(),size: recent.size || 0,type: 'file',lastModified: recent.lastModified};}return {path,name: path.split('/').pop(),size: 0,type: 'file'};});}render() {const browseMode = this.state.currentBrowse;let displayFiles = [];10 unmodified linesdisplayFiles = this.state.recent;document.getElementById('breadcrumbs').innerHTML = '<span class="breadcrumb-item active">Recent</span>';} else if (browseMode === 'starred') {displayFiles = this.buildStarredDisplayFiles();document.getElementById('breadcrumbs').innerHTML = '<span class="breadcrumb-item active">Starred</span>';}13 unmodified linesonPreview: (file) => this.openPreview(file),onDownload: (url, name) => this.downloadFile(url, name),onRename: (path, name) => this.openRenameModal(path, name),onStar: (path) => {const wasStarred = this.state.starred.includes(path);this.state.toggleStar(path);this.ui.showToast(wasStarred ? 'Removed from Starred' : 'Added to Starred', 'success');},onDelete: (path, name) => this.openDeleteModal(path, name),onHistory: (path, name) => this.openHistory(path, name),getUrl: (path) => getFileUrl(this.hf.apiBase || '/api', path)123 unmodified linesif (!this.pendingDelete) return;const path = this.pendingDelete;const btn = document.getElementById('confirmDeleteBtn');if (btn) btn.classList.add('loading');try {const isFolder = this.cachedFolders.some(f => f.path === path);if (isFolder) {await this.hf.deleteFolder(path);this.state.starred.filter(starredPath => starredPath === path || starredPath.startsWith(`${path}/`)).forEach(starredPath => this.state.removeStar(starredPath));} else {await this.hf.deleteFile(path);this.state.removeStar(path);this.state.removeRecent(path);}this.ui.showToast('Deleted successfully', 'success');document.getElementById('deleteModal').classList.remove('active');41 unmodified linesconst path = this.pendingRename.path;const oldName = this.pendingRename.originalName;const parentPath = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '';const newPath = parentPath ? `${parentPath}/${newName}` : newName;btn.classList.add('loading');try {const res = await this.hf.renameItem(path, newName);if (res.success) {this.state.replacePathReferences(path, newPath, newName);this.ui.showToast(`Renamed "${oldName}" to "${newName}"`, 'success');document.getElementById('renameModal').classList.remove('active');this.pendingRename = null;
72 unmodified lines73747576777872 unmodified linesthis.notify('TOGGLE_STAR', path);}addToRecent(file) {if (!file || !file.path) return;this.recent = [file, ...this.recent.filter(f => f.path !== file.path)].slice(0, 20);72 unmodified lines73747576777879808182838485868788899091929394959697989910010110210310410510610710810911072 unmodified linesthis.notify('TOGGLE_STAR', path);}removeStar(path) {if (typeof path !== 'string') return;this.starred = this.starred.filter(p => p !== path);this.save(STARRED_KEY, this.starred);this.notify('REMOVE_STAR', path);}removeRecent(path) {if (typeof path !== 'string') return;this.recent = this.recent.filter(f => f.path !== path);this.save(RECENT_KEY, this.recent);this.notify('REMOVE_RECENT', path);}replacePathReferences(oldPath, newPath, newName = null) {if (typeof oldPath !== 'string' || typeof newPath !== 'string') return;this.starred = this.starred.map(p => (p === oldPath ? newPath : p));this.save(STARRED_KEY, this.starred);this.recent = this.recent.map(item => {if (item.path !== oldPath) return item;return {...item,path: newPath,name: newName || item.name};});this.save(RECENT_KEY, this.recent);this.notify('REPLACE_PATH_REFERENCES', { oldPath, newPath });}addToRecent(file) {if (!file || !file.path) return;this.recent = [file, ...this.recent.filter(f => f.path !== file.path)].slice(0, 20);
133 unmodified lines1341351361371381391403 unmodified lines1441451461471481491503 unmodified lines15415515615715815916016116216318 unmodified lines1821831841851861871881891901913 unmodified lines195196197198199200201202203204133 unmodified linesfiles.forEach(file => {const card = document.createElement('div');card.className = mode === 'grid' ? 'file-card' : 'file-list-item';const { icon, color } = getFileIcon(file.name);const ext = getExt(file.name).toUpperCase();const size = formatSize(file.size);3 unmodified linesif (mode === 'grid') {const isImg = isImage(file.name);const previewHTML = isImg? `<img src="${url}" alt="${file.name}" loading="lazy" onerror="this.parentElement.innerHTML='<span class=\\'file-icon\\'>📄</span>'">`: `<span class="file-icon">${getFileEmoji(ext)}</span>`;card.innerHTML = `3 unmodified lines<h4 class="file-name" title="${file.name}">${file.name}</h4><p class="file-meta">${size} • ${file.lastModified ? formatDate(file.lastModified) : 'Recently'}</p></div><div class="file-actions">⋮</div><div class="quick-actions"><button class="quick-btn" data-action="preview" title="Preview"><i class="ph-fill ph-eye"></i></button><button class="quick-btn" data-action="download" title="Download"><i class="ph-fill ph-download-simple"></i></button><button class="quick-btn" data-action="rename" title="Rename"><i class="ph-fill ph-pencil-simple"></i></button><button class="quick-btn" data-action="history" title="History"><i class="ph-fill ph-clock-counter-clockwise"></i></button></div>`;18 unmodified linesactions.onPreview(file);};// Action Handlerscard.addEventListener('click', (e) => {const btn = e.target.closest('[data-action]');if (!btn) return;const action = btn.dataset.action;if (action === 'preview') actions.onPreview(file);else if (action === 'download') actions.onDownload(url, file.name);3 unmodified lineselse if (action === 'history') actions.onHistory(file.path, file.name);});this.containers.files.appendChild(card);});}showEmpty() {this.containers.files.innerHTML = `<div class="empty-state">133 unmodified lines1341351361371381391403 unmodified lines1441451461471481491503 unmodified lines15415515615715815916016116216316416516616716816917017117217317417517617718 unmodified lines1961971981992002012022032042052063 unmodified lines210211212213214215216217218219220221222223224225226227228229230231232233133 unmodified linesfiles.forEach(file => {const card = document.createElement('div');card.className = mode === 'grid' ? 'file-card' : 'file-list-item';const { icon, color } = getFileIcon(file.name);const ext = getExt(file.name).toUpperCase();const size = formatSize(file.size);3 unmodified linesif (mode === 'grid') {const isImg = isImage(file.name);const previewHTML = isImg? `<img src="${url}" alt="${file.name}" loading="lazy" onerror="this.parentElement.innerHTML='<span class=\\'file-icon\\'>📄</span>'">`: `<span class="file-icon">${getFileEmoji(ext)}</span>`;card.innerHTML = `3 unmodified lines<h4 class="file-name" title="${file.name}">${file.name}</h4><p class="file-meta">${size} • ${file.lastModified ? formatDate(file.lastModified) : 'Recently'}</p></div><div class="file-actions card-menu"><button class="icon-btn card-menu-btn" title="More options"><i class="ph-bold ph-dots-three-vertical"></i></button><div class="dropdown-menu"><button class="dropdown-item" data-action="star"><i class="ph-fill ph-star"></i> ${starred ? 'Unstar' : 'Star'}</button><button class="dropdown-item" data-action="rename"><i class="ph-fill ph-pencil-simple"></i> Rename</button><button class="dropdown-item danger" data-action="delete"><i class="ph-fill ph-trash"></i> Delete</button></div></div><div class="quick-actions"><button class="quick-btn" data-action="preview" title="Preview"><i class="ph-fill ph-eye"></i></button><button class="quick-btn" data-action="download" title="Download"><i class="ph-fill ph-download-simple"></i></button><button class="quick-btn" data-action="star" title="${starred ? 'Unstar' : 'Star'}"><i class="ph-fill ph-star${starred ? '' : '-bold'}"></i></button><button class="quick-btn" data-action="rename" title="Rename"><i class="ph-fill ph-pencil-simple"></i></button><button class="quick-btn" data-action="history" title="History"><i class="ph-fill ph-clock-counter-clockwise"></i></button></div>`;18 unmodified linesactions.onPreview(file);};card.addEventListener('click', (e) => {const btn = e.target.closest('[data-action]');if (!btn) return;e.stopPropagation();card.querySelectorAll('.dropdown-menu.open').forEach(m => m.classList.remove('open'));const action = btn.dataset.action;if (action === 'preview') actions.onPreview(file);else if (action === 'download') actions.onDownload(url, file.name);3 unmodified lineselse if (action === 'history') actions.onHistory(file.path, file.name);});const menuBtn = card.querySelector('.card-menu-btn');const menu = card.querySelector('.dropdown-menu');if (menuBtn && menu) {menuBtn.onclick = (e) => {e.preventDefault();e.stopPropagation();document.querySelectorAll('.dropdown-menu.open').forEach(m => {if (m !== menu) m.classList.remove('open');});menu.classList.toggle('open');};menu.onclick = (e) => e.stopPropagation();}this.containers.files.appendChild(card);});}showEmpty() {this.containers.files.innerHTML = `<div class="empty-state">
731 unmodified lines732733734735736737731 unmodified linesborder: 1px solid var(--border-color);}.file-card:hover .file-actions {opacity: 1;}731 unmodified lines732733734735736737738739740741742743744745731 unmodified linesborder: 1px solid var(--border-color);}.file-actions .card-menu-btn {width: 100%;height: 100%;padding: 0;border-radius: 50%;font-size: 16px;}.file-card:hover .file-actions {opacity: 1;}
js/main.js+43-12
js/state/stateManager.js+32
js/ui/uiRenderer.js+34-5
styles.css+8