scorpiox-bot-plugin-files
←ui.htmlhtml
614 lines · 26 KBHEADBlameDownload
1<!DOCTYPE html>
2<html lang="en"><head><meta charset="utf-8">
3<meta name="viewport" content="width=device-width,initial-scale=1">
4<title>files</title>
5<!--TOKENS-->
6<style>
7*{box-sizing:border-box}
8html,body{margin:0;height:100%}
9body{background:var(--bg);color:var(--text);font-family:var(--font);
10 font-size:14px;line-height:1.5;-webkit-font-smoothing:antialiased;overflow:hidden}
11#app{display:grid;grid-template-columns:252px minmax(0,1fr);height:100%;min-height:0}
12.pane{min-width:0;min-height:0;display:flex;flex-direction:column}
13#side{background:var(--bg-2);border-right:1px solid var(--line)}
14.side-hd{display:flex;align-items:center;gap:8px;padding:12px 12px 8px}
15.brand{font-size:12px;font-weight:700;letter-spacing:.09em;text-transform:uppercase;
16 color:var(--text-2);flex:1}
17.side-ft{display:flex;align-items:center;gap:8px;padding:8px 12px;
18 border-top:1px solid var(--line);font-size:11px;color:var(--text-2)}
19.side-ft .grow{flex:1}
20.side-lbl{font-size:10.5px;font-weight:700;letter-spacing:.1em;text-transform:uppercase;
21 color:var(--text-2);padding:4px 14px 6px}
22.icon{display:inline-flex;align-items:center;justify-content:center;gap:6px;
23 background:var(--surface-2);border:1px solid var(--line);color:var(--text);
24 border-radius:9px;width:30px;height:30px;padding:0;cursor:pointer;font-family:inherit;
25 font-size:12px;font-weight:600;flex:0 0 auto}
26.icon:hover{background:var(--surface-hover);border-color:var(--line-2)}
27.icon:active{transform:scale(.96)}
28.icon svg{width:15px;height:15px;display:block;fill:none;stroke:currentColor;
29 stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round}
30.icon.on{background:var(--surface-2);border-color:var(--line-2);color:var(--blue)}
31.rootlist{flex:0 0 auto;padding:0 8px 10px;border-bottom:1px solid var(--line)}
32.root{display:flex;align-items:center;gap:9px;width:100%;text-align:left;
33 background:transparent;border:0;border-radius:9px;padding:8px 10px;color:var(--text);
34 cursor:pointer;font-family:inherit;font-size:13px;margin-bottom:2px}
35.root:hover{background:var(--surface-hover)}
36.root.sel{background:var(--surface-2);box-shadow:inset 2px 0 0 var(--blue)}
37.root svg{width:14px;height:14px;flex:0 0 auto;fill:none;stroke:currentColor;stroke-width:1.7;
38 stroke-linecap:round;stroke-linejoin:round;color:var(--text-2)}
39.root .rl{min-width:0;flex:1}
40.root .rl .n{display:block;font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
41.root .rl .p{display:block;font-size:10.5px;color:var(--text-2);overflow:hidden;
42 text-overflow:ellipsis;white-space:nowrap;font-family:var(--mono)}
43#main{background:var(--bg);min-width:0}
44.toolbar{display:flex;align-items:center;gap:7px;padding:10px 14px;
45 border-bottom:1px solid var(--line);flex:0 0 auto;min-width:0}
46.crumbs{flex:1 1 auto;min-width:0;display:flex;align-items:center;gap:2px;
47 overflow-x:auto;scrollbar-width:none;white-space:nowrap}
48.crumbs::-webkit-scrollbar{display:none}
49.crumb{background:transparent;border:0;color:var(--text-2);font-family:var(--mono);
50 font-size:12px;padding:3px 5px;border-radius:6px;cursor:pointer;flex:0 0 auto}
51.crumb:hover{background:var(--surface-hover);color:var(--text)}
52.crumb.last{color:var(--text);font-weight:600}
53.crumb .sep{color:var(--text-2);opacity:.6;padding:0 1px}
54.body{flex:1 1 auto;display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.15fr);
55 min-height:0}
56.body.noprev{grid-template-columns:minmax(0,1fr)}
57.listwrap{min-width:0;min-height:0;display:flex;flex-direction:column;
58 border-right:1px solid var(--line)}
59.entries{flex:1 1 auto;overflow-y:auto;padding:6px 8px 20px}
60.row{display:flex;align-items:center;gap:9px;width:100%;text-align:left;
61 background:transparent;border:0;border-radius:9px;padding:7px 9px;color:var(--text);
62 cursor:pointer;font-family:inherit;font-size:13px}
63.row:hover{background:var(--surface-hover)}
64.row.sel{background:var(--surface-2);box-shadow:inset 2px 0 0 var(--blue)}
65.row .ico{width:16px;height:16px;flex:0 0 auto;color:var(--text-2);display:flex}
66.row .ico svg{width:16px;height:16px;fill:none;stroke:currentColor;stroke-width:1.7;
67 stroke-linecap:round;stroke-linejoin:round}
68.row.dir .ico{color:var(--blue)}
69.row .nm{flex:1 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
70.row .mt{flex:0 0 auto;font-family:var(--mono);font-size:11px;color:var(--text-2)}
71.empty{padding:22px 14px;color:var(--text-2);font-size:12.5px;text-align:center}
72.hidden{display:none!important}
73.preview{min-width:0;min-height:0;display:flex;flex-direction:column}
74.prev-hd{display:flex;align-items:center;gap:8px;padding:9px 14px;
75 border-bottom:1px solid var(--line);flex:0 0 auto;min-width:0}
76.prev-hd .nm{flex:1 1 auto;min-width:0;font-weight:600;font-size:13px;
77 overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
78.prev-hd .mt{font-family:var(--mono);font-size:11px;color:var(--text-2);white-space:nowrap}
79.prev-body{flex:1 1 auto;overflow:auto;min-height:0}
80.codetxt{margin:0;padding:0;font-family:var(--mono);font-size:12.5px;line-height:1.6;
81 display:grid;grid-template-columns:auto minmax(0,1fr);min-width:0}
82.codetxt .ln{color:var(--text-2);text-align:right;padding:0 10px 0 14px;
83 user-select:none;background:var(--bg-2);border-right:1px solid var(--line)}
84.codetxt .tx{padding:0 14px;white-space:pre-wrap;word-break:break-word;
85 font-family:var(--mono);font-size:12.5px}
86.md{padding:16px 20px 40px;max-width:860px}
87.md h1,.md h2,.md h3,.md h4{line-height:1.25;margin:18px 0 8px;font-weight:650}
88.md h1{font-size:21px}.md h2{font-size:18px}.md h3{font-size:15.5px}.md h4{font-size:14px}
89.md p{margin:0 0 11px}
90.md ul,.md ol{margin:0 0 11px;padding-left:22px}
91.md li{margin:3px 0}
92.md code{font-family:var(--mono);font-size:12px;background:var(--code-bg);
93 padding:1px 5px;border-radius:5px}
94.md pre{background:var(--code-bg);border:1px solid var(--line);border-radius:10px;
95 padding:11px 13px;overflow-x:auto;margin:0 0 12px}
96.md pre code{background:transparent;padding:0;font-size:12px;line-height:1.55}
97.md hr{border:0;border-top:1px solid var(--line);margin:16px 0}
98.md a{color:var(--blue);text-decoration:none;font-weight:600}
99.md a:hover{text-decoration:underline}
100.md blockquote{margin:0 0 12px;padding:2px 0 2px 13px;border-left:3px solid var(--line-2);
101 color:var(--text-2)}
102.imgwrap{padding:16px;display:flex;align-items:flex-start;justify-content:center}
103.imgwrap img{max-width:100%;height:auto;border-radius:10px;border:1px solid var(--line);
104 background:var(--bg-2)}
105.note{padding:16px 18px;color:var(--text-2);font-size:12.5px}
106.welcome{flex:1 1 auto;display:flex;flex-direction:column;align-items:center;
107 justify-content:center;gap:10px;color:var(--text-2);font-size:13px;padding:24px;
108 text-align:center;min-height:0}
109.welcome svg{width:34px;height:34px;fill:none;stroke:currentColor;stroke-width:1.4;
110 stroke-linecap:round;stroke-linejoin:round;opacity:.55}
111.toast{position:fixed;left:50%;bottom:18px;transform:translateX(-50%);
112 background:var(--modal-bg);border:1px solid var(--line-2);color:var(--text);
113 padding:8px 14px;border-radius:999px;font-size:12.5px;box-shadow:var(--shadow-lg);
114 opacity:0;pointer-events:none;transition:opacity .16s var(--ease);z-index:9;
115 max-width:calc(100% - 24px);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
116.toast.show{opacity:1}
117@media (max-width:1100px){
118 #app{grid-template-columns:216px minmax(0,1fr)}
119 .body{grid-template-columns:minmax(0,1fr) minmax(0,1fr)}
120}
121@media (max-width:820px){
122 body{overflow:hidden}
123 #app{grid-template-columns:minmax(0,1fr)}
124 #side,#main{grid-column:1;grid-row:1}
125 #app[data-view="browse"] #side{display:none}
126 #app[data-view="roots"] #main{display:none}
127 .body{grid-template-columns:minmax(0,1fr)}
128 #app[data-view="browse"] .body{grid-template-columns:minmax(0,1fr)}
129 #app[data-view="browse"] .preview{display:none}
130 #app[data-view="preview"] .listwrap{display:none}
131 #app[data-view="preview"] .body{grid-template-columns:minmax(0,1fr)}
132 .back{display:inline-flex!important}
133 .toolbar{padding:8px 10px;gap:6px}
134 .md{padding:12px 14px 32px}
135 .codetxt .tx{padding:0 10px}
136}
137@media (min-width:821px){.back{display:none!important}}
138</style>
139</head><body>
140<div id="app" data-view="roots">
141 <aside id="side" class="pane">
142 <div class="side-hd">
143 <span class="brand">files</span>
144 <button id="btnUp" class="icon" title="Up one level" aria-label="Up one level">
145 <svg viewBox="0 0 24 24"><path d="M12 19V5M5 12l7-7 7 7"/></svg></button>
146 <button id="btnRefresh" class="icon" title="Refresh" aria-label="Refresh">
147 <svg viewBox="0 0 24 24"><path d="M20 11a8 8 0 1 0-2.3 5.7"/><path d="M20 4v7h-7"/></svg></button>
148 </div>
149 <div class="side-lbl">Roots</div>
150 <div id="roots" class="rootlist"></div>
151 <div class="side-ft">
152 <span id="count">0 items</span><span class="grow"></span>
153 <button id="btnHidden" class="icon" title="Show hidden files" aria-label="Show hidden files">
154 <svg viewBox="0 0 24 24"><path d="M1 12s4-7 11-7 11 7 11 7-4 7-11 7S1 12 1 12z"/><circle cx="12" cy="12" r="3"/></svg></button>
155 <button id="btnTheme" class="icon embed-hide" title="Toggle appearance" aria-label="Toggle appearance">
156 <svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="4.5"/><path d="M12 2v2M12 20v2M2 12h2M20 12h2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M19.1 4.9l-1.4 1.4M6.3 17.7l-1.4 1.4"/></svg></button>
157 </div>
158 </aside>
159 <main id="main" class="pane">
160 <div class="toolbar">
161 <button id="btnBack" class="icon back" title="Back to list" aria-label="Back to list">
162 <svg viewBox="0 0 24 24"><path d="M15 5l-7 7 7 7"/></svg></button>
163 <div id="crumbs" class="crumbs"></div>
164 <button id="btnDownload" class="icon" title="Download file" aria-label="Download file">
165 <svg viewBox="0 0 24 24"><path d="M12 3v12M7 11l5 5 5-5M4 21h16"/></svg></button>
166 </div>
167 <div id="body" class="body">
168 <div class="listwrap">
169 <div id="entries" class="entries"></div>
170 <div id="empty" class="empty hidden">This directory is empty.</div>
171 </div>
172 <div id="preview" class="preview">
173 <div id="prevHd" class="prev-hd hidden">
174 <span id="prevName" class="nm"></span>
175 <span id="prevMeta" class="mt"></span>
176 </div>
177 <div id="prevBody" class="prev-body">
178 <div id="welcome" class="welcome">
179 <svg viewBox="0 0 24 24"><path d="M3 7h6l2 2h10v11H3z"/></svg>
180 <div>Pick a root, then open a file to preview it.</div>
181 <div style="font-size:11.5px">Arrows move &middot; Enter opens &middot; Backspace goes up</div>
182 </div>
183 </div>
184 </div>
185 </div>
186 </main>
187</div>
188<div id="toast" class="toast"></div>
189<script>
190(function(){
191"use strict";
192var app=document.getElementById('app');
193var rootsEl=document.getElementById('roots');
194var entriesEl=document.getElementById('entries');
195var emptyEl=document.getElementById('empty');
196var countEl=document.getElementById('count');
197var crumbsEl=document.getElementById('crumbs');
198var bodyEl=document.getElementById('body');
199var prevEl=document.getElementById('preview');
200var prevHd=document.getElementById('prevHd');
201var prevName=document.getElementById('prevName');
202var prevMeta=document.getElementById('prevMeta');
203var prevBody=document.getElementById('prevBody');
204var toastEl=document.getElementById('toast');
205var btnHidden=document.getElementById('btnHidden');
206var btnDownload=document.getElementById('btnDownload');
207
208var q=new URLSearchParams(location.search);
209var node=q.get('node')||'';
210var sid=q.get('id')||'';
211var pwd=q.get('pwd')||(node&&localStorage.getItem('bot_pwd_'+node))||
212 localStorage.getItem('bot_active_pwd')||'';
213var HUB=q.get('plugin')||'';
214
215var roots=[], entries=[], curPath='', curParent=null, curRoot=null;
216var selIndex=-1, showHidden=false, curFile=null;
217
218/* Two call routes, and getting this wrong is the most common plugin bug:
219 * page served through the hub (bot.scorpiox.net/plugins?plugin=plugin_files&node=..)
220 * -> call the hub proxy back: /plugins?plugin=plugin_files&action=..
221 * opened directly on the node
222 * -> the plugin itself is the route: /plugin_files?action=..
223 * Calling /plugins?plugin= on the node returns the discovery list, not us. */
224function url(action,extra){
225 var u=(HUB?'/plugins?plugin='+encodeURIComponent(HUB):'/plugin_files')+
226 '?action='+encodeURIComponent(action)+
227 (extra?('&'+extra):'')+
228 (node?'&node='+encodeURIComponent(node):'')+
229 (sid?'&id='+encodeURIComponent(sid):'')+
230 (pwd?'&pwd='+encodeURIComponent(pwd):'');
231 return u;
232}
233function hdr(extra){
234 var h=extra||{};
235 if(pwd)h['X-Bot-Password']=pwd;
236 return h;
237}
238function api(action,extra){
239 return fetch(url(action,extra),{headers:hdr({})}).then(function(r){
240 if(!r.ok)throw new Error('HTTP '+r.status);
241 return r.json();
242 });
243}
244
245function toast(msg){
246 toastEl.textContent=msg;
247 toastEl.classList.add('show');
248 clearTimeout(toast._t);
249 toast._t=setTimeout(function(){toastEl.classList.remove('show');},2000);
250}
251function setView(v){app.setAttribute('data-view',v);}
252
253function fmtSize(n){
254 if(n===null||n===undefined)return '';
255 if(n<1024)return n+' B';
256 if(n<1048576)return (n/1024).toFixed(1)+' KB';
257 if(n<1073741824)return (n/1048576).toFixed(1)+' MB';
258 return (n/1073741824).toFixed(1)+' GB';
259}
260function fmtTime(iso){
261 if(!iso)return '';
262 try{
263 var d=new Date(iso),now=new Date(),diff=(now-d)/1000;
264 if(diff<60)return 'just now';
265 if(diff<3600)return Math.floor(diff/60)+'m ago';
266 if(diff<86400)return Math.floor(diff/3600)+'h ago';
267 if(diff<604800)return Math.floor(diff/86400)+'d ago';
268 return d.toLocaleDateString();
269 }catch(e){return ''}
270}
271
272var SVG={
273 dir:'<svg viewBox="0 0 24 24"><path d="M3 7h6l2 2h10v11H3z"/></svg>',
274 file:'<svg viewBox="0 0 24 24"><path d="M6 3h9l5 5v13H6z"/><path d="M14 3v5h5"/></svg>',
275 link:'<svg viewBox="0 0 24 24"><path d="M10 13a4 4 0 0 0 6 .5l2.5-2.5a4 4 0 0 0-6-6L11 6.5"/><path d="M14 11a4 4 0 0 0-6-.5L5.5 13a4 4 0 0 0 6 6L13 17.5"/></svg>',
276 img:'<svg viewBox="0 0 24 24"><rect x="3" y="5" width="18" height="14" rx="2"/><circle cx="8.5" cy="10" r="1.6"/><path d="M21 16l-5-5-6 6"/></svg>',
277 root:'<svg viewBox="0 0 24 24"><path d="M3 7h6l2 2h10v11H3z"/></svg>'
278};
279
280function renderRoots(){
281 while(rootsEl.firstChild)rootsEl.removeChild(rootsEl.firstChild);
282 roots.forEach(function(r,i){
283 var b=document.createElement('button');
284 b.className='root'+(curRoot===r.path?' sel':'');
285 var ic=document.createElement('span');ic.innerHTML=SVG.root;
286 var wrap=document.createElement('span');wrap.className='rl';
287 var n=document.createElement('span');n.className='n';n.textContent=r.name;
288 var p=document.createElement('span');p.className='p';p.textContent=r.path;
289 wrap.appendChild(n);wrap.appendChild(p);
290 b.appendChild(ic);b.appendChild(wrap);
291 b.onclick=function(){openDir(r.path);setView('browse');};
292 rootsEl.appendChild(b);
293 });
294}
295
296function renderCrumbs(){
297 while(crumbsEl.firstChild)crumbsEl.removeChild(crumbsEl.firstChild);
298 if(!curPath){
299 var hint=document.createElement('span');hint.className='crumb last';
300 hint.textContent='Select a root';crumbsEl.appendChild(hint);return;
301 }
302 var base=curRoot||'';
303 var rel=curPath;
304 if(base&&curPath.indexOf(base)===0)rel=curPath.slice(base.length);
305 var parts=rel.split(/[\/\\]+/).filter(function(s){return s.length;});
306 var b0=document.createElement('button');
307 b0.className='crumb'+(parts.length?'':' last');
308 b0.textContent=base.split(/[\/\\]+/).filter(Boolean).pop()||base||'/';
309 b0.title=base;
310 b0.onclick=function(){openDir(base);};
311 crumbsEl.appendChild(b0);
312 var acc=base;
313 parts.forEach(function(p,i){
314 var sp=document.createElement('span');sp.className='sep';sp.textContent='/';
315 crumbsEl.appendChild(sp);
316 acc+=(acc&&acc[acc.length-1]!=='/'&&acc[acc.length-1]!=='\\'?'/':'')+p;
317 var b=document.createElement('button');
318 b.className='crumb'+(i===parts.length-1?' last':'');
319 b.textContent=p;b.title=acc;
320 var target=acc;
321 b.onclick=function(){openDir(target);};
322 crumbsEl.appendChild(b);
323 });
324}
325
326function isImage(name){
327 return /\.(png|jpe?g|gif|webp|bmp|ico|svg)$/i.test(name||'');
328}
329function isMarkdown(name){
330 return /\.(md|markdown)$/i.test(name||'');
331}
332function isTextish(name){
333 return /\.(txt|md|markdown|json|js|ts|css|html?|c|h|cpp|hpp|py|sh|yml|yaml|toml|ini|conf|log|csv|xml|sql|go|rs|java|rb|php|gitignore|env)$/i.test(name||'');
334}
335
336function renderEntries(){
337 while(entriesEl.firstChild)entriesEl.removeChild(entriesEl.firstChild);
338 if(selIndex>=entries.length)selIndex=entries.length?entries.length-1:-1;
339 entries.forEach(function(e,i){
340 var b=document.createElement('button');
341 b.className='row'+(e.type==='dir'?' dir':'')+(i===selIndex?' sel':'');
342 var ic=document.createElement('span');ic.className='ico';
343 ic.innerHTML = e.link?SVG.link : (e.type==='dir'?SVG.dir : (isImage(e.name)?SVG.img:SVG.file));
344 var nm=document.createElement('span');nm.className='nm';nm.textContent=e.name;
345 var mt=document.createElement('span');mt.className='mt';
346 mt.textContent=(e.type==='dir'?'':fmtSize(e.size));
347 b.appendChild(ic);b.appendChild(nm);b.appendChild(mt);
348 b.onclick=function(){selIndex=i;renderEntries();activate(e);};
349 entriesEl.appendChild(b);
350 });
351 emptyEl.classList.toggle('hidden',entries.length>0);
352 countEl.textContent=entries.length+(entries.length===1?' item':' items');
353 var sel=entriesEl.querySelector('.row.sel');
354 if(sel&&sel.scrollIntoView)sel.scrollIntoView({block:'nearest'});
355}
356
357function openDir(path){
358 return api('ls','path='+encodeURIComponent(path)+(showHidden?'&hidden=1':''))
359 .then(function(d){
360 if(!d||!d.ok)throw new Error((d&&d.error)||'failed');
361 curPath=d.path;curParent=d.parent;curRoot=d.root;
362 entries=d.entries||[];selIndex=entries.length?0:-1;
363 renderCrumbs();renderEntries();
364 if(!entries.length){clearPreview();}
365 else if(window.matchMedia('(max-width:820px)').matches){setView('browse');}
366 return d;
367 }).catch(function(e){toast('Cannot open folder: '+e.message);});
368}
369
370function clearPreview(){
371 prevHd.classList.add('hidden');
372 while(prevBody.firstChild)prevBody.removeChild(prevBody.firstChild);
373 var w=document.createElement('div');w.className='welcome';
374 w.innerHTML='<svg viewBox="0 0 24 24"><path d="M3 7h6l2 2h10v11H3z"/></svg>'+
375 '<div>Pick a root, then open a file to preview it.</div>'+
376 '<div style="font-size:11.5px">Arrows move &middot; Enter opens &middot; Backspace goes up</div>';
377 prevBody.appendChild(w);
378 curFile=null;
379 btnDownload.classList.add('hidden');
380}
381
382function activate(e){
383 if(e.type==='dir'){openDir(joinPath(curPath,e.name));setView('browse');return;}
384 previewFile(joinPath(curPath,e.name),e);
385}
386function joinPath(dir,name){
387 if(!dir)return name;
388 var last=dir[dir.length-1];
389 return dir+(last==='/'||last==='\\'?'':'/')+name;
390}
391
392function setHd(name,meta){
393 prevHd.classList.remove('hidden');
394 prevName.textContent=name;
395 prevMeta.textContent=meta||'';
396 btnDownload.classList.remove('hidden');
397}
398
399function previewFile(path,ent){
400 curFile=path;
401 setView('preview');
402 prevHd.classList.remove('hidden');
403 prevName.textContent=ent&&ent.name?ent.name:path.split(/[\/\\]/).pop();
404 prevMeta.textContent=ent?fmtSize(ent.size):'';
405 btnDownload.classList.remove('hidden');
406 while(prevBody.firstChild)prevBody.removeChild(prevBody.firstChild);
407 prevBody.scrollTop=0;
408
409 var name=prevName.textContent;
410 if(isImage(name)){
411 var wrap=document.createElement('div');wrap.className='imgwrap';
412 var img=document.createElement('img');
413 img.alt=name;
414 img.src=url('raw','path='+encodeURIComponent(path));
415 img.onerror=function(){
416 wrap.innerHTML='';
417 var n=document.createElement('div');n.className='note';
418 n.textContent='This image could not be displayed.';
419 wrap.appendChild(n);
420 };
421 wrap.appendChild(img);prevBody.appendChild(wrap);
422 return;
423 }
424
425 api('read','path='+encodeURIComponent(path)).then(function(d){
426 if(!d||!d.ok)throw new Error((d&&d.error)||'failed');
427 prevMeta.textContent=fmtSize(d.size)+(d.truncated?' (first 1 MB)':'');
428 if(d.binary){
429 var n=document.createElement('div');n.className='note';
430 n.textContent='Binary file ('+fmtSize(d.size)+') - use Download to inspect it locally.';
431 prevBody.appendChild(n);return;
432 }
433 if(!isTextish(name)&&!isMarkdown(name)){
434 prevMeta.textContent=fmtSize(d.size)+(d.truncated?' (first 1 MB)':'')+' · plain text';
435 }
436 if(isMarkdown(name))renderMarkdown(d.text||'',prevBody);
437 else renderCode(d.text||'',prevBody);
438 }).catch(function(e){
439 while(prevBody.firstChild)prevBody.removeChild(prevBody.firstChild);
440 var n=document.createElement('div');n.className='note';
441 n.textContent='Cannot preview this file: '+e.message;
442 prevBody.appendChild(n);
443 });
444}
445
446/* Line numbers + text. Every line is a text node, so file content can never
447 * become markup. */
448function renderCode(text,host){
449 var lines=text.split('\n');
450 if(lines.length&&lines[lines.length-1]==='')lines.pop();
451 var pre=document.createElement('div');pre.className='codetxt';
452 var ln=document.createElement('div');ln.className='ln';
453 var tx=document.createElement('div');tx.className='tx';
454 var nums=[],body=[];
455 for(var i=0;i<lines.length;i++){nums.push(String(i+1));body.push(lines[i]);}
456 ln.textContent=nums.join('\n');
457 tx.textContent=body.join('\n');
458 pre.appendChild(ln);pre.appendChild(tx);
459 host.appendChild(pre);
460}
461
462/* Tiny markdown renderer: headings, lists, fenced code, blockquote, hr,
463 * inline code/bold/italic/links. No CDN, no innerHTML with file content. */
464function renderMarkdown(text,host){
465 var box=document.createElement('div');box.className='md';
466 var lines=text.split('\n');
467 var i=0,para=[],list=null;
468
469 function flushPara(){
470 if(!para.length)return;
471 var p=document.createElement('p');
472 inline(para.join(' '),p);
473 box.appendChild(p);para=[];
474 }
475 function flushList(){
476 if(!list)return;
477 box.appendChild(list);list=null;
478 }
479 function inline(s,parent){
480 var re=/(`[^`]*`)|(\*\*[^*]+\*\*)|(\*[^*]+\*)|(\[[^\]]+\]\([^)]+\))/g;
481 var last=0,m;
482 while((m=re.exec(s))!==null){
483 if(m.index>last)parent.appendChild(document.createTextNode(s.slice(last,m.index)));
484 var tok=m[0];
485 if(tok[0]==='`'){
486 var c=document.createElement('code');c.textContent=tok.slice(1,-1);parent.appendChild(c);
487 }else if(tok.slice(0,2)==='**'){
488 var b=document.createElement('strong');b.textContent=tok.slice(2,-2);parent.appendChild(b);
489 }else if(tok[0]==='*'){
490 var em=document.createElement('em');em.textContent=tok.slice(1,-1);parent.appendChild(em);
491 }else{
492 var mm=/^\[([^\]]+)\]\(([^)]+)\)$/.exec(tok);
493 if(mm&&/^(https?:\/\/|\/|#|mailto:)/i.test(mm[2])){
494 var a=document.createElement('a');
495 a.textContent=mm[1];a.href=mm[2];
496 a.target='_blank';a.rel='noopener noreferrer';
497 parent.appendChild(a);
498 }else if(mm){
499 parent.appendChild(document.createTextNode(mm[1]));
500 }
501 }
502 last=m.index+tok.length;
503 }
504 if(last<s.length)parent.appendChild(document.createTextNode(s.slice(last)));
505 }
506
507 while(i<lines.length){
508 var line=lines[i];
509 if(/^```/.test(line)){
510 flushPara();flushList();
511 var buf=[];i++;
512 while(i<lines.length&&!/^```/.test(lines[i])){buf.push(lines[i]);i++;}
513 i++;
514 var pre=document.createElement('pre');
515 var code=document.createElement('code');code.textContent=buf.join('\n');
516 pre.appendChild(code);box.appendChild(pre);
517 continue;
518 }
519 if(/^\s*$/.test(line)){flushPara();flushList();i++;continue;}
520 var h=/^(#{1,4})\s+(.*)$/.exec(line);
521 if(h){
522 flushPara();flushList();
523 var el=document.createElement('h'+h[1].length);
524 inline(h[2],el);box.appendChild(el);i++;continue;
525 }
526 if(/^\s*([-*+]|\d+\.)\s+/.test(line)){
527 flushPara();
528 var ordered=/^\s*\d+\.\s+/.test(line);
529 if(!list||list.tagName.toLowerCase()!==(ordered?'ol':'ul')){
530 flushList();list=document.createElement(ordered?'ol':'ul');
531 }
532 var li=document.createElement('li');
533 inline(line.replace(/^\s*([-*+]|\d+\.)\s+/,''),li);
534 list.appendChild(li);i++;continue;
535 }
536 if(/^>\s?/.test(line)){
537 flushPara();flushList();
538 var bq=document.createElement('blockquote');
539 inline(line.replace(/^>\s?/,''),bq);
540 box.appendChild(bq);i++;continue;
541 }
542 if(/^(-{3,}|\*{3,}|_{3,})$/.test(line.trim())){
543 flushPara();flushList();
544 box.appendChild(document.createElement('hr'));i++;continue;
545 }
546 para.push(line);i++;
547 }
548 flushPara();flushList();
549 host.appendChild(box);
550}
551
552/* --- wiring ------------------------------------------------------- */
553
554document.getElementById('btnRefresh').onclick=function(){
555 if(curPath)openDir(curPath);else loadRoots();
556};
557document.getElementById('btnUp').onclick=function(){
558 if(curParent)openDir(curParent);
559 else if(curPath&&curRoot&&curPath!==curRoot)openDir(curRoot);
560 else toast('Already at the root');
561};
562document.getElementById('btnBack').onclick=function(){setView('browse');};
563document.getElementById('btnTheme').onclick=function(){
564 if(window.sxApplyTheme)window.sxApplyTheme(window.sxTheme&&window.sxTheme()==='dark'?'light':'dark');
565};
566btnHidden.onclick=function(){
567 showHidden=!showHidden;
568 btnHidden.classList.toggle('on',showHidden);
569 btnHidden.title=showHidden?'Hide hidden files':'Show hidden files';
570 if(curPath)openDir(curPath);
571};
572btnDownload.onclick=function(){
573 if(!curFile)return;
574 var a=document.createElement('a');
575 a.href=url('raw','path='+encodeURIComponent(curFile)+'&download=1');
576 a.rel='noopener';
577 document.body.appendChild(a);a.click();document.body.removeChild(a);
578};
579
580document.addEventListener('keydown',function(ev){
581 var tag=(ev.target&&ev.target.tagName||'').toLowerCase();
582 if(tag==='input'||tag==='textarea')return;
583 if(ev.key==='ArrowDown'||ev.key==='ArrowUp'){
584 if(!entries.length)return;
585 ev.preventDefault();
586 selIndex+=(ev.key==='ArrowDown'?1:-1);
587 if(selIndex<0)selIndex=0;
588 if(selIndex>=entries.length)selIndex=entries.length-1;
589 renderEntries();
590 }else if(ev.key==='Enter'){
591 if(selIndex>=0&&entries[selIndex]){ev.preventDefault();activate(entries[selIndex]);}
592 }else if(ev.key==='Backspace'){
593 if(curParent){ev.preventDefault();openDir(curParent);}
594 }else if(ev.key==='/'&&ev.ctrlKey===false){
595 return;
596 }
597});
598
599function loadRoots(){
600 return api('roots').then(function(d){
601 roots=(d&&d.roots)||[];
602 renderRoots();
603 /* Wide screens open the first root straight away; a phone keeps the root
604 * list on screen, because a hidden sidebar there means an empty page. */
605 if(roots.length&&!curPath&&window.matchMedia('(min-width:821px)').matches){
606 openDir(roots[0].path);
607 }
608 }).catch(function(e){toast('Cannot list roots: '+e.message);});
609}
610
611loadRoots();
612})();
613</script>
614</body></html>