Repository navigation
Expand file tree
/
Copy pathui.js
More file actions
357 lines (324 loc) · 15.4 KB
/
Copy pathui.js
File metadata and controls
357 lines (324 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Shared UI helpers: API, icons, dialogs, menus, toasts.
export const $ = (sel, root = document) => root.querySelector(sel);
export const $$ = (sel, root = document) => [...root.querySelectorAll(sel)];
export function esc(s) {
return String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
}
export const bus = new EventTarget();
// The control the person just clicked (or pressed Enter in) shows a spinner while the request it started runs.
let trigger = null;
for (const type of ["click", "keydown"]) {
document.addEventListener(type, (e) => {
if (type === "keydown" && e.key !== "Enter") return;
const el = e.target.closest?.("button, [data-action], [data-act], .menu-item, input, select");
if (el) trigger = { el, at: performance.now() };
}, true);
}
function busyOn() {
const t = trigger;
trigger = null;
if (!t || performance.now() - t.at > 400 || !t.el.isConnected) return () => {};
const el = t.el;
const timer = setTimeout(() => { el.classList.add("busy"); el.setAttribute("aria-busy", "true"); }, 150); // skip a flash on fast replies
return () => { clearTimeout(timer); el.classList.remove("busy"); el.removeAttribute("aria-busy"); };
}
/** Markup for an inline "working on it" row. */
export function loadingHTML(text = "Loading…") {
return `<div class="loading-row" role="status"><span class="spinner sm"></span><span>${esc(text)}</span></div>`;
}
export async function api(path, opts = {}) {
const done = busyOn();
try {
return await request(path, opts);
} finally {
done();
}
}
async function request(path, { method = "GET", body } = {}) {
const res = await fetch(path, {
method,
headers: body === undefined ? {} : { "Content-Type": "application/json" },
body: body === undefined ? undefined : JSON.stringify(body),
});
let data = null;
try {
data = await res.json();
} catch {
/* empty body */
}
if (res.status === 401 && (data?.error === "locked" || data?.error === "setup_required")) bus.dispatchEvent(new Event("locked"));
if (res.status === 423) bus.dispatchEvent(new Event("locked")); // another device is in use
if (!res.ok) {
const e = new Error(data?.error || `HTTP ${res.status}`);
e.data = data;
throw e;
}
return data;
}
const AUTH_ERRORS = {
invalid_password: "Wrong password.",
wrong_current_password: "The current password is wrong.",
password_too_short: "Use at least 8 characters.",
passwords_do_not_match: "The new password and its confirmation don't match.",
invalid_setup_code: "That setup code is wrong. It is printed where shelldeck runs (and saved in its data folder as setup-code).",
password_already_set: "A password already exists. Reload to log in.",
};
/** Human text for an auth API error. */
export function authError(e) {
if (e.message === "too_many_attempts") return `Too many attempts. Try again in ${e.data?.retry_after || "a few"} seconds.`;
return AUTH_ERRORS[e.message] || e.message;
}
/** Give every password field in `root` a show/hide eye button. */
export function withEyes(root) {
for (const input of root.querySelectorAll('input[type="password"]:not([data-eye])')) {
input.dataset.eye = "1";
const wrap = document.createElement("span");
wrap.className = "pw-wrap";
input.replaceWith(wrap);
const btn = document.createElement("button");
btn.type = "button";
btn.className = "pw-eye";
btn.setAttribute("aria-label", "Show password");
btn.setAttribute("aria-pressed", "false");
btn.title = "Show password";
btn.innerHTML = icon("eye");
btn.addEventListener("click", () => {
const show = input.type === "password";
input.type = show ? "text" : "password";
btn.innerHTML = icon(show ? "eye-off" : "eye");
btn.setAttribute("aria-pressed", String(show));
btn.setAttribute("aria-label", show ? "Hide password" : "Show password");
btn.title = show ? "Hide password" : "Show password";
input.focus();
});
wrap.append(input, btn);
}
}
// Icons: lucide-style strokes (ISC). Inline so the app works offline.
const P = {
plus: '<path d="M12 5v14M5 12h14"/>',
x: '<path d="M18 6 6 18M6 6l12 12"/>',
search: '<circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/>',
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"/>',
"folder-plus": '<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"/><path d="M12 10v6M9 13h6"/>',
sparkle: '<path d="M12 3 13.9 8.6 19.5 10.5 13.9 12.4 12 18 10.1 12.4 4.5 10.5 10.1 8.6Z"/><path d="M19 3v4M17 5h4M5 17v3M3.5 18.5h3"/>',
terminal: '<path d="m4 17 6-6-6-6M12 19h8"/>',
bookmark: '<path d="m19 21-7-4-7 4V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2Z"/>',
clock: '<circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/>',
kanban: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M8 7v7M12 7v4M16 7v9"/>',
settings: '<path d="M4 21v-7M4 10V3M12 21v-9M12 8V3M20 21v-5M20 12V3M1 14h6M9 8h6M17 16h6"/>',
panel: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M9 3v18"/>',
"split-h": '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M12 3v18"/>',
"split-v": '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 12h18"/>',
chevron: '<path d="m6 9 6 6 6-6"/>',
activity: '<path d="M22 12h-4l-3 9L9 3l-3 9H2"/>',
"chevron-up": '<path d="m18 15-6-6-6 6"/>',
more: '<circle cx="5" cy="12" r="1"/><circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/>',
maximize: '<path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/>',
minimize: '<path d="M8 3v3a2 2 0 0 1-2 2H3m18 0h-3a2 2 0 0 1-2-2V3m0 18v-3a2 2 0 0 1 2-2h3M3 16h3a2 2 0 0 1 2 2v3"/>',
play: '<path d="M6 3 20 12 6 21Z"/>',
radio: '<path d="M4.9 19.1C1 15.2 1 8.8 4.9 4.9M7.8 16.2c-2.3-2.3-2.3-6.1 0-8.5M16.2 7.8c2.3 2.3 2.3 6.1 0 8.5M19.1 4.9C23 8.8 23 15.1 19.1 19"/><circle cx="12" cy="12" r="2"/>',
history: '<path d="M3 12a9 9 0 1 0 3-6.7L3 8"/><path d="M3 3v5h5"/><path d="M12 7v5l3 2"/>',
external: '<path d="M15 3h6v6M10 14 21 3M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/>',
pencil: '<path d="M12 20h9M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z"/>',
trash: '<path d="M3 6h18M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>',
devices: '<rect x="2" y="4" width="14" height="10" rx="1.5"/><path d="M5 18h8"/><rect x="17" y="8" width="5" height="12" rx="1"/>',
lock: '<rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/>',
up: '<path d="M12 19V5M5 12l7-7 7 7"/>',
clipboard: '<rect x="8" y="2" width="8" height="4" rx="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/>',
keyboard: '<rect x="2" y="4" width="20" height="16" rx="2"/><path d="M6 8h.01M10 8h.01M14 8h.01M18 8h.01M8 12h8M7 16h10"/>',
power: '<path d="M12 2v10M18.4 6.6a9 9 0 1 1-12.77.04"/>',
eye: '<path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12Z"/><circle cx="12" cy="12" r="3"/>',
"eye-off": '<path d="M9.9 4.2A10 10 0 0 1 12 4c6.5 0 10 8 10 8a17 17 0 0 1-2.2 3.2M6.6 6.6C3.9 8.3 2 12 2 12s3.5 8 10 8a9.7 9.7 0 0 0 5.4-1.6M9.9 9.9a3 3 0 0 0 4.2 4.2M2 2l20 20"/>',
grid: '<rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><rect x="14" y="14" width="7" height="7" rx="1.5"/>',
sun: '<circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/>',
"git-branch": '<line x1="6" y1="3" x2="6" y2="15"/><circle cx="18" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M18 9a9 9 0 0 1-9 9"/>',
refresh: '<path d="M21 12a9 9 0 1 1-2.6-6.36M21 3v6h-6"/>',
share: '<circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><path d="m8.6 13.5 6.8 4M15.4 6.5l-6.8 4"/>',
note: '<path d="M14 3H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9Z"/><path d="M14 3v6h6M8 13h8M8 17h5"/>',
layers: '<path d="m12 3 9 5-9 5-9-5 9-5Z"/><path d="m3 13 9 5 9-5"/>',
};
export function icon(name) {
return `<i data-icon="${name}"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">${P[name] || ""}</svg></i>`;
}
export function hydrateIcons(root = document) {
for (const el of $$("i[data-icon]:empty", root)) el.outerHTML = icon(el.dataset.icon);
}
// ---------------------------------------------------------------- dialogs
const dialogs = [];
export function topDialog() {
return dialogs.at(-1);
}
/** Opens a dialog. `body` is HTML. Returns {el, close}. */
export function dialog({ title = "", body = "", foot = "", wide = false, cls = "", onClose } = {}) {
const bg = document.createElement("div");
bg.className = "dialog-bg";
bg.innerHTML = `<div class="dialog ${wide ? "wide" : ""} ${cls}" role="dialog" aria-modal="true" aria-label="${esc(title)}" tabindex="-1">
${title ? `<div class="dialog-head"><h2>${esc(title)}</h2><button class="icon-btn" data-close aria-label="Close">${icon("x")}</button></div>` : ""}
<div class="dialog-body">${body}</div>
${foot ? `<div class="dialog-foot">${foot}</div>` : ""}
</div>`;
const prevFocus = document.activeElement;
const d = {
el: bg.firstElementChild,
canClose: null, // () => false vetoes closing (e.g. unsaved edits); close(true) skips it
close(force = false) {
if (!bg.isConnected || (!force && d.canClose && !d.canClose())) return;
bg.remove();
dialogs.splice(dialogs.indexOf(d), 1);
onClose?.();
prevFocus?.focus?.();
},
};
bg.addEventListener("mousedown", (e) => {
if (e.target === bg) d.close();
});
bg.addEventListener("click", (e) => {
if (e.target.closest("[data-close]")) d.close();
});
$("#dialog-root").append(bg);
dialogs.push(d);
hydrateIcons(bg);
// focus the dialog itself when it has no field, or keys (Escape) keep going to the terminal behind it
setTimeout(() => ($("[autofocus], input, select, textarea, button.primary", d.el) || d.el).focus(), 0);
return d;
}
export function confirmDialog(message, { ok = "Confirm", danger = false } = {}) {
return new Promise((resolve) => {
let result = false;
const d = dialog({
title: "Are you sure?",
body: `<p class="muted" style="margin:0">${esc(message)}</p>`,
foot: `<button class="btn" data-close>Cancel</button><button class="btn primary ${danger ? "danger" : ""}" data-ok>${esc(ok)}</button>`,
onClose: () => resolve(result),
});
$("[data-ok]", d.el).onclick = () => {
result = true;
d.close();
};
setTimeout(() => $("[data-ok]", d.el).focus(), 0);
});
}
export function promptDialog(title, value = "", { label = "Name", ok = "Save", optional = false } = {}) {
return new Promise((resolve) => {
let result = null;
const d = dialog({
title,
body: `<form><label class="field"><span>${esc(label)}</span><input type="text" name="v" value="${esc(value)}" autofocus ${optional ? "" : "required"} /></label></form>`,
foot: `<button class="btn" data-close>Cancel</button><button class="btn primary" data-ok>${esc(ok)}</button>`,
onClose: () => resolve(result),
});
const submit = (e) => {
e?.preventDefault();
const v = $("input", d.el).value.trim();
if (!v && !optional) return;
result = v;
d.close();
};
$("form", d.el).onsubmit = submit;
$("[data-ok]", d.el).onclick = submit;
setTimeout(() => $("input", d.el).select(), 0);
});
}
// ------------------------------------------------------------------ menus
let openMenu = null;
export function closeMenu() {
openMenu?.remove();
openMenu = null;
}
/** items: {label, icon, danger, onClick, hint} | "sep" | {header} */
export function menu(anchor, items) {
closeMenu();
const m = document.createElement("div");
m.className = "menu";
m.setAttribute("role", "menu");
m.innerHTML = items
.map((it, i) => {
if (it === "sep") return "<hr />";
if (it.header) return `<div class="menu-label">${esc(it.header)}</div>`;
return `<button role="menuitem" data-i="${i}" class="${it.danger ? "danger" : ""}">${it.icon ? icon(it.icon) : ""}<span style="flex:1">${esc(it.label)}</span>${it.hint ? `<span class="faint">${esc(it.hint)}</span>` : ""}</button>`;
})
.join("");
m.addEventListener("click", (e) => {
const b = e.target.closest("button[data-i]");
if (!b) return;
closeMenu();
items[+b.dataset.i].onClick?.();
});
$("#menu-root").append(m);
const r = anchor.getBoundingClientRect ? anchor.getBoundingClientRect() : { left: anchor.x, right: anchor.x, top: anchor.y, bottom: anchor.y };
const mw = m.offsetWidth;
const mh = m.offsetHeight;
const left = Math.min(r.left, innerWidth - mw - 8);
const top = r.bottom + 4 + mh > innerHeight ? Math.max(8, r.top - mh - 4) : r.bottom + 4;
m.style.left = `${Math.max(8, left)}px`;
m.style.top = `${top}px`;
openMenu = m;
m.querySelector("button")?.focus();
return m;
}
document.addEventListener("pointerdown", (e) => {
if (openMenu && !openMenu.contains(e.target)) closeMenu();
});
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && openMenu) {
closeMenu();
e.stopPropagation();
} else if (e.key === "Escape" && dialogs.length) {
e.preventDefault();
e.stopPropagation();
topDialog().close();
}
});
// ----------------------------------------------------------------- toasts
export function toast({ title, body = "", kind = "", actions = [], timeout = 5000 }) {
const t = document.createElement("div");
t.className = `toast ${kind}`;
t.innerHTML = `<div class="t-title">${esc(title)}</div>${body ? `<div class="t-body">${esc(body)}</div>` : ""}${
actions.length ? `<div class="row">${actions.map((a, i) => `<button class="btn sm ${i === 0 ? "primary" : ""}" data-i="${i}">${esc(a.label)}</button>`).join("")}</div>` : ""
}`;
const close = () => t.remove();
t.addEventListener("click", (e) => {
const b = e.target.closest("button[data-i]");
if (!b) return;
actions[+b.dataset.i].onClick?.();
close();
});
$("#toasts").append(t);
if (timeout) setTimeout(close, timeout);
return { el: t, close };
}
export function toastError(err) {
toast({ title: "Something went wrong", body: err?.message || String(err), kind: "error" });
}
// ------------------------------------------------------------------ misc
export function fmtTime(iso) {
if (!iso) return "";
const d = new Date(iso);
if (Number.isNaN(+d)) return iso;
return d.toLocaleString(undefined, { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" });
}
/** ISO string -> value for <input type="datetime-local"> in local time. */
export function toLocalInput(iso) {
if (!iso) return "";
const d = new Date(iso);
if (Number.isNaN(+d)) return "";
const pad = (n) => String(n).padStart(2, "0");
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
}
export function fromLocalInput(value) {
return value ? new Date(value).toISOString() : null;
}
export const store = {
get(key, fallback) {
try {
const v = localStorage.getItem(`shelldeck.${key}`);
return v === null ? fallback : JSON.parse(v);
} catch {
return fallback;
}
},
set(key, value) {
localStorage.setItem(`shelldeck.${key}`, JSON.stringify(value));
},
};