v1.5.0
AtheraStudio Code
- (function () {
- "use strict";
-
- var A = window.Athera;
- var util = A.util;
- var fs = A.fs;
-
- var INVALID = /[\\:*?"<>|]/;
- var ROW_H = 26;
- var OVERSCAN = 12;
-
- var rows = [];
- var spacer = null, win = null, message = null;
- var paintQueued = false;
- var focusPath = null;
- var loading = new Set();
- var drag = null;
- var dropPath = null;
- var hover = { path: null, timer: 0 };
- var multi = new Set();
- var anchorPath = null;
- var nodes = new Map();
- var enter = new Map();
- var enterUntil = 0;
-
- var ex = (A.explorer = {
- el: null,
- cache: new Map(),
- expanded: new Set(),
- selected: null,
- busy: null,
-
- init: function (el) {
- ex.el = el;
- el.innerHTML = "";
- spacer = util.el("div", { class: "ath-tree-spacer" });
- win = util.el("div", { class: "ath-tree-window" });
- message = util.el("div", { class: "ath-tree-message", hidden: true });
- spacer.appendChild(win);
- el.appendChild(spacer);
- el.appendChild(message);
- el.addEventListener("scroll", queuePaint, { passive: true });
- window.addEventListener("resize", queuePaint);
- win.addEventListener("animationend", onAnimEnd);
- el.addEventListener("click", onClick);
- el.addEventListener("contextmenu", onContext);
- el.addEventListener("keydown", onKey);
- el.addEventListener("focusin", function (e) {
- var row = e.target.closest && e.target.closest(".ath-tree-row");
- if (row) focusPath = row.getAttribute("data-path");
- });
- el.addEventListener("dragstart", onDragStart);
- el.addEventListener("dragover", onDragOver);
- el.addEventListener("dragleave", onDragLeave);
- el.addEventListener("drop", onDrop);
- el.addEventListener("dragend", endDrag);
- window.addEventListener("dragend", endDrag, true);
- window.addEventListener("drop", function () { setTimeout(endDrag, 0); }, true);
- A.on("workspace", function () {
- ex.cache.clear();
- ex.expanded.clear();
- loading.clear();
- clearNodes();
- setSel(null);
- focusPath = null;
- el.scrollTop = 0;
- ex.render();
- });
- A.on("active-change", function (tab) {
- if (tab && tab.path != null && fs.provider) ex.reveal(tab.path);
- else paint();
- });
- A.on("lang", function () { clearNodes(); if (fs.provider) ex.render(); });
- },
-
- load: async function (dir) {
- if (!ex.cache.has(dir)) {
- var entries = (await fs.provider.list(dir)).filter(function (e) {
- return e.kind === "dir" ? fs.HIDDEN_DIRS.indexOf(e.name) < 0 : fs.HIDDEN_FILES.indexOf(e.name) < 0;
- });
- ex.cache.set(dir, entries);
- }
- return ex.cache.get(dir);
- },
-
- refresh: async function (dir) {
- A.emit("files-changed");
- if (dir == null) ex.cache.clear();
- else ex.cache.delete(dir);
- await ex.render();
- },
-
- render: function () {
- ex.busy = (ex.busy || Promise.resolve()).then(draw, draw);
- return ex.busy;
- },
-
- reveal: async function (path) {
- var dir = util.dirname(path), changed = false;
- while (dir) {
- if (!ex.expanded.has(dir)) { ex.expanded.add(dir); changed = true; }
- dir = util.dirname(dir);
- }
- setSel({ path: path, kind: "file" });
- if (changed) await ex.render();
- ex.scrollTo(path);
- },
-
- scrollTo: function (path) {
- var i = indexOf(path);
- if (i < 0) return paint();
- var el = ex.el, top = i * ROW_H;
- if (top < el.scrollTop) el.scrollTop = top;
- else if (top + ROW_H > el.scrollTop + el.clientHeight) el.scrollTop = top + ROW_H - el.clientHeight;
- paint();
- },
-
- rowOf: function (path) {
- return win ? win.querySelector('.ath-tree-row[data-path="' + CSS.escape(path) + '"]') : null;
- },
-
- markActive: function () {
- paint();
- },
-
- targetDir: function () {
- var s = ex.selected;
- if (!s) return "";
- return s.kind === "dir" ? s.path : util.dirname(s.path);
- },
-
- uniqueName: async function (dir, name) {
- var dot = name.lastIndexOf(".");
- var stem = dot > 0 ? name.slice(0, dot) : name, ext = dot > 0 ? name.slice(dot) : "";
- var candidate = name, i = 2;
- while (await fs.provider.exists(util.join(dir, candidate))) candidate = stem + "-" + i++ + ext;
- return candidate;
- },
-
- newFile: async function (dir) {
- if (!fs.provider) return;
- dir = dir == null ? ex.targetDir() : dir;
- var label = dir ? A.t("explorer.new_file_label_in", { dir: dir }) : A.t("explorer.new_file_label");
- var name = await A.ui.prompt(A.t("explorer.new_file"), label, "", A.t("common.create"));
- if (!name) return;
- if (INVALID.test(name)) return A.ui.toast(A.t("explorer.invalid_name"), "danger");
- var path = util.resolve(dir, name);
- if (!path) return;
- try {
- await fs.provider.createFile(path, A.templates.forFile(path));
- } catch (e) {
- return A.ui.toast(e.message, "danger");
- }
- await afterCreate(path);
- A.tabs.openPath(path);
- },
-
- newFolder: async function (dir) {
- if (!fs.provider) return;
- dir = dir == null ? ex.targetDir() : dir;
- var label = dir ? A.t("explorer.new_folder_label_in", { dir: dir }) : A.t("explorer.new_folder_label");
- var name = await A.ui.prompt(A.t("explorer.new_folder"), label, "", A.t("common.create"));
- if (!name) return;
- if (INVALID.test(name)) return A.ui.toast(A.t("explorer.invalid_name"), "danger");
- var path = util.resolve(dir, name);
- if (!path) return;
- try {
- await fs.provider.createDir(path);
- } catch (e) {
- return A.ui.toast(e.message, "danger");
- }
- ex.expanded.add(path);
- await afterCreate(path, "dir");
- },
-
- rename: async function (path, kind) {
- var name = await A.ui.prompt(A.t("explorer.rename_title"), A.t("explorer.rename_label"), util.basename(path), A.t("common.rename"));
- if (!name || name === util.basename(path)) return;
- if (INVALID.test(name) || name.indexOf("/") >= 0 || name === "." || name === "..") {
- return A.ui.toast(A.t("explorer.invalid_name"), "danger");
- }
- try {
- var target = await fs.provider.rename(path, name, kind);
- A.tabs.renamePath(path, target);
- if (kind === "dir") remapExpanded(path, target);
- setSel({ path: target, kind: kind });
- focusPath = target;
- await refreshAround([util.dirname(path)], kind === "dir" ? [path] : []);
- ex.scrollTo(target);
- } catch (e) {
- A.ui.toast(A.t("explorer.rename_failed", { error: e.message }), "danger");
- }
- },
-
- remove: async function (path, kind) {
- var msg = A.t(kind === "dir" ? "explorer.delete_folder_confirm" : "explorer.delete_file_confirm", { path: path });
- if (!(await A.ui.confirm(A.t("common.delete"), msg, A.t("common.delete"), true))) return;
- try {
- await fs.provider.remove(path);
- A.tabs.removePath(path);
- Array.from(ex.expanded).forEach(function (p) {
- if (p === path || p.indexOf(path + "/") === 0) ex.expanded.delete(p);
- });
- setSel(null);
- await refreshAround([util.dirname(path)], kind === "dir" ? [path] : []);
- } catch (e) {
- A.ui.toast(A.t("explorer.delete_failed", { error: e.message }), "danger");
- }
- },
-
- move: async function (path, kind, destDir) {
- try {
- var target = await fs.provider.move(path, destDir, kind);
- A.tabs.renamePath(path, target);
- if (kind === "dir") remapExpanded(path, target);
- var dir = destDir;
- while (dir) { ex.expanded.add(dir); dir = util.dirname(dir); }
- setSel({ path: target, kind: kind });
- focusPath = target;
- await refreshAround([util.dirname(path), destDir], kind === "dir" ? [path] : []);
- ex.scrollTo(target);
- A.ui.toast(A.t("explorer.moved", { name: util.basename(path), dir: destDir || fs.provider.name }));
- } catch (e) {
- A.ui.toast(A.t("explorer.move_failed", { error: e.message }), "danger");
- }
- },
-
- removeMany: async function (items) {
- items = prune(items);
- if (items.length < 2) return items[0] ? ex.remove(items[0].path, items[0].kind) : undefined;
- if (!(await A.ui.confirm(A.t("common.delete"), A.tn("explorer.delete_many_confirm", items.length), A.t("common.delete"), true))) return;
- var errors = [], dirs = [], goneDirs = [];
- for (var it of items) {
- try {
- await fs.provider.remove(it.path);
- A.tabs.removePath(it.path);
- Array.from(ex.expanded).forEach(function (p) {
- if (p === it.path || p.indexOf(it.path + "/") === 0) ex.expanded.delete(p);
- });
- dirs.push(util.dirname(it.path));
- if (it.kind === "dir") goneDirs.push(it.path);
- } catch (e) { errors.push(e.message); }
- }
- setSel(null);
- await refreshAround(dirs, goneDirs);
- if (errors.length) A.ui.toast(A.t("explorer.delete_failed", { error: errors[0] }), "danger");
- },
-
- copyIn: async function (handles, destDir) {
- var items = [], emptyDirs = [], topDirs = [], skipped = false;
- for (var h of handles) {
- if (h.kind === "directory") {
- var target = util.join(destDir, h.name);
- if (await isInsideWorkspace(h, destDir, target)) { skipped = true; continue; }
- await collectDir(h, target, items, emptyDirs);
- topDirs.push(target);
- } else {
- var f = h._file || (await h.getFile());
- items.push({ path: util.join(destDir, f.name), file: f });
- }
- }
- if (skipped) A.ui.toast(A.t("explorer.copy_into_self"), "warning");
- if (!items.length && !emptyDirs.length) return;
- var dirName = destDir || fs.provider.name;
- if (!(await yesNo(A.t("explorer.copy_title", { dir: dirName }), A.t("explorer.copy_confirm")))) return;
- var clash = new Set();
- for (var it of items) {
- if (await fs.provider.exists(it.path)) clash.add(it.path);
- }
- var replace = false;
- if (clash.size) replace = await yesNo(A.t("explorer.replace_title"), A.t("explorer.replace_confirm"));
- var done = [], errors = [];
- for (var item of items) {
- var exists = clash.has(item.path);
- if (exists && !replace) continue;
- try {
- await fs.provider.write(item.path, item.file || (await item.handle.getFile()));
- done.push(item.path);
- if (exists && A.tabs.byPath(item.path)) {
- A.tabs.removePath(item.path);
- await A.tabs.openPath(item.path);
- }
- } catch (e) { errors.push(e.message); }
- }
- for (var d of emptyDirs) {
- try {
- if (!(await fs.provider.exists(d))) await fs.provider.createDir(d);
- } catch (e) { errors.push(e.message); }
- }
- var dir = destDir;
- while (dir) { ex.expanded.add(dir); dir = util.dirname(dir); }
- topDirs.forEach(function (p) { ex.expanded.add(p); });
- await refreshAround([destDir], topDirs);
- var first = topDirs[0] || done[0];
- if (first) ex.scrollTo(first);
- if (done.length) A.ui.toast(A.tn("explorer.copied_many", done.length, { dir: dirName }), "success");
- if (errors.length) A.ui.toast(A.t("explorer.copy_failed", { error: errors[0] }), "danger");
- },
-
- moveMany: async function (items, destDir) {
- items = prune(items).filter(function (it) { return canDrop(it, destDir); });
- if (!items.length) return;
- if (items.length === 1) return ex.move(items[0].path, items[0].kind, destDir);
- var targets = [], srcDirs = [destDir], goneDirs = [], errors = [];
- for (var it of items) {
- try {
- var target = await fs.provider.move(it.path, destDir, it.kind);
- A.tabs.renamePath(it.path, target);
- if (it.kind === "dir") { remapExpanded(it.path, target); goneDirs.push(it.path); }
- srcDirs.push(util.dirname(it.path));
- targets.push({ path: target, kind: it.kind });
- } catch (e) { errors.push(e.message); }
- }
- var dir = destDir;
- while (dir) { ex.expanded.add(dir); dir = util.dirname(dir); }
- if (targets.length) {
- setSel(targets[0]);
- targets.forEach(function (t) { multi.add(t.path); });
- focusPath = targets[0].path;
- }
- await refreshAround(srcDirs, goneDirs);
- if (targets.length) {
- ex.scrollTo(targets[0].path);
- A.ui.toast(A.tn("explorer.moved_many", targets.length, { dir: destDir || fs.provider.name }));
- }
- if (errors.length) A.ui.toast(A.t("explorer.move_failed", { error: errors[0] }), "danger");
- },
-
- dragging: function () {
- return drag ? dragItems(drag) : null;
- },
-
- collapseAll: function () {
- ex.expanded.clear();
- ex.el.scrollTop = 0;
- ex.render();
- }
- });
-
- async function collectDir(handle, target, items, emptyDirs) {
- var count = 0;
- for await (var e of handle.values()) {
- if (e.kind === "directory") {
- if (fs.HIDDEN_DIRS.indexOf(e.name) >= 0) continue;
- count++;
- await collectDir(e, util.join(target, e.name), items, emptyDirs);
- } else {
- if (fs.HIDDEN_FILES.indexOf(e.name) >= 0) continue;
- count++;
- items.push({ path: util.join(target, e.name), handle: e });
- }
- }
- if (!count) emptyDirs.push(target);
- }
-
- async function isInsideWorkspace(handle, destDir, target) {
- var root = fs.provider && fs.provider.root;
- if (!root || typeof root.resolve !== "function") return false;
- var rel = null;
- try { rel = await root.resolve(handle); } catch (e) { return false; }
- if (!rel) return false;
- var src = rel.join("/");
- if (src === "") return true;
- return target === src || destDir === src || destDir.indexOf(src + "/") === 0;
- }
-
- function remapExpanded(path, target) {
- Array.from(ex.expanded).forEach(function (p) {
- if (p === path || p.indexOf(path + "/") === 0) {
- ex.expanded.delete(p);
- ex.expanded.add(target + p.slice(path.length));
- }
- });
- }
-
- async function refreshAround(dirs, removedDirs) {
- A.emit("files-changed");
- dirs.forEach(function (d) { ex.cache.delete(d); });
- removedDirs.forEach(function (gone) {
- Array.from(ex.cache.keys()).forEach(function (k) {
- if (k === gone || k.indexOf(gone + "/") === 0) ex.cache.delete(k);
- });
- });
- await ex.render();
- }
-
- async function afterCreate(path, kind) {
- var dir = util.dirname(path), dirs = [dir];
- while (dir) {
- ex.expanded.add(dir);
- dir = util.dirname(dir);
- dirs.push(dir);
- }
- setSel({ path: path, kind: kind || "file" });
- focusPath = path;
- await refreshAround(dirs, []);
- ex.scrollTo(path);
- }
-
- function visibleExpanded() {
- var out = [];
- function walk(dir) {
- var entries = ex.cache.get(dir);
- if (!entries) return;
- for (var e of entries) {
- if (e.kind !== "dir") continue;
- var p = util.join(dir, e.name);
- if (!ex.expanded.has(p)) continue;
- if (ex.cache.has(p)) walk(p);
- else out.push(p);
- }
- }
- if (!ex.cache.has("")) out.push("");
- else walk("");
- return out;
- }
-
- function flatten() {
- var out = [];
- function walk(dir, depth) {
- var entries = ex.cache.get(dir);
- if (!entries) {
- if (loading.has(dir)) out.push({ path: dir + "/\u0000loading", name: "", kind: "loading", depth: depth });
- return;
- }
- for (var e of entries) {
- var p = util.join(dir, e.name);
- var open = e.kind === "dir" && ex.expanded.has(p);
- out.push({ path: p, name: e.name, kind: e.kind, depth: depth, open: open });
- if (open) walk(p, depth + 1);
- }
- }
- walk("", 0);
- return out;
- }
-
- async function draw() {
- if (!fs.provider) {
- setRows([]);
- showMessage("");
- paint();
- return;
- }
- var provider = fs.provider;
- for (var round = 0; round < 50; round++) {
- var missing = visibleExpanded();
- if (!missing.length) break;
- missing.forEach(function (d) { loading.add(d); });
- setRows(flatten());
- paint();
- var results = await Promise.all(missing.map(function (d) {
- return ex.load(d).then(function () { return null; }, function (e) { return { dir: d, error: e }; });
- }));
- if (provider !== fs.provider) return;
- missing.forEach(function (d) { loading.delete(d); });
- var rootError = null;
- results.forEach(function (r) {
- if (!r) return;
- if (r.dir === "") rootError = r.error;
- else ex.expanded.delete(r.dir);
- });
- if (rootError) {
- setRows([]);
- showMessage(A.t("explorer.read_error", { error: rootError.message }), true);
- paint();
- return;
- }
- }
- setRows(flatten());
- showMessage(rows.length ? "" : A.t("explorer.empty"));
- paint();
- }
-
- function showMessage(text, isError) {
- message.hidden = !text;
- message.textContent = text;
- message.className = "ath-tree-message" + (isError ? " is-error" : "");
- }
-
- function indexOf(path) {
- for (var i = 0; i < rows.length; i++) if (rows[i].path === path) return i;
- return -1;
- }
-
- function queuePaint() {
- if (paintQueued) return;
- paintQueued = true;
- requestAnimationFrame(function () {
- paintQueued = false;
- paint();
- });
- }
-
- function onAnimEnd(e) {
- var t = e.target;
- if (e.animationName === "ath-row-in") {
- t.classList.remove("is-entering");
- t.style.removeProperty("--i");
- } else if (e.animationName === "ath-ficon-pop") {
- t.classList.remove("is-toggled");
- }
- }
-
- function setRows(next) {
- var prev = new Set(rows.map(function (r) { return r.path; }));
- var fresh = new Map(), n = 0;
- next.forEach(function (r) {
- if (!prev.has(r.path)) fresh.set(r.path, Math.min(n++, 14));
- });
- enter = fresh;
- enterUntil = performance.now() + 600;
- rows = next;
- }
-
- function clearNodes() {
- nodes.forEach(function (n) { n.remove(); });
- nodes.clear();
- enter = new Map();
- }
-
- function buildRow(r) {
- var node = document.createElement("div");
- node.className = "ath-tree-row";
- node.setAttribute("role", "treeitem");
- node.setAttribute("tabindex", "-1");
- node.setAttribute("draggable", "true");
- node.setAttribute("data-path", r.path);
- node.setAttribute("data-kind", r.kind);
- node.style.setProperty("--depth", r.depth);
- if (r.kind === "loading") {
- node.classList.add("is-loading");
- node.innerHTML = '<span class="ath-tree-chevron"></span><span class="ath-spinner ath-spinner-sm"></span>' +
- '<span class="ath-tree-name">' + util.esc(A.t("palette.loading").replace(/…$/, "")) + "…</span>";
- return node;
- }
- node.title = r.path;
- if (r.kind === "dir") {
- node.classList.add("is-dir");
- node.innerHTML = '<span class="ath-tree-chevron">' + A.icon("chevron") + '</span><span class="ath-ficon ath-ficon-dir">' +
- A.icon(r.open ? "folderOpen" : "folder") + "</span>" +
- '<span class="ath-tree-name">' + util.esc(r.name) + "</span>";
- node.setAttribute("aria-expanded", String(!!r.open));
- node._open = !!r.open;
- } else {
- node.innerHTML = '<span class="ath-tree-chevron"></span>' + A.fileBadge(r.name) +
- '<span class="ath-tree-name">' + util.esc(r.name) + "</span>";
- }
- return node;
- }
-
- function syncRow(node, r, active) {
- if (r.kind === "loading") return;
- var c = node.classList;
- var dragging = !!drag && dragItems(drag).some(function (it) { return it.path === r.path; });
- var sel = multi.has(r.path);
- c.toggle("is-open", !!r.open);
- c.toggle("is-active", r.path === active);
- c.toggle("is-selected", sel);
- c.toggle("is-multi", multi.size > 1 && sel);
- c.toggle("is-dragging", dragging);
- c.toggle("is-drop-target", dropPath === r.path);
- if (r.kind === "dir" && node._open !== !!r.open) {
- node._open = !!r.open;
- node.setAttribute("aria-expanded", String(!!r.open));
- var icon = node.querySelector(".ath-ficon-dir");
- if (icon) {
- icon.innerHTML = A.icon(r.open ? "folderOpen" : "folder");
- icon.classList.remove("is-toggled");
- void icon.offsetWidth;
- icon.classList.add("is-toggled");
- }
- }
- }
-
- function paint() {
- if (!win) return;
- var el = ex.el;
- spacer.style.height = rows.length * ROW_H + "px";
- var height = el.clientHeight || 600;
- var start = Math.max(0, Math.floor(el.scrollTop / ROW_H) - OVERSCAN);
- var end = Math.min(rows.length, Math.ceil((el.scrollTop + height) / ROW_H) + OVERSCAN);
- var hadFocus = el.contains(document.activeElement) && document.activeElement !== el;
- var active = A.tabs.active && A.tabs.active.path;
- var animate = performance.now() < enterUntil;
- var seen = new Set(), prevNode = null;
- for (var i = start; i < end; i++) {
- var r = rows[i];
- var node = nodes.get(r.path);
- if (node && node.getAttribute("data-kind") !== r.kind) {
- node.remove();
- nodes.delete(r.path);
- node = null;
- }
- if (!node) {
- node = buildRow(r);
- nodes.set(r.path, node);
- if (animate && enter.has(r.path)) {
- node.style.setProperty("--i", enter.get(r.path));
- node.classList.add("is-entering");
- enter.delete(r.path);
- }
- }
- syncRow(node, r, active);
- seen.add(r.path);
- var ref = prevNode ? prevNode.nextSibling : win.firstChild;
- if (node !== ref) win.insertBefore(node, ref);
- prevNode = node;
- }
- nodes.forEach(function (n, path) {
- if (!seen.has(path)) {
- n.remove();
- nodes.delete(path);
- }
- });
- win.style.transform = "translateY(" + start * ROW_H + "px)";
- if (hadFocus && focusPath) {
- var row = ex.rowOf(focusPath);
- if (row) row.focus({ preventScroll: true });
- else el.focus({ preventScroll: true });
- }
- }
-
- function prune(items) {
- return items.filter(function (it) {
- return !items.some(function (o) { return o !== it && o.kind === "dir" && it.path.indexOf(o.path + "/") === 0; });
- });
- }
-
- function selectedItems() {
- return rows.filter(function (r) { return r.kind !== "loading" && multi.has(r.path); })
- .map(function (r) { return { path: r.path, kind: r.kind }; });
- }
-
- function dragItems(d) { return d.items || [d]; }
-
- function canDropAny(d, dest) {
- return dragItems(d).some(function (it) { return canDrop(it, dest); });
- }
-
- function setSel(d) {
- ex.selected = d || null;
- multi = new Set(d ? [d.path] : []);
- anchorPath = d ? d.path : null;
- }
-
- function selectRange(d, additive) {
- var b = indexOf(d.path);
- var a = anchorPath != null ? indexOf(anchorPath) : -1;
- if (a < 0) { anchorPath = d.path; a = b; }
- if (b < 0) return;
- if (!additive) multi = new Set();
- for (var i = Math.min(a, b); i <= Math.max(a, b); i++) {
- if (rows[i].kind !== "loading") multi.add(rows[i].path);
- }
- ex.selected = d;
- focusPath = d.path;
- paint();
- }
-
- function toggleSel(d) {
- if (multi.has(d.path)) {
- multi.delete(d.path);
- if (ex.selected && ex.selected.path === d.path) {
- var left = Array.from(multi).pop();
- ex.selected = left ? { path: left, kind: (rows[indexOf(left)] || {}).kind || "file" } : null;
- }
- } else {
- multi.add(d.path);
- ex.selected = d;
- }
- anchorPath = d.path;
- focusPath = d.path;
- paint();
- }
-
- function selectAll() {
- multi = new Set();
- rows.forEach(function (r) { if (r.kind !== "loading") multi.add(r.path); });
- if (!ex.selected || !multi.has(ex.selected.path)) {
- var i = focusPath ? indexOf(focusPath) : -1;
- var d = rowAt(i >= 0 ? i : 0);
- if (d) ex.selected = d;
- }
- if (anchorPath == null) anchorPath = ex.selected ? ex.selected.path : null;
- paint();
- }
-
- function rowData(row) {
- return { path: row.getAttribute("data-path"), kind: row.getAttribute("data-kind") };
- }
-
- function rowAt(i) {
- return rows[i] ? { path: rows[i].path, kind: rows[i].kind } : null;
- }
-
- async function activate(d) {
- if (!d || d.kind === "loading") return;
- setSel(d);
- focusPath = d.path;
- if (d.kind === "dir") {
- if (ex.expanded.has(d.path)) ex.expanded.delete(d.path);
- else ex.expanded.add(d.path);
- await ex.render();
- } else {
- paint();
- await A.tabs.openPath(d.path, { focusEditor: false });
- }
- }
-
- function onClick(e) {
- var row = e.target.closest(".ath-tree-row");
- if (!row) {
- setSel(null);
- paint();
- return;
- }
- row.focus({ preventScroll: true });
- var d = rowData(row);
- if (d.kind === "loading") return;
- var mod = e.ctrlKey || e.metaKey;
- if (e.shiftKey) return selectRange(d, mod);
- if (mod) return toggleSel(d);
- activate(d);
- }
-
- function onContext(e) {
- if (!fs.provider) return;
- e.preventDefault();
- var row = e.target.closest(".ath-tree-row:not(.is-loading)");
- var d = row ? rowData(row) : { path: "", kind: "dir" };
- var inMulti = !!row && multi.size > 1 && multi.has(d.path);
- if (inMulti) {
- focusPath = d.path;
- A.ui.contextMenu(e.clientX, e.clientY, [
- { label: A.t("common.delete") + " (" + multi.size + ")", icon: "trash", kbd: "Del", danger: true, action: function () { ex.removeMany(selectedItems()); } },
- "-",
- { label: A.t("explorer.refresh"), icon: "refresh", action: function () { ex.refresh(null); } }
- ]);
- return;
- }
- if (row) { setSel(d); focusPath = d.path; paint(); }
- var dir = d.kind === "dir" ? d.path : util.dirname(d.path);
- var items = [
- { label: A.t("explorer.new_file"), icon: "filePlus", kbd: "Alt+N", action: function () { ex.newFile(dir); } },
- { label: A.t("explorer.new_folder"), icon: "folderPlus", action: function () { ex.newFolder(dir); } }
- ];
- if (row) {
- items.push("-",
- { label: A.t("explorer.rename_title"), icon: "pencil", kbd: "F2", action: function () { ex.rename(d.path, d.kind); } },
- { label: A.t("explorer.copy_path"), icon: "copy", action: function () { copy(d.path); } },
- "-",
- { label: A.t("common.delete"), icon: "trash", kbd: "Del", danger: true, action: function () { ex.remove(d.path, d.kind); } });
- }
- items.push("-", { label: A.t("explorer.refresh"), icon: "refresh", action: function () { ex.refresh(null); } });
- A.ui.contextMenu(e.clientX, e.clientY, items);
- }
-
- function copy(text) {
- A.ui.copyText(text).then(function () { A.ui.toast(A.t("explorer.copied", { path: text })); }, function () { });
- }
-
- function hasFiles(e) {
- return !!e.dataTransfer && Array.from(e.dataTransfer.types).indexOf("Files") >= 0;
- }
-
- function externalDrop(e) {
- return !drag && hasFiles(e) && !!fs.provider && fs.provider.kind === "disk";
- }
-
- function yesNo(title, message) {
- return A.ui.dialog({
- title: title,
- body: util.el("p", { class: "ath-dialog-text" }, message),
- size: "sm",
- actions: [
- { label: A.t("common.no"), value: false, kind: "ghost" },
- { label: A.t("common.yes"), value: true, kind: "primary" }
- ]
- }).then(function (r) { return r === true; });
- }
-
- function dropDir(e) {
- var row = e.target.closest && e.target.closest(".ath-tree-row:not(.is-loading)");
- if (!row) return "";
- var d = rowData(row);
- return d.kind === "dir" ? d.path : util.dirname(d.path);
- }
-
- function canDrop(item, dest) {
- if (!item || !fs.provider) return false;
- if (util.dirname(item.path) === dest) return false;
- if (item.kind === "dir" && (dest === item.path || dest.indexOf(item.path + "/") === 0)) return false;
- return true;
- }
-
- function markDrop(dest) {
- var next = dest || null;
- ex.el.classList.toggle("is-drop-root", dest === "");
- if (next === dropPath) return;
- dropPath = next;
- win.querySelectorAll(".is-drop-target").forEach(function (r) { r.classList.remove("is-drop-target"); });
- if (dropPath) {
- var row = ex.rowOf(dropPath);
- if (row) row.classList.add("is-drop-target");
- }
- }
-
- function clearDrop() {
- clearTimeout(hover.timer);
- hover.path = null;
- dropPath = null;
- if (!ex.el) return;
- ex.el.classList.remove("is-drop-root");
- win.querySelectorAll(".is-drop-target").forEach(function (r) { r.classList.remove("is-drop-target"); });
- }
-
- function endDrag() {
- if (!drag) return;
- drag = null;
- clearDrop();
- win.querySelectorAll(".is-dragging").forEach(function (r) { r.classList.remove("is-dragging"); });
- }
-
- function hostPaths(items) {
- var desktop = window.atheraDesktop;
- var base = fs.provider && fs.provider.root && fs.provider.root._path;
- if (!desktop || !desktop.isDesktop || typeof desktop.startDrag !== "function" || !base) return [];
- var sep = base.indexOf("\\") >= 0 && base.indexOf("/") < 0 ? "\\" : "/";
- return items.filter(function (it) { return it.kind === "file"; }).map(function (it) {
- return base.replace(/[\\/]+$/, "") + sep + it.path.split("/").join(sep);
- });
- }
-
- function onDragStart(e) {
- var row = e.target.closest(".ath-tree-row:not(.is-loading)");
- if (!row || !fs.provider) return;
- A.ui.closeContextMenu();
- var started = rowData(row);
- if (multi.size > 1 && multi.has(started.path)) started.items = selectedItems();
- if (e.altKey) {
- var paths = hostPaths(dragItems(started));
- if (paths.length) {
- e.preventDefault();
- window.atheraDesktop.startDrag(paths);
- return;
- }
- }
- drag = started;
- e.dataTransfer.effectAllowed = "move";
- e.dataTransfer.setData("text/plain", drag.path);
- requestAnimationFrame(function () {
- dragItems(started).forEach(function (it) {
- var r = ex.rowOf(it.path);
- if (r) r.classList.add("is-dragging");
- });
- });
- }
-
- function onDragOver(e) {
- if (externalDrop(e)) {
- e.preventDefault();
- e.dataTransfer.dropEffect = "copy";
- markDrop(dropDir(e));
- return;
- }
- if (!drag) return;
- var dest = dropDir(e);
- if (!canDropAny(drag, dest)) {
- clearDrop();
- e.dataTransfer.dropEffect = "none";
- return;
- }
- e.preventDefault();
- e.dataTransfer.dropEffect = "move";
- markDrop(dest);
- var row = e.target.closest(".ath-tree-row");
- var d = row && rowData(row);
- if (d && d.kind === "dir" && !ex.expanded.has(d.path)) {
- if (hover.path !== d.path) {
- clearTimeout(hover.timer);
- hover.path = d.path;
- hover.timer = setTimeout(function () {
- if (!drag || hover.path !== d.path) return;
- ex.expanded.add(d.path);
- ex.render();
- }, 650);
- }
- } else {
- clearTimeout(hover.timer);
- hover.path = null;
- }
- }
-
- function onDragLeave(e) {
- if (!ex.el.contains(e.relatedTarget)) clearDrop();
- }
-
- function onDrop(e) {
- if (externalDrop(e)) {
- e.preventDefault();
- e.athHandled = true;
- var target = dropDir(e);
- clearDrop();
- var jobs = Array.from(e.dataTransfer.items).filter(function (it) { return it.kind === "file"; }).map(function (it) {
- if (it.getAsFileSystemHandle) return it.getAsFileSystemHandle();
- var f = it.getAsFile();
- return Promise.resolve(f ? { kind: "file", _file: f } : null);
- });
- Promise.all(jobs).then(function (handles) {
- return ex.copyIn(handles.filter(Boolean), target);
- }).catch(function (err) {
- A.ui.toast(A.t("explorer.copy_failed", { error: err.message }), "danger");
- });
- return;
- }
- if (!drag) return;
- e.preventDefault();
- e.stopPropagation();
- var d = drag, dest = dropDir(e);
- endDrag();
- if (d.items) ex.moveMany(d.items, dest);
- else if (canDrop(d, dest)) ex.move(d.path, d.kind, dest);
- }
-
- function focusIndex(i) {
- var d = rowAt(i);
- if (!d || d.kind === "loading") return;
- focusPath = d.path;
- ex.scrollTo(d.path);
- var row = ex.rowOf(d.path);
- if (row) row.focus({ preventScroll: true });
- }
-
- function onKey(e) {
- if (!rows.length) return;
- if ((e.ctrlKey || e.metaKey) && !e.altKey && !e.shiftKey && e.key.toLowerCase() === "a") {
- e.preventDefault();
- selectAll();
- return;
- }
- if (e.key === "Escape" && multi.size > 1) {
- e.preventDefault();
- setSel(ex.selected);
- paint();
- return;
- }
- var i = focusPath ? indexOf(focusPath) : -1;
- if (i < 0) {
- if (e.key === "ArrowDown" || e.key === "Home") { e.preventDefault(); focusIndex(0); }
- return;
- }
- var d = rowAt(i);
- var page = Math.max(1, Math.floor(ex.el.clientHeight / ROW_H) - 1);
-
- function go(t) {
- if (e.shiftKey) {
- if (anchorPath == null || indexOf(anchorPath) < 0) anchorPath = d.path;
- focusIndex(t);
- selectRange(rowAt(t), false);
- } else focusIndex(t);
- }
- switch (e.key) {
- case "ArrowDown": e.preventDefault(); go(Math.min(rows.length - 1, i + 1)); break;
- case "ArrowUp": e.preventDefault(); go(Math.max(0, i - 1)); break;
- case "PageDown": e.preventDefault(); go(Math.min(rows.length - 1, i + page)); break;
- case "PageUp": e.preventDefault(); go(Math.max(0, i - page)); break;
- case "Home": e.preventDefault(); go(0); break;
- case "End": e.preventDefault(); go(rows.length - 1); break;
- case "ArrowRight":
- if (d.kind === "dir" && !ex.expanded.has(d.path)) { e.preventDefault(); activate(d); }
- break;
- case "ArrowLeft":
- e.preventDefault();
- if (d.kind === "dir" && ex.expanded.has(d.path)) activate(d);
- else if (util.dirname(d.path)) focusIndex(indexOf(util.dirname(d.path)));
- break;
- case "Enter": e.preventDefault(); activate(d); break;
- case "F2": e.preventDefault(); ex.rename(d.path, d.kind); break;
- case "Delete":
- e.preventDefault();
- if (multi.size > 1) ex.removeMany(selectedItems());
- else ex.remove(d.path, d.kind);
- break;
- }
- }
- })();
-