200 lines
9.3 KiB
HTML
200 lines
9.3 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<style>
|
|
/*@COMMON_CSS@*/
|
|
.pal{display:flex;flex-direction:column;background:hsl(var(--card))}
|
|
.bar{display:flex;align-items:center;gap:8px;border-bottom:1px solid hsl(var(--border));padding:0 12px}
|
|
.bar .ic{width:16px;height:16px;color:hsl(var(--muted-foreground))}
|
|
.bar input{min-width:0;flex:1;border:0;outline:none;background:transparent;padding:12px 0;font-size:14px}
|
|
.bar input::placeholder{color:hsl(var(--muted-foreground))}
|
|
.bar .caps{opacity:.7}
|
|
.list{overflow-y:auto;padding:6px}
|
|
.row{display:flex;width:100%;align-items:center;gap:10px;border:0;border-radius:6px;background:none;padding:6px 10px;text-align:left;font-size:14px;cursor:pointer}
|
|
.row.on{background:hsl(var(--accent));color:hsl(var(--accent-foreground))}
|
|
.row .ic{color:hsl(var(--muted-foreground))}
|
|
/* a plugin's own glyph, one colour like the rest (drawn as a mask) */
|
|
.row .glyph{display:inline-block;background:currentColor;-webkit-mask:var(--g) center/contain no-repeat;mask:var(--g) center/contain no-repeat}
|
|
.row .txt{min-width:0;flex:1;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
.row .hint{margin-left:8px;font-size:12px;color:hsl(var(--muted-foreground))}
|
|
.row .caps{flex:none;opacity:.8}
|
|
.empty{padding:24px 8px;text-align:center;font-size:12px;color:hsl(var(--muted-foreground))}
|
|
.err{padding:16px 12px;font-size:12px;color:hsl(var(--destructive))}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="pal" role="dialog" id="pal">
|
|
<div class="bar">
|
|
<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
|
|
<input id="q" autocomplete="off" spellcheck="false">
|
|
<span id="esc"></span>
|
|
</div>
|
|
<div class="list" id="list" role="listbox"></div>
|
|
</div>
|
|
<script>
|
|
/*@COMMON_JS@*/
|
|
(function () {
|
|
"use strict";
|
|
var list = document.getElementById("list");
|
|
var q = document.getElementById("q");
|
|
if (!A) { list.innerHTML = '<div class="err">' + esc(tr("This page runs inside ai-agent.")) + "</div>"; return; }
|
|
|
|
var ICON = {
|
|
action: '<path d="M4 14a1 1 0 0 1-.78-1.63l9.9-10.2a.5.5 0 0 1 .86.46l-1.92 6.02A1 1 0 0 0 13 10h7a1 1 0 0 1 .78 1.63l-9.9 10.2a.5.5 0 0 1-.86-.46l1.92-6.02A1 1 0 0 0 11 14z"/>',
|
|
page: '<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"/>',
|
|
conversation: '<path d="M14 9a2 2 0 0 1-2 2H6l-4 4V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2z"/><path d="M18 9h2a2 2 0 0 1 2 2v11l-4-4h-6a2 2 0 0 1-2-2v-1"/>',
|
|
plugin: '<path d="M15.39 4.39a1 1 0 0 0 1.68-.474 2.5 2.5 0 1 1 3.014 3.015 1 1 0 0 0-.474 1.68l1.683 1.682a2.414 2.414 0 0 1 0 3.414L19.61 15.39a1 1 0 0 1-1.68-.474 2.5 2.5 0 1 0-3.014 3.015 1 1 0 0 1 .474 1.68l-1.683 1.682a2.414 2.414 0 0 1-3.414 0L8.61 19.61a1 1 0 0 0-1.68.474 2.5 2.5 0 1 1-3.014-3.015 1 1 0 0 0 .474-1.68l-1.683-1.682a2.414 2.414 0 0 1 0-3.414L4.39 8.61a1 1 0 0 1 1.68.474 2.5 2.5 0 1 0 3.014-3.015 1 1 0 0 1-.474-1.68l1.683-1.682a2.414 2.414 0 0 1 3.414 0z"/>',
|
|
switch: '<path d="M12 2v10"/><path d="M18.4 6.6a9 9 0 1 1-12.77.04"/>'
|
|
};
|
|
var ENTER = '<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="9 10 4 15 9 20"/><path d="M20 4v7a4 4 0 0 1-4 4H4"/></svg>';
|
|
function icon(kind, uri) {
|
|
// a plugin's glyph: a checked base64 SVG URI (common.js), as a mask
|
|
if (uri) return '<span class="ic glyph" style="--g:url("' + uri + '")" aria-hidden="true"></span>';
|
|
return '<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' + (ICON[kind] || "") + "</svg>";
|
|
}
|
|
|
|
var S = { items: [], shown: [], active: 0, error: null, seq: 0 };
|
|
q.placeholder = tr("Run an action, open a page or a conversation…");
|
|
q.setAttribute("aria-label", tr("Command palette"));
|
|
document.getElementById("esc").innerHTML = capsHtml("escape");
|
|
|
|
/** The panel's height: the box and the list, at most ~min(32rem, 70vh) of
|
|
* the app's window (the frame's own viewport is only what we ask for). */
|
|
var MAX_H = Math.min(512, Math.round(0.7 * ((window.screen && window.screen.availHeight) || window.outerHeight || 800)));
|
|
function fit() {
|
|
var bar = list.previousElementSibling.getBoundingClientRect().height;
|
|
list.style.maxHeight = Math.max(120, MAX_H - bar) + "px";
|
|
var h = Math.ceil(document.getElementById("pal").getBoundingClientRect().height);
|
|
try { A.resize(h); } catch (e) {}
|
|
}
|
|
|
|
function render() {
|
|
if (S.error) { list.innerHTML = '<div class="err">' + esc(S.error) + "</div>"; fit(); return; }
|
|
S.shown = shown(S.items, q.value);
|
|
if (S.active >= S.shown.length) S.active = 0;
|
|
if (!S.shown.length) { list.innerHTML = '<p class="empty">' + esc(tr("No matches.")) + "</p>"; fit(); return; }
|
|
list.innerHTML = S.shown.map(rowHtml).join("");
|
|
list.scrollTop = 0;
|
|
fit();
|
|
}
|
|
|
|
function rowHtml(it, i) {
|
|
var on = i === S.active;
|
|
var right = it.binding ? capsHtml(it.binding) : (on ? ENTER : "");
|
|
return '<button type="button" tabindex="-1" role="option" class="row' + (on ? " on" : "") + '" data-i="' + i + '" aria-selected="' + on + '">' +
|
|
icon(it.kind, it.icon) + '<span class="txt">' + esc(it.label) + (it.hint ? '<span class="hint">' + esc(it.hint) + "</span>" : "") + "</span>" +
|
|
right + "</button>";
|
|
}
|
|
|
|
/** Move the highlight: only its old and new rows are redrawn (the ↵ moves
|
|
* with it); from the keys, the new one scrolls into view. */
|
|
function setActive(i, fromKeys) {
|
|
if (i === S.active) return;
|
|
var old = S.active;
|
|
S.active = i;
|
|
[old, i].forEach(function (k) {
|
|
var el = list.querySelector('[data-i="' + k + '"]');
|
|
if (el && S.shown[k]) el.outerHTML = rowHtml(S.shown[k], k);
|
|
});
|
|
var el = list.querySelector('[data-i="' + i + '"]');
|
|
if (fromKeys && el && el.scrollIntoView) el.scrollIntoView({ block: "nearest" });
|
|
}
|
|
|
|
/** The lists, fresh from the app: what answers now, what this person may
|
|
* open, the conversations of the profile on screen, the plugins they may
|
|
* turn off or on (an app without that bridge: none). */
|
|
function load() {
|
|
if (typeof A.commands !== "function" || typeof A.places !== "function" || typeof A.conversations !== "function") {
|
|
S.error = tr("This app can't list its commands yet — update it.");
|
|
render();
|
|
return Promise.resolve();
|
|
}
|
|
var switches = typeof A.pluginSwitches === "function"
|
|
? A.pluginSwitches().then(null, function () { return {}; }) : Promise.resolve({});
|
|
return Promise.all([A.commands(), A.places(), A.conversations(), switches]).then(function (r) {
|
|
var cmds = r[0] || {};
|
|
if (typeof cmds.mac === "boolean") MAC = cmds.mac;
|
|
S.error = null;
|
|
S.items = buildItems(cmds, (r[1] || {}).places, (r[2] || {}).conversations, (r[3] || {}).switches);
|
|
render();
|
|
}, function (e) {
|
|
S.error = String((e && e.message) || e);
|
|
render();
|
|
});
|
|
}
|
|
|
|
function close() { try { A.modal.close(); } catch (e) {} }
|
|
|
|
function go(it) {
|
|
if (!it) return;
|
|
if (it.run.plugin) {
|
|
// the app closes the palette and asks first (Enter confirms there)
|
|
A.switchPlugin(it.run.plugin, it.run.on).then(null, function (e) {
|
|
try { A.toast(String((e && e.message) || e), "error"); } catch (e2) {}
|
|
});
|
|
return;
|
|
}
|
|
if (it.run.command) {
|
|
// the app closes the palette, then runs it (a focus it takes stays taken)
|
|
A.runCommand(it.run.command);
|
|
return;
|
|
}
|
|
close();
|
|
A.navigate(it.run.to);
|
|
}
|
|
|
|
function focusBox() {
|
|
q.focus();
|
|
requestAnimationFrame(function () { q.focus(); });
|
|
}
|
|
|
|
/* each opening: an empty box, fresh lists, the focus */
|
|
A.onUpdate(function (u) {
|
|
if (!u || !u.open || u.seq === S.seq) return;
|
|
S.seq = u.seq;
|
|
q.value = "";
|
|
S.active = 0;
|
|
render();
|
|
focusBox();
|
|
load();
|
|
});
|
|
|
|
q.addEventListener("input", function () { S.active = 0; render(); });
|
|
q.addEventListener("keydown", function (e) {
|
|
var down = e.key === "ArrowDown" || (e.ctrlKey && !e.metaKey && e.key === "n");
|
|
var up = e.key === "ArrowUp" || (e.ctrlKey && !e.metaKey && e.key === "p");
|
|
if (down || up) {
|
|
e.preventDefault();
|
|
var n = S.shown.length;
|
|
if (n) setActive((S.active + (down ? 1 : -1) + n) % n, true);
|
|
} else if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
go(S.shown[S.active]);
|
|
} else if (e.key === "Escape") {
|
|
e.preventDefault();
|
|
close();
|
|
} else if (e.key === "Tab") {
|
|
e.preventDefault(); // the focus stays in the box
|
|
}
|
|
// ⌘K and the rest go on to the app: its palette key closes it
|
|
});
|
|
list.addEventListener("mousemove", function (e) {
|
|
var row = e.target.closest && e.target.closest("[data-i]");
|
|
if (row) setActive(Number(row.getAttribute("data-i")));
|
|
});
|
|
list.addEventListener("mousedown", function (e) { e.preventDefault(); }); // keep the focus in the box
|
|
list.addEventListener("click", function (e) {
|
|
var row = e.target.closest && e.target.closest("[data-i]");
|
|
if (row) go(S.shown[Number(row.getAttribute("data-i"))]);
|
|
});
|
|
|
|
render();
|
|
load();
|
|
try { A.ready(); } catch (e) {}
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|