Files
clawd/dashboard/files.html

1279 lines
48 KiB
HTML

<!DOCTYPE html>
<html lang="ro">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/svg+xml" href="favicon.svg">
<title>Echo · Files</title>
<link rel="stylesheet" href="common.css">
<script src="https://unpkg.com/lucide@latest/dist/umd/lucide.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="swipe-nav.js"></script>
<style>
.main {
display: flex;
flex-direction: column;
height: calc(100vh - 52px);
}
/* Toolbar */
.toolbar {
padding: var(--space-3) var(--space-5);
background: var(--bg-surface);
border-bottom: 1px solid var(--border);
display: flex;
justify-content: space-between;
align-items: center;
gap: var(--space-4);
flex-wrap: wrap;
}
.breadcrumb {
display: flex;
align-items: center;
gap: var(--space-1);
font-size: var(--text-sm);
color: var(--text-muted);
flex-wrap: wrap;
}
.breadcrumb-item {
color: var(--text-muted);
text-decoration: none;
padding: var(--space-1) var(--space-2);
border-radius: var(--radius-sm);
transition: all var(--transition-fast);
cursor: pointer;
}
.breadcrumb-item:hover {
color: var(--text-primary);
background: var(--bg-surface-hover);
}
.breadcrumb-item.current {
color: var(--text-primary);
}
.breadcrumb-sep {
color: var(--text-muted);
}
.breadcrumb-sep svg {
width: 14px;
height: 14px;
}
.toolbar-actions {
display: flex;
gap: var(--space-2);
}
/* View modes */
.view-toggle {
display: flex;
gap: 2px;
background: var(--bg-base);
border-radius: var(--radius-md);
padding: 2px;
}
.view-btn {
padding: var(--space-2);
background: transparent;
border: none;
color: var(--text-muted);
cursor: pointer;
border-radius: var(--radius-sm);
display: flex;
align-items: center;
}
.view-btn:hover {
color: var(--text-primary);
}
.view-btn.active {
background: var(--accent);
color: white;
}
.view-btn svg {
width: 16px;
height: 16px;
}
.sort-select {
background: transparent;
border: none;
color: var(--text-secondary);
font-size: var(--text-sm);
padding: var(--space-1) var(--space-2);
cursor: pointer;
outline: none;
}
.sort-select option {
background: var(--bg-base);
color: var(--text-primary);
}
/* ========== LIST VIEW - Windows Explorer style ========== */
.file-grid.view-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: var(--space-1);
}
.file-grid.view-list .file-item {
flex-direction: row;
padding: var(--space-1) var(--space-2);
gap: var(--space-2);
background: transparent;
border: none;
}
.file-grid.view-list .file-item:hover {
background: var(--bg-surface-hover);
}
.file-grid.view-list .file-icon {
width: 16px;
height: 16px;
flex-shrink: 0;
}
.file-grid.view-list .file-icon svg {
width: 16px;
height: 16px;
}
.file-grid.view-list .file-name {
font-size: var(--text-sm);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.file-grid.view-list .file-meta {
display: none;
}
/* ========== DETAILS VIEW - Windows Explorer style with columns ========== */
.file-grid.view-details {
display: flex;
flex-direction: column;
gap: 0;
}
.file-grid.view-details .file-header {
display: grid;
grid-template-columns: 24px 1fr 100px 80px 120px;
align-items: center;
padding: var(--space-2) var(--space-3);
gap: var(--space-3);
background: var(--bg-surface);
border-bottom: 2px solid var(--border);
font-size: var(--text-xs);
font-weight: 600;
color: var(--text-muted);
position: sticky;
top: 0;
}
.file-grid.view-details .file-header span {
cursor: pointer;
}
.file-grid.view-details .file-header span:hover {
color: var(--text-primary);
}
.file-grid.view-details .file-item {
display: grid;
grid-template-columns: 24px 1fr 100px 80px 120px;
align-items: center;
padding: var(--space-2) var(--space-3);
gap: var(--space-3);
background: transparent;
border: none;
border-bottom: 1px solid var(--border);
border-radius: 0;
}
.file-grid.view-details .file-item:hover {
background: var(--bg-surface-hover);
}
.file-grid.view-details .file-icon {
width: 18px;
height: 18px;
}
.file-grid.view-details .file-icon svg {
width: 18px;
height: 18px;
}
.file-grid.view-details .file-name {
font-size: var(--text-sm);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
text-align: left;
}
.file-grid.view-details .file-meta {
display: contents;
}
.file-grid.view-details .file-type,
.file-grid.view-details .file-size,
.file-grid.view-details .file-date {
font-size: var(--text-xs);
color: var(--text-muted);
white-space: nowrap;
}
.file-grid.view-details .file-type {
text-transform: uppercase;
}
/* ========== TILES VIEW - Original grid style ========== */
.file-grid.view-tiles {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
gap: var(--space-3);
}
/* Content area */
.content-area {
flex: 1;
display: flex;
overflow: hidden;
}
/* Browse panel */
.browse-panel {
flex: 1;
overflow: auto;
padding: var(--space-5);
}
.browse-panel.hidden {
display: none;
}
.file-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: var(--space-3);
}
.file-item {
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius-md);
padding: var(--space-4);
cursor: pointer;
transition: all var(--transition-fast);
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
gap: var(--space-2);
}
.file-item:hover {
border-color: var(--accent);
transform: translateY(-2px);
box-shadow: var(--shadow-sm);
}
.file-item.active {
border-color: var(--accent);
background: var(--accent-subtle);
}
.file-item.git-changed {
border-left: 3px solid var(--warning);
}
.git-badge {
display: inline-block;
font-size: 10px;
font-weight: 700;
padding: 1px 4px;
border-radius: 3px;
margin-right: 4px;
font-family: var(--font-mono);
}
.git-modified { background: #3b82f620; color: #3b82f6; }
.git-added { background: #22c55e20; color: #22c55e; }
.git-deleted { background: #ef444420; color: #ef4444; }
.git-untracked { background: #f59e0b20; color: #f59e0b; }
.git-renamed { background: #8b5cf620; color: #8b5cf6; }
.diff-btn {
background: var(--accent);
color: white;
border: none;
padding: 2px 8px;
border-radius: 4px;
font-size: 11px;
cursor: pointer;
font-family: var(--font-mono);
}
.diff-btn:hover {
opacity: 0.8;
}
.file-icon {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
color: var(--text-muted);
}
.file-icon svg {
width: 32px;
height: 32px;
}
.file-icon.folder svg {
color: var(--accent);
}
.file-name {
font-size: var(--text-sm);
color: var(--text-primary);
word-break: break-word;
line-height: 1.3;
}
.file-size {
font-size: var(--text-xs);
color: var(--text-muted);
}
/* Editor panel */
.editor-panel {
flex: 1;
display: none;
flex-direction: column;
background: var(--bg-base);
}
.editor-panel.active {
display: flex;
}
.editor-header {
padding: var(--space-3) var(--space-5);
background: var(--bg-surface);
border-bottom: 1px solid var(--border);
display: flex;
justify-content: space-between;
align-items: center;
}
.editor-title {
display: flex;
align-items: center;
gap: var(--space-2);
color: var(--text-primary);
font-size: var(--text-sm);
font-weight: 500;
}
.editor-title svg {
width: 16px;
height: 16px;
color: var(--text-muted);
}
.editor-actions {
display: flex;
gap: var(--space-2);
}
.editor-body {
flex: 1;
overflow: hidden;
}
#codeEditor {
width: 100%;
height: 100%;
background: transparent;
color: var(--text-secondary);
border: none;
padding: var(--space-5);
font-family: var(--font-mono);
font-size: 14px;
line-height: 1.6;
resize: none;
outline: none;
}
#markdownPreview {
display: none;
width: 100%;
height: 100%;
padding: var(--space-5);
overflow-y: auto;
color: var(--text-secondary);
line-height: 1.7;
}
#markdownPreview h1, #markdownPreview h2, #markdownPreview h3 {
color: var(--text-primary);
margin-top: 1.5em;
margin-bottom: 0.5em;
}
#markdownPreview h1 { font-size: 1.8em; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; }
#markdownPreview h2 { font-size: 1.4em; }
#markdownPreview h3 { font-size: 1.2em; }
#markdownPreview p { margin-bottom: 1em; }
#markdownPreview ul, #markdownPreview ol { margin-bottom: 1em; padding-left: 2em; }
#markdownPreview li { margin-bottom: 0.3em; }
#markdownPreview strong { color: var(--text-primary); }
#markdownPreview hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }
#markdownPreview code { background: var(--bg-surface); padding: 2px 6px; border-radius: 4px; font-family: var(--font-mono); }
#markdownPreview pre { background: var(--bg-surface); padding: 1em; border-radius: 8px; overflow-x: auto; }
#markdownPreview blockquote { border-left: 3px solid var(--accent); padding-left: 1em; margin-left: 0; color: var(--text-muted); }
#markdownPreview a, #markdownPreview .file-link { color: var(--accent); text-decoration: none; }
#markdownPreview a:hover, #markdownPreview .file-link:hover { text-decoration: underline; }
.preview-active #codeEditor { display: none; }
.preview-active #markdownPreview { display: block; }
.btn-preview.active { background: var(--accent); color: white; }
.btn-diff.active { background: var(--warning); color: white; }
#gitFilterBtn.active { background: var(--warning); color: white; }
.editor-footer {
padding: var(--space-2) var(--space-5);
background: var(--bg-surface);
border-top: 1px solid var(--border);
display: flex;
justify-content: space-between;
font-size: var(--text-xs);
color: var(--text-muted);
}
.status-saved { color: var(--success); }
.status-modified { color: var(--warning); }
.status-error { color: var(--error); }
/* Empty state */
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
color: var(--text-muted);
gap: var(--space-3);
}
.empty-state svg {
width: 48px;
height: 48px;
opacity: 0.5;
}
@media (max-width: 768px) {
.toolbar {
padding: var(--space-3);
}
.file-grid {
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
}
}
</style>
</head>
<body>
<header class="header">
<a href="index.html" class="logo">
<i data-lucide="circle-dot"></i>
Echo
</a>
<nav class="nav">
<a href="index.html" class="nav-item">
<i data-lucide="layout-list"></i>
<span>Tasks</span>
</a>
<a href="notes.html" class="nav-item">
<i data-lucide="file-text"></i>
<span>KB</span>
</a>
<a href="files.html" class="nav-item active">
<i data-lucide="folder"></i>
<span>Files</span>
</a>
<button class="theme-toggle" onclick="toggleTheme()" title="Schimbă tema">
<i data-lucide="sun" id="themeIcon"></i>
</button>
</nav>
</header>
<main class="main">
<div class="toolbar">
<div class="breadcrumb" id="breadcrumb">
<span class="breadcrumb-item current" onclick="loadPath('')">~/clawd</span>
</div>
<div class="toolbar-actions">
<!-- Git Filter Toggle -->
<div class="view-toggle">
<button class="view-btn" id="gitFilterBtn" onclick="toggleGitFilter()" title="Git Changes">
<i data-lucide="git-branch"></i>
</button>
</div>
<!-- View Mode Toggle -->
<div class="view-toggle" id="viewModeToggle">
<button class="view-btn" data-view="list" onclick="setViewMode('list')" title="Listă">
<i data-lucide="list"></i>
</button>
<button class="view-btn" data-view="details" onclick="setViewMode('details')" title="Detalii">
<i data-lucide="layout-list"></i>
</button>
<button class="view-btn active" data-view="tiles" onclick="setViewMode('tiles')" title="Tiles">
<i data-lucide="layout-grid"></i>
</button>
</div>
<!-- Sort Toggle -->
<div class="view-toggle">
<select class="sort-select" id="sortBy" onchange="sortFiles()">
<option value="name">Nume</option>
<option value="type">Tip</option>
<option value="size">Mărime</option>
<option value="date">Dată</option>
</select>
<button class="view-btn" id="sortDirBtn" onclick="toggleSortDir()" title="Ordine">
<i data-lucide="arrow-down-a-z" id="sortDirIcon"></i>
</button>
</div>
<!-- Browse/Editor Toggle -->
<div class="view-toggle">
<button class="view-btn active" id="browseBtn" onclick="showBrowse()" title="Browse">
<i data-lucide="folder"></i>
</button>
<button class="view-btn" id="editorBtn" onclick="showEditor()" title="Editor">
<i data-lucide="code"></i>
</button>
</div>
</div>
</div>
<div class="content-area">
<div class="browse-panel" id="browsePanel">
<div class="file-grid" id="fileGrid">
<div class="empty-state">
<i data-lucide="loader"></i>
<p>Se încarcă...</p>
</div>
</div>
</div>
<div class="editor-panel" id="editorPanel">
<div class="editor-header">
<div class="editor-title">
<i data-lucide="file"></i>
<span id="editorFileName">Niciun fișier</span>
</div>
<div class="editor-actions">
<button class="btn btn-ghost" onclick="showBrowse()" title="Înapoi">
<i data-lucide="arrow-left"></i>
</button>
<button class="btn btn-ghost btn-preview" onclick="togglePreview()" id="previewBtn" style="display:none;" title="Preview Markdown">
<i data-lucide="eye"></i>
</button>
<button class="btn btn-ghost btn-diff" onclick="toggleDiff()" id="diffBtn" style="display:none;" title="Git Diff">
<i data-lucide="git-compare"></i>
</button>
<button class="btn btn-ghost" onclick="reloadFile()" id="reloadBtn" disabled title="Reload">
<i data-lucide="refresh-cw"></i>
</button>
<button class="btn btn-primary" onclick="saveFile()" id="saveBtn" disabled>
<i data-lucide="save"></i>
Save
</button>
</div>
</div>
<div class="editor-body" id="editorBody">
<textarea id="codeEditor" placeholder="Selectează un fișier..."></textarea>
<div id="markdownPreview"></div>
</div>
<div class="editor-footer">
<span id="statusText">Ready</span>
<span id="fileInfo"></span>
</div>
</div>
</div>
</main>
<script>
// Theme
function initTheme() {
const saved = localStorage.getItem('theme') || 'dark';
document.documentElement.setAttribute('data-theme', saved);
updateThemeIcon(saved);
}
function toggleTheme() {
const current = document.documentElement.getAttribute('data-theme') || 'dark';
const next = current === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', next);
localStorage.setItem('theme', next);
updateThemeIcon(next);
}
function updateThemeIcon(theme) {
const icon = document.getElementById('themeIcon');
if (icon) {
icon.setAttribute('data-lucide', theme === 'dark' ? 'sun' : 'moon');
lucide.createIcons();
}
}
initTheme();
lucide.createIcons();
const API_BASE = window.location.pathname.includes('/echo/') ? '/echo' : '';
let currentPath = '';
let currentFile = null;
let originalContent = '';
let isModified = false;
let currentViewMode = localStorage.getItem('filesViewMode') || 'tiles';
let currentSortBy = localStorage.getItem('filesSortBy') || 'name';
let currentSortDir = localStorage.getItem('filesSortDir') || 'asc';
let currentItems = [];
let gitStatus = {}; // Map of filepath -> status (M, A, D, ??)
let gitOnlyMode = false; // Show only git-changed files
// Initialize view mode
function initViewMode() {
setViewMode(currentViewMode, false);
document.getElementById('sortBy').value = currentSortBy;
updateSortIcon();
}
function setViewMode(mode, reload = true) {
currentViewMode = mode;
localStorage.setItem('filesViewMode', mode);
// Update buttons
document.querySelectorAll('#viewModeToggle .view-btn').forEach(btn => {
btn.classList.toggle('active', btn.dataset.view === mode);
});
// Update grid class
const grid = document.getElementById('fileGrid');
grid.classList.remove('view-list', 'view-details', 'view-tiles');
grid.classList.add('view-' + mode);
if (reload && currentItems.length > 0) {
renderFileGrid(currentItems);
}
}
function sortFiles() {
currentSortBy = document.getElementById('sortBy').value;
localStorage.setItem('filesSortBy', currentSortBy);
if (currentItems.length > 0) {
renderFileGrid(currentItems);
}
}
function setSortBy(field) {
if (currentSortBy === field) {
toggleSortDir();
} else {
currentSortBy = field;
document.getElementById('sortBy').value = field;
localStorage.setItem('filesSortBy', currentSortBy);
if (currentItems.length > 0) {
renderFileGrid(currentItems);
}
}
}
function toggleSortDir() {
currentSortDir = currentSortDir === 'asc' ? 'desc' : 'asc';
localStorage.setItem('filesSortDir', currentSortDir);
updateSortIcon();
if (currentItems.length > 0) {
renderFileGrid(currentItems);
}
}
function updateSortIcon() {
const icon = document.getElementById('sortDirIcon');
const iconName = currentSortDir === 'asc' ? 'arrow-down-a-z' : 'arrow-up-z-a';
icon.setAttribute('data-lucide', iconName);
lucide.createIcons();
}
function showBrowse() {
if (isModified && !confirm('Ai modificări nesalvate. Continui?')) return;
document.getElementById('browsePanel').classList.remove('hidden');
document.getElementById('editorPanel').classList.remove('active');
document.getElementById('browseBtn').classList.add('active');
document.getElementById('editorBtn').classList.remove('active');
}
function showEditor() {
document.getElementById('browsePanel').classList.add('hidden');
document.getElementById('editorPanel').classList.add('active');
document.getElementById('browseBtn').classList.remove('active');
document.getElementById('editorBtn').classList.add('active');
}
async function loadGitStatus() {
try {
const response = await fetch(`${API_BASE}/api/git?` + Date.now());
const data = await response.json();
gitStatus = {};
if (data.uncommittedParsed) {
data.uncommittedParsed.forEach(item => {
gitStatus[item.path] = item.status;
});
}
} catch (e) {
console.error('Failed to load git status:', e);
}
}
async function showDiff(filepath, event) {
if (event) event.stopPropagation();
try {
const response = await fetch(`${API_BASE}/api/diff?path=${encodeURIComponent(filepath)}`);
const data = await response.json();
// Show in a modal or the editor
const diffHtml = data.diff
.split('\n')
.map(line => {
if (line.startsWith('+') && !line.startsWith('+++')) {
return `<span style="color:var(--success)">${escapeHtml(line)}</span>`;
} else if (line.startsWith('-') && !line.startsWith('---')) {
return `<span style="color:var(--error)">${escapeHtml(line)}</span>`;
} else if (line.startsWith('@@')) {
return `<span style="color:var(--accent)">${escapeHtml(line)}</span>`;
}
return escapeHtml(line);
})
.join('\n');
// Open file in editor with diff view
document.getElementById('editorFileName').textContent = `DIFF: ${filepath}`;
document.getElementById('codeEditor').value = data.diff;
document.getElementById('markdownPreview').innerHTML = `<pre style="font-family:var(--font-mono);font-size:13px;line-height:1.5">${diffHtml}</pre>`;
document.getElementById('editorBody').classList.add('preview-active');
document.getElementById('previewBtn').style.display = 'flex';
document.getElementById('previewBtn').classList.add('active');
document.getElementById('saveBtn').disabled = true;
document.getElementById('reloadBtn').disabled = true;
currentFile = null;
setStatus('Diff view', 'saved');
showEditor();
} catch (e) {
alert('Eroare la încărcare diff: ' + e.message);
}
}
function escapeHtml(text) {
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
async function loadPath(path = '') {
currentPath = path;
updateBreadcrumb();
try {
const response = await fetch(`${API_BASE}/api/files?path=${encodeURIComponent(path)}&action=list`);
const data = await response.json();
if (data.error) {
showError(data.error);
return;
}
if (data.type === 'dir') {
await loadGitStatus(); // Refresh git status
renderFileGrid(data.items);
updateURL(path);
} else if (data.type === 'file') {
openFile(path, data);
}
} catch (e) {
showError('Eroare: ' + e.message);
}
}
function updateBreadcrumb() {
const breadcrumb = document.getElementById('breadcrumb');
const parts = currentPath.split('/').filter(p => p);
let html = `<span class="breadcrumb-item ${parts.length === 0 ? 'current' : ''}" onclick="loadPath('')">~/clawd</span>`;
let buildPath = '';
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
buildPath += (buildPath ? '/' : '') + part;
const p = buildPath;
const isCurrent = i === parts.length - 1;
html += `
<span class="breadcrumb-sep"><i data-lucide="chevron-right"></i></span>
<span class="breadcrumb-item ${isCurrent ? 'current' : ''}" onclick="loadPath('${p}')">${part}</span>
`;
}
breadcrumb.innerHTML = html;
lucide.createIcons();
}
function renderFileGrid(items) {
// Store items for re-rendering on view/sort change
currentItems = items;
const grid = document.getElementById('fileGrid');
grid.classList.remove('view-list', 'view-details', 'view-tiles');
grid.classList.add('view-' + currentViewMode);
if (items.length === 0) {
grid.innerHTML = `
<div class="empty-state">
<i data-lucide="folder-open"></i>
<p>Folder gol</p>
</div>
`;
lucide.createIcons();
return;
}
// Sort items
const getExt = (name) => name.includes('.') ? name.split('.').pop().toLowerCase() : '';
const sorted = [...items].sort((a, b) => {
// Directories always first
if (a.type !== b.type) return a.type === 'dir' ? -1 : 1;
let cmp = 0;
if (currentSortBy === 'name') {
cmp = a.name.localeCompare(b.name);
} else if (currentSortBy === 'date') {
cmp = (a.mtime || 0) - (b.mtime || 0);
} else if (currentSortBy === 'size') {
cmp = (a.size || 0) - (b.size || 0);
} else if (currentSortBy === 'type') {
cmp = getExt(a.name).localeCompare(getExt(b.name));
}
return currentSortDir === 'asc' ? cmp : -cmp;
});
// Add header for details view
let headerHtml = '';
if (currentViewMode === 'details') {
const arrow = (field) => currentSortBy === field ? (currentSortDir === 'asc' ? ' ▲' : ' ▼') : '';
headerHtml = `
<div class="file-header">
<span></span>
<span onclick="setSortBy('name')" style="cursor:pointer">Nume${arrow('name')}</span>
<span onclick="setSortBy('type')" style="cursor:pointer">Tip${arrow('type')}</span>
<span onclick="setSortBy('size')" style="cursor:pointer">Mărime${arrow('size')}</span>
<span onclick="setSortBy('date')" style="cursor:pointer">Dată${arrow('date')}</span>
</div>
`;
}
grid.innerHTML = headerHtml + sorted.map(item => {
const dateStr = item.mtime ? new Date(item.mtime * 1000).toLocaleString('ro-RO', {
day: '2-digit', month: 'short', hour: '2-digit', minute: '2-digit'
}) : '';
const fileType = item.type === 'dir' ? 'Folder' : getFileType(item.name);
const sizeStr = item.size !== undefined ? formatSize(item.size) : '-';
// Git status
const gStatus = gitStatus[item.path] || '';
const gitBadge = gStatus ? getGitBadge(gStatus) : '';
const hasGitChange = !!gStatus;
if (currentViewMode === 'details') {
return `
<div class="file-item ${currentFile === item.path ? 'active' : ''} ${hasGitChange ? 'git-changed' : ''}" onclick="handleClick('${item.path}', '${item.type}')">
<div class="file-icon ${item.type === 'dir' ? 'folder' : ''}">
<i data-lucide="${item.type === 'dir' ? 'folder' : getFileIcon(item.name)}"></i>
</div>
<div class="file-name">${gitBadge}${item.name}</div>
<div class="file-meta">
<span class="file-type">${fileType}</span>
<span class="file-size">${sizeStr}</span>
<span class="file-date">${hasGitChange ? `<button class="diff-btn" onclick="showDiff('${item.path}', event)" title="Vezi diff">diff</button>` : (dateStr || '-')}</span>
</div>
</div>
`;
} else if (currentViewMode === 'list') {
return `
<div class="file-item ${currentFile === item.path ? 'active' : ''} ${hasGitChange ? 'git-changed' : ''}" onclick="handleClick('${item.path}', '${item.type}')">
<div class="file-icon ${item.type === 'dir' ? 'folder' : ''}">
<i data-lucide="${item.type === 'dir' ? 'folder' : getFileIcon(item.name)}"></i>
</div>
<div class="file-name">${gitBadge}${item.name}</div>
</div>
`;
} else {
// Tiles view - original style
return `
<div class="file-item ${currentFile === item.path ? 'active' : ''} ${hasGitChange ? 'git-changed' : ''}" onclick="handleClick('${item.path}', '${item.type}')">
<div class="file-icon ${item.type === 'dir' ? 'folder' : ''}">
<i data-lucide="${item.type === 'dir' ? 'folder' : getFileIcon(item.name)}"></i>
</div>
<div class="file-name">${gitBadge}${item.name}</div>
</div>
`;
}
}).join('');
lucide.createIcons();
}
function getFileIcon(name) {
const ext = name.split('.').pop().toLowerCase();
const icons = {
'md': 'file-text',
'txt': 'file-text',
'json': 'file-json',
'js': 'file-code',
'py': 'file-code',
'html': 'file-code',
'css': 'file-code',
'sh': 'terminal',
'yml': 'file-cog',
'yaml': 'file-cog',
'log': 'file-text',
'xsd': 'file-code',
'pdf': 'file-text'
};
return icons[ext] || 'file';
}
function getFileType(name) {
const ext = name.split('.').pop().toLowerCase();
const types = {
'md': 'Markdown',
'txt': 'Text',
'json': 'JSON',
'js': 'JavaScript',
'py': 'Python',
'html': 'HTML',
'css': 'CSS',
'sh': 'Shell',
'yml': 'YAML',
'yaml': 'YAML',
'log': 'Log',
'xsd': 'XML Schema',
'pdf': 'PDF'
};
return types[ext] || ext.toUpperCase();
}
function formatSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
}
function getGitBadge(status) {
const badges = {
'M': '<span class="git-badge git-modified" title="Modificat">M</span>',
'A': '<span class="git-badge git-added" title="Adăugat">A</span>',
'D': '<span class="git-badge git-deleted" title="Șters">D</span>',
'??': '<span class="git-badge git-untracked" title="Nou (untracked)">+</span>',
'R': '<span class="git-badge git-renamed" title="Redenumit">R</span>',
};
return badges[status] || `<span class="git-badge">${status}</span>`;
}
function handleClick(path, type) {
if (type === 'dir') {
loadPath(path);
} else {
if (isModified && !confirm('Ai modificări nesalvate. Continui?')) return;
loadPath(path);
}
}
function openFile(path, data) {
currentFile = path;
originalContent = data.content;
updateURL(path);
document.getElementById('editorFileName').textContent = data.name;
document.getElementById('codeEditor').value = data.content;
document.getElementById('saveBtn').disabled = false;
document.getElementById('reloadBtn').disabled = false;
document.getElementById('fileInfo').textContent = formatSize(data.size);
// Show preview button for markdown files
const isMarkdown = path.endsWith('.md');
document.getElementById('previewBtn').style.display = isMarkdown ? 'flex' : 'none';
// Always show diff button - let user check if file has changes
document.getElementById('diffBtn').style.display = 'flex';
document.getElementById('diffBtn').classList.remove('active');
// Auto-activate preview for markdown files
if (isMarkdown) {
const preview = document.getElementById('markdownPreview');
preview.innerHTML = marked.parse(data.content);
document.getElementById('editorBody').classList.add('preview-active');
document.getElementById('previewBtn').classList.add('active');
} else {
document.getElementById('editorBody').classList.remove('preview-active');
document.getElementById('previewBtn').classList.remove('active');
}
if (data.truncated) {
setStatus('Fișier trunchiat', 'error');
} else {
setStatus('Loaded', 'saved');
}
isModified = false;
showEditor();
}
function togglePreview() {
const editorBody = document.getElementById('editorBody');
const previewBtn = document.getElementById('previewBtn');
const diffBtn = document.getElementById('diffBtn');
const preview = document.getElementById('markdownPreview');
const content = document.getElementById('codeEditor').value;
if (editorBody.classList.contains('preview-active')) {
// Switch to edit mode
editorBody.classList.remove('preview-active');
previewBtn.classList.remove('active');
if (diffBtn) diffBtn.classList.remove('active');
setStatus('Edit mode', 'saved');
} else {
// Switch to preview mode
preview.innerHTML = marked.parse(content);
editorBody.classList.add('preview-active');
previewBtn.classList.add('active');
if (diffBtn) diffBtn.classList.remove('active');
setStatus('Preview mode', 'saved');
}
}
async function toggleDiff() {
if (!currentFile) return;
const editorBody = document.getElementById('editorBody');
const diffBtn = document.getElementById('diffBtn');
const previewBtn = document.getElementById('previewBtn');
const preview = document.getElementById('markdownPreview');
// If already showing diff, switch back to edit
if (diffBtn.classList.contains('active')) {
editorBody.classList.remove('preview-active');
diffBtn.classList.remove('active');
setStatus('Edit mode', 'saved');
return;
}
try {
setStatus('Se încarcă diff...', 'modified');
const response = await fetch(`${API_BASE}/api/diff?path=${encodeURIComponent(currentFile)}`);
const data = await response.json();
if (data.error) {
setStatus('Eroare: ' + data.error, 'error');
return;
}
// Format diff with colors
const diffHtml = data.diff
.split('\n')
.map(line => {
if (line.startsWith('+') && !line.startsWith('+++')) {
return `<span style="color:var(--success)">${escapeHtml(line)}</span>`;
} else if (line.startsWith('-') && !line.startsWith('---')) {
return `<span style="color:var(--error)">${escapeHtml(line)}</span>`;
} else if (line.startsWith('@@')) {
return `<span style="color:var(--accent)">${escapeHtml(line)}</span>`;
}
return escapeHtml(line);
})
.join('\n');
preview.innerHTML = `<pre style="font-family:var(--font-mono);font-size:13px;line-height:1.5;white-space:pre-wrap">${diffHtml || 'Nicio modificare față de ultima versiune comisă.'}</pre>`;
editorBody.classList.add('preview-active');
diffBtn.classList.add('active');
if (previewBtn) previewBtn.classList.remove('active');
setStatus('Diff view', 'saved');
} catch (e) {
setStatus('Eroare: ' + e.message, 'error');
}
}
async function saveFile() {
if (!currentFile) return;
const content = document.getElementById('codeEditor').value;
try {
document.getElementById('saveBtn').disabled = true;
setStatus('Se salvează...', 'modified');
const response = await fetch(`${API_BASE}/api/files`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ path: currentFile, content })
});
const data = await response.json();
if (data.error) {
setStatus('Eroare: ' + data.error, 'error');
} else {
originalContent = content;
isModified = false;
setStatus('Salvat ✓', 'saved');
}
} catch (e) {
setStatus('Eroare: ' + e.message, 'error');
}
document.getElementById('saveBtn').disabled = false;
}
function reloadFile() {
if (currentFile && confirm('Renunți la modificări?')) {
loadPath(currentFile);
}
}
function setStatus(text, type) {
const status = document.getElementById('statusText');
status.textContent = text;
status.className = 'status-' + type;
}
function showError(msg) {
document.getElementById('fileGrid').innerHTML = `
<div class="empty-state">
<i data-lucide="alert-circle"></i>
<p style="color: var(--error)">${msg}</p>
</div>
`;
lucide.createIcons();
}
function updateURL(path) {
if (path) {
history.replaceState(null, '', `#${path}`);
} else {
history.replaceState(null, '', window.location.pathname);
}
}
function getPathFromURL() {
const hash = window.location.hash;
return hash ? decodeURIComponent(hash.slice(1)) : '';
}
window.addEventListener('hashchange', () => loadPath(getPathFromURL()));
document.getElementById('codeEditor').addEventListener('input', function() {
isModified = this.value !== originalContent;
setStatus(isModified ? 'Modified' : 'Ready', isModified ? 'modified' : 'saved');
});
document.addEventListener('keydown', e => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
saveFile();
}
});
// Init
initViewMode();
// Check for git mode
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get('git') === '1') {
gitOnlyMode = true;
loadGitChangedFiles();
} else {
loadPath(getPathFromURL());
}
function toggleGitFilter() {
gitOnlyMode = !gitOnlyMode;
const btn = document.getElementById('gitFilterBtn');
btn.classList.toggle('active', gitOnlyMode);
if (gitOnlyMode) {
loadGitChangedFiles();
} else {
// Return to normal browse
window.history.replaceState(null, '', 'files.html');
loadPath('');
}
}
async function loadGitChangedFiles() {
await loadGitStatus();
const changedPaths = Object.keys(gitStatus);
// Update button state
document.getElementById('gitFilterBtn').classList.add('active');
if (changedPaths.length === 0) {
document.getElementById('breadcrumb').innerHTML = `
<span class="breadcrumb-item" onclick="toggleGitFilter()">~/clawd</span>
<span class="breadcrumb-sep"><i data-lucide="chevron-right"></i></span>
<span class="breadcrumb-item current" style="color:var(--success)">✓ Git curat</span>
`;
lucide.createIcons();
showError('Nicio modificare git - totul e comis!');
return;
}
// Update breadcrumb
document.getElementById('breadcrumb').innerHTML = `
<span class="breadcrumb-item" onclick="toggleGitFilter()">~/clawd</span>
<span class="breadcrumb-sep"><i data-lucide="chevron-right"></i></span>
<span class="breadcrumb-item current" style="color:var(--warning)">🔸 Git Changes (${changedPaths.length})</span>
`;
lucide.createIcons();
// Create virtual items for changed files
const items = changedPaths.map(path => ({
name: path,
path: path,
type: 'file',
size: null,
mtime: null
}));
currentItems = items;
renderFileGrid(items);
}
</script>
</body>
</html>