547 lines
30 KiB
HTML
547 lines
30 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>Context files</title>
|
|
<style>
|
|
html, body { margin: 0; height: 100%; overflow: hidden; }
|
|
* { box-sizing: border-box; }
|
|
svg.i { width: 15px; height: 15px; flex: none; fill: none; stroke: currentColor; stroke-width: 2;
|
|
stroke-linecap: round; stroke-linejoin: round; }
|
|
svg.s { width: 13px; height: 13px; }
|
|
.app { display: flex; height: 100vh; min-height: 0; }
|
|
.side { width: 320px; flex: none; display: flex; flex-direction: column; min-height: 0;
|
|
border-right: 1px solid hsl(var(--border)); }
|
|
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
|
|
@media (max-width: 760px) {
|
|
.side { width: 100%; border-right: 0; }
|
|
.app.picked .side { display: none; }
|
|
.app:not(.picked) .main { display: none; }
|
|
}
|
|
@media (min-width: 761px) { .only-phone { display: none !important; } }
|
|
|
|
/* the tree */
|
|
.search { padding: 8px; border-bottom: 1px solid hsl(var(--border)); }
|
|
.search label { display: flex; align-items: center; gap: 6px; border-radius: 6px; background: hsl(var(--muted)); padding: 0 8px;
|
|
color: hsl(var(--muted-foreground)); }
|
|
.search input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 6px 0; font: inherit; font-size: 13px;
|
|
color: hsl(var(--foreground)); outline: none; }
|
|
.search button { border: 0; background: none; padding: 0; color: inherit; cursor: pointer; display: flex; }
|
|
.total { display: flex; align-items: center; gap: 6px; padding: 7px 12px; font-size: 12px;
|
|
border-bottom: 1px solid hsl(var(--border)); color: hsl(var(--muted-foreground)); }
|
|
.total b { margin-left: auto; font: 600 12px ui-monospace, SFMono-Regular, Menlo, monospace; color: hsl(var(--foreground)); }
|
|
.total svg { color: hsl(var(--primary)); }
|
|
.tree { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 0; }
|
|
.ti { display: flex; align-items: center; gap: 6px; width: 100%; border: 0; background: none; padding: 5px 8px 5px 0;
|
|
font: inherit; font-size: 13px; text-align: left; color: hsl(var(--foreground)); cursor: pointer; min-width: 0; }
|
|
.ti:hover { background: hsl(var(--accent) / .6); }
|
|
.ti.sel { background: hsl(var(--accent)); color: hsl(var(--accent-foreground)); }
|
|
.ti .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
|
|
.ti.dir .nm { font-weight: 500; }
|
|
.ti .chev { color: hsl(var(--muted-foreground)); }
|
|
.ti .ico { color: hsl(var(--muted-foreground)); }
|
|
.ti .ico.prim { color: hsl(var(--primary)); }
|
|
.ti .sp { margin-left: auto; }
|
|
.dot { width: 6px; height: 6px; border-radius: 999px; background: hsl(var(--primary)); flex: none; }
|
|
.tok { flex: none; border-radius: 999px; background: hsl(var(--muted)); padding: 2px 6px; margin-right: 4px;
|
|
font: 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace; color: hsl(var(--muted-foreground)); font-variant-numeric: tabular-nums; }
|
|
.none { padding: 24px 12px; text-align: center; font-size: 12px; color: hsl(var(--muted-foreground)); }
|
|
|
|
/* the editor */
|
|
.bar { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-bottom: 1px solid hsl(var(--border)); min-height: 45px; }
|
|
.bar .who { min-width: 0; flex: 1; }
|
|
.bar .nm { font-size: 13.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
.bar .pt { font: 11px ui-monospace, SFMono-Regular, Menlo, monospace; color: hsl(var(--muted-foreground));
|
|
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
.ib { display: inline-flex; align-items: center; justify-content: center; gap: 4px; height: 30px; min-width: 30px; padding: 0 8px;
|
|
border: 1px solid hsl(var(--border)); border-radius: 6px; background: none; color: hsl(var(--foreground));
|
|
font: inherit; font-size: 12px; cursor: pointer; flex: none; }
|
|
.ib:hover:not(:disabled) { background: hsl(var(--accent)); }
|
|
.ib:disabled { opacity: .45; cursor: default; }
|
|
.ib.primary { border-color: transparent; background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
|
|
.ib.primary:hover:not(:disabled) { background: hsl(var(--primary) / .9); }
|
|
.ib.ghost { border-color: transparent; }
|
|
.ib.on { background: hsl(var(--accent)); }
|
|
@media (max-width: 520px) { .ib .lb { display: none; } }
|
|
.ro { flex: none; border-radius: 6px; padding: 2px 6px; font-size: 10.5px; font-weight: 600;
|
|
color: rgb(245 158 11); background: rgb(245 158 11 / .12); }
|
|
.body { flex: 1; min-height: 0; position: relative; }
|
|
textarea { position: absolute; inset: 0; width: 100%; height: 100%; resize: none; border: 0; outline: none; margin: 0;
|
|
padding: 12px 14px; background: transparent; color: hsl(var(--foreground)); tab-size: 2;
|
|
font: 12.5px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
textarea[readonly] { color: hsl(var(--foreground) / .85); }
|
|
textarea::placeholder { color: hsl(var(--muted-foreground)); }
|
|
.preview { position: absolute; inset: 0; overflow: auto; padding: 12px 16px; }
|
|
.status { display: flex; align-items: center; gap: 12px; padding: 5px 12px; border-top: 1px solid hsl(var(--border));
|
|
font: 11px ui-monospace, SFMono-Regular, Menlo, monospace; color: hsl(var(--muted-foreground)); }
|
|
.status .st { margin-left: auto; font-family: inherit; }
|
|
.status .err { color: hsl(var(--destructive)); }
|
|
.empty { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; text-align: center;
|
|
font-size: 13px; color: hsl(var(--muted-foreground)); }
|
|
|
|
/* markdown preview */
|
|
.md { font-size: 14px; line-height: 1.6; max-width: 860px; overflow-wrap: anywhere; }
|
|
.md > :first-child { margin-top: 0; }
|
|
.md h1, .md h2, .md h3, .md h4 { line-height: 1.3; margin: 1.2em 0 .5em; }
|
|
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.22em; } .md h3 { font-size: 1.08em; } .md h4 { font-size: 1em; }
|
|
.md p { margin: .6em 0; }
|
|
.md ul, .md ol { margin: .5em 0; padding-left: 1.4em; }
|
|
.md li.task { list-style: none; margin-left: -1.2em; }
|
|
.md code { font: .88em ui-monospace, SFMono-Regular, Menlo, monospace; background: hsl(var(--muted)); border-radius: 4px; padding: 1px 4px; }
|
|
.md pre { background: hsl(var(--muted) / .6); border-radius: 8px; padding: 10px 12px; overflow-x: auto; }
|
|
.md pre code { background: none; padding: 0; font-size: 12.5px; }
|
|
.md blockquote { margin: .6em 0; border-left: 3px solid hsl(var(--border)); padding-left: 10px; color: hsl(var(--muted-foreground)); }
|
|
.md hr { border: 0; border-top: 1px solid hsl(var(--border)); margin: 1.2em 0; }
|
|
.md a { color: hsl(var(--primary)); }
|
|
.md .scroller { overflow-x: auto; margin: .6em 0; }
|
|
.md table { border-collapse: collapse; font-size: 13px; }
|
|
.md th, .md td { border: 1px solid hsl(var(--border)); padding: 4px 8px; text-align: left; vertical-align: top; }
|
|
.md th { background: hsl(var(--muted) / .5); white-space: nowrap; }
|
|
.md .fm { font: 11.5px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; color: hsl(var(--muted-foreground));
|
|
border: 1px dashed hsl(var(--border)); border-radius: 8px; padding: 6px 10px; white-space: pre-wrap; margin-bottom: 1em; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="app" id="app">
|
|
<aside class="side">
|
|
<div class="search"><label><svg class="i s" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
|
|
<input id="q" placeholder="Search files…" spellcheck="false" autocomplete="off" />
|
|
<button id="qx" title="Clear" style="display:none"><svg class="i s" viewBox="0 0 24 24"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button></label></div>
|
|
<div class="total" id="total"></div>
|
|
<div class="tree" id="tree"></div>
|
|
</aside>
|
|
<main class="main" id="main"></main>
|
|
</div>
|
|
<script>
|
|
(function () {
|
|
"use strict";
|
|
var A = window.aiAgentPlugin;
|
|
var init = (A && A.init) || {};
|
|
var app = document.getElementById("app");
|
|
var treeEl = document.getElementById("tree");
|
|
var mainEl = document.getElementById("main");
|
|
var qEl = document.getElementById("q");
|
|
|
|
function esc(s) {
|
|
return String(s == null ? "" : s).replace(/[&<>"']/g, function (c) {
|
|
return { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c];
|
|
});
|
|
}
|
|
function fmtNum(n) { return Math.round(n || 0).toLocaleString("en-US"); }
|
|
function fmtTokens(n) {
|
|
n = n || 0;
|
|
var a = Math.abs(n);
|
|
if (a < 1000) return fmtNum(n);
|
|
var div = a >= 1e9 ? 1e9 : a >= 1e6 ? 1e6 : 1e3, suf = a >= 1e9 ? "B" : a >= 1e6 ? "M" : "K";
|
|
var v = n / div;
|
|
return (Math.abs(v) >= 100 ? v.toFixed(0) : v.toFixed(1).replace(/\.0$/, "")) + suf;
|
|
}
|
|
function fmtCost(usd) {
|
|
var v = usd || 0;
|
|
return "$" + (!v ? "0" : v < 0.01 ? v.toFixed(4) : v < 1 ? v.toFixed(3) : v < 100 ? v.toFixed(2) : Math.round(v).toLocaleString("en-US"));
|
|
}
|
|
/** The live estimate while a file has unsaved edits (chars / 4), as the app's. */
|
|
function estimateTokens(text) { return Math.ceil(String(text || "").length / 4); }
|
|
var P = {
|
|
chevR: '<path d="m9 18 6-6-6-6"/>',
|
|
chevD: '<path d="m6 9 6 6 6-6"/>',
|
|
folder: '<path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/>',
|
|
folderOpen: '<path d="m6 14 1.5-2.9A2 2 0 0 1 9.24 10H20a2 2 0 0 1 1.94 2.5l-1.54 6a2 2 0 0 1-1.95 1.5H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H18a2 2 0 0 1 2 2v2"/>',
|
|
text: '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/>',
|
|
code: '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="m10 13-2 2 2 2"/><path d="m14 17 2-2-2-2"/>',
|
|
cog: '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><circle cx="12" cy="15" r="2"/>',
|
|
json: '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 12a1 1 0 0 0-1 1v1a1 1 0 0 1-1 1 1 1 0 0 1 1 1v1a1 1 0 0 0 1 1"/><path d="M14 18a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1 1 1 0 0 1-1-1v-1a1 1 0 0 0-1-1"/>',
|
|
dollar: '<line x1="12" x2="12" y1="2" y2="22"/><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/>',
|
|
back: '<path d="m12 19-7-7 7-7"/><path d="M19 12H5"/>',
|
|
save: '<path d="M15.2 3a2 2 0 0 1 1.4.6l3.8 3.8a2 2 0 0 1 .6 1.4V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"/><path d="M17 21v-7a1 1 0 0 0-1-1H8a1 1 0 0 0-1 1v7"/><path d="M7 3v4a1 1 0 0 0 1 1h7"/>',
|
|
undo: '<path d="M3 7v6h6"/><path d="M21 17a9 9 0 0 0-9-9 9 9 0 0 0-6 2.3L3 13"/>',
|
|
eye: '<path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/>',
|
|
pen: '<path d="M12 20h9"/><path d="M16.376 3.622a1 1 0 0 1 3.002 3.002L7.368 18.635a2 2 0 0 1-.855.506l-2.872.838a.5.5 0 0 1-.62-.62l.838-2.872a2 2 0 0 1 .506-.854z"/>'
|
|
};
|
|
function ic(n, c) { return '<svg class="i ' + (c || "") + '" viewBox="0 0 24 24">' + P[n] + "</svg>"; }
|
|
function fileIcon(kind) {
|
|
if (kind === "script") return ic("cog", "ico");
|
|
if (kind === "config") return ic("json", "ico");
|
|
if (kind === "claude-md" || kind === "skill-md") return ic("text", "ico prim");
|
|
if (kind === "markdown") return ic("text", "ico");
|
|
return ic("code", "ico");
|
|
}
|
|
|
|
// ── markdown preview (escaped) ───────────────────────────────────────────
|
|
function inline(s) {
|
|
s = esc(s);
|
|
var codes = [];
|
|
s = s.replace(/`([^`]+)`/g, function (_m, c) { codes.push(c); return "\u0000" + (codes.length - 1) + "\u0000"; });
|
|
s = s.replace(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>');
|
|
s = s.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
|
|
s = s.replace(/(^|[^*\w])\*([^*\s][^*]*)\*/g, "$1<em>$2</em>");
|
|
s = s.replace(/\u0000(\d+)\u0000/g, function (_m, i) { return "<code>" + codes[+i] + "</code>"; });
|
|
return s;
|
|
}
|
|
function md(src) {
|
|
src = String(src || "").replace(/\r/g, "");
|
|
var out = [], fm = /^---\n([\s\S]*?)\n---\n/.exec(src);
|
|
if (fm) { out.push('<div class="fm">' + esc(fm[1]) + "</div>"); src = src.slice(fm[0].length); }
|
|
var lines = src.split("\n"), i = 0, list = null;
|
|
function closeList() { if (list) { out.push("</" + list + ">"); list = null; } }
|
|
while (i < lines.length) {
|
|
var l = lines[i];
|
|
if (/^\s*(```|~~~)/.test(l)) {
|
|
closeList();
|
|
var buf = []; i++;
|
|
while (i < lines.length && !/^\s*(```|~~~)/.test(lines[i])) { buf.push(lines[i]); i++; }
|
|
i++;
|
|
out.push("<pre><code>" + esc(buf.join("\n")) + "</code></pre>");
|
|
continue;
|
|
}
|
|
var h = l.match(/^(#{1,6})\s+(.*)$/);
|
|
if (h) { closeList(); var n = Math.min(4, h[1].length); out.push("<h" + n + ">" + inline(h[2]) + "</h" + n + ">"); i++; continue; }
|
|
if (/^\s*\|/.test(l)) {
|
|
closeList();
|
|
var html = '<div class="scroller"><table>', first = true;
|
|
while (i < lines.length && /^\s*\|/.test(lines[i])) {
|
|
var cells = lines[i].trim().replace(/^\||\|$/g, "").split("|"); i++;
|
|
if (cells.every(function (c) { return /^\s*:?-+:?\s*$/.test(c); })) continue;
|
|
var tag = first ? "th" : "td"; first = false;
|
|
html += "<tr>" + cells.map(function (c) { return "<" + tag + ">" + inline(c.trim()) + "</" + tag + ">"; }).join("") + "</tr>";
|
|
}
|
|
out.push(html + "</table></div>");
|
|
continue;
|
|
}
|
|
var li = l.match(/^\s{0,3}([-*+]|\d+[.)])\s+(.*)$/);
|
|
if (li) {
|
|
var kind = /\d/.test(li[1]) ? "ol" : "ul";
|
|
if (list !== kind) { closeList(); out.push("<" + kind + ">"); list = kind; }
|
|
var item = li[2]; i++;
|
|
while (i < lines.length && /^\s{2,}\S/.test(lines[i]) && !/^\s*([-*+]|\d+[.)])\s+/.test(lines[i])) { item += " " + lines[i].trim(); i++; }
|
|
var task = item.match(/^\[([ xX])\]\s*(.*)$/);
|
|
out.push(task ? '<li class="task">' + (task[1] === " " ? "☐ " : "☑ ") + inline(task[2]) + "</li>" : "<li>" + inline(item) + "</li>");
|
|
continue;
|
|
}
|
|
if (/^>\s?/.test(l)) { closeList(); out.push("<blockquote>" + inline(l.replace(/^>\s?/, "")) + "</blockquote>"); i++; continue; }
|
|
if (/^\s*(---+|\*\*\*+)\s*$/.test(l)) { closeList(); out.push("<hr />"); i++; continue; }
|
|
if (!l.trim()) { closeList(); i++; continue; }
|
|
closeList();
|
|
var para = [l]; i++;
|
|
while (i < lines.length && lines[i].trim() && !/^(#{1,6}\s|\s*(```|~~~)|\s*\||\s{0,3}([-*+]|\d+[.)])\s|>)/.test(lines[i])) { para.push(lines[i]); i++; }
|
|
out.push("<p>" + inline(para.join(" ")) + "</p>");
|
|
}
|
|
closeList();
|
|
return out.join("\n");
|
|
}
|
|
|
|
// ── host ─────────────────────────────────────────────────────────────────
|
|
function read(name, payload, cb) {
|
|
if (A.query) return A.query(name, payload || {}, cb);
|
|
var live = true;
|
|
function run() {
|
|
return A.action(name, payload || {}).then(
|
|
function (d) { if (live) cb(d, { fetching: false }); },
|
|
function (e) { if (live) cb(null, { error: String((e && e.message) || e), fetching: false }); });
|
|
}
|
|
run();
|
|
return { stop: function () { live = false; }, refresh: run };
|
|
}
|
|
function toast(m, tone) { if (A.toast) A.toast(m, tone || "info"); }
|
|
|
|
// ── state ────────────────────────────────────────────────────────────────
|
|
var bundle = null; // the catalog
|
|
var files = {}; // path → entry (bundle row, or the fresher one a read / save answered)
|
|
var content = {}; // path → text on disk
|
|
var drafts = {}; // path → unsaved text (the hub keeps them: they follow you)
|
|
var touched = {}; // paths edited on this page (a hub copy never overwrites them)
|
|
var selected = (init.query && init.query.path) || "";
|
|
var preview = false;
|
|
var saving = false;
|
|
var saveError = "";
|
|
var openDirs = {}; // dir path → open?
|
|
var fileRead = null;
|
|
|
|
var routed = !!(A.onRoute && A.setRoute);
|
|
if (routed) A.onRoute(function (r) {
|
|
var p = (r && r.query && r.query.path) || "";
|
|
if (p !== selected) select(p, true);
|
|
});
|
|
function select(path, fromRoute) {
|
|
if (path === selected && !fromRoute) { app.classList.toggle("picked", !!path); return; }
|
|
flushDraft();
|
|
selected = path;
|
|
preview = false;
|
|
saveError = "";
|
|
if (!fromRoute && routed) A.setRoute({ query: path ? { path: path } : {} });
|
|
else if (!fromRoute) A.navigate("/plugins/" + A.id + "/editor" + (path ? "?path=" + encodeURIComponent(path) : ""));
|
|
// its folders open in the tree
|
|
var parts = path.split("/");
|
|
for (var i = 1; i < parts.length; i++) openDirs[parts.slice(0, i).join("/")] = true;
|
|
loadFile();
|
|
renderTree();
|
|
renderMain();
|
|
}
|
|
function loadFile() {
|
|
if (fileRead) { fileRead.stop(); fileRead = null; }
|
|
if (!selected) return;
|
|
var path = selected;
|
|
fileRead = read("file", { path: path }, function (d, meta) {
|
|
if (path !== selected) return;
|
|
if (!d) {
|
|
if (meta.error && !meta.fetching && content[path] === undefined) { content[path] = null; files[path] = files[path] || null; renderMain(meta.error); }
|
|
return;
|
|
}
|
|
var first = content[path] === undefined || content[path] === null;
|
|
content[path] = d.content || "";
|
|
files[path] = d;
|
|
if (first || !isDirty(path)) renderMain(); else renderStatus();
|
|
});
|
|
}
|
|
|
|
function draftOf(path) { return Object.prototype.hasOwnProperty.call(drafts, path) ? drafts[path] : undefined; }
|
|
function isDirty(path) {
|
|
var d = draftOf(path);
|
|
return d !== undefined && typeof content[path] === "string" && d !== content[path];
|
|
}
|
|
|
|
// Drafts go to the hub a moment after typing stops (and at once when the
|
|
// page is left), so an edit started at the desk is there on the phone.
|
|
var draftTimer = null, draftPending = null;
|
|
function queueDraft(path, text) {
|
|
draftPending = { path: path, content: text };
|
|
clearTimeout(draftTimer);
|
|
draftTimer = setTimeout(flushDraft, 800);
|
|
}
|
|
function flushDraft() {
|
|
clearTimeout(draftTimer);
|
|
var p = draftPending;
|
|
draftPending = null;
|
|
if (!p) return;
|
|
A.action("draft", p).catch(function () {});
|
|
}
|
|
window.addEventListener("pagehide", flushDraft);
|
|
document.addEventListener("visibilitychange", function () { if (document.visibilityState === "hidden") flushDraft(); });
|
|
|
|
// ── the tree ─────────────────────────────────────────────────────────────
|
|
function buildTree(list) {
|
|
var root = { name: "", path: "", dir: true, children: [] };
|
|
list.forEach(function (f) {
|
|
var parts = f.path.split("/"), node = root, acc = "";
|
|
parts.forEach(function (part, i) {
|
|
acc = acc ? acc + "/" + part : part;
|
|
var leaf = i === parts.length - 1;
|
|
var child = null;
|
|
for (var k = 0; k < node.children.length; k++) if (node.children[k].name === part && node.children[k].dir === !leaf) { child = node.children[k]; break; }
|
|
if (!child) { child = { name: part, path: acc, dir: !leaf, file: leaf ? f : null, children: [] }; node.children.push(child); }
|
|
node = child;
|
|
});
|
|
});
|
|
(function sort(n) {
|
|
n.children.sort(function (a, b) { return a.dir !== b.dir ? (a.dir ? -1 : 1) : a.name.localeCompare(b.name); });
|
|
n.children.forEach(sort);
|
|
})(root);
|
|
return root;
|
|
}
|
|
function stats(n) {
|
|
if (n.file) return { tokens: n.file.tokens || 0, cost: n.file.cost || 0 };
|
|
return n.children.reduce(function (acc, c) { var s = stats(c); return { tokens: acc.tokens + s.tokens, cost: acc.cost + s.cost }; }, { tokens: 0, cost: 0 });
|
|
}
|
|
function renderTree() {
|
|
if (!bundle) return;
|
|
var q = qEl.value.trim().toLowerCase();
|
|
var list = (bundle.files || []).filter(function (f) { return !q || f.path.toLowerCase().indexOf(q) >= 0; });
|
|
var tree = buildTree(list);
|
|
if (!tree.children.length) { treeEl.innerHTML = '<div class="none">No files match.</div>'; return; }
|
|
var h = "";
|
|
(function rows(n, depth) {
|
|
n.children.forEach(function (c) {
|
|
var pad = "padding-left:" + (depth * 12 + 8) + "px";
|
|
if (c.dir) {
|
|
var open = !!q || (openDirs[c.path] !== undefined ? openDirs[c.path] : depth < 1);
|
|
var s = stats(c);
|
|
h += '<button class="ti dir" style="' + pad + '" data-dir="' + esc(c.path) + '" data-open="' + (open ? 1 : 0) + '">' +
|
|
ic(open ? "chevD" : "chevR", "s chev") + ic(open ? "folderOpen" : "folder", "ico") + '<span class="nm">' + esc(c.name) +
|
|
'</span><span class="sp"></span>' + (s.tokens ? '<span class="tok" title="' + esc(fmtNum(s.tokens) + " tokens · " + fmtCost(s.cost)) + '">' + fmtTokens(s.tokens) + "</span>" : "") + "</button>";
|
|
if (open) rows(c, depth + 1);
|
|
} else {
|
|
var f = c.file;
|
|
h += '<button class="ti' + (c.path === selected ? " sel" : "") + '" style="' + pad + '" data-path="' + esc(c.path) + '">' +
|
|
'<span style="width:13px;flex:none"></span>' + fileIcon(f.kind) + '<span class="nm">' + esc(c.name) + "</span>" +
|
|
(isDirty(c.path) || (draftOf(c.path) !== undefined && content[c.path] === undefined) ? '<span class="dot" title="unsaved"></span>' : "") +
|
|
'<span class="sp"></span>' + (f.tokens ? '<span class="tok" title="' + esc(fmtNum(f.tokens) + " tokens · " + fmtCost(f.cost) + (f.estimated ? " (estimate — not yet counted)" : "")) + '">' +
|
|
(f.estimated ? "~" : "") + fmtTokens(f.tokens) + "</span>" : "") + "</button>";
|
|
}
|
|
});
|
|
})(tree, 0);
|
|
treeEl.innerHTML = h;
|
|
var sel = treeEl.querySelector(".ti.sel");
|
|
if (sel && sel.scrollIntoView && !treeEl.dataset.scrolled) { sel.scrollIntoView({ block: "center" }); treeEl.dataset.scrolled = "1"; }
|
|
}
|
|
treeEl.addEventListener("click", function (e) {
|
|
var b = e.target.closest && e.target.closest(".ti");
|
|
if (!b) return;
|
|
if (b.hasAttribute("data-dir")) { openDirs[b.getAttribute("data-dir")] = b.getAttribute("data-open") !== "1"; renderTree(); return; }
|
|
select(b.getAttribute("data-path"));
|
|
app.classList.add("picked");
|
|
});
|
|
qEl.addEventListener("input", function () { document.getElementById("qx").style.display = qEl.value ? "" : "none"; renderTree(); });
|
|
document.getElementById("qx").addEventListener("click", function () { qEl.value = ""; this.style.display = "none"; renderTree(); qEl.focus(); });
|
|
|
|
function renderTotal() {
|
|
if (!bundle) return;
|
|
var t = bundle.totals || {};
|
|
document.getElementById("total").innerHTML = ic("dollar", "s") + "<span>context total</span><b>" + fmtTokens(t.tokens) + " tok · " + fmtCost(t.cost) + "</b>";
|
|
if (A.setHeader) A.setHeader({ subtitle: fmtNum(t.files) + " files · " + fmtTokens(t.tokens) + " tokens · " + fmtCost(t.cost) });
|
|
}
|
|
|
|
// ── the editor ───────────────────────────────────────────────────────────
|
|
var ta = null;
|
|
function renderMain(err) {
|
|
app.classList.toggle("picked", !!selected);
|
|
if (!selected) {
|
|
mainEl.innerHTML = '<div class="empty">Pick a file — CLAUDE.md, a skill, an agent, a hook — to read and edit it here.</div>';
|
|
ta = null;
|
|
return;
|
|
}
|
|
var f = files[selected] || (bundle && (bundle.files || []).filter(function (x) { return x.path === selected; })[0]) || null;
|
|
var name = selected.split("/").pop();
|
|
var bar = '<div class="bar"><button class="ib ghost only-phone" id="back" title="Files">' + ic("back") + "</button>" +
|
|
fileIcon(f && f.kind) + '<div class="who"><div class="nm">' + esc(name) + '</div><div class="pt">' + esc(selected) + "</div></div>";
|
|
if (content[selected] === undefined || content[selected] === null) {
|
|
var missing = content[selected] === null || (bundle && !f);
|
|
mainEl.innerHTML = bar + '</div><div class="empty">' + (missing ? esc(err && !/not.?found|404/i.test(err) ? err : "File not found in the current context.") : "") + "</div>";
|
|
ta = null;
|
|
return;
|
|
}
|
|
var editable = !!(f && f.editable);
|
|
var isMd = /\.(md|markdown)$/i.test(selected);
|
|
if (!editable) bar += '<span class="ro" title="The data/ tree is read-only here">read-only</span>';
|
|
if (isMd) bar += '<button class="ib ' + (preview ? "on" : "") + '" id="pv" title="' + (preview ? "Edit" : "Preview") + '">' + ic(preview ? "pen" : "eye", "s") + '<span class="lb">' + (preview ? "Edit" : "Preview") + "</span></button>";
|
|
if (editable) {
|
|
bar += '<button class="ib" id="rv" title="Revert">' + ic("undo", "s") + '<span class="lb">Revert</span></button>' +
|
|
'<button class="ib primary" id="sv" title="Save (⌘S)">' + ic("save", "s") + '<span class="lb">Save</span></button>';
|
|
}
|
|
bar += "</div>";
|
|
var value = draftOf(selected) !== undefined ? draftOf(selected) : content[selected];
|
|
var bodyHtml = preview
|
|
? '<div class="preview"><article class="md">' + md(value) + "</article></div>"
|
|
: '<textarea id="ta" spellcheck="false" placeholder="empty file"' + (editable ? "" : " readonly") + "></textarea>";
|
|
mainEl.innerHTML = bar + '<div class="body">' + bodyHtml + '</div><div class="status" id="status"></div>';
|
|
ta = document.getElementById("ta");
|
|
if (ta) {
|
|
ta.value = value;
|
|
ta.addEventListener("input", onInput);
|
|
ta.addEventListener("keydown", onKey);
|
|
ta.addEventListener("blur", function () { if (isDirty(selected) && !saving) save(); });
|
|
}
|
|
renderStatus();
|
|
}
|
|
function renderStatus() {
|
|
var el = document.getElementById("status");
|
|
if (!el) return;
|
|
var f = files[selected] || {};
|
|
var dirty = isDirty(selected);
|
|
var value = draftOf(selected) !== undefined ? draftOf(selected) : content[selected] || "";
|
|
var tokens = dirty ? estimateTokens(value) : f.tokens || 0;
|
|
var lines = value ? value.split("\n").length : 0;
|
|
var st = saving ? "saving…" : saveError ? '<span class="err">' + esc(saveError) + "</span>" : dirty ? "● unsaved" : "";
|
|
el.innerHTML = "<span>" + fmtNum(lines) + " lines</span><span title=\"" + esc(fmtNum(tokens) + " tokens" + (dirty || f.estimated ? " (estimate)" : "")) + "\">" +
|
|
(dirty || f.estimated ? "~" : "") + fmtTokens(tokens) + " tok</span>" + (dirty ? "" : "<span>" + fmtCost(f.cost) + "</span>") + '<span class="st">' + st + "</span>";
|
|
var sv = document.getElementById("sv"), rv = document.getElementById("rv");
|
|
if (sv) sv.disabled = !dirty || saving;
|
|
if (rv) rv.disabled = !dirty || saving;
|
|
}
|
|
function onInput() {
|
|
var v = ta.value;
|
|
touched[selected] = true;
|
|
if (v === content[selected]) { delete drafts[selected]; queueDraft(selected, null); }
|
|
else { drafts[selected] = v; queueDraft(selected, v); }
|
|
saveError = "";
|
|
renderStatus();
|
|
var item = treeEl.querySelector('.ti[data-path="' + CSS.escape(selected) + '"]');
|
|
if (item && !!item.querySelector(".dot") !== isDirty(selected)) renderTree();
|
|
}
|
|
function onKey(e) {
|
|
if ((e.metaKey || e.ctrlKey) && (e.key === "s" || e.key === "S")) { e.preventDefault(); save(); return; }
|
|
if (e.key === "Tab" && !e.shiftKey && !e.metaKey && !e.ctrlKey && !e.altKey && !ta.readOnly) {
|
|
e.preventDefault();
|
|
var s = ta.selectionStart, en = ta.selectionEnd;
|
|
ta.setRangeText(" ", s, en, "end");
|
|
onInput();
|
|
}
|
|
}
|
|
function save() {
|
|
var path = selected;
|
|
if (!isDirty(path) || saving) return;
|
|
var text = drafts[path];
|
|
saving = true;
|
|
saveError = "";
|
|
draftPending = null;
|
|
clearTimeout(draftTimer);
|
|
renderStatus();
|
|
A.action("save", { path: path, content: text }).then(function (f) {
|
|
saving = false;
|
|
content[path] = f && typeof f.content === "string" ? f.content : text;
|
|
if (f) files[path] = f;
|
|
if (drafts[path] === text) delete drafts[path];
|
|
toast("Saved " + path.split("/").pop(), "success");
|
|
if (bundleRead) bundleRead.refresh();
|
|
if (fileRead && path === selected) fileRead.refresh();
|
|
if (path === selected) renderStatus();
|
|
renderTree();
|
|
}, function (e) {
|
|
saving = false;
|
|
saveError = String((e && e.message) || e);
|
|
renderStatus();
|
|
toast("Couldn't save: " + saveError, "error");
|
|
});
|
|
}
|
|
mainEl.addEventListener("click", function (e) {
|
|
var b = e.target.closest && e.target.closest("button");
|
|
if (!b) return;
|
|
if (b.id === "back") { flushDraft(); app.classList.remove("picked"); select(""); }
|
|
else if (b.id === "pv") { preview = !preview; renderMain(); }
|
|
else if (b.id === "sv") save();
|
|
else if (b.id === "rv") {
|
|
touched[selected] = true;
|
|
delete drafts[selected];
|
|
queueDraft(selected, null);
|
|
flushDraft();
|
|
saveError = "";
|
|
renderMain();
|
|
renderTree();
|
|
}
|
|
});
|
|
|
|
// ── load ─────────────────────────────────────────────────────────────────
|
|
var bundleRead = read("bundle", {}, function (d, meta) {
|
|
if (!d) {
|
|
if (meta.error && !bundle) treeEl.innerHTML = '<div class="none">' + esc(meta.error) + "</div>";
|
|
return;
|
|
}
|
|
bundle = d;
|
|
(d.files || []).forEach(function (f) { if (!files[f.path] || files[f.path].tokens !== f.tokens) files[f.path] = Object.assign({}, files[f.path] || {}, f); });
|
|
renderTotal();
|
|
renderTree();
|
|
if (selected && content[selected] === undefined) renderMain();
|
|
else renderStatus();
|
|
});
|
|
// The host answers a read at once from this device's last copy, then fresh:
|
|
// take every answer, never over what was typed here since the page opened.
|
|
read("drafts", {}, function (d) {
|
|
if (!d) return;
|
|
var ds = d.drafts || {};
|
|
for (var k in ds) if (!touched[k]) drafts[k] = ds[k];
|
|
for (var j in drafts) if (!touched[j] && !(j in ds)) delete drafts[j];
|
|
renderTree();
|
|
if (selected && ta && isDirty(selected) && document.activeElement !== ta) renderMain();
|
|
else renderStatus();
|
|
});
|
|
if (selected) {
|
|
var parts = selected.split("/");
|
|
for (var i = 1; i < parts.length; i++) openDirs[parts.slice(0, i).join("/")] = true;
|
|
loadFile();
|
|
}
|
|
renderMain();
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|