refac
This commit is contained in:
1 parent
5caa91a493
commit
2dadc5435a
7 files changed
+101
-38
No files matched your search
@@ -93,6 +93,7 @@
|
||||
let currentPath = savedPath;
|
||||
let fileRoot: TerminalFileRoot | null = null;
|
||||
let entries: FileEntry[] = [];
|
||||
let currentWritable = true;
|
||||
let loading = false;
|
||||
let error: string | null = null;
|
||||
|
||||
@@ -175,6 +176,7 @@
|
||||
|
||||
// ── File preview state ───────────────────────────────────────────────
|
||||
let selectedFile: string | null = null;
|
||||
let selectedFileWritable = true;
|
||||
let previewPort: number | null = null;
|
||||
let fileContent: string | null = null;
|
||||
let fileImageUrl: string | null = null;
|
||||
@@ -410,6 +412,7 @@
|
||||
loading = true;
|
||||
error = null;
|
||||
selectedFile = null;
|
||||
selectedFileWritable = true;
|
||||
previewPort = null;
|
||||
clearFilePreview();
|
||||
clearSelection();
|
||||
@@ -428,7 +431,8 @@
|
||||
'Failed to load directory. Check your Terminal connection in Settings → Integrations.';
|
||||
entries = [];
|
||||
} else {
|
||||
entries = sortEntries(result);
|
||||
currentWritable = result.writable !== false;
|
||||
entries = sortEntries(result.entries);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -439,6 +443,7 @@
|
||||
}
|
||||
|
||||
const filePath = `${currentPath}${entry.name}`;
|
||||
selectedFileWritable = entry.writable !== false;
|
||||
pushNavHistory(currentPath, filePath);
|
||||
|
||||
const terminal = selectedTerminal;
|
||||
@@ -566,6 +571,7 @@
|
||||
// ── Drag-and-drop upload ─────────────────────────────────────────────
|
||||
const handleDragOver = (e: DragEvent) => {
|
||||
if (selectedFile) return;
|
||||
if (!currentWritable) return;
|
||||
if (!e.dataTransfer?.types.includes('Files')) return;
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
@@ -578,7 +584,7 @@
|
||||
isDragOver = false;
|
||||
|
||||
const terminal = selectedTerminal;
|
||||
if (selectedFile || !terminal) return;
|
||||
if (selectedFile || !terminal || !currentWritable) return;
|
||||
|
||||
const droppedFiles = Array.from(e.dataTransfer?.files ?? []);
|
||||
if (!droppedFiles.length) return;
|
||||
@@ -593,7 +599,7 @@
|
||||
|
||||
const handleUploadFiles = async (files: File[]) => {
|
||||
const terminal = selectedTerminal;
|
||||
if (!files.length || !terminal) return;
|
||||
if (!files.length || !terminal || !currentWritable) return;
|
||||
|
||||
uploading = true;
|
||||
for (const file of files) {
|
||||
@@ -605,6 +611,7 @@
|
||||
|
||||
// ── Folder creation ──────────────────────────────────────────────────
|
||||
const startNewFolder = async () => {
|
||||
if (!currentWritable) return;
|
||||
creatingFolder = true;
|
||||
newFolderName = '';
|
||||
await tick();
|
||||
@@ -634,6 +641,7 @@
|
||||
|
||||
// ── File creation ────────────────────────────────────────────────────
|
||||
const startNewFile = async () => {
|
||||
if (!currentWritable) return;
|
||||
creatingFile = true;
|
||||
newFileName = '';
|
||||
await tick();
|
||||
@@ -658,7 +666,7 @@
|
||||
// ── Delete ───────────────────────────────────────────────────────────
|
||||
const handleDelete = async (path: string, name: string) => {
|
||||
const terminal = selectedTerminal;
|
||||
if (!terminal) return;
|
||||
if (!terminal || !currentWritable) return;
|
||||
|
||||
const result = await deleteEntry(terminal.url, terminal.key, path, chatId ?? undefined);
|
||||
toast[result ? 'success' : 'error'](
|
||||
@@ -668,6 +676,7 @@
|
||||
};
|
||||
|
||||
const requestDelete = (path: string, name: string) => {
|
||||
if (!currentWritable) return;
|
||||
deleteTarget = { path, name };
|
||||
showDeleteConfirm = true;
|
||||
};
|
||||
@@ -675,7 +684,7 @@
|
||||
// ── Move (drag-and-drop) ────────────────────────────────────────────
|
||||
const handleMove = async (source: string, destFolder: string) => {
|
||||
const terminal = selectedTerminal;
|
||||
if (!terminal) return;
|
||||
if (!terminal || !currentWritable) return;
|
||||
|
||||
const fileName = source.split('/').pop() ?? '';
|
||||
const destination = `${destFolder}${fileName}`;
|
||||
@@ -704,7 +713,7 @@
|
||||
// ── Rename ──────────────────────────────────────────────────────────
|
||||
const handleRename = async (oldPath: string, newName: string) => {
|
||||
const terminal = selectedTerminal;
|
||||
if (!terminal || !newName) return;
|
||||
if (!terminal || !newName || !currentWritable) return;
|
||||
|
||||
const dir = oldPath.substring(0, oldPath.lastIndexOf('/') + 1) || currentPath;
|
||||
const destination = `${dir}${newName}`;
|
||||
@@ -733,6 +742,13 @@
|
||||
|
||||
$: selectedCount = selectedEntries.size;
|
||||
$: hasSelectedFiles = [...selectedEntries].some((p) => !p.endsWith('/'));
|
||||
$: selectedEntriesWritable =
|
||||
currentWritable &&
|
||||
[...selectedEntries].every((path) => {
|
||||
const name = path.replace(/\/$/, '').split('/').pop();
|
||||
const entry = entries.find((item) => item.name === name);
|
||||
return entry?.writable !== false;
|
||||
});
|
||||
|
||||
const clearSelection = () => {
|
||||
selectedEntries = new Set();
|
||||
@@ -792,7 +808,7 @@
|
||||
|
||||
const bulkDelete = async () => {
|
||||
const terminal = selectedTerminal;
|
||||
if (!terminal) return;
|
||||
if (!terminal || !selectedEntriesWritable) return;
|
||||
|
||||
const paths = [...selectedEntries];
|
||||
let ok = 0;
|
||||
@@ -1046,6 +1062,7 @@
|
||||
breadcrumbs={buildBreadcrumbs(currentPath)}
|
||||
{selectedFile}
|
||||
{loading}
|
||||
writable={currentWritable}
|
||||
{canGoBack}
|
||||
{canGoForward}
|
||||
{sortBy}
|
||||
@@ -1143,9 +1160,9 @@
|
||||
{#if isHtml && showRaw}
|
||||
<Tooltip content={$i18n.t('Save')}>
|
||||
<button
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400"
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 disabled:opacity-30 disabled:hover:bg-transparent"
|
||||
on:click={() => filePreviewRef?.saveCodeFile()}
|
||||
disabled={saving}
|
||||
disabled={saving || !selectedFileWritable}
|
||||
aria-label={$i18n.t('Save')}
|
||||
>
|
||||
{#if saving}
|
||||
@@ -1171,9 +1188,9 @@
|
||||
{:else if isMarkdown && showRaw}
|
||||
<Tooltip content={$i18n.t('Save')}>
|
||||
<button
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400"
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 disabled:opacity-30 disabled:hover:bg-transparent"
|
||||
on:click={() => filePreviewRef?.saveCodeFile()}
|
||||
disabled={saving}
|
||||
disabled={saving || !selectedFileWritable}
|
||||
aria-label={$i18n.t('Save')}
|
||||
>
|
||||
{#if saving}
|
||||
@@ -1199,9 +1216,9 @@
|
||||
{:else if isCode}
|
||||
<Tooltip content={$i18n.t('Save')}>
|
||||
<button
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400"
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 disabled:opacity-30 disabled:hover:bg-transparent"
|
||||
on:click={() => filePreviewRef?.saveCodeFile()}
|
||||
disabled={saving}
|
||||
disabled={saving || !selectedFileWritable}
|
||||
aria-label={$i18n.t('Save')}
|
||||
>
|
||||
{#if saving}
|
||||
@@ -1243,9 +1260,9 @@
|
||||
</Tooltip>
|
||||
<Tooltip content={$i18n.t('Save')}>
|
||||
<button
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400"
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 disabled:opacity-30 disabled:hover:bg-transparent"
|
||||
on:click={() => filePreviewRef?.saveEdit()}
|
||||
disabled={saving}
|
||||
disabled={saving || !selectedFileWritable}
|
||||
aria-label={$i18n.t('Save')}
|
||||
>
|
||||
{#if saving}
|
||||
@@ -1269,8 +1286,9 @@
|
||||
{:else}
|
||||
<Tooltip content={$i18n.t('Edit')}>
|
||||
<button
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400"
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 disabled:opacity-30 disabled:hover:bg-transparent"
|
||||
on:click={() => filePreviewRef?.startEdit()}
|
||||
disabled={!selectedFileWritable}
|
||||
aria-label={$i18n.t('Edit')}
|
||||
>
|
||||
<PenAlt className="size-3.5" />
|
||||
@@ -1309,7 +1327,7 @@
|
||||
<Tooltip content={$i18n.t('Download')}>
|
||||
<button
|
||||
class="shrink-0 p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400"
|
||||
on:click={() => downloadFile(selectedFile)}
|
||||
on:click={() => selectedFile && downloadFile(selectedFile)}
|
||||
aria-label={$i18n.t('Download')}
|
||||
>
|
||||
<svg
|
||||
@@ -1335,6 +1353,7 @@
|
||||
<BulkActionBar
|
||||
count={selectedCount}
|
||||
hasFiles={hasSelectedFiles}
|
||||
canDelete={selectedEntriesWritable}
|
||||
onDelete={() => {
|
||||
deleteTarget = { path: '__bulk__', name: `${selectedCount} items` };
|
||||
showDeleteConfirm = true;
|
||||
@@ -1370,6 +1389,7 @@
|
||||
bind:saving
|
||||
bind:currentSlide
|
||||
{selectedFile}
|
||||
readOnly={!selectedFileWritable}
|
||||
{fileLoading}
|
||||
{fileImageUrl}
|
||||
{fileVideoUrl}
|
||||
@@ -1394,7 +1414,7 @@
|
||||
overlay={overlay || isDraggingHandle}
|
||||
onSave={async (content) => {
|
||||
const terminal = selectedTerminal;
|
||||
if (!terminal || !selectedFile) return;
|
||||
if (!terminal || !selectedFile || !selectedFileWritable) return;
|
||||
const fileName = selectedFile.split('/').pop() ?? 'file';
|
||||
const dir = selectedFile.substring(0, selectedFile.lastIndexOf('/') + 1) || '/';
|
||||
const file = new File([content], fileName, { type: 'text/plain' });
|
||||
@@ -1490,6 +1510,7 @@
|
||||
onSelect={handleSelect}
|
||||
onLongPress={enterSelectionMode}
|
||||
showDate={sortBy === 'date'}
|
||||
parentWritable={currentWritable}
|
||||
/>
|
||||
{/each}
|
||||
</ul>
|
||||
|
||||
Reference in new issue
Block a user