feat: drag files between file panes (move or copy, SSH hosts too) and into a terminal of the same machine

This commit is contained in:
redtea committed 2026-10-15 21:42:21 +03:00
1 parent a30b89f1d8
commit f2740c50b4
12 files changed
+484 -32

No files matched your search

+14
View File
@@ -342,6 +342,8 @@ body.split1 .float-add { top:42px; right:16px; }
body.multi .float-add { opacity:0; pointer-events:none; }
.tabview { position:absolute; inset:8px; opacity:0; visibility:hidden; transform:translateY(4px) scale(.995);
transition:opacity .22s ease, transform .22s ease, visibility 0s .22s; }
/* A tab fading out takes no pointer: a drop goes to the tab coming in. */
.tabview:not(.on) { pointer-events:none; }
.tabview.on { opacity:1; visibility:visible; transform:none; transition:opacity .22s ease, transform .22s ease, visibility 0s; }
.pane { position:absolute; display:flex; flex-direction:column; border-radius:7px; overflow:hidden;
transition:left .26s ease, top .26s ease, width .26s ease, height .26s ease, border-color .2s; }
@@ -373,6 +375,18 @@ body:not(.touch) .tabview:not(.split) .xterm .xterm-viewport { right:-7px; }
box-shadow:0 12px 30px rgba(0,0,0,.55); transition:transform .04s linear; }
.drag-ghost[data-hint]:not([data-hint=""])::after { content:attr(data-hint); display:block; margin-top:3px; font-size:11px; color:var(--muted); }
body.dnd, body.dnd * { cursor:grabbing !important; }
/* Files dragged out of a pane: the target's outline, and the small window
where they were dropped. */
.hint.drophint { position:fixed; z-index:55; transition:none; }
.dropdlg { position:fixed; z-index:56; width:300px; max-width:calc(100vw - 16px); padding:12px; display:flex; flex-direction:column; gap:8px;
background:var(--panel); border:1px solid var(--border); border-radius:10px; box-shadow:0 14px 36px rgba(0,0,0,.55); font-size:13px; }
.dropdlg[hidden] { display:none; }
.dropdlg .dd-what { font-weight:600; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.dropdlg .dd-to { overflow-wrap:anywhere; }
.dropdlg .dd-c { display:flex; align-items:center; gap:6px; }
.dropdlg .mv-prog { margin-top:0; }
.dropdlg .dd-btns { display:flex; gap:6px; justify-content:flex-end; }
.fpane .fp-list tr[data-name] { user-select:none; }
.empty { position:absolute; inset:0; display:flex; flex-direction:column; align-items:center; justify-content:center; gap:12px; color:var(--muted); }
.empty[hidden] { display:none; }
/* The "+" menu: a terminal here, or on another machine. */
+196
View File
@@ -0,0 +1,196 @@
// Dragging files out of a file pane: onto another file pane or a folder in
// it to move or copy them there (between machines and SSH hosts too, by the
// transfer the move dialog uses), or into a terminal of the same place to
// paste their paths. A terminal of another place refuses them, red, with
// the reason. Held over a tab's title for half a second, the tab opens to
// drop into. Esc or empty space cancels. Computers only.
(function () {
'use strict';
if (!Workspace.desk) return;
const FV = window.FileViews;
const {toast, join, esc} = FV;
let d = null, justDropped = false;
const box = document.createElement('div');
box.className = 'hint drophint';
box.hidden = true;
document.body.appendChild(box);
// start: a pointerdown on a row of pane; the drag begins past 6px.
function start(ev, pane, tr) {
if (ev.button !== 0 || ev.target.closest('input, button, a, .act')) return;
const name = tr.dataset.name;
const names = pane.selected.has(name) ? [...pane.selected] : [name];
d = {pane, names, x0: ev.clientX, y0: ev.clientY, id: ev.pointerId, on: false,
item: {source: pane.source, dir: pane.cwd, paths: names.map(n => join(pane.cwd, n))}};
}
function begin() {
d.on = true;
const g = d.ghost = document.createElement('div');
g.className = 'drag-ghost';
g.textContent = d.names.length === 1 ? d.names[0] : d.names.length + ' элементов';
document.body.appendChild(g);
document.body.classList.add('dnd');
getSelection().removeAllRanges();
}
function show(r, label, bad) {
box.hidden = false;
box.classList.toggle('bad', !!bad);
box.style.left = r.left + 'px'; box.style.top = r.top + 'px';
box.style.width = r.width + 'px'; box.style.height = r.height + 'px';
box.textContent = label;
}
// inside: dropping folder p into itself or below it.
const inside = (dir, p) => dir === p || dir.startsWith(p.replace(/\/+$/, '') + '/');
function move(ev) {
if (!d || ev.pointerId !== d.id) return;
if (!d.on) {
if (Math.hypot(ev.clientX - d.x0, ev.clientY - d.y0) < 6) return;
begin();
}
d.ghost.style.transform = 'translate(' + (ev.clientX + 12) + 'px,' + (ev.clientY + 10) + 'px)';
d.target = null;
box.hidden = true;
d.ghost.dataset.hint = '';
// Over a tab's title: it opens after half a second.
const tab = Workspace.tabAt(ev.clientX, ev.clientY);
if (tab !== d.overTab) {
clearTimeout(d.tabTimer);
d.overTab = tab;
if (tab) d.tabTimer = setTimeout(() => { if (d && d.overTab === tab) Workspace.activate(tab); }, 500);
}
if (tab) { d.ghost.dataset.hint = 'открыть вкладку'; return; }
const pane = Workspace.paneAt(ev.clientX, ev.clientY);
if (!pane || !pane.dropTarget) return;
const t = pane.dropTarget(ev.clientX, ev.clientY);
const a = WSUtil.dropAction(d.item, t);
const r = t.kind === 'folder' ? t.row.getBoundingClientRect() : pane.el.getBoundingClientRect();
if (a.kind === 'deny') { show(r, a.reason, true); return; }
if (a.kind === 'none') return;
if (a.kind === 'transfer' && t.source.id === d.item.source.id && d.item.paths.some(p => inside(t.dir, p))) {
show(r, 'папку не положить в саму себя', true);
return;
}
show(r, a.kind === 'paste' ? 'вставить путь' : 'сюда: ' + t.dir);
d.target = {pane, t, a};
}
function end(ev, cancel) {
if (!d || (ev && ev.pointerId !== d.id)) return;
const x = d;
d = null;
clearTimeout(x.tabTimer);
if (!x.on) return;
x.ghost.remove();
box.hidden = true;
document.body.classList.remove('dnd');
justDropped = true;
setTimeout(() => { justDropped = false; }, 0);
if (cancel || !x.target) return;
const {pane, t, a} = x.target;
Workspace.setActivePane(pane);
if (a.kind === 'paste') pane.drop(x.item);
else if (a.kind === 'transfer') ask(x.item, x.pane, t, ev.clientX, ev.clientY);
}
window.addEventListener('pointermove', move);
window.addEventListener('pointerup', ev => end(ev));
window.addEventListener('pointercancel', ev => end(ev, true));
window.addEventListener('keydown', ev => { if (ev.key === 'Escape' && d) end(null, true); });
// A drop is not a click on the row it started from.
document.addEventListener('click', ev => { if (justDropped) { ev.stopPropagation(); ev.preventDefault(); } }, true);
// ---------- the small window where the files were dropped ----------
const dlg = document.createElement('div');
dlg.className = 'dropdlg';
dlg.hidden = true;
dlg.innerHTML =
'<div class="dd-what"></div><div class="dd-to muted"></div>' +
'<label class="dd-c">если уже есть: <select class="dd-conflict">' +
'<option value="error">сообщить</option><option value="replace">заменить</option><option value="skip">оставить</option></select></label>' +
'<div class="mv-prog dd-prog" hidden><div class="mv-bar"><i class="dd-bar"></i></div><div class="muted dd-ptext"></div></div>' +
'<div class="dd-btns"><button type="button" class="btn small primary dd-move">Переместить</button>' +
'<button type="button" class="btn small dd-copy">Копировать</button>' +
'<button type="button" class="btn small ghost dd-cancel">Отмена</button></div>';
document.body.appendChild(dlg);
const q = s => dlg.querySelector(s);
let cur = null;
function ask(item, from, t, x, y) {
if (cur && cur.job) { toast('дождитесь конца передачи', true); return; }
cur = {item, from, t, job: null, stop: false};
const n = item.paths.length;
q('.dd-what').textContent = n === 1 ? item.paths[0].split('/').pop() : n + ' элементов';
q('.dd-to').innerHTML = '→ ' + esc(t.source.label) + ':' + esc(t.dir);
q('.dd-conflict').value = 'error';
q('.dd-prog').hidden = true;
q('.dd-move').disabled = q('.dd-copy').disabled = false;
q('.dd-cancel').textContent = 'Отмена';
dlg.hidden = false;
const w = dlg.offsetWidth, h = dlg.offsetHeight;
dlg.style.left = Math.max(8, Math.min(innerWidth - w - 8, x - w / 2)) + 'px';
dlg.style.top = Math.max(8, Math.min(innerHeight - h - 8, y + 12)) + 'px';
q('.dd-move').focus();
}
function close() { dlg.hidden = true; cur = null; }
q('.dd-cancel').onclick = () => {
if (cur && cur.job) { cur.stop = true; fetch('/api/panel/fs/job/' + cur.job + '/cancel', {method: 'POST'}); q('.dd-ptext').textContent = 'отмена…'; return; }
close();
};
dlg.addEventListener('keydown', ev => { if (ev.key === 'Escape' && !(cur && cur.job)) close(); });
document.addEventListener('pointerdown', ev => { if (!dlg.hidden && !dlg.contains(ev.target) && !(cur && cur.job)) close(); }, true);
q('.dd-move').onclick = () => run(true);
q('.dd-copy').onclick = () => run(false);
async function run(isMove) {
const c = cur, {item, from, t} = c, conflict = q('.dd-conflict').value;
q('.dd-move').disabled = q('.dd-copy').disabled = true;
// One place (a machine, or one SSH host): copied there by its own means.
// Between places: the transfer of the move dialog, one item at a time.
const here = item.source.id === t.source.id;
let done = 0, skipped = 0, err = '';
if (!here) {
q('.dd-prog').hidden = false;
q('.dd-bar').style.width = '0';
q('.dd-bar').parentNode.classList.add('wait');
q('.dd-ptext').textContent = 'подготовка…';
q('.dd-cancel').textContent = 'Стоп';
}
for (const p of item.paths) {
if (c.stop) break;
const name = p.split('/').pop();
let r;
if (here) {
r = await from.src.op('copy', {src: p, dst: join(t.dir, name), move: isMove, conflict});
} else {
const body = {src: p, name, dir: t.dir, conflict, move: isMove};
if (t.source.kind === 'ssh') { body.to = t.pane.agent.id; body.to_conn = t.pane.src.conn; }
else body.to = t.pane.agent.id;
if (from.kind === 'sshfiles') body.from_conn = from.src.conn;
// The transfer is the machine's, whichever side is an SSH host.
r = await new MachineSource(from.agent).op('ship', body);
if (r.ok) {
c.job = r.data.job;
const pre = item.paths.length > 1 ? name + ': ' : '';
r = await FV.followJob(c.job, (pr, text) => {
if (pr.total > 0 || pr.done > 0) {
q('.dd-bar').parentNode.classList.remove('wait');
q('.dd-bar').style.width = (pr.total ? Math.min(100, pr.done / pr.total * 100) : 0).toFixed(1) + '%';
}
if (text) q('.dd-ptext').textContent = pre + text;
});
c.job = null;
}
}
if (!r.ok) { err = r.error || 'ошибка'; break; }
done++;
skipped += (r.data && r.data.skipped) || 0;
}
close();
from.reload();
if (t.pane !== from) t.pane.reload();
if (err) toast((done ? 'сделано ' + done + ' из ' + item.paths.length + '; ' : '') + err, true);
else toast((isMove ? 'перемещено' : 'скопировано') + (item.paths.length > 1 ? ': ' + done : '') + (skipped ? ', оставлено как было: ' + skipped : ''));
}
window.FileDnD = {start, get active() { return !!(d && d.on); }};
})();
+2 -1
View File
@@ -148,7 +148,7 @@
dropTarget(x, y) {
const el = document.elementFromPoint(x, y);
const tr = el && el.closest && el.closest('tr[data-dir]');
if (tr && this.el.contains(tr)) return {kind: 'folder', source: this.source, dir: join(this.cwd, tr.dataset.dir), pane: this};
if (tr && this.el.contains(tr)) return {kind: 'folder', source: this.source, dir: join(this.cwd, tr.dataset.dir), pane: this, row: tr};
return {kind: 'files', source: this.source, dir: this.cwd, pane: this};
}
@@ -251,6 +251,7 @@
tr.querySelector('[data-act="ren"]').onclick = () => FV.move(this, e);
const uz = tr.querySelector('[data-act="unzip"]');
if (uz) uz.onclick = () => this.unzip(e);
if (window.FileDnD) tr.addEventListener('pointerdown', ev => FileDnD.start(ev, this, tr));
tb.appendChild(tr);
}
}
+30 -18
View File
@@ -748,6 +748,31 @@
$('mv-prog').hidden = !on;
if (on) requestAnimationFrame(() => $('mv-prog').scrollIntoView({block: 'nearest', behavior: 'smooth'}));
}
// followJob waits for a transfer job to end, telling onProgress how far
// it has got (the progress and a line about it), and gives its result.
async function followJob(job, onProgress) {
let v = 0;
const seen = [];
for (;;) {
let j;
try { j = await fetch('/api/panel/fs/job/' + job + '?v=' + v, {cache: 'no-store'}).then(x => x.json()); }
catch (e) { await new Promise(ok => setTimeout(ok, 1500)); continue; }
v = j.v || 0;
const p = j.progress || {};
let text = '';
if (p.total > 0 || p.done > 0) {
// Speed over the last few seconds.
seen.push([Date.now(), p.done]);
while (seen.length > 2 && Date.now() - seen[0][0] > 4000) seen.shift();
const dt = (seen[seen.length - 1][0] - seen[0][0]) / 1000, rate = dt > 0.5 ? (seen[seen.length - 1][1] - seen[0][1]) / dt : 0;
text = fmtSize(p.done) + ' из ' + fmtSize(p.total) + ' · файлов: ' + p.files + (rate ? ' · ' + fmtSize(Math.round(rate)) + '/с' : '');
} else if (p.note) {
text = p.note === 'подсчёт' ? 'подсчёт…' : p.note + '…';
}
onProgress(p, text);
if (j.done) return j.result || {ok: false, error: 'ошибка'};
}
}
// ship sends to the other machine and follows the job to its end.
async function ship(dir, name, conflict, move) {
const body = {src: M.src, name, dir, conflict, move};
@@ -771,27 +796,14 @@
$('mv-pw').value = ''; $('mv-pass').value = '';
if (!r.ok) { busy(false); toast(r.error || 'ошибка', true); return; }
const job = M.job = r.data.job;
let v = 0, res = null;
const t0 = Date.now(), seen = [];
while (!res) {
let j;
try { j = await fetch('/api/panel/fs/job/' + job + '?v=' + v, {cache: 'no-store'}).then(x => x.json()); }
catch (e) { await new Promise(ok => setTimeout(ok, 1500)); continue; }
v = j.v || 0;
const p = j.progress || {};
const t0 = Date.now();
const res = await followJob(job, (p, text) => {
if (p.total > 0 || p.done > 0) {
$('mv-bar').parentNode.classList.remove('wait');
$('mv-bar').style.width = (p.total ? Math.min(100, p.done / p.total * 100) : 0).toFixed(1) + '%';
// Speed over the last few seconds.
seen.push([Date.now(), p.done]);
while (seen.length > 2 && Date.now() - seen[0][0] > 4000) seen.shift();
const dt = (seen[seen.length - 1][0] - seen[0][0]) / 1000, rate = dt > 0.5 ? (seen[seen.length - 1][1] - seen[0][1]) / dt : 0;
$('mv-ptext').textContent = fmtSize(p.done) + ' из ' + fmtSize(p.total) + ' · файлов: ' + p.files + (rate ? ' · ' + fmtSize(Math.round(rate)) + '/с' : '');
} else if (p.note) {
$('mv-ptext').textContent = p.note === 'подсчёт' ? 'подсчёт…' : p.note + '…';
}
if (j.done) res = j.result;
}
if (text) $('mv-ptext').textContent = text;
});
M.job = null;
if (!res.ok) { busy(false); toast(res.error || 'ошибка', true); return; }
$('mv-bar').style.width = '100%';
@@ -927,6 +939,6 @@
del: (pane, e) => use(pane, del)(e),
uploads(pane) { upPane = pane; openModal($('updlg')); renderUploadList(); },
uploadsChanged(pane) { if (pane === upPane) renderUploadList(); },
sendFile, toast, openModal, join, parent, fmtSize, fmtTime, esc, b64ToBytes
sendFile, followJob, toast, openModal, join, parent, fmtSize, fmtTime, esc, b64ToBytes
};
})();
+5 -1
View File
@@ -664,12 +664,16 @@
if (location.pathname !== '/terminal' && window.App) App.go('/terminal');
return t;
}
const in_ = (el, x, y) => { const r = el.getBoundingClientRect(); return x >= r.left && x <= r.right && y >= r.top && y <= r.bottom; };
window.Workspace = {
paneTypes, desk, touch, open, activate,
hasLiveSessions: () => tabs.some(t => t.panes.some(p => p.live())),
panes: () => tabs.flatMap(t => t.panes),
activePane: () => activePane,
renderPane: p => { const t = tabOf(p); if (t) renderTab(t); if (p === activePane) showStatus(); },
setActivePane, tabOf, renderTab, showStatus, closePane, startDrag
setActivePane, tabOf, renderTab, showStatus, closePane, startDrag,
// paneAt, tabAt: the pane of the shown tab, the tab title at a point.
paneAt: (x, y) => active && active.panes.find(p => in_(p.el, x, y)),
tabAt: (x, y) => document.body.classList.contains('multi') ? tabs.find(t => in_(t.el, x, y)) : null
};
})();
@@ -33,3 +33,9 @@ test('fmtSize', () => {
assert.strictEqual(U.fmtSize(10), '10 B');
assert.strictEqual(U.fmtSize(2048), '2.0 K');
});
test('dropAction: other SSH user on the same host is another place', () => {
const a = {kind: 'ssh', id: 'm1|root@h:22', label: 'root@h'};
const b = {kind: 'ssh', id: 'm1|bob@h:22', label: 'bob@h'};
assert.strictEqual(U.dropAction({source: a, paths: ['/x'], dir: '/'}, {kind: 'term', source: b}).kind, 'deny');
});