pinokiod 8.3.32 → 8.3.35
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/kernel/api/uri/index.js +6 -0
- package/package.json +1 -1
- package/server/public/agent-bar-about.js +27 -7
- package/server/public/agent-bar-agents.css +154 -0
- package/server/public/agent-bar-agents.js +346 -0
- package/server/public/agent-bar-launch.css +274 -0
- package/server/public/agent-bar-launch.js +333 -0
- package/server/public/agent-bar-panel.js +2 -0
- package/server/public/agent-bar.js +5 -1
- package/server/views/app.ejs +4 -0
- package/server/views/partials/home_ask_button.ejs +1 -1
- package/server/views/partials/main_sidebar.ejs +4 -0
- package/server/views/terminal.ejs +2 -0
- package/test/agent-bar-about.test.js +38 -0
- package/test/agent-bar-agents.test.js +297 -0
- package/test/agent-bar-launch.test.js +375 -0
- package/test/uri-api.test.js +23 -0
package/kernel/api/uri/index.js
CHANGED
|
@@ -44,6 +44,12 @@ class URI {
|
|
|
44
44
|
const uri = this.build(req.params)
|
|
45
45
|
ondata({ raw: `\r\nopening uri: ${uri}\r\n` })
|
|
46
46
|
const result = await Util.openURI(uri)
|
|
47
|
+
// The system refused the link (most often: no app is installed for it). The
|
|
48
|
+
// step fails, so the script stops here and does not report a launch.
|
|
49
|
+
if (result && result.ok === false) {
|
|
50
|
+
const scheme = uri.slice(0, Math.max(0, uri.indexOf(':')))
|
|
51
|
+
throw new Error(`Nothing on this computer could open the ${scheme}: link. The app for it may not be installed.\n${result.error || ''}`.trim())
|
|
52
|
+
}
|
|
47
53
|
return { uri, result }
|
|
48
54
|
}
|
|
49
55
|
}
|
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
// ("Ask · <app>"), the empty text ("Ask Pinokio about <app>…"),
|
|
10
10
|
// what the face says in the open view, and a chip with the
|
|
11
11
|
// app's icon and name at the left of the open view's bottom row.
|
|
12
|
+
// click again -> close the view, keeping the app and the draft.
|
|
12
13
|
// the chip's x, or another mode -> an ordinary Ask again.
|
|
13
14
|
// send -> the same request the app's own page sends for "Ask Pinokio
|
|
14
15
|
// about <app>" (POST /brain/ask with `app`), so the agent starts
|
|
@@ -35,12 +36,19 @@
|
|
|
35
36
|
let chipIcon = null;
|
|
36
37
|
let chipName = null;
|
|
37
38
|
let chipX = null;
|
|
39
|
+
let activeButton = null;
|
|
38
40
|
// { id, name, folder, icon }: the app's folder name, what it is called, the
|
|
39
41
|
// folder's full path and its icon's address
|
|
40
42
|
let app = null;
|
|
41
43
|
|
|
42
44
|
const isBusy = () => Boolean(root) && root.dataset.busy === 'true';
|
|
43
45
|
const onAsk = () => Boolean(root) && root.dataset.item === 'ask';
|
|
46
|
+
const isEngaged = () => root.dataset.engaged === 'true';
|
|
47
|
+
|
|
48
|
+
function setExpanded(button, expanded) {
|
|
49
|
+
button.classList.toggle('is-open', expanded);
|
|
50
|
+
button.setAttribute('aria-expanded', String(expanded));
|
|
51
|
+
}
|
|
44
52
|
|
|
45
53
|
// The chip sits in the open view's bottom row (made by agent-bar-compose.js,
|
|
46
54
|
// possibly after this file runs), at the left, after the mode chip.
|
|
@@ -80,6 +88,10 @@
|
|
|
80
88
|
if (!root) return;
|
|
81
89
|
// another mode: an Ask pointed at an app is no longer what the bar shows
|
|
82
90
|
if (app && !onAsk()) app = null;
|
|
91
|
+
if (activeButton) {
|
|
92
|
+
setExpanded(activeButton, Boolean(app) && isEngaged());
|
|
93
|
+
if (!app) activeButton = null;
|
|
94
|
+
}
|
|
83
95
|
if (!placeChip()) return;
|
|
84
96
|
setHidden(chip, !app);
|
|
85
97
|
if (!app) return;
|
|
@@ -92,13 +104,21 @@
|
|
|
92
104
|
chipX.disabled = isBusy();
|
|
93
105
|
}
|
|
94
106
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
107
|
+
function toggle(button) {
|
|
108
|
+
if (!root || isBusy()) return;
|
|
109
|
+
const next = fromButton(button);
|
|
110
|
+
if (!next.id || !next.folder) return;
|
|
111
|
+
if (onAsk() && isEngaged() && app?.folder === next.folder) {
|
|
112
|
+
bar.leave();
|
|
113
|
+
button.focus();
|
|
114
|
+
sync();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (activeButton && activeButton !== button) setExpanded(activeButton, false);
|
|
118
|
+
activeButton = button;
|
|
98
119
|
app = next;
|
|
99
120
|
bar.choose('ask');
|
|
100
121
|
sync();
|
|
101
|
-
return true;
|
|
102
122
|
}
|
|
103
123
|
|
|
104
124
|
// The user took the app off: an ordinary Ask, the cursor still in the text.
|
|
@@ -155,7 +175,7 @@
|
|
|
155
175
|
// the home bar only; an app's page has its own bar, which is about that app already
|
|
156
176
|
if (!document.body || document.body.classList.contains('app-page')) return;
|
|
157
177
|
root = document.querySelector('.agent-bar');
|
|
158
|
-
if (!root || typeof bar.choose !== 'function' || typeof bar.refresh !== 'function') {
|
|
178
|
+
if (!root || typeof bar.choose !== 'function' || typeof bar.refresh !== 'function' || typeof bar.leave !== 'function') {
|
|
159
179
|
root = null;
|
|
160
180
|
return;
|
|
161
181
|
}
|
|
@@ -170,7 +190,7 @@
|
|
|
170
190
|
};
|
|
171
191
|
new MutationObserver(sync).observe(root, {
|
|
172
192
|
attributes: true,
|
|
173
|
-
attributeFilter: ['data-item', 'data-busy', 'data-compose', 'data-about'],
|
|
193
|
+
attributeFilter: ['data-item', 'data-busy', 'data-compose', 'data-about', 'data-engaged'],
|
|
174
194
|
});
|
|
175
195
|
sync();
|
|
176
196
|
}
|
|
@@ -192,7 +212,7 @@
|
|
|
192
212
|
if (!button) return;
|
|
193
213
|
event.preventDefault();
|
|
194
214
|
event.stopPropagation();
|
|
195
|
-
|
|
215
|
+
toggle(button);
|
|
196
216
|
}, true);
|
|
197
217
|
// (and Enter or Space on the button is the button's, not the row's)
|
|
198
218
|
document.addEventListener('keydown', (event) => {
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/* The agent menu (agent-bar-agents.js): a search box and two tabs on top that
|
|
2
|
+
stay put, and the list under them, which scrolls on its own. (The bar's other
|
|
3
|
+
menus use the same box, hence the data-menu.) */
|
|
4
|
+
.agent-bar .agent-bar__menu.agent-menu[data-menu="agent"] {
|
|
5
|
+
display: flex;
|
|
6
|
+
flex-direction: column;
|
|
7
|
+
padding: 0;
|
|
8
|
+
overflow: hidden;
|
|
9
|
+
}
|
|
10
|
+
.agent-menu__head {
|
|
11
|
+
flex: none;
|
|
12
|
+
display: flex;
|
|
13
|
+
flex-direction: column;
|
|
14
|
+
gap: 6px;
|
|
15
|
+
padding: 6px 6px 4px;
|
|
16
|
+
border-bottom: 1px solid transparent;
|
|
17
|
+
}
|
|
18
|
+
.agent-menu.is-scrolled .agent-menu__head {
|
|
19
|
+
border-bottom-color: var(--agent-bar-border);
|
|
20
|
+
}
|
|
21
|
+
.agent-menu__list {
|
|
22
|
+
flex: 1 1 auto;
|
|
23
|
+
min-height: 0;
|
|
24
|
+
overflow-y: auto;
|
|
25
|
+
overscroll-behavior: contain;
|
|
26
|
+
padding: 2px 6px 6px;
|
|
27
|
+
}
|
|
28
|
+
.agent-menu__head[hidden] + .agent-menu__list {
|
|
29
|
+
padding-top: 6px;
|
|
30
|
+
}
|
|
31
|
+
.agent-menu .agent-bar__row.is-active {
|
|
32
|
+
background: var(--agent-bar-chip);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* search */
|
|
36
|
+
.agent-menu__search {
|
|
37
|
+
height: 30px;
|
|
38
|
+
display: flex;
|
|
39
|
+
align-items: center;
|
|
40
|
+
gap: 7px;
|
|
41
|
+
padding: 0 4px 0 9px;
|
|
42
|
+
border-radius: 8px;
|
|
43
|
+
background: var(--agent-bar-chip);
|
|
44
|
+
color: var(--agent-bar-muted);
|
|
45
|
+
cursor: text;
|
|
46
|
+
}
|
|
47
|
+
.agent-menu__search:focus-within {
|
|
48
|
+
box-shadow: inset 0 0 0 1px var(--agent-bar-border-strong);
|
|
49
|
+
}
|
|
50
|
+
.agent-menu__glass {
|
|
51
|
+
flex: none;
|
|
52
|
+
font-size: 11px;
|
|
53
|
+
}
|
|
54
|
+
.agent-bar .agent-menu__input {
|
|
55
|
+
flex: 1 1 auto;
|
|
56
|
+
min-width: 0;
|
|
57
|
+
width: auto;
|
|
58
|
+
height: 100%;
|
|
59
|
+
margin: 0;
|
|
60
|
+
padding: 0;
|
|
61
|
+
border: 0;
|
|
62
|
+
border-radius: 0;
|
|
63
|
+
outline: 0;
|
|
64
|
+
box-shadow: none;
|
|
65
|
+
background: transparent;
|
|
66
|
+
color: var(--agent-bar-text);
|
|
67
|
+
font: 400 13px system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
68
|
+
-webkit-appearance: none;
|
|
69
|
+
appearance: none;
|
|
70
|
+
}
|
|
71
|
+
.agent-bar .agent-menu__input::placeholder {
|
|
72
|
+
color: var(--agent-bar-muted);
|
|
73
|
+
opacity: 1;
|
|
74
|
+
}
|
|
75
|
+
.agent-bar .agent-menu__clear {
|
|
76
|
+
flex: none;
|
|
77
|
+
width: 22px;
|
|
78
|
+
height: 22px;
|
|
79
|
+
display: flex;
|
|
80
|
+
align-items: center;
|
|
81
|
+
justify-content: center;
|
|
82
|
+
padding: 0;
|
|
83
|
+
border: 0;
|
|
84
|
+
border-radius: 50%;
|
|
85
|
+
background: transparent;
|
|
86
|
+
font-size: 11px;
|
|
87
|
+
color: var(--agent-bar-muted);
|
|
88
|
+
}
|
|
89
|
+
.agent-bar .agent-menu__clear:hover {
|
|
90
|
+
background: var(--agent-bar-chip-hover);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* tabs: Terminal and Desktop, each with its count */
|
|
94
|
+
.agent-menu__tabs {
|
|
95
|
+
display: flex;
|
|
96
|
+
gap: 2px;
|
|
97
|
+
padding: 2px;
|
|
98
|
+
border-radius: 8px;
|
|
99
|
+
background: var(--agent-bar-chip);
|
|
100
|
+
}
|
|
101
|
+
.agent-bar .agent-menu__tab {
|
|
102
|
+
flex: 1 1 0;
|
|
103
|
+
min-width: 0;
|
|
104
|
+
height: 24px;
|
|
105
|
+
display: flex;
|
|
106
|
+
align-items: center;
|
|
107
|
+
justify-content: center;
|
|
108
|
+
gap: 6px;
|
|
109
|
+
padding: 0 6px;
|
|
110
|
+
border: 0;
|
|
111
|
+
border-radius: 6px;
|
|
112
|
+
background: transparent;
|
|
113
|
+
font-size: 12px;
|
|
114
|
+
font-weight: 500;
|
|
115
|
+
color: var(--agent-bar-muted);
|
|
116
|
+
}
|
|
117
|
+
.agent-bar .agent-menu__tab[aria-selected="true"] {
|
|
118
|
+
background: var(--agent-bar-surface);
|
|
119
|
+
box-shadow: 0 1px 2px rgba(15, 23, 42, 0.14);
|
|
120
|
+
color: var(--agent-bar-text);
|
|
121
|
+
}
|
|
122
|
+
body.dark .agent-bar .agent-menu__tab[aria-selected="true"] {
|
|
123
|
+
background: #4a4b4d;
|
|
124
|
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
|
|
125
|
+
}
|
|
126
|
+
.agent-bar .agent-menu__tab:focus-visible {
|
|
127
|
+
box-shadow: inset 0 0 0 1px var(--agent-bar-border-strong);
|
|
128
|
+
}
|
|
129
|
+
.agent-menu__tab-label {
|
|
130
|
+
overflow: hidden;
|
|
131
|
+
text-overflow: ellipsis;
|
|
132
|
+
white-space: nowrap;
|
|
133
|
+
}
|
|
134
|
+
.agent-menu__count {
|
|
135
|
+
font-size: 11px;
|
|
136
|
+
font-weight: 400;
|
|
137
|
+
font-variant-numeric: tabular-nums;
|
|
138
|
+
color: var(--agent-bar-muted);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/* A touch screen zooms into a field whose text is smaller than 16px. */
|
|
142
|
+
@media (pointer: coarse) {
|
|
143
|
+
.agent-bar .agent-menu__input {
|
|
144
|
+
font-size: 16px;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/* the full-screen view on a phone (agent-bar-compose.js): its rows are taller */
|
|
148
|
+
html .agent-bar.agent-compose[data-compose="screen"] .agent-menu__search {
|
|
149
|
+
height: 40px;
|
|
150
|
+
}
|
|
151
|
+
html .agent-bar.agent-compose[data-compose="screen"] .agent-menu__tab {
|
|
152
|
+
height: 32px;
|
|
153
|
+
font-size: 14px;
|
|
154
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
// Agent bar: the agent menu, with a search box and, when both kinds are
|
|
2
|
+
// installed, two tabs: Terminal (agents that run in a terminal) and Desktop
|
|
3
|
+
// (plugins that open a desktop app). Both bars draw their agent menu through
|
|
4
|
+
// agentMenuRows in agent-bar.js, which hands it to fill() here.
|
|
5
|
+
//
|
|
6
|
+
// - The menu opens on the tab that holds the chosen agent, with its row in view.
|
|
7
|
+
// - With a keyboard the search box takes the cursor as the menu opens. On a
|
|
8
|
+
// touch screen it does not, so the on-screen keyboard does not cover the list.
|
|
9
|
+
// - Typing searches both tabs, by name and by folder name. While there is text
|
|
10
|
+
// the tabs step aside and the matches show under the two headings. The first
|
|
11
|
+
// match is lit, so Enter picks it; Up and Down move. Escape clears the text,
|
|
12
|
+
// then closes the menu.
|
|
13
|
+
// - The search box and the tabs stay put; only the list scrolls.
|
|
14
|
+
// - Two agents with the same name show their folder names.
|
|
15
|
+
//
|
|
16
|
+
// The bars redraw their menu from empty whenever anything in them changes (a
|
|
17
|
+
// running count, a message). So what was typed, the tab, the lit row, the
|
|
18
|
+
// list's scroll and the cursor are kept here, per menu, and put back on every
|
|
19
|
+
// redraw. They start over each time the menu opens.
|
|
20
|
+
(function (window, document) {
|
|
21
|
+
'use strict';
|
|
22
|
+
if (window.PinokioAgentMenu) return;
|
|
23
|
+
|
|
24
|
+
const views = new WeakMap();
|
|
25
|
+
const helpers = () => window.PinokioAgentBar.helpers;
|
|
26
|
+
const isTouch = () => Boolean(window.matchMedia && window.matchMedia('(hover: none)').matches);
|
|
27
|
+
const isDesktop = (tool) => tool.category === 'IDE';
|
|
28
|
+
const folderOf = (tool) => String(tool.value || '').split('/').filter(Boolean).pop() || '';
|
|
29
|
+
const byLabel = (a, b) => String(a.label).toLowerCase().localeCompare(String(b.label).toLowerCase())
|
|
30
|
+
|| folderOf(a).toLowerCase().localeCompare(folderOf(b).toLowerCase());
|
|
31
|
+
const wordsOf = (text) => String(text || '').toLowerCase().split(/\s+/).filter(Boolean);
|
|
32
|
+
const matches = (tool, words) => {
|
|
33
|
+
const hay = `${tool.label} ${folderOf(tool)}`.toLowerCase();
|
|
34
|
+
return words.every((word) => hay.includes(word));
|
|
35
|
+
};
|
|
36
|
+
// Setting `hidden` to the value it already has still counts as a change for
|
|
37
|
+
// anything watching the bar, so it is set only when it differs.
|
|
38
|
+
const setHidden = (node, hidden) => {
|
|
39
|
+
if (node.hidden !== hidden) node.hidden = hidden;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function makeView(menu) {
|
|
43
|
+
const { el, faIcon } = helpers();
|
|
44
|
+
const v = {
|
|
45
|
+
menu,
|
|
46
|
+
open: false,
|
|
47
|
+
query: '',
|
|
48
|
+
tab: 'terminal',
|
|
49
|
+
active: -1,
|
|
50
|
+
rows: [],
|
|
51
|
+
scroll: 0,
|
|
52
|
+
focused: false,
|
|
53
|
+
caret: null,
|
|
54
|
+
back: null,
|
|
55
|
+
tools: [],
|
|
56
|
+
loaded: false,
|
|
57
|
+
current: '',
|
|
58
|
+
pick: null,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
v.head = el('div', 'agent-menu__head');
|
|
62
|
+
v.search = el('label', 'agent-menu__search');
|
|
63
|
+
v.input = el('input', 'agent-menu__input');
|
|
64
|
+
v.input.type = 'text';
|
|
65
|
+
v.input.placeholder = 'Search agents';
|
|
66
|
+
v.input.autocomplete = 'off';
|
|
67
|
+
v.input.spellcheck = false;
|
|
68
|
+
v.input.setAttribute('aria-label', 'Search agents');
|
|
69
|
+
v.input.setAttribute('enterkeyhint', 'go');
|
|
70
|
+
v.clear = el('button', 'agent-menu__clear');
|
|
71
|
+
v.clear.type = 'button';
|
|
72
|
+
v.clear.hidden = true;
|
|
73
|
+
v.clear.setAttribute('aria-label', 'Clear search');
|
|
74
|
+
v.clear.appendChild(faIcon('fa-solid fa-xmark'));
|
|
75
|
+
v.search.append(faIcon('fa-solid fa-magnifying-glass agent-menu__glass'), v.input, v.clear);
|
|
76
|
+
|
|
77
|
+
v.tabs = el('div', 'agent-menu__tabs');
|
|
78
|
+
v.tabs.setAttribute('role', 'tablist');
|
|
79
|
+
v.tabs.setAttribute('aria-label', 'Kind of agent');
|
|
80
|
+
v.counts = {};
|
|
81
|
+
[['terminal', 'Terminal'], ['desktop', 'Desktop']].forEach(([key, label]) => {
|
|
82
|
+
const tab = el('button', 'agent-menu__tab');
|
|
83
|
+
tab.type = 'button';
|
|
84
|
+
tab.dataset.tab = key;
|
|
85
|
+
tab.setAttribute('role', 'tab');
|
|
86
|
+
v.counts[key] = el('span', 'agent-menu__count');
|
|
87
|
+
tab.append(el('span', 'agent-menu__tab-label', label), v.counts[key]);
|
|
88
|
+
v.tabs.appendChild(tab);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
v.head.append(v.search, v.tabs);
|
|
92
|
+
v.list = el('div', 'agent-menu__list');
|
|
93
|
+
|
|
94
|
+
// ---- the search box ----
|
|
95
|
+
const keepCaret = () => {
|
|
96
|
+
v.caret = [v.input.selectionStart, v.input.selectionEnd];
|
|
97
|
+
};
|
|
98
|
+
v.input.addEventListener('focus', () => { v.focused = true; });
|
|
99
|
+
// A redraw takes the box out of the page for a moment, which may count as
|
|
100
|
+
// leaving it. Whether it was really left is decided once the redraw is over.
|
|
101
|
+
v.input.addEventListener('blur', () => {
|
|
102
|
+
window.setTimeout(() => { v.focused = document.activeElement === v.input; }, 0);
|
|
103
|
+
});
|
|
104
|
+
['keyup', 'pointerup', 'select'].forEach((type) => v.input.addEventListener(type, keepCaret));
|
|
105
|
+
v.input.addEventListener('input', () => {
|
|
106
|
+
keepCaret();
|
|
107
|
+
v.query = v.input.value;
|
|
108
|
+
v.active = wordsOf(v.query).length ? 0 : -1;
|
|
109
|
+
v.scroll = 0;
|
|
110
|
+
draw(v);
|
|
111
|
+
v.list.scrollTop = 0;
|
|
112
|
+
});
|
|
113
|
+
v.input.addEventListener('keydown', (event) => {
|
|
114
|
+
if (event.isComposing) return;
|
|
115
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
if (!v.rows.length) return;
|
|
118
|
+
const step = event.key === 'ArrowDown' ? 1 : -1;
|
|
119
|
+
if (v.active < 0) v.active = step > 0 ? 0 : v.rows.length - 1;
|
|
120
|
+
else v.active = Math.min(v.rows.length - 1, Math.max(0, v.active + step));
|
|
121
|
+
paintActive(v);
|
|
122
|
+
reveal(v, v.rows[v.active]);
|
|
123
|
+
} else if (event.key === 'Enter') {
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
const row = v.rows[v.active];
|
|
126
|
+
if (row) choose(v, row.tool);
|
|
127
|
+
} else if (event.key === 'Escape') {
|
|
128
|
+
if (v.query) {
|
|
129
|
+
// the text goes first; the menu stays
|
|
130
|
+
event.preventDefault();
|
|
131
|
+
event.stopPropagation();
|
|
132
|
+
clearQuery(v);
|
|
133
|
+
} else {
|
|
134
|
+
// the bar closes the menu; the cursor goes back to where it was
|
|
135
|
+
window.setTimeout(() => {
|
|
136
|
+
if (!v.open && v.back && v.back.isConnected) v.back.focus({ preventScroll: true });
|
|
137
|
+
}, 0);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
// pressing the clear button leaves the cursor in the box
|
|
142
|
+
v.clear.addEventListener('pointerdown', (event) => { event.preventDefault(); });
|
|
143
|
+
v.clear.addEventListener('click', (event) => {
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
clearQuery(v);
|
|
146
|
+
v.input.focus({ preventScroll: true });
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// ---- the tabs ----
|
|
150
|
+
// A press on a tab leaves the cursor where it is (the search box, or the
|
|
151
|
+
// text on a phone, whose keyboard would otherwise drop and move the screen).
|
|
152
|
+
v.tabs.addEventListener('pointerdown', (event) => {
|
|
153
|
+
if (event.target.closest && event.target.closest('.agent-menu__tab')) event.preventDefault();
|
|
154
|
+
});
|
|
155
|
+
v.tabs.addEventListener('click', (event) => {
|
|
156
|
+
const tab = event.target.closest && event.target.closest('.agent-menu__tab');
|
|
157
|
+
if (!tab) return;
|
|
158
|
+
setTab(v, tab.dataset.tab);
|
|
159
|
+
// Enter or Space on a tab leaves the cursor on the tab
|
|
160
|
+
if (event.detail === 0) tab.focus({ preventScroll: true });
|
|
161
|
+
});
|
|
162
|
+
v.tabs.addEventListener('keydown', (event) => {
|
|
163
|
+
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
|
|
164
|
+
event.preventDefault();
|
|
165
|
+
const key = event.key === 'ArrowLeft' ? 'terminal' : 'desktop';
|
|
166
|
+
setTab(v, key);
|
|
167
|
+
const tab = v.tabs.querySelector(`[data-tab="${key}"]`);
|
|
168
|
+
if (tab) tab.focus({ preventScroll: true });
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
v.list.addEventListener('scroll', () => {
|
|
172
|
+
v.scroll = v.list.scrollTop;
|
|
173
|
+
menu.classList.toggle('is-scrolled', v.list.scrollTop > 0);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// The menu closing (or turning into another of the bar's menus, which use the
|
|
177
|
+
// same box) ends this opening; the next one starts over.
|
|
178
|
+
new MutationObserver(() => {
|
|
179
|
+
if (!menu.hidden && menu.dataset.menu === 'agent' && menu.contains(v.head)) return;
|
|
180
|
+
v.open = false;
|
|
181
|
+
menu.classList.remove('agent-menu', 'is-scrolled');
|
|
182
|
+
}).observe(menu, { attributes: true, attributeFilter: ['hidden', 'data-menu'], childList: true });
|
|
183
|
+
|
|
184
|
+
return v;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function clearQuery(v) {
|
|
188
|
+
v.input.value = '';
|
|
189
|
+
v.query = '';
|
|
190
|
+
v.active = -1;
|
|
191
|
+
v.scroll = 0;
|
|
192
|
+
v.caret = null;
|
|
193
|
+
draw(v);
|
|
194
|
+
v.list.scrollTop = 0;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function setTab(v, key) {
|
|
198
|
+
if (v.tab === key) return;
|
|
199
|
+
v.tab = key;
|
|
200
|
+
v.active = -1;
|
|
201
|
+
v.scroll = 0;
|
|
202
|
+
draw(v);
|
|
203
|
+
v.list.scrollTop = 0;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function choose(v, tool) {
|
|
207
|
+
if (typeof v.pick === 'function') v.pick(tool);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function paintActive(v) {
|
|
211
|
+
v.rows.forEach((row, index) => row.node.classList.toggle('is-active', index === v.active));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Scrolls the list (never the page) just enough to show the row.
|
|
215
|
+
function reveal(v, row) {
|
|
216
|
+
if (!row) return;
|
|
217
|
+
const list = v.list;
|
|
218
|
+
const top = row.node.offsetTop - list.offsetTop;
|
|
219
|
+
const bottom = top + row.node.offsetHeight;
|
|
220
|
+
if (top < list.scrollTop) list.scrollTop = Math.max(0, top - 4);
|
|
221
|
+
else if (bottom > list.scrollTop + list.clientHeight) list.scrollTop = bottom - list.clientHeight + 4;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function draw(v) {
|
|
225
|
+
const { el, menuRow, toolIcon } = helpers();
|
|
226
|
+
const terminal = v.tools.filter((tool) => !isDesktop(tool)).sort(byLabel);
|
|
227
|
+
const desktop = v.tools.filter(isDesktop).sort(byLabel);
|
|
228
|
+
const both = terminal.length > 0 && desktop.length > 0;
|
|
229
|
+
const words = wordsOf(v.query);
|
|
230
|
+
const searching = words.length > 0;
|
|
231
|
+
if (!both && desktop.length && !terminal.length) v.tab = 'desktop';
|
|
232
|
+
if (!both && terminal.length && !desktop.length) v.tab = 'terminal';
|
|
233
|
+
|
|
234
|
+
setHidden(v.search, !v.loaded || !v.tools.length);
|
|
235
|
+
setHidden(v.clear, !v.query);
|
|
236
|
+
setHidden(v.tabs, !both || searching);
|
|
237
|
+
setHidden(v.head, v.search.hidden && v.tabs.hidden);
|
|
238
|
+
v.counts.terminal.textContent = String(terminal.length);
|
|
239
|
+
v.counts.desktop.textContent = String(desktop.length);
|
|
240
|
+
v.tabs.querySelectorAll('.agent-menu__tab').forEach((tab) => {
|
|
241
|
+
const on = tab.dataset.tab === v.tab;
|
|
242
|
+
tab.setAttribute('aria-selected', String(on));
|
|
243
|
+
tab.tabIndex = on ? 0 : -1;
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// the same name twice: the folder name tells them apart
|
|
247
|
+
const seen = {};
|
|
248
|
+
v.tools.forEach((tool) => { seen[tool.label] = (seen[tool.label] || 0) + 1; });
|
|
249
|
+
|
|
250
|
+
v.list.textContent = '';
|
|
251
|
+
v.rows = [];
|
|
252
|
+
const note = (text) => v.list.appendChild(el('div', 'agent-bar__menu-empty', text));
|
|
253
|
+
const addRow = (tool) => {
|
|
254
|
+
const index = v.rows.length;
|
|
255
|
+
const node = menuRow(
|
|
256
|
+
toolIcon(tool),
|
|
257
|
+
tool.label,
|
|
258
|
+
seen[tool.label] > 1 ? folderOf(tool) : '',
|
|
259
|
+
tool.value === v.current,
|
|
260
|
+
() => choose(v, tool),
|
|
261
|
+
);
|
|
262
|
+
node.addEventListener('mousemove', () => {
|
|
263
|
+
if (v.active === index) return;
|
|
264
|
+
v.active = index;
|
|
265
|
+
paintActive(v);
|
|
266
|
+
});
|
|
267
|
+
v.rows.push({ tool, node });
|
|
268
|
+
v.list.appendChild(node);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
if (!v.loaded) {
|
|
272
|
+
note('Loading…');
|
|
273
|
+
} else if (!v.tools.length) {
|
|
274
|
+
note('No agents available');
|
|
275
|
+
} else if (searching) {
|
|
276
|
+
const groups = [['Terminal', terminal], ['Desktop', desktop]]
|
|
277
|
+
.map(([title, tools]) => [title, tools.filter((tool) => matches(tool, words))])
|
|
278
|
+
.filter(([, tools]) => tools.length);
|
|
279
|
+
if (!groups.length) note(`No agents match “${v.query.trim()}”`);
|
|
280
|
+
groups.forEach(([title, tools]) => {
|
|
281
|
+
if (both) v.list.appendChild(el('div', 'agent-bar__menu-title', title));
|
|
282
|
+
tools.forEach(addRow);
|
|
283
|
+
});
|
|
284
|
+
} else {
|
|
285
|
+
(v.tab === 'desktop' ? desktop : terminal).forEach(addRow);
|
|
286
|
+
}
|
|
287
|
+
if (v.active >= v.rows.length) v.active = v.rows.length - 1;
|
|
288
|
+
paintActive(v);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Called by agentMenuRows (agent-bar.js) each time a bar draws its agent menu,
|
|
292
|
+
// right after emptying it. `pick(tool)` is called with the one chosen.
|
|
293
|
+
function fill(menu, tools, loaded, current, pick) {
|
|
294
|
+
const v = views.get(menu) || makeView(menu);
|
|
295
|
+
views.set(menu, v);
|
|
296
|
+
const fresh = !v.open;
|
|
297
|
+
v.open = true;
|
|
298
|
+
v.tools = Array.isArray(tools) ? tools : [];
|
|
299
|
+
v.loaded = Boolean(loaded);
|
|
300
|
+
v.current = current || '';
|
|
301
|
+
v.pick = pick;
|
|
302
|
+
menu.classList.add('agent-menu');
|
|
303
|
+
|
|
304
|
+
if (fresh) {
|
|
305
|
+
const active = document.activeElement;
|
|
306
|
+
v.back = active && active !== document.body && !menu.contains(active) ? active : null;
|
|
307
|
+
v.input.value = '';
|
|
308
|
+
v.query = '';
|
|
309
|
+
v.active = -1;
|
|
310
|
+
v.scroll = 0;
|
|
311
|
+
v.focused = false;
|
|
312
|
+
v.caret = null;
|
|
313
|
+
v.placed = false;
|
|
314
|
+
v.wantsCursor = !isTouch();
|
|
315
|
+
}
|
|
316
|
+
// Once the agents are known: the tab that holds the chosen one. (While they
|
|
317
|
+
// are still being read there is nothing to place, and no search box.)
|
|
318
|
+
const placing = v.loaded && !v.placed;
|
|
319
|
+
if (placing) {
|
|
320
|
+
const chosen = v.tools.find((tool) => tool.value === v.current);
|
|
321
|
+
v.tab = chosen && isDesktop(chosen) ? 'desktop' : 'terminal';
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
menu.append(v.head, v.list);
|
|
325
|
+
draw(v);
|
|
326
|
+
v.list.scrollTop = v.scroll;
|
|
327
|
+
if (placing) {
|
|
328
|
+
v.placed = true;
|
|
329
|
+
reveal(v, v.rows.find((row) => row.tool.value === v.current));
|
|
330
|
+
v.scroll = v.list.scrollTop;
|
|
331
|
+
}
|
|
332
|
+
menu.classList.toggle('is-scrolled', v.list.scrollTop > 0);
|
|
333
|
+
|
|
334
|
+
if (v.wantsCursor && !v.search.hidden) {
|
|
335
|
+
v.wantsCursor = false;
|
|
336
|
+
v.input.focus({ preventScroll: true });
|
|
337
|
+
} else if (!fresh && v.focused && document.activeElement !== v.input) {
|
|
338
|
+
v.input.focus({ preventScroll: true });
|
|
339
|
+
if (v.caret) {
|
|
340
|
+
try { v.input.setSelectionRange(v.caret[0], v.caret[1]); } catch (_) { /* not a text field */ }
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
window.PinokioAgentMenu = { fill };
|
|
346
|
+
})(window, document);
|