@ngockhoale/ckit 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/src/gui/file-index.d.ts +8 -0
- package/dist/src/gui/file-index.js +65 -0
- package/dist/src/gui/server.d.ts +7 -0
- package/dist/src/gui/server.js +50 -5
- package/dist/src/gui/workspace-files.d.ts +35 -0
- package/dist/src/gui/workspace-files.js +278 -0
- package/dist/src/team/engine.js +19 -1
- package/dist/src/team/types.d.ts +3 -2
- package/gui/app.css +61 -2
- package/gui/app.js +91 -12
- package/gui/explorer.js +368 -0
- package/gui/index.html +3 -3
- package/package.json +1 -1
package/gui/explorer.js
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/* Workspace explorer popup: browse, preview, upload, create folders, rename, move to trash; polls the open folder. Vanilla JS, loaded after app.js. */
|
|
2
|
+
(function () {
|
|
3
|
+
'use strict';
|
|
4
|
+
var ck = window.__ck && window.__ck.api; if (!ck) return;
|
|
5
|
+
var h0 = ck.h, TOKEN = ck.token, S = ck.S, $ = function (id) { return document.getElementById(id); };
|
|
6
|
+
/* ck.h sets unknown keys as properties; data-* / aria-* / role must be real attributes */
|
|
7
|
+
function h(tag, attrs, kids) {
|
|
8
|
+
var plain = {}, special = {};
|
|
9
|
+
Object.keys(attrs || {}).forEach(function (k) { if (/^(data-|aria-)/.test(k) || k === 'role') special[k] = attrs[k]; else plain[k] = attrs[k]; });
|
|
10
|
+
var e = h0(tag, plain, kids); Object.keys(special).forEach(function (k) { e.setAttribute(k, special[k]); }); return e;
|
|
11
|
+
}
|
|
12
|
+
var EX = null, POLL_MS = 2000, PREVIEW_TEXT_MAX = 2 * 1048576;
|
|
13
|
+
var IMG = /\.(png|jpe?g|gif|webp)$/i, DOC = /\.(pdf|docx?|xlsx?|pptx?|md|markdown|txt|csv|drawio|rtf)$/i, CODE = /\.(m?[jt]sx?|cjs|json|ya?ml|toml|html?|css|scss|py|rb|go|rs|java|kt|c|h|cpp|cs|php|sh|sql|xml|svg|vue)$/i;
|
|
14
|
+
var TEXTY = /\.(txt|md|markdown|json|jsonl|ya?ml|toml|ini|cfg|conf|csv|tsv|log|xml|html?|svg|css|scss|less|m?[jt]sx?|cjs|py|rb|go|rs|java|kt|c|h|cpp|hpp|cs|php|sh|zsh|bat|sql|vue|svelte|drawio|gitignore|editorconfig|lock)$/i;
|
|
15
|
+
|
|
16
|
+
function kindOf(e) { return e.dir ? 'dir' : IMG.test(e.name) ? 'img' : DOC.test(e.name) ? 'doc' : CODE.test(e.name) ? 'code' : 'oth'; }
|
|
17
|
+
var GLYPH = {dir: '▸', img: '◩', doc: '≡', code: '</>', oth: '·'};
|
|
18
|
+
function size(n) { return n >= 1073741824 ? (n / 1073741824).toFixed(1) + ' GB' : n >= 1048576 ? (n / 1048576).toFixed(1) + ' MB' : n >= 1024 ? Math.round(n / 1024) + ' KB' : n + ' B'; }
|
|
19
|
+
function ago(ms) {
|
|
20
|
+
var s = Math.max(0, Math.round((Date.now() - ms) / 1000));
|
|
21
|
+
if (s < 10) return 'just now'; if (s < 60) return s + ' sec ago'; var m = Math.round(s / 60); if (m < 60) return m + ' min ago';
|
|
22
|
+
var hr = Math.round(m / 60); if (hr < 24) return hr + ' hr ago'; var d = Math.round(hr / 24); if (d < 30) return d + (d === 1 ? ' day ago' : ' days ago');
|
|
23
|
+
return new Date(ms).toLocaleDateString();
|
|
24
|
+
}
|
|
25
|
+
var join = function (a, b) { return a ? a + '/' + b : b; };
|
|
26
|
+
var parentOf = function (p) { var i = p.lastIndexOf('/'); return i < 0 ? '' : p.slice(0, i); };
|
|
27
|
+
var wsName = function () { return (S.projectRoot || '').split(/[\\/]/).filter(Boolean).pop() || 'Workspace'; };
|
|
28
|
+
var fileUrl = function (path, dl) { return '/api/fs/file?path=' + encodeURIComponent(path) + (dl ? '&download=1' : '') + '&t=' + encodeURIComponent(TOKEN); };
|
|
29
|
+
|
|
30
|
+
function fs(method, path, body) {
|
|
31
|
+
return fetch(path, {method: method, headers: {'x-ckit-token': TOKEN, 'content-type': 'application/json'}, body: body === undefined ? undefined : JSON.stringify(body)})
|
|
32
|
+
.then(function (r) { return r.json().catch(function () { return {}; }).then(function (j) { return {status: r.status, body: j}; }); }, function () { return {status: 0, body: {error: 'Cannot reach CKit; check that ckit is still running.'}}; });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* confirm dialog, same look as askText */
|
|
36
|
+
function askConfirm(o) {
|
|
37
|
+
return new Promise(function (done) {
|
|
38
|
+
var close = function (v) { layer.remove(); done(v); };
|
|
39
|
+
var okb = h('button', {type: 'button', class: 'primary', text: o.okText, onclick: function () { close(true); }});
|
|
40
|
+
var card = h('div', {class: 'askcard'}, [h('header', {}, [h('b', {text: o.title}), h('button', {class: 'askx', type: 'button', text: '✕', title: 'Cancel', onclick: function () { close(false); }})]),
|
|
41
|
+
h('div', {class: 'askbody'}, [h('p', {style: 'margin:4px 0 8px;font-size:13px;color:var(--tx)', text: o.text}), o.hint ? h('p', {text: o.hint}) : null]),
|
|
42
|
+
h('footer', {}, [h('span', {class: 'askkeys', text: 'Enter to confirm · Esc to cancel'}), h('button', {type: 'button', text: 'Cancel', onclick: function () { close(false); }}), okb])]);
|
|
43
|
+
card.setAttribute('role', 'dialog'); card.setAttribute('aria-modal', 'true');
|
|
44
|
+
var layer = h('div', {class: 'asklayer'}, [card]);
|
|
45
|
+
layer.addEventListener('mousedown', function (e) { if (e.target === layer) close(false); });
|
|
46
|
+
layer.addEventListener('keydown', function (e) { if (e.key === 'Escape') { e.stopPropagation(); close(false); } else if (e.key === 'Enter') { e.preventDefault(); e.stopPropagation(); close(true); } });
|
|
47
|
+
document.body.appendChild(layer); okb.focus();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* ---------- open / close ---------- */
|
|
52
|
+
function open(startPath) {
|
|
53
|
+
if (EX) return;
|
|
54
|
+
EX = {path: startPath || '', entries: [], sig: '', sel: [], anchor: null, sort: {k: 'name', dir: 1}, filter: '', base: {}, hist: {}, flash: {}, uploads: [], prev: null, prevTok: 0, busy: false, err: '', opened: Date.now(), timer: 0, dropDir: null, rootNames: null, loaded: false};
|
|
55
|
+
var up = h('input', {type: 'file', multiple: true, hidden: true}); up.addEventListener('change', function () { upload(up.files, EX.path); up.value = ''; });
|
|
56
|
+
EX.el = {};
|
|
57
|
+
var E = EX.el;
|
|
58
|
+
E.bc = h('div', {class: 'exbc'}); E.filter = h('input', {class: 'exfilter', placeholder: 'Filter this folder…', spellcheck: false, autocomplete: 'off', 'aria-label': 'Filter files in this folder'});
|
|
59
|
+
E.filter.addEventListener('input', function () { EX.filter = E.filter.value.trim().toLowerCase(); renderList(); });
|
|
60
|
+
E.btnRef = h('button', {type: 'button', class: 'exref', text: '+ References folder', title: 'Files you put here stay in the project; point agents at them with @references/…', onclick: makeReferences});
|
|
61
|
+
E.tools = h('div', {class: 'extool'}, [h('button', {type: 'button', class: 'exup', title: 'Go up one folder (Backspace)', text: '↑', onclick: goUp}), E.bc, E.filter,
|
|
62
|
+
h('button', {type: 'button', text: '+ New folder', onclick: newFolder}), h('button', {type: 'button', class: 'primary', text: '⬆ Upload', onclick: function () { up.click(); }}), E.btnRef, up]);
|
|
63
|
+
E.head = h('div', {class: 'exrow exhead'}, [h('span'), sortHead('name', 'Name'), sortHead('size', 'Size'), sortHead('mtime', 'Modified'), h('span')]);
|
|
64
|
+
E.rows = h('div', {class: 'exrows', tabIndex: 0, role: 'listbox', 'aria-label': 'Files and folders'});
|
|
65
|
+
E.rows.addEventListener('click', function (ev) { if (ev.target === E.rows) { EX.sel = []; syncSel(); } });
|
|
66
|
+
E.empty = h('div', {class: 'exempty'});
|
|
67
|
+
E.list = h('div', {class: 'exlist'}, [E.head, E.rows, E.empty, h('div', {class: 'exdrop', text: 'Drop to upload here'})]);
|
|
68
|
+
E.prev = h('aside', {class: 'exprev'});
|
|
69
|
+
E.ups = h('div', {class: 'exups'});
|
|
70
|
+
E.status = h('span', {class: 'exstat'}); E.hint = h('span', {class: 'exhint', text: 'Files in references/ stay in the project; point agents at them with @references/…'});
|
|
71
|
+
E.live = h('span', {class: 'exlive', title: 'This list refreshes by itself every 2 seconds'}, [h('i'), 'live']);
|
|
72
|
+
var dlg = h('div', {class: 'dlg wide exdlg', role: 'dialog', 'aria-modal': 'true', 'aria-label': 'Workspace files'}, [
|
|
73
|
+
h('header', {}, [h('b', {text: 'WORKSPACE FILES'}), h('span', {class: 'exroot mono', title: S.projectRoot || '', text: '' + (S.projectRoot || '')}), h('button', {type: 'button', class: 'excopy', text: 'Copy path', onclick: copyPath}), E.live, h('button', {type: 'button', text: '✕', title: 'Close (Esc)', onclick: close})]),
|
|
74
|
+
E.tools, h('div', {class: 'exmain'}, [E.list, E.prev]), E.ups, h('footer', {class: 'exfoot'}, [E.status, E.hint])]);
|
|
75
|
+
E.dlg = dlg; ck.openModal(dlg);
|
|
76
|
+
EX.obs = new MutationObserver(function () { if (EX && (M.hidden || !M.contains(E.dlg))) close(); }); EX.obs.observe(M, {childList: true, attributes: true, attributeFilter: ['hidden']});
|
|
77
|
+
document.addEventListener('keydown', onKey, true);
|
|
78
|
+
document.addEventListener('visibilitychange', onVis);
|
|
79
|
+
EX.timer = setInterval(poll, POLL_MS);
|
|
80
|
+
renderPrev(); load(EX.path, true);
|
|
81
|
+
setTimeout(function () { if (EX) E.rows.focus(); }, 30);
|
|
82
|
+
}
|
|
83
|
+
function close() {
|
|
84
|
+
if (!EX) return;
|
|
85
|
+
EX.obs.disconnect(); clearInterval(EX.timer); document.removeEventListener('keydown', onKey, true); document.removeEventListener('visibilitychange', onVis);
|
|
86
|
+
if (EX.prevUrl) URL.revokeObjectURL(EX.prevUrl);
|
|
87
|
+
EX = null; ck.closeModal();
|
|
88
|
+
}
|
|
89
|
+
function copyPath() { if (navigator.clipboard) navigator.clipboard.writeText(S.projectRoot || '').then(function () { ck.toast('Folder path copied'); }, function () {}); }
|
|
90
|
+
|
|
91
|
+
/* ---------- data ---------- */
|
|
92
|
+
function load(path, first) {
|
|
93
|
+
if (!EX) return; var my = EX; my.busy = true;
|
|
94
|
+
return fs('GET', '/api/fs/list?path=' + encodeURIComponent(path)).then(function (r) {
|
|
95
|
+
if (EX !== my) return; my.busy = false;
|
|
96
|
+
if (r.status === 200) { my.err = ''; if (path !== my.path || first) { my.path = path; } apply(r.body, true); return; }
|
|
97
|
+
if (r.status === 404 && path) { ck.toast('That folder is gone; showing its parent.'); return load(parentOf(path)); }
|
|
98
|
+
my.err = r.body.error || 'Could not read this folder.'; if (my.loaded) { setStatus(); } else { renderList(); }
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function poll() {
|
|
102
|
+
var my = EX; if (!my || my.busy || document.hidden || my.dragging) return; my.busy = true;
|
|
103
|
+
fs('GET', '/api/fs/list?path=' + encodeURIComponent(my.path)).then(function (r) {
|
|
104
|
+
if (EX !== my) return; my.busy = false;
|
|
105
|
+
if (r.status === 200) { my.err = ''; if (r.body.sig !== my.sig || r.body.path !== my.path) apply(r.body, false); else refreshAges(); setStatus(); }
|
|
106
|
+
else if (r.status === 404 && my.path) { ck.toast('That folder is gone; showing its parent.'); load(parentOf(my.path)); }
|
|
107
|
+
else { my.err = r.body.error || 'Connection lost; retrying…'; setStatus(); }
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function onVis() { if (!document.hidden) poll(); }
|
|
111
|
+
function apply(body, navigated) {
|
|
112
|
+
var prevNames = {}; EX.entries.forEach(function (e) { prevNames[e.name] = e; });
|
|
113
|
+
var key = body.path, base = EX.base[key];
|
|
114
|
+
if (!base) { base = EX.base[key] = {}; body.entries.forEach(function (e) { base[e.name] = e.mtime; }); }
|
|
115
|
+
var fl = EX.flash[key] || (EX.flash[key] = {});
|
|
116
|
+
body.entries.forEach(function (e) {
|
|
117
|
+
if (!(e.name in base)) { fl[e.name] = 'new'; }
|
|
118
|
+
else if (base[e.name] !== e.mtime && fl[e.name] !== 'new') { fl[e.name] = 'changed'; }
|
|
119
|
+
});
|
|
120
|
+
var fresh = {};
|
|
121
|
+
if (!navigated || EX.loaded) body.entries.forEach(function (e) { var p = prevNames[e.name]; if (EX.loaded && EX.lastPath === key && (!p || p.mtime !== e.mtime)) fresh[e.name] = 1; });
|
|
122
|
+
EX.entries = body.entries; EX.sig = body.sig; EX.truncated = body.truncated; EX.pulse = fresh;
|
|
123
|
+
var pathChanged = EX.lastPath !== key; EX.lastPath = key; EX.loaded = true;
|
|
124
|
+
var keep = EX.sel.filter(function (n) { return body.entries.some(function (e) { return e.name === n; }); }); if (pathChanged) keep = []; EX.sel = keep;
|
|
125
|
+
if (pathChanged) EX.el.filter.value = '', EX.filter = '';
|
|
126
|
+
renderBc(); renderList(pathChanged); syncSel(); renderRefBtn(); if (EX.prev && EX.prev.path && !body.entries.some(function (e) { return join(key, e.name) === EX.prev.path; }) && parentOf(EX.prev.path) === key) { EX.prev = null; renderPrev(); } else if (EX.prev && EX.prev.path && parentOf(EX.prev.path) === key) { var pe = body.entries.filter(function (e) { return join(key, e.name) === EX.prev.path; })[0]; if (pe && pe.mtime !== EX.prev.mtime) showPreview(pe); }
|
|
127
|
+
}
|
|
128
|
+
function renderRefBtn() {
|
|
129
|
+
var E = EX.el; if (EX.path === '') EX.rootNames = EX.entries.map(function (e) { return e.name; });
|
|
130
|
+
E.btnRef.hidden = !(EX.rootNames && EX.rootNames.indexOf('references') < 0);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/* ---------- rendering ---------- */
|
|
134
|
+
function sortHead(k, label) {
|
|
135
|
+
return h('button', {type: 'button', class: 'exsort', 'data-k': k, text: label, title: 'Sort by ' + label.toLowerCase(), onclick: function () { if (EX.sort.k === k) EX.sort.dir = -EX.sort.dir; else EX.sort = {k: k, dir: k === 'mtime' ? -1 : 1}; renderList(); }});
|
|
136
|
+
}
|
|
137
|
+
function renderBc() {
|
|
138
|
+
var E = EX.el; E.bc.innerHTML = '';
|
|
139
|
+
var segs = EX.path ? EX.path.split('/') : [], acc = '';
|
|
140
|
+
var mk = function (label, p, last) {
|
|
141
|
+
var b = h('button', {type: 'button', class: 'excrumb' + (last ? ' cur' : ''), text: label, title: p || S.projectRoot || '', onclick: function () { go(p); }});
|
|
142
|
+
b.addEventListener('dragover', function (ev) { if (ev.dataTransfer && Array.prototype.indexOf.call(ev.dataTransfer.types || [], 'application/x-ckit-move') >= 0 && !last) { ev.preventDefault(); b.classList.add('dropto'); } });
|
|
143
|
+
b.addEventListener('dragleave', function () { b.classList.remove('dropto'); });
|
|
144
|
+
b.addEventListener('drop', function (ev) { b.classList.remove('dropto'); var mv = ev.dataTransfer.getData('application/x-ckit-move'); if (mv) { ev.preventDefault(); ev.stopPropagation(); moveInto(JSON.parse(mv), p); } });
|
|
145
|
+
return b;
|
|
146
|
+
};
|
|
147
|
+
E.bc.appendChild(mk(wsName(), '', segs.length === 0));
|
|
148
|
+
segs.forEach(function (s, i) { acc = join(acc, s); E.bc.appendChild(h('span', {class: 'exsep', text: '›'})); E.bc.appendChild(mk(s, acc, i === segs.length - 1)); });
|
|
149
|
+
}
|
|
150
|
+
function visible() {
|
|
151
|
+
var f = EX.filter, k = EX.sort.k, d = EX.sort.dir;
|
|
152
|
+
var a = EX.entries.filter(function (e) { return !f || e.name.toLowerCase().indexOf(f) >= 0; });
|
|
153
|
+
a.sort(function (x, y) {
|
|
154
|
+
if (x.dir !== y.dir) return x.dir ? -1 : 1;
|
|
155
|
+
var c = k === 'size' ? x.size - y.size : k === 'mtime' ? x.mtime - y.mtime : x.name.localeCompare(y.name, undefined, {numeric: true, sensitivity: 'base'});
|
|
156
|
+
return (c || x.name.localeCompare(y.name, undefined, {numeric: true})) * (k === 'name' || c ? d : 1);
|
|
157
|
+
});
|
|
158
|
+
return a;
|
|
159
|
+
}
|
|
160
|
+
function badgeFor(e) {
|
|
161
|
+
var f = (EX.flash[EX.path] || {})[e.name];
|
|
162
|
+
if (f === 'new') return h('span', {class: 'exbadge new', text: 'NEW', title: 'Appeared since you opened this window'});
|
|
163
|
+
if (f === 'changed') return h('span', {class: 'exbadge chg', text: 'CHANGED', title: 'Changed since you opened this window'});
|
|
164
|
+
if (S.run && S.run.running && S.started && e.mtime >= S.started && !e.dir) return h('span', {class: 'exbadge run', text: 'THIS RUN', title: 'Written after the current run started'});
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
function renderList(resetScroll) {
|
|
168
|
+
var E = EX.el, top = E.rows.scrollTop, list = visible(), focus = document.activeElement && E.rows.contains(document.activeElement);
|
|
169
|
+
Array.prototype.forEach.call(E.head.querySelectorAll('.exsort'), function (b) { var on = b.getAttribute('data-k') === EX.sort.k; b.classList.toggle('on', on); b.setAttribute('data-arrow', on ? (EX.sort.dir > 0 ? '▲' : '▼') : ''); });
|
|
170
|
+
var frag = document.createDocumentFragment();
|
|
171
|
+
list.forEach(function (e) { frag.appendChild(row(e)); });
|
|
172
|
+
E.rows.innerHTML = ''; E.rows.appendChild(frag); E.rows.scrollTop = resetScroll ? 0 : top;
|
|
173
|
+
var none = !list.length;
|
|
174
|
+
E.empty.hidden = !none; E.head.hidden = none && !EX.entries.length && !EX.err;
|
|
175
|
+
if (none) {
|
|
176
|
+
E.empty.innerHTML = '';
|
|
177
|
+
if (EX.err) E.empty.appendChild(h('div', {class: 'exetitle err', text: EX.err}));
|
|
178
|
+
else if (EX.entries.length) E.empty.appendChild(h('div', {class: 'exetitle', text: 'Nothing here matches “' + EX.el.filter.value + '”.'}));
|
|
179
|
+
else { E.empty.appendChild(h('div', {class: 'exebig', text: '▸'})); E.empty.appendChild(h('div', {class: 'exetitle', text: 'This folder is empty'})); E.empty.appendChild(h('div', {class: 'exesub', text: 'Drag files here from your computer, or press Upload. Files the agents create will show up here by themselves.'})); }
|
|
180
|
+
}
|
|
181
|
+
syncSel(); setStatus();
|
|
182
|
+
if (focus) E.rows.focus();
|
|
183
|
+
}
|
|
184
|
+
function actBtn(txt, title, fn) { var b = h('button', {type: 'button', class: 'exact', text: txt, title: title, tabIndex: -1}); b.addEventListener('click', function (ev) { ev.stopPropagation(); fn(); }); b.addEventListener('dblclick', function (ev) { ev.stopPropagation(); }); return b; }
|
|
185
|
+
function row(e) {
|
|
186
|
+
var k = kindOf(e), pulse = EX.pulse && EX.pulse[e.name];
|
|
187
|
+
var r = h('div', {class: 'exrow exitem' + (pulse ? ' pulse' : ''), role: 'option', 'data-name': e.name, draggable: true, title: e.name}, [
|
|
188
|
+
h('span', {class: 'exic ' + k, text: GLYPH[k]}), h('span', {class: 'exname'}, [h('span', {class: 'exn', text: e.name}), badgeFor(e)]),
|
|
189
|
+
h('span', {class: 'exsize', text: e.dir ? '—' : size(e.size)}), h('span', {class: 'exmod', 'data-m': e.mtime, text: ago(e.mtime)}),
|
|
190
|
+
h('span', {class: 'exacts'}, [e.dir ? null : actBtn('⬇', 'Download', function () { download(e); }), actBtn('@', 'Mention in request', function () { mention(e); }), actBtn('✎', 'Rename (F2)', function () { rename(e); }), actBtn('🗑', 'Move to trash (Delete, or ⌘/Ctrl+Backspace)', function () { trash([e]); })])]);
|
|
191
|
+
r.addEventListener('click', function (ev) { select(e.name, ev); EX.el.rows.focus(); });
|
|
192
|
+
r.addEventListener('dblclick', function () { openItem(e); });
|
|
193
|
+
r.addEventListener('dragstart', function (ev) { if (EX.sel.indexOf(e.name) < 0) { EX.sel = [e.name]; syncSel(); } ev.dataTransfer.setData('application/x-ckit-move', JSON.stringify(EX.sel.map(function (n) { return join(EX.path, n); }))); ev.dataTransfer.effectAllowed = 'move'; EX.dragging = true; });
|
|
194
|
+
r.addEventListener('dragend', function () { EX.dragging = false; });
|
|
195
|
+
return r;
|
|
196
|
+
}
|
|
197
|
+
function refreshAges() { Array.prototype.forEach.call(EX.el.rows.querySelectorAll('.exmod'), function (c) { var t = ago(Number(c.getAttribute('data-m'))); if (c.textContent !== t) c.textContent = t; }); }
|
|
198
|
+
function setStatus() {
|
|
199
|
+
var E = EX.el, dirs = EX.entries.filter(function (e) { return e.dir; }).length, files = EX.entries.length - dirs;
|
|
200
|
+
var sel = EX.sel.length ? ' · ' + EX.sel.length + ' selected' : '';
|
|
201
|
+
E.status.textContent = EX.err ? EX.err : (dirs + (dirs === 1 ? ' folder' : ' folders') + ', ' + files + (files === 1 ? ' file' : ' files') + sel + (EX.truncated ? ' · showing the first 5000 items' : ''));
|
|
202
|
+
E.status.classList.toggle('err', !!EX.err); E.live.classList.toggle('off', !!EX.err);
|
|
203
|
+
}
|
|
204
|
+
function byName(n) { return EX.entries.filter(function (e) { return e.name === n; })[0]; }
|
|
205
|
+
function syncSel() {
|
|
206
|
+
Array.prototype.forEach.call(EX.el.rows.children, function (r) { var on = EX.sel.indexOf(r.getAttribute('data-name')) >= 0; r.classList.toggle('sel', on); r.setAttribute('aria-selected', on ? 'true' : 'false'); });
|
|
207
|
+
setStatus();
|
|
208
|
+
}
|
|
209
|
+
function select(name, ev) {
|
|
210
|
+
var names = visible().map(function (e) { return e.name; });
|
|
211
|
+
if (ev && ev.shiftKey && EX.anchor && names.indexOf(EX.anchor) >= 0) { var a = names.indexOf(EX.anchor), b = names.indexOf(name); EX.sel = names.slice(Math.min(a, b), Math.max(a, b) + 1); }
|
|
212
|
+
else if (ev && (ev.metaKey || ev.ctrlKey)) { var i = EX.sel.indexOf(name); if (i >= 0) EX.sel.splice(i, 1); else EX.sel.push(name); EX.anchor = name; }
|
|
213
|
+
else { EX.sel = [name]; EX.anchor = name; }
|
|
214
|
+
syncSel();
|
|
215
|
+
var one = EX.sel.length === 1 ? byName(EX.sel[0]) : null; if (one && !one.dir) showPreview(one); else if (!one && EX.prev) { EX.prev = null; renderPrev(); } else if (one && one.dir && EX.prev) { EX.prev = null; renderPrev(); }
|
|
216
|
+
var r = EX.el.rows.querySelector('[data-name="' + (window.CSS && CSS.escape ? CSS.escape(name) : name.replace(/"/g, '\\"')) + '"]'); if (r && r.scrollIntoView) r.scrollIntoView({block: 'nearest'});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/* ---------- preview ---------- */
|
|
220
|
+
function renderPrev() {
|
|
221
|
+
var E = EX.el, p = EX.prev; E.prev.innerHTML = ''; E.dlg.classList.toggle('hasprev', !!p);
|
|
222
|
+
if (EX.prevUrl) { URL.revokeObjectURL(EX.prevUrl); EX.prevUrl = null; }
|
|
223
|
+
if (!p) { E.prev.appendChild(h('div', {class: 'exnoprev'}, [h('div', {class: 'exebig', text: '≡'}), h('div', {text: 'Select a file to preview it here.'}), h('div', {class: 'exesub', text: 'Double-click a folder to open it.'})])); return; }
|
|
224
|
+
var path = p.path;
|
|
225
|
+
E.prev.appendChild(h('div', {class: 'exph'}, [h('b', {class: 'exphn', text: p.name, title: path}), h('button', {type: 'button', class: 'askx', text: '✕', title: 'Close preview (Esc)', onclick: closePrev})]));
|
|
226
|
+
E.prev.appendChild(h('div', {class: 'exmeta'}, [h('span', {text: size(p.size)}), h('span', {text: 'modified ' + ago(p.mtime)})]));
|
|
227
|
+
E.prev.appendChild(h('div', {class: 'exbtns'}, [h('a', {class: 'btn', href: fileUrl(path, true), text: '⬇ Download', download: p.name}), h('button', {type: 'button', text: '@ Mention in request', onclick: function () { mention(p); }})]));
|
|
228
|
+
E.prevBody = h('div', {class: 'exbody'}); E.prev.appendChild(E.prevBody);
|
|
229
|
+
}
|
|
230
|
+
function closePrev() { EX.prev = null; EX.prevTok++; renderPrev(); }
|
|
231
|
+
function showPreview(e) {
|
|
232
|
+
var path = join(EX.path, e.name), tok = ++EX.prevTok; EX.prev = {path: path, name: e.name, size: e.size, mtime: e.mtime}; renderPrev();
|
|
233
|
+
var body = EX.el.prevBody, other = function (msg) { body.innerHTML = ''; body.appendChild(h('div', {class: 'exnoprev'}, [h('div', {class: 'exebig', text: GLYPH[kindOf(e)]}), h('div', {text: msg})])); };
|
|
234
|
+
if (IMG.test(e.name)) {
|
|
235
|
+
if (e.size > 30 * 1048576) return other('This image is large (' + size(e.size) + '). Use Download to open it.');
|
|
236
|
+
body.appendChild(h('div', {class: 'exload', text: 'Loading…'}));
|
|
237
|
+
fetch(fileUrl(path)).then(function (r) { return r.ok ? r.blob() : Promise.reject(); }).then(function (b) { if (!EX || EX.prevTok !== tok) return; EX.prevUrl = URL.createObjectURL(b); body.innerHTML = ''; body.appendChild(h('img', {class: 'eximg', src: EX.prevUrl, alt: e.name})); }, function () { if (EX && EX.prevTok === tok) other('Could not load this image.'); });
|
|
238
|
+
} else if (/\.pdf$/i.test(e.name)) {
|
|
239
|
+
body.appendChild(h('div', {class: 'exnoprev'}, [h('div', {class: 'exebig', text: '≡'}), h('div', {text: 'PDF · ' + size(e.size)}), h('a', {class: 'btn primary', href: fileUrl(path), target: '_blank', rel: 'noopener', text: 'Open in a new tab'})]));
|
|
240
|
+
} else if (TEXTY.test(e.name) || (!/\./.test(e.name.slice(1)) && e.size < PREVIEW_TEXT_MAX)) {
|
|
241
|
+
if (e.size > PREVIEW_TEXT_MAX) return other('This file is large (' + size(e.size) + '). Use Download to read it.');
|
|
242
|
+
body.appendChild(h('div', {class: 'exload', text: 'Loading…'}));
|
|
243
|
+
fetch(fileUrl(path)).then(function (r) { return r.ok ? r.text() : Promise.reject(); }).then(function (t) { if (!EX || EX.prevTok !== tok) return; body.innerHTML = ''; body.appendChild(h('pre', {class: 'extext', text: t.length ? t : '(This file is empty.)'})); }, function () { if (EX && EX.prevTok === tok) other('No preview for this file. Use Download.'); });
|
|
244
|
+
} else other('No preview for this type of file (' + size(e.size) + '). Use Download to open it.');
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/* ---------- actions ---------- */
|
|
248
|
+
function go(path) { if (!EX) return; EX.prev = null; EX.sel = []; renderPrev(); load(path); }
|
|
249
|
+
function goUp() { if (EX.path) go(parentOf(EX.path)); }
|
|
250
|
+
function openItem(e) { if (e.dir) go(join(EX.path, e.name)); else showPreview(e); }
|
|
251
|
+
function download(e) { var a = h('a', {href: fileUrl(join(EX.path, e.name), true), download: e.name}); document.body.appendChild(a); a.click(); a.remove(); }
|
|
252
|
+
function mention(e) {
|
|
253
|
+
var path = join(EX.path, e.name) + (e.dir ? '/' : ''), msg = $('msg'), txt = '@' + (/\s/.test(path) ? '"' + path + '"' : path) + ' ';
|
|
254
|
+
var v = msg.value; msg.value = v + (v && !/\s$/.test(v) ? ' ' : '') + txt; close(); msg.focus(); msg.setSelectionRange(msg.value.length, msg.value.length); msg.dispatchEvent(new Event('input', {bubbles: true}));
|
|
255
|
+
}
|
|
256
|
+
function nameCheck(v) { return /[\\/]/.test(v) ? 'A name cannot contain slashes.' : /[\u0000-\u001f]/.test(v) ? 'Remove the hidden characters from the name.' : v === '.' || v === '..' ? 'Choose a different name.' : ''; }
|
|
257
|
+
function newFolder() {
|
|
258
|
+
ck.askText({title: 'New folder', label: 'Folder name', placeholder: 'e.g. references', okText: 'Create folder', hint: 'It is created inside “' + (EX.path || wsName()) + '”.', check: nameCheck}).then(function (n) {
|
|
259
|
+
if (!n || !EX) return; var p = join(EX.path, n);
|
|
260
|
+
fs('POST', '/api/fs/mkdir', {path: p}).then(function (r) { if (r.status !== 200) return ck.toast(r.body.error || 'Could not create the folder.'); ck.toast('Folder created'); load(EX.path).then(function () { if (EX) { EX.sel = [n]; syncSel(); } }); });
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function makeReferences() {
|
|
264
|
+
fs('POST', '/api/fs/mkdir', {path: 'references'}).then(function (r) { if (r.status !== 200 && r.status !== 409) return ck.toast(r.body.error || 'Could not create the folder.'); if (EX) { EX.rootNames = (EX.rootNames || []).concat('references'); go('references'); } });
|
|
265
|
+
}
|
|
266
|
+
function rename(e) {
|
|
267
|
+
ck.askText({title: 'Rename', label: 'New name', value: e.name, okText: 'Rename', check: function (v) { return nameCheck(v) || (v === e.name ? 'That is the current name; type a new one.' : ''); }}).then(function (n) {
|
|
268
|
+
if (!n || !EX) return; var base = EX.path;
|
|
269
|
+
fs('POST', '/api/fs/rename', {from: join(base, e.name), to: join(base, n)}).then(function (r) { if (r.status !== 200) return ck.toast(r.body.error || 'Could not rename.'); if (EX) { EX.prev = null; renderPrev(); load(EX.path).then(function () { if (EX) { EX.sel = [n]; syncSel(); } }); } });
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
function moveInto(paths, dest) {
|
|
273
|
+
var jobs = paths.map(function (p) { var n = p.slice(p.lastIndexOf('/') + 1); return {from: p, to: join(dest, n)}; }).filter(function (j) { return j.from !== j.to && parentOf(j.from) !== dest; });
|
|
274
|
+
if (!jobs.length) return;
|
|
275
|
+
Promise.all(jobs.map(function (j) { return fs('POST', '/api/fs/rename', j); })).then(function (rs) {
|
|
276
|
+
var bad = rs.filter(function (r) { return r.status !== 200; })[0]; if (bad) ck.toast(bad.body.error || 'Could not move.'); else ck.toast('Moved to ' + (dest || wsName()));
|
|
277
|
+
if (EX) { EX.sel = []; EX.prev = null; renderPrev(); load(EX.path); }
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
function trash(items) {
|
|
281
|
+
if (!items.length) return;
|
|
282
|
+
var one = items.length === 1, label = one ? '“' + items[0].name + '”' : items.length + ' items';
|
|
283
|
+
askConfirm({title: 'Move to trash', text: 'Move ' + label + ' to the trash?', hint: 'Nothing is deleted for good: the ' + (one ? (items[0].dir ? 'folder' : 'file') : 'items') + ' will be kept in the .ckit/trash folder of this workspace, where you can get ' + (one ? 'it' : 'them') + ' back.', okText: 'Move to trash'}).then(function (ok) {
|
|
284
|
+
if (!ok || !EX) return; var base = EX.path;
|
|
285
|
+
Promise.all(items.map(function (e) { return fs('POST', '/api/fs/delete', {path: join(base, e.name)}); })).then(function (rs) {
|
|
286
|
+
var bad = rs.filter(function (r) { return r.status !== 200; })[0]; ck.toast(bad ? (bad.body.error || 'Could not move to trash.') : 'Moved to trash (.ckit/trash). Nothing was deleted for good.');
|
|
287
|
+
if (EX) { EX.sel = []; EX.prev = null; renderPrev(); load(EX.path); EX.el.rows.focus(); }
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/* ---------- upload ---------- */
|
|
293
|
+
function upload(files, dir) {
|
|
294
|
+
Array.prototype.forEach.call(files || [], function (f) {
|
|
295
|
+
var u = {name: f.name || 'file', size: f.size, pct: 0, state: 'up', dir: dir};
|
|
296
|
+
EX.uploads.push(u); renderUps();
|
|
297
|
+
var x = new XMLHttpRequest(), my = EX;
|
|
298
|
+
x.open('POST', '/api/fs/upload?path=' + encodeURIComponent(dir) + '&name=' + encodeURIComponent(u.name)); x.setRequestHeader('x-ckit-token', TOKEN); x.setRequestHeader('content-type', 'application/octet-stream');
|
|
299
|
+
x.upload.onprogress = function (ev) { if (ev.lengthComputable) { u.pct = Math.round(ev.loaded / ev.total * 100); renderUps(); } };
|
|
300
|
+
x.onload = function () {
|
|
301
|
+
var j = {}; try { j = JSON.parse(x.responseText); } catch (e) { /* not json */ }
|
|
302
|
+
if (x.status === 200) { u.state = 'ok'; u.pct = 100; u.final = j.name; u.renamed = j.renamed; } else { u.state = 'bad'; u.error = j.error || 'Upload failed.'; }
|
|
303
|
+
finishUp(my, u);
|
|
304
|
+
};
|
|
305
|
+
x.onerror = function () { u.state = 'bad'; u.error = 'Cannot reach CKit; check that ckit is still running.'; finishUp(my, u); };
|
|
306
|
+
x.send(f);
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
function finishUp(my, u) {
|
|
310
|
+
if (EX !== my) return; renderUps();
|
|
311
|
+
if (u.state === 'ok') { setTimeout(function () { if (EX === my) { my.uploads = my.uploads.filter(function (x) { return x !== u; }); renderUps(); } }, 6000); if (u.dir === my.path) load(my.path); }
|
|
312
|
+
}
|
|
313
|
+
function renderUps() {
|
|
314
|
+
var box = EX.el.ups; box.innerHTML = ''; box.hidden = !EX.uploads.length;
|
|
315
|
+
EX.uploads.forEach(function (u) {
|
|
316
|
+
var st = u.state === 'bad' ? u.error : u.state === 'ok' ? (u.renamed ? 'saved as “' + u.final + '”' : 'done') : u.pct + '%';
|
|
317
|
+
box.appendChild(h('div', {class: 'exu ' + u.state}, [h('span', {class: 'exun', text: u.name, title: u.name}), h('span', {class: 'exubar'}, [h('i', {style: 'width:' + u.pct + '%'})]), h('span', {class: 'exust', text: st}),
|
|
318
|
+
u.state === 'up' ? null : h('button', {type: 'button', class: 'askx', text: '✕', title: 'Dismiss', onclick: function () { EX.uploads = EX.uploads.filter(function (x) { return x !== u; }); renderUps(); }})]));
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
var hasFiles = function (ev) { return ev.dataTransfer && Array.prototype.indexOf.call(ev.dataTransfer.types || [], 'Files') >= 0; };
|
|
322
|
+
var isMove = function (ev) { return ev.dataTransfer && Array.prototype.indexOf.call(ev.dataTransfer.types || [], 'application/x-ckit-move') >= 0; };
|
|
323
|
+
function dropTarget(ev) { var r = ev.target.closest && ev.target.closest('.exitem'); if (!r) return null; var e = byName(r.getAttribute('data-name')); return e && e.dir ? {row: r, e: e} : null; }
|
|
324
|
+
function clearDrop() { var E = EX && EX.el; if (!E) return; E.list.classList.remove('over'); Array.prototype.forEach.call(E.rows.querySelectorAll('.dropto'), function (r) { r.classList.remove('dropto'); }); }
|
|
325
|
+
/* Capture on #modal: files dropped while the explorer is open go to the workspace folder, never to the composer attachments. */
|
|
326
|
+
var M = $('modal');
|
|
327
|
+
M.addEventListener('dragover', function (ev) {
|
|
328
|
+
if (!EX || !(hasFiles(ev) || isMove(ev))) return; ev.preventDefault(); ev.stopPropagation(); clearDrop();
|
|
329
|
+
var inList = EX.el.list.contains(ev.target), t = inList ? dropTarget(ev) : null; ev.dataTransfer.dropEffect = hasFiles(ev) ? 'copy' : 'move';
|
|
330
|
+
if (t) t.row.classList.add('dropto'); else if (inList && hasFiles(ev)) EX.el.list.classList.add('over');
|
|
331
|
+
});
|
|
332
|
+
M.addEventListener('dragleave', function (ev) { if (EX && (!ev.relatedTarget || !M.contains(ev.relatedTarget))) clearDrop(); });
|
|
333
|
+
M.addEventListener('drop', function (ev) {
|
|
334
|
+
if (!EX || !(hasFiles(ev) || isMove(ev))) return; ev.preventDefault(); ev.stopPropagation();
|
|
335
|
+
var t = EX.el.list.contains(ev.target) ? dropTarget(ev) : null; clearDrop(); EX.dragging = false;
|
|
336
|
+
if (isMove(ev) && !hasFiles(ev)) { if (t) { var mv = JSON.parse(ev.dataTransfer.getData('application/x-ckit-move') || '[]').filter(function (p) { return p !== join(EX.path, t.e.name); }); moveInto(mv, join(EX.path, t.e.name)); } return; }
|
|
337
|
+
upload(ev.dataTransfer.files, t ? join(EX.path, t.e.name) : EX.path);
|
|
338
|
+
});
|
|
339
|
+
/* the composer's document-level handlers must not light up behind the popup */
|
|
340
|
+
document.addEventListener('dragover', function (ev) { if (EX && hasFiles(ev)) { var c = $('composer'); c.classList.remove('drop'); } }, true);
|
|
341
|
+
|
|
342
|
+
/* ---------- keyboard ---------- */
|
|
343
|
+
function onKey(ev) {
|
|
344
|
+
if (!EX || document.querySelector('.asklayer')) return;
|
|
345
|
+
var t = ev.target, typing = t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA');
|
|
346
|
+
if (ev.key === 'Escape') {
|
|
347
|
+
ev.stopImmediatePropagation(); ev.preventDefault();
|
|
348
|
+
if (typing && EX.el.filter.value) { EX.el.filter.value = ''; EX.filter = ''; renderList(); } else if (EX.prev) closePrev(); else close();
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (typing) { if (ev.key === 'ArrowDown' && t === EX.el.filter) { ev.preventDefault(); EX.el.rows.focus(); } return; }
|
|
352
|
+
if (!EX.el.dlg.contains(t) && t !== document.body) return;
|
|
353
|
+
var list = visible(), names = list.map(function (e) { return e.name; }), at = ev.shiftKey && EX.cursor ? EX.cursor : EX.anchor, cur = at ? names.indexOf(at) : -1, k = ev.key;
|
|
354
|
+
if (k === 'ArrowDown' || k === 'ArrowUp') {
|
|
355
|
+
ev.preventDefault(); if (!names.length) return;
|
|
356
|
+
var i = k === 'ArrowDown' ? Math.min(names.length - 1, cur + 1) : Math.max(0, cur < 0 ? 0 : cur - 1);
|
|
357
|
+
if (ev.shiftKey && EX.anchor && names.indexOf(EX.anchor) >= 0) { var a = names.indexOf(EX.anchor); EX.sel = names.slice(Math.min(a, i), Math.max(a, i) + 1); EX.cursor = names[i]; syncSel(); } else { EX.cursor = null; select(names[i], {}); }
|
|
358
|
+
} else if (k === 'Enter') { var one = EX.sel.length === 1 ? byName(EX.sel[0]) : null; if (one) { ev.preventDefault(); openItem(one); } }
|
|
359
|
+
else if (k === 'Backspace' && (ev.metaKey || ev.ctrlKey)) { ev.preventDefault(); trash(EX.sel.map(byName).filter(Boolean)); }
|
|
360
|
+
else if (k === 'Backspace') { ev.preventDefault(); goUp(); }
|
|
361
|
+
else if (k === 'F2') { var r = EX.sel.length === 1 ? byName(EX.sel[0]) : null; if (r) { ev.preventDefault(); rename(r); } }
|
|
362
|
+
else if (k === 'Delete') { ev.preventDefault(); trash(EX.sel.map(byName).filter(Boolean)); }
|
|
363
|
+
else if ((k === 'a' || k === 'A') && (ev.metaKey || ev.ctrlKey)) { ev.preventDefault(); EX.sel = names; syncSel(); }
|
|
364
|
+
else if (k === 'Home' && names.length) { ev.preventDefault(); select(names[0], {}); } else if (k === 'End' && names.length) { ev.preventDefault(); select(names[names.length - 1], {}); }
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
window.__ckExplorer = {open: open, close: close};
|
|
368
|
+
})();
|
package/gui/index.html
CHANGED
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
<section id="dock">
|
|
33
33
|
<div id="feedwrap"><div class="dh"><span class="ml">CONVERSATION</span><span class="ml" id="feedcount"></span></div><div id="feed"></div></div>
|
|
34
34
|
<div id="composer">
|
|
35
|
-
<div class="dh"><span class="ml" id="clabel">OWNER → DIRECTOR</span><button class="chip" id="attach" title="Attach any files: images, PDF, Word, Excel, drawio, XML… You can also drop or paste them here.">📎 Attach files</button><input type="file" id="file" multiple hidden></div>
|
|
35
|
+
<div class="dh"><span class="ml" id="clabel">OWNER → DIRECTOR</span><span class="ctools"><button class="chip" id="enh" title="Rewrite your request so the Director cannot misread it. You decide whether to use the result.">✦ Enhance</button><button class="chip" id="attach" title="Attach any files: images, PDF, Word, Excel, drawio, XML… You can also drop or paste them here.">📎 Attach files</button></span><input type="file" id="file" multiple hidden></div>
|
|
36
36
|
<div class="chips" id="chips"></div><div class="chips" id="wbtns" hidden></div><span id="lanewrap" hidden><select id="lane" title="Lane for this request. Auto: the Director decides which lane fits."></select></span>
|
|
37
37
|
<div class="attlist" id="atts"></div>
|
|
38
|
-
<div class="cbox"><textarea id="msg" rows="2" placeholder="Message the Director… (Enter to send)"></textarea><button class="primary" id="send">Send ▸</button></div>
|
|
38
|
+
<div class="cbox"><textarea id="msg" rows="2" placeholder="Message the Director… (Enter to send, @ for a file)"></textarea><button class="primary" id="send">Send ▸</button></div>
|
|
39
39
|
<div id="cnote"></div>
|
|
40
40
|
</div>
|
|
41
41
|
</section>
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
</div>
|
|
48
48
|
<div id="modal" hidden></div>
|
|
49
49
|
<div id="overlay" hidden></div>
|
|
50
|
-
<script src="network.js"></script><script src="app.js"></script>
|
|
50
|
+
<script src="network.js"></script><script src="app.js"></script><script src="explorer.js"></script>
|
|
51
51
|
</body></html>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ngockhoale/ckit",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "CKit — local Agent Company: a Director plus BA / Implement / QC teams running your own codex, claude and omp CLIs, with a web Mission Control (ckit gui).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|