pi-durable-bot 0.7.2 → 0.8.0
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/CHANGELOG.md +26 -0
- package/README.md +10 -2
- package/dist/agent/agent.js +2 -1
- package/dist/agent/agent.js.map +1 -1
- package/dist/agent/scopes.js +267 -0
- package/dist/agent/scopes.js.map +1 -0
- package/dist/agent/screen.js +37 -2
- package/dist/agent/screen.js.map +1 -1
- package/dist/bot/bot.js +58 -11
- package/dist/bot/bot.js.map +1 -1
- package/dist/bot/definition.js +7 -0
- package/dist/bot/definition.js.map +1 -1
- package/dist/bot/prompt.js +26 -9
- package/dist/bot/prompt.js.map +1 -1
- package/dist/cli/bots.js +69 -5
- package/dist/cli/bots.js.map +1 -1
- package/dist/cli/chat.js +59 -4
- package/dist/cli/chat.js.map +1 -1
- package/dist/cli/commands.js +23 -2
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/init.js +4 -19
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/pages.js +25 -6
- package/dist/cli/pages.js.map +1 -1
- package/dist/cli/service.js +30 -1
- package/dist/cli/service.js.map +1 -1
- package/dist/main.js +8 -0
- package/dist/main.js.map +1 -1
- package/dist/pages/desktop-page.js +293 -0
- package/dist/pages/desktop-page.js.map +1 -0
- package/dist/pages/desktop.js +672 -0
- package/dist/pages/desktop.js.map +1 -0
- package/dist/pages/host.js +55 -2
- package/dist/pages/host.js.map +1 -1
- package/dist/pages/rfb.js +389 -0
- package/dist/pages/rfb.js.map +1 -0
- package/dist/pages/server.js +172 -1
- package/dist/pages/server.js.map +1 -1
- package/dist/pages/store.js +26 -4
- package/dist/pages/store.js.map +1 -1
- package/docs/for-agents.md +17 -2
- package/kit/desktop/desktop.css +1010 -0
- package/kit/desktop/desktop.js +1421 -0
- package/kit/desktop/novnc/AUTHORS +13 -0
- package/kit/desktop/novnc/LICENSE.BSD-2-Clause +22 -0
- package/kit/desktop/novnc/LICENSE.BSD-3-Clause +24 -0
- package/kit/desktop/novnc/LICENSE.MPL-2.0 +373 -0
- package/kit/desktop/novnc/LICENSE.OFL-1.1 +91 -0
- package/kit/desktop/novnc/LICENSE.pako +21 -0
- package/kit/desktop/novnc/LICENSE.txt +62 -0
- package/kit/desktop/novnc.js +14404 -0
- package/package.json +5 -5
- package/skills/desktop-page/SKILL.md +153 -0
- package/skills/discord-channel/SKILL.md +4 -4
- package/skills/pages/SKILL.md +9 -4
- package/skills/slack-channel/SKILL.md +6 -5
- package/skills/telegram-channel/SKILL.md +4 -4
|
@@ -0,0 +1,1421 @@
|
|
|
1
|
+
// The desktop page of Pots: a person sees and controls the bot's screen, does one step, and hands it back. Pots serves
|
|
2
|
+
// this page for a desktop link (/d/<token>/) and carries its WebSocket (ws) to the VNC server of the bot's screen,
|
|
3
|
+
// signed in already; noVNC (novnc.js) draws the picture and sends the mouse and keys. This file adds what a person on a
|
|
4
|
+
// phone needs: the picture fills the screen and takes the phone's shape when the desktop can change size, pinch zooms
|
|
5
|
+
// the picture (not the page under it), a typing field sends each character as it is typed, and a few floating
|
|
6
|
+
// controls. On a computer, the keyboard types straight in. Every request goes relative to the page's address, so a
|
|
7
|
+
// path that a proxy puts in front stays.
|
|
8
|
+
|
|
9
|
+
const facts = JSON.parse(document.getElementById("pots-facts").textContent);
|
|
10
|
+
const W = facts.words;
|
|
11
|
+
const $ = (id) => document.getElementById(id);
|
|
12
|
+
const fill = (text, values) => text.replace(/\{(\w+)\}/g, (all, key) => (key in values ? String(values[key]) : all));
|
|
13
|
+
const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
|
|
14
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
15
|
+
|
|
16
|
+
/** A phone or a tablet: touch first, no keyboard to count on. */
|
|
17
|
+
const touch = matchMedia("(pointer: coarse)").matches;
|
|
18
|
+
const localApple = /Mac|iPhone|iPad|iPod/.test(navigator.platform || "") || /Mac OS X/.test(navigator.userAgent);
|
|
19
|
+
/** On a Mac, ⌘ is what Ctrl is on the desktop of a Linux or Windows computer. */
|
|
20
|
+
const cmdIsCtrl = localApple && facts.remote !== "darwin";
|
|
21
|
+
|
|
22
|
+
/** The desktop is at least this wide when it takes a phone's shape: browsers such as Chrome are no narrower. */
|
|
23
|
+
const MIN_WIDTH = 500;
|
|
24
|
+
const MIN_HEIGHT = 300;
|
|
25
|
+
/** On a computer, the desktop takes the window's size up to this. */
|
|
26
|
+
const MAX_WIDTH = 1920;
|
|
27
|
+
const MAX_HEIGHT = 1200;
|
|
28
|
+
const MAX_ZOOM = 5;
|
|
29
|
+
/** In trackpad mode, the pointer moves this much faster than the finger. */
|
|
30
|
+
const PAD_SPEED = 1.5;
|
|
31
|
+
|
|
32
|
+
const base = new URL(".", location.href);
|
|
33
|
+
|
|
34
|
+
/** Pots' answer to a request under the link's address; status 0 when there was no connection. */
|
|
35
|
+
async function api(path, body) {
|
|
36
|
+
try {
|
|
37
|
+
const response = await fetch(new URL(path, base), {
|
|
38
|
+
method: body === undefined ? "GET" : "POST",
|
|
39
|
+
cache: "no-store",
|
|
40
|
+
credentials: "same-origin",
|
|
41
|
+
headers: body === undefined ? undefined : { "content-type": "application/json" },
|
|
42
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
43
|
+
});
|
|
44
|
+
let value = {};
|
|
45
|
+
try {
|
|
46
|
+
value = await response.json();
|
|
47
|
+
} catch {
|
|
48
|
+
// not JSON
|
|
49
|
+
}
|
|
50
|
+
return { status: response.status, ...value };
|
|
51
|
+
} catch {
|
|
52
|
+
return { status: 0, ok: false };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ---- The link ----
|
|
57
|
+
|
|
58
|
+
/** The link's state from Pots: state, mine, now, expires, started, resizable, size. */
|
|
59
|
+
let link = null;
|
|
60
|
+
/** Pots' clock minus this device's. */
|
|
61
|
+
let offset = 0;
|
|
62
|
+
let over = false;
|
|
63
|
+
|
|
64
|
+
const serverNow = () => Date.now() + offset;
|
|
65
|
+
const localTime = (ms) => new Date(ms - offset).toLocaleTimeString(document.documentElement.lang || [], { hour: "2-digit", minute: "2-digit" });
|
|
66
|
+
|
|
67
|
+
async function refresh() {
|
|
68
|
+
const answer = await api("state");
|
|
69
|
+
// While Pots restarts, its page server may not know the link for a moment; a session waits for it to come back.
|
|
70
|
+
if (answer.status === 404) return $("session") ? answer : showEnded("missing");
|
|
71
|
+
if (!answer.state) return answer;
|
|
72
|
+
link = answer;
|
|
73
|
+
offset = answer.now - Date.now();
|
|
74
|
+
if (answer.state !== "open" && answer.state !== "started") showEnded(answer.state);
|
|
75
|
+
return answer;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ---- The start screen ----
|
|
79
|
+
|
|
80
|
+
const startButton = $("start-button");
|
|
81
|
+
const startMessage = (text) => ($("start-msg").textContent = text || "");
|
|
82
|
+
|
|
83
|
+
function showStart() {
|
|
84
|
+
if (!link) return;
|
|
85
|
+
$("start-meta").textContent = `${fill(W.linkUntil, { time: localTime(link.expires) })} · ${fill(W.afterStart, { n: facts.minutes })}`;
|
|
86
|
+
if (link.state === "started" && !link.mine) {
|
|
87
|
+
startButton.disabled = true;
|
|
88
|
+
startMessage(W.elsewhere);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
startButton.textContent = link.state === "started" ? W.continue : W.start;
|
|
92
|
+
startButton.disabled = false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function start() {
|
|
96
|
+
startButton.disabled = true;
|
|
97
|
+
startMessage("");
|
|
98
|
+
const answer = await api("start", {});
|
|
99
|
+
if (!answer.ok) {
|
|
100
|
+
if (answer.status === 0) startMessage(W.network);
|
|
101
|
+
else if (answer.code === "elsewhere") startMessage(W.elsewhere);
|
|
102
|
+
else if (answer.code === "busy") startMessage(W.busy);
|
|
103
|
+
else if (answer.code === "unreachable") startMessage(W.unreachable);
|
|
104
|
+
else if (answer.status === 404 || answer.status === 410) return refresh();
|
|
105
|
+
else startMessage(answer.error || W.network);
|
|
106
|
+
startButton.disabled = answer.code === "elsewhere";
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
offset = answer.now - Date.now();
|
|
110
|
+
link = { ...link, state: "started", mine: true, expires: answer.expires, started: answer.started, resizable: answer.resizable, size: answer.size };
|
|
111
|
+
await openSession();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---- The session ----
|
|
115
|
+
|
|
116
|
+
let novnc = null;
|
|
117
|
+
let rfb = null;
|
|
118
|
+
let connected = false;
|
|
119
|
+
let attempts = 0;
|
|
120
|
+
let reconnectTimer;
|
|
121
|
+
let closeCode = 0;
|
|
122
|
+
let everConnected = false;
|
|
123
|
+
/** match: the desktop takes this screen's shape; whole: its own size, fitted; actual: its own size, 1:1. */
|
|
124
|
+
const view = { mode: "match", zoom: 1, x: 0, y: 0 };
|
|
125
|
+
let resizeRefused = false;
|
|
126
|
+
let resizeTimer;
|
|
127
|
+
let trackpad = false;
|
|
128
|
+
let pointer = null;
|
|
129
|
+
let lastTap = null;
|
|
130
|
+
let keyboardOpen = false;
|
|
131
|
+
let warnedMinute = false;
|
|
132
|
+
|
|
133
|
+
function h(tag, attributes = {}, ...children) {
|
|
134
|
+
const element = document.createElement(tag);
|
|
135
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
136
|
+
if (value === false || value === undefined || value === null) continue;
|
|
137
|
+
if (name === "class") element.className = value;
|
|
138
|
+
else if (name === "text") element.textContent = value;
|
|
139
|
+
else if (name.startsWith("on")) element.addEventListener(name.slice(2), value);
|
|
140
|
+
else element.setAttribute(name, value === true ? "" : value);
|
|
141
|
+
}
|
|
142
|
+
for (const child of children) if (child !== null && child !== undefined) element.append(child);
|
|
143
|
+
return element;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const SVG = "http://www.w3.org/2000/svg";
|
|
147
|
+
function icon(paths, extra = []) {
|
|
148
|
+
const svg = document.createElementNS(SVG, "svg");
|
|
149
|
+
svg.setAttribute("viewBox", "0 0 24 24");
|
|
150
|
+
svg.setAttribute("aria-hidden", "true");
|
|
151
|
+
for (const d of paths) {
|
|
152
|
+
const path = document.createElementNS(SVG, "path");
|
|
153
|
+
path.setAttribute("d", d);
|
|
154
|
+
svg.append(path);
|
|
155
|
+
}
|
|
156
|
+
for (const [tag, attributes] of extra) {
|
|
157
|
+
const element = document.createElementNS(SVG, tag);
|
|
158
|
+
for (const [name, value] of Object.entries(attributes)) element.setAttribute(name, value);
|
|
159
|
+
svg.append(element);
|
|
160
|
+
}
|
|
161
|
+
return svg;
|
|
162
|
+
}
|
|
163
|
+
const keyboardIcon = () => icon(["M6.5 10h.01M9.5 10h.01M12.5 10h.01M15.5 10h.01M18 10h.01M8 14.2h8"], [["rect", { x: "2.5", y: "6", width: "19", height: "12", rx: "2.5" }]]);
|
|
164
|
+
const moreIcon = () => icon([], [["circle", { cx: "5.5", cy: "12", r: "1.2" }], ["circle", { cx: "12", cy: "12", r: "1.2" }], ["circle", { cx: "18.5", cy: "12", r: "1.2" }]]);
|
|
165
|
+
const checkIcon = () => icon(["M5 12.5l4.5 4.5L19 7.5"]);
|
|
166
|
+
const linesIcon = () => icon(["M5 7h14M5 12h14M5 17h9"]);
|
|
167
|
+
const closeIcon = () => icon(["M6.5 6.5l11 11M17.5 6.5l-11 11"]);
|
|
168
|
+
const fitIcon = () => icon(["M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5"]);
|
|
169
|
+
|
|
170
|
+
function buildSession() {
|
|
171
|
+
const session = h("div", { id: "session", class: touch ? "" : "desktop" });
|
|
172
|
+
// One group of controls that floats on the picture: the task button (the connection's dot and the time left; a tap
|
|
173
|
+
// shows the bot's words), keyboard, more and Done.
|
|
174
|
+
const dock = h(
|
|
175
|
+
"div",
|
|
176
|
+
{ id: "dock", class: "glass" },
|
|
177
|
+
h(
|
|
178
|
+
"button",
|
|
179
|
+
{ type: "button", class: "chip", id: "chip", "aria-label": W.task, title: W.task, "aria-expanded": "false", "aria-controls": "hint", onclick: () => toggleHint() },
|
|
180
|
+
h("i", { class: "dot", id: "dot" }),
|
|
181
|
+
h("span", { class: "time", id: "time" }),
|
|
182
|
+
linesIcon(),
|
|
183
|
+
),
|
|
184
|
+
h("button", { type: "button", class: "zoomed", id: "zoom", hidden: true, onclick: () => setMode(view.mode === "actual" ? "whole" : view.mode, true) }),
|
|
185
|
+
touch ? h("button", { type: "button", class: "ib", id: "keyboard-button", "aria-label": W.keyboard, title: W.keyboard, onclick: () => openKeyboard() }, keyboardIcon()) : null,
|
|
186
|
+
h("button", { type: "button", class: "ib", id: "more-button", "aria-label": W.more, title: W.more, onclick: () => (touch ? openSheet("more") : toggleMenu()) }, moreIcon()),
|
|
187
|
+
h("button", { type: "button", class: "done", title: W.done, onclick: () => openSheet("hand-back") }, checkIcon(), h("span", { text: W.done })),
|
|
188
|
+
);
|
|
189
|
+
dragDock(dock);
|
|
190
|
+
|
|
191
|
+
// The bot's words in full, as the start screen showed them, and how to use the page.
|
|
192
|
+
const words = h("div", { class: "hint-text words", id: "hint-text" });
|
|
193
|
+
for (const node of $("ask").childNodes) words.append(node.cloneNode(true));
|
|
194
|
+
const inputHint = facts.input === "password" ? W.hintPassword : facts.input === "code" ? W.hintCode : facts.input === "text" ? W.hintText : null;
|
|
195
|
+
const tips = touch ? [W.gestures, inputHint] : [cmdIsCtrl ? W.keysHintMac : W.keysHint];
|
|
196
|
+
const hint = h(
|
|
197
|
+
"div",
|
|
198
|
+
{ id: "hint", class: "glass away", role: "dialog", "aria-label": W.task, hidden: true },
|
|
199
|
+
h("div", { class: "hint-top" }, h("span", { text: W.asks }), h("button", { type: "button", class: "x", "aria-label": W.fold, title: W.fold, onclick: () => closeHint() }, closeIcon())),
|
|
200
|
+
words,
|
|
201
|
+
h("div", { class: "hint-tips" }, ...tips.filter(Boolean).map((tip) => h("p", { text: tip }))),
|
|
202
|
+
h("div", { class: "hint-meta", id: "hint-meta" }),
|
|
203
|
+
);
|
|
204
|
+
// Read in its own time: a touch, a scroll or the mouse on it keeps it open.
|
|
205
|
+
for (const type of ["pointerdown", "wheel"]) hint.addEventListener(type, holdHint, { passive: true });
|
|
206
|
+
words.addEventListener("scroll", () => (holdHint(), markOverflow()), { passive: true });
|
|
207
|
+
|
|
208
|
+
const typed = h("input", { id: "typed", autocomplete: "off", autocapitalize: "off", autocorrect: "off", spellcheck: "false", enterkeyhint: "send", "aria-label": fill(W.typeInto, {}) });
|
|
209
|
+
const strip = h(
|
|
210
|
+
"div",
|
|
211
|
+
{ id: "strip", hidden: true },
|
|
212
|
+
h("button", { type: "button", class: "k", id: "lock", title: W.hideTyped, "aria-label": W.hideTyped, text: "🔒", onpointerdown: keepFocus, onclick: toggleLock }),
|
|
213
|
+
h("label", {}, h("span", { text: W.typeInto }), typed),
|
|
214
|
+
h("button", { type: "button", class: "k", title: W.enter, "aria-label": W.enter, text: "⏎", onpointerdown: keepFocus, onclick: () => (sendKey("Return"), clearTyped()) }),
|
|
215
|
+
h("button", { type: "button", class: "k", title: W.closeKeyboard, "aria-label": W.closeKeyboard, text: "⌄", onpointerdown: keepFocus, onclick: closeKeyboard }),
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const tile = (glyph, label, action) =>
|
|
219
|
+
h(
|
|
220
|
+
"button",
|
|
221
|
+
{
|
|
222
|
+
type: "button",
|
|
223
|
+
class: "tile",
|
|
224
|
+
onclick: () => {
|
|
225
|
+
closeSheets();
|
|
226
|
+
action();
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
h("i", { text: glyph }),
|
|
230
|
+
label,
|
|
231
|
+
);
|
|
232
|
+
const arrows = h(
|
|
233
|
+
"div",
|
|
234
|
+
{ class: "tiles", id: "arrows", hidden: true },
|
|
235
|
+
tile("←", "", () => sendKey("Left")),
|
|
236
|
+
tile("→", "", () => sendKey("Right")),
|
|
237
|
+
tile("↑", "", () => sendKey("Up")),
|
|
238
|
+
tile("↓", "", () => sendKey("Down")),
|
|
239
|
+
);
|
|
240
|
+
const picture = pictureControl("sheet-picture");
|
|
241
|
+
const more = h(
|
|
242
|
+
"div",
|
|
243
|
+
{ id: "more", class: "sheet", hidden: true },
|
|
244
|
+
h("div", { class: "grab" }),
|
|
245
|
+
h(
|
|
246
|
+
"div",
|
|
247
|
+
{ class: "tiles" },
|
|
248
|
+
tile("⎋", W.esc, () => sendKey("Escape")),
|
|
249
|
+
tile("⇥", W.tab, () => sendKey("Tab")),
|
|
250
|
+
tile("⏎", W.enter, () => sendKey("Return")),
|
|
251
|
+
tile("⌫", W.backspace, () => sendKey("BackSpace")),
|
|
252
|
+
h("button", { type: "button", class: "tile", onclick: () => ($("arrows").hidden = !$("arrows").hidden) }, h("i", { text: "←→" }), "↑↓"),
|
|
253
|
+
tile("⌃A", W.selectAll, () => combo("Control_L", "a")),
|
|
254
|
+
tile("📋", W.paste, () => typeClipboard()),
|
|
255
|
+
tile("⟳", W.reconnect, () => reconnect(true)),
|
|
256
|
+
),
|
|
257
|
+
arrows,
|
|
258
|
+
h(
|
|
259
|
+
"div",
|
|
260
|
+
{ class: "list" },
|
|
261
|
+
h(
|
|
262
|
+
"button",
|
|
263
|
+
{
|
|
264
|
+
type: "button",
|
|
265
|
+
class: "li",
|
|
266
|
+
onclick: () => {
|
|
267
|
+
trackpad = !trackpad;
|
|
268
|
+
$("pad-toggle").classList.toggle("on", trackpad);
|
|
269
|
+
if (trackpad && rfb) showPointer(rfb);
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
h("span", {}, W.trackpad, h("small", { text: W.trackpadNote })),
|
|
273
|
+
h("span", { class: "tog", id: "pad-toggle" }),
|
|
274
|
+
),
|
|
275
|
+
h("div", { class: "li" }, h("span", { text: W.picture }), picture),
|
|
276
|
+
h("button", { type: "button", class: "li red", onclick: () => openSheet("hand-back") }, h("span", { text: W.cannotRow }), h("span", { text: "›" })),
|
|
277
|
+
),
|
|
278
|
+
);
|
|
279
|
+
const note = h("textarea", { id: "note", maxlength: "1000", rows: "2", placeholder: W.notePlaceholder });
|
|
280
|
+
const handBack = h(
|
|
281
|
+
"div",
|
|
282
|
+
{ id: "hand-back", class: "sheet", hidden: true, role: "dialog", "aria-labelledby": "hand-back-title" },
|
|
283
|
+
h("div", { class: "grab" }),
|
|
284
|
+
h("h2", { id: "hand-back-title", text: W.handBackTitle }),
|
|
285
|
+
h("p", { text: W.handBackNote }),
|
|
286
|
+
note,
|
|
287
|
+
h("p", { class: "msg", id: "hand-back-msg", role: "alert" }),
|
|
288
|
+
h("button", { type: "button", class: "pri", id: "hand-done", text: W.handBackDone, onclick: () => handBackNow("done") }),
|
|
289
|
+
h("button", { type: "button", class: "sec", id: "hand-cannot", text: W.handBackCannot, onclick: () => handBackNow("cannot") }),
|
|
290
|
+
h("button", { type: "button", class: "sec", text: W.keepGoing, onclick: closeSheets }),
|
|
291
|
+
);
|
|
292
|
+
const keyButton = (label, ...keys) => h("button", { type: "button", text: label, onclick: () => (combo(...keys), closeMenu()) });
|
|
293
|
+
const menu = h(
|
|
294
|
+
"div",
|
|
295
|
+
{ id: "menu", hidden: true },
|
|
296
|
+
h("button", { type: "button", class: "li", onclick: () => (fullscreen(), closeMenu()) }, h("span", { text: W.fullscreen }), h("kbd", { text: "F" })),
|
|
297
|
+
h("button", { type: "button", class: "li", onclick: () => (typeClipboard(), closeMenu()) }, h("span", { text: W.typeClipboard }), h("kbd", { text: cmdIsCtrl ? "⌘⇧V" : "" })),
|
|
298
|
+
h("div", { class: "li" }, h("span", { text: W.keys })),
|
|
299
|
+
h(
|
|
300
|
+
"div",
|
|
301
|
+
{ class: "keys" },
|
|
302
|
+
keyButton("Ctrl+Alt+Del", "Control_L", "Alt_L", "Delete"),
|
|
303
|
+
keyButton("Alt+Tab", "Alt_L", "Tab"),
|
|
304
|
+
keyButton("Alt+F4", "Alt_L", "F4"),
|
|
305
|
+
keyButton("Ctrl+Esc", "Control_L", "Escape"),
|
|
306
|
+
keyButton("Super", "Super_L"),
|
|
307
|
+
),
|
|
308
|
+
h("div", { class: "li" }, h("span", { text: W.picture }), pictureControl("menu-picture")),
|
|
309
|
+
h("button", { type: "button", class: "li", onclick: () => (reconnect(true), closeMenu()) }, h("span", { text: W.reconnect })),
|
|
310
|
+
h("button", { type: "button", class: "li red", onclick: () => (closeMenu(), openSheet("hand-back")) }, h("span", { text: W.cannotRow })),
|
|
311
|
+
);
|
|
312
|
+
session.append(
|
|
313
|
+
h("div", { id: "screen" }),
|
|
314
|
+
h("div", { id: "dim", hidden: true, onclick: () => !connected && reconnect(true) }, h("p", { id: "dim-text" })),
|
|
315
|
+
h("div", { id: "safe", "aria-hidden": "true" }),
|
|
316
|
+
dock,
|
|
317
|
+
hint,
|
|
318
|
+
h("div", { id: "toast", class: "glass", hidden: true }),
|
|
319
|
+
strip,
|
|
320
|
+
h("div", { id: "scrim", hidden: true, onclick: closeSheets }),
|
|
321
|
+
more,
|
|
322
|
+
handBack,
|
|
323
|
+
menu,
|
|
324
|
+
);
|
|
325
|
+
document.body.append(session);
|
|
326
|
+
|
|
327
|
+
typed.addEventListener("compositionstart", () => (composing = true));
|
|
328
|
+
typed.addEventListener("compositionend", () => {
|
|
329
|
+
composing = false;
|
|
330
|
+
syncTyped();
|
|
331
|
+
});
|
|
332
|
+
typed.addEventListener("input", (event) => {
|
|
333
|
+
if (!event.isComposing && !composing) syncTyped();
|
|
334
|
+
});
|
|
335
|
+
typed.addEventListener("keydown", onTypedKey);
|
|
336
|
+
applyInputKind();
|
|
337
|
+
|
|
338
|
+
if (window.visualViewport) {
|
|
339
|
+
visualViewport.addEventListener("resize", placeForKeyboard);
|
|
340
|
+
visualViewport.addEventListener("scroll", placeForKeyboard);
|
|
341
|
+
}
|
|
342
|
+
window.addEventListener("resize", () => (placeForKeyboard(), placeDock()));
|
|
343
|
+
// The dock keeps its edge when it changes size, as when the zoom shows or the time loses a digit.
|
|
344
|
+
new ResizeObserver(() => placeDock()).observe(dock);
|
|
345
|
+
// A press anywhere else folds the task panel and the menu; on the picture, the press goes on to the desktop too.
|
|
346
|
+
document.addEventListener("pointerdown", onPressAnywhere, true);
|
|
347
|
+
if (!touch) {
|
|
348
|
+
// Captured before noVNC, which keeps the keyboard's events to itself.
|
|
349
|
+
window.addEventListener("keydown", onDesktopKey, true);
|
|
350
|
+
}
|
|
351
|
+
dockPlace = loadDockPlace();
|
|
352
|
+
tick();
|
|
353
|
+
placeDock();
|
|
354
|
+
setInterval(tick, 1000);
|
|
355
|
+
setInterval(() => {
|
|
356
|
+
if (!over) refresh();
|
|
357
|
+
}, 15_000);
|
|
358
|
+
document.addEventListener("visibilitychange", () => {
|
|
359
|
+
if (document.hidden || over) return;
|
|
360
|
+
refresh();
|
|
361
|
+
if (!connected) reconnect(false);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function pictureControl(id) {
|
|
366
|
+
const button = (mode, label) => h("button", { type: "button", "data-mode": mode, text: label, onclick: () => setMode(mode, true) });
|
|
367
|
+
return h("span", { class: "seg", id }, button("match", touch ? W.fitPhone : W.fitWindow), button("whole", W.whole), button("actual", W.actual));
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function updatePictureControls() {
|
|
371
|
+
for (const seg of document.querySelectorAll(".seg")) {
|
|
372
|
+
for (const button of seg.querySelectorAll("button")) {
|
|
373
|
+
button.classList.toggle("on", button.dataset.mode === view.mode);
|
|
374
|
+
if (button.dataset.mode === "match") button.disabled = !canResize();
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const canResize = () => !!rfb && rfb._supportsSetDesktopSize && !resizeRefused;
|
|
380
|
+
|
|
381
|
+
async function openSession() {
|
|
382
|
+
try {
|
|
383
|
+
novnc ??= await import("./novnc.js");
|
|
384
|
+
} catch {
|
|
385
|
+
startMessage(W.network);
|
|
386
|
+
startButton.disabled = false;
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
if (!$("session")) buildSession();
|
|
390
|
+
$("start").hidden = true;
|
|
391
|
+
$("session").hidden = false;
|
|
392
|
+
document.documentElement.style.overflow = "hidden";
|
|
393
|
+
view.mode = link.resizable ? "match" : "whole";
|
|
394
|
+
connect();
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function connect() {
|
|
398
|
+
if (over) return;
|
|
399
|
+
clearTimeout(reconnectTimer);
|
|
400
|
+
dropConnection();
|
|
401
|
+
dim(everConnected ? W.reconnecting : W.connecting);
|
|
402
|
+
setDot("wait");
|
|
403
|
+
const url = new URL("ws", base);
|
|
404
|
+
url.protocol = location.protocol === "https:" ? "wss:" : "ws:";
|
|
405
|
+
let r;
|
|
406
|
+
try {
|
|
407
|
+
r = new novnc.RFB($("screen"), url.href, { shared: true });
|
|
408
|
+
} catch {
|
|
409
|
+
return dropped();
|
|
410
|
+
}
|
|
411
|
+
rfb = r;
|
|
412
|
+
closeCode = 0;
|
|
413
|
+
r.scaleViewport = false;
|
|
414
|
+
r.clipViewport = false;
|
|
415
|
+
r.resizeSession = false;
|
|
416
|
+
r.showDotCursor = touch;
|
|
417
|
+
r.qualityLevel = 6;
|
|
418
|
+
r.compressionLevel = 2;
|
|
419
|
+
r.focusOnClick = !touch;
|
|
420
|
+
r.background = "#000";
|
|
421
|
+
r._screen.style.overflow = "hidden";
|
|
422
|
+
r._sock._websocket?.addEventListener("close", (event) => (closeCode = event.code));
|
|
423
|
+
hookView(r);
|
|
424
|
+
hookGestures(r);
|
|
425
|
+
hookKeyboard(r);
|
|
426
|
+
r.addEventListener("connect", () => {
|
|
427
|
+
if (rfb !== r) return;
|
|
428
|
+
connected = true;
|
|
429
|
+
attempts = 0;
|
|
430
|
+
dim(null);
|
|
431
|
+
setDot("on");
|
|
432
|
+
if (!everConnected) {
|
|
433
|
+
everConnected = true;
|
|
434
|
+
firstConnect();
|
|
435
|
+
}
|
|
436
|
+
if (!touch) r.focus();
|
|
437
|
+
applyView();
|
|
438
|
+
// A desktop that keeps its own size starts zoomed in on the area the bot named.
|
|
439
|
+
if (view.mode !== "match" && view.zoom === 1) focusArea();
|
|
440
|
+
});
|
|
441
|
+
r.addEventListener("disconnect", () => {
|
|
442
|
+
if (rfb !== r) return;
|
|
443
|
+
rfb = null;
|
|
444
|
+
connected = false;
|
|
445
|
+
dropped();
|
|
446
|
+
});
|
|
447
|
+
r.addEventListener("clipboard", (event) => {
|
|
448
|
+
// On a computer, what is copied on the desktop is copied here too, when this page has the focus.
|
|
449
|
+
if (!touch && document.hasFocus()) navigator.clipboard?.writeText(event.detail.text).catch(() => {});
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function dropConnection() {
|
|
454
|
+
const old = rfb;
|
|
455
|
+
rfb = null;
|
|
456
|
+
connected = false;
|
|
457
|
+
if (old) {
|
|
458
|
+
try {
|
|
459
|
+
old.disconnect();
|
|
460
|
+
} catch {
|
|
461
|
+
// already gone
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function reconnect(now) {
|
|
467
|
+
if (over) return;
|
|
468
|
+
attempts = now ? 0 : attempts;
|
|
469
|
+
connect();
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
async function dropped() {
|
|
473
|
+
if (over) return;
|
|
474
|
+
// noVNC tells of the disconnect from the socket's own close handler, before this page's handler has the close code.
|
|
475
|
+
await sleep(0);
|
|
476
|
+
if (over) return;
|
|
477
|
+
setDot("off");
|
|
478
|
+
dim(W.reconnecting);
|
|
479
|
+
if (closeCode === 4001) {
|
|
480
|
+
// The link was opened again in another tab or window of this browser; that one has it now.
|
|
481
|
+
dim(W.elsewhere);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const state = await refresh();
|
|
485
|
+
if (over) return;
|
|
486
|
+
if (state.state && state.state !== "started" && state.status !== 404) return;
|
|
487
|
+
if (state.state === "started" && !state.mine) {
|
|
488
|
+
// This browser no longer has the link's cookie: back to the start screen, which says so.
|
|
489
|
+
$("session").hidden = true;
|
|
490
|
+
$("start").hidden = false;
|
|
491
|
+
document.documentElement.style.overflow = "";
|
|
492
|
+
showStart();
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
clearTimeout(reconnectTimer);
|
|
496
|
+
const wait = Math.min(10_000, 1_000 * 2 ** attempts);
|
|
497
|
+
attempts++;
|
|
498
|
+
reconnectTimer = setTimeout(() => {
|
|
499
|
+
if (!document.hidden) connect();
|
|
500
|
+
}, wait);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function firstConnect() {
|
|
504
|
+
// The bot's words and the tips show for a while, then fold into the task button.
|
|
505
|
+
openHint(true);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// ---- The picture: size, zoom and pan ----
|
|
509
|
+
|
|
510
|
+
function screenRect() {
|
|
511
|
+
return $("screen").getBoundingClientRect();
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function scaleOf(r) {
|
|
515
|
+
return r._display.scale || 1;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** The size the desktop should have now, or null to leave it. */
|
|
519
|
+
function wantedSize() {
|
|
520
|
+
if (view.mode === "match") {
|
|
521
|
+
if (!canResize()) return null;
|
|
522
|
+
const box = screenRect();
|
|
523
|
+
if (box.width < 50 || box.height < 50) return null;
|
|
524
|
+
if (touch) {
|
|
525
|
+
const width = Math.max(MIN_WIDTH, Math.round(box.width));
|
|
526
|
+
const scale = box.width / width;
|
|
527
|
+
return { width, height: Math.max(MIN_HEIGHT, Math.round(box.height / scale)) };
|
|
528
|
+
}
|
|
529
|
+
return { width: clamp(Math.round(box.width), MIN_WIDTH, MAX_WIDTH), height: clamp(Math.round(box.height), MIN_HEIGHT, MAX_HEIGHT) };
|
|
530
|
+
}
|
|
531
|
+
// The desktop's own size, as it was at Start.
|
|
532
|
+
return rfb && rfb._supportsSetDesktopSize && link?.size ? { width: link.size.width, height: link.size.height } : null;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function requestSize() {
|
|
536
|
+
const r = rfb;
|
|
537
|
+
if (!r || r._rfbConnectionState !== "connected" || !r._supportsSetDesktopSize || r._pendingRemoteResize) return;
|
|
538
|
+
// With the keyboard open, the phone's screen looks shorter; the desktop keeps its shape.
|
|
539
|
+
if (keyboardOpen) return;
|
|
540
|
+
const want = wantedSize();
|
|
541
|
+
if (!want || (want.width === r._fbWidth && want.height === r._fbHeight)) return;
|
|
542
|
+
const since = Date.now() - r._lastResize;
|
|
543
|
+
if (since < 250) {
|
|
544
|
+
clearTimeout(resizeTimer);
|
|
545
|
+
resizeTimer = setTimeout(requestSize, 250 - since);
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
r._pendingRemoteResize = true;
|
|
549
|
+
r._lastResize = Date.now();
|
|
550
|
+
novnc.RFB.messages.setDesktopSize(r._sock, want.width, want.height, r._screenID, r._screenFlags);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** Lay the picture out: fitted to the screen, times the zoom, scrolled to the pan. */
|
|
554
|
+
function applyView() {
|
|
555
|
+
const r = rfb;
|
|
556
|
+
if (!r) return;
|
|
557
|
+
const width = r._fbWidth;
|
|
558
|
+
const height = r._fbHeight;
|
|
559
|
+
if (!width || !height) return;
|
|
560
|
+
const box = screenRect();
|
|
561
|
+
const fit = Math.min(box.width / width, box.height / height);
|
|
562
|
+
if (view.mode === "actual") view.zoom = Math.max(1, 1 / fit);
|
|
563
|
+
const scale = fit * view.zoom;
|
|
564
|
+
r._display.scale = scale;
|
|
565
|
+
view.x = clamp(view.x, 0, Math.max(0, width * scale - box.width));
|
|
566
|
+
view.y = clamp(view.y, 0, Math.max(0, height * scale - box.height));
|
|
567
|
+
r._screen.scrollLeft = view.x;
|
|
568
|
+
r._screen.scrollTop = view.y;
|
|
569
|
+
const zoom = $("zoom");
|
|
570
|
+
if (zoom) {
|
|
571
|
+
const n = Math.round(view.zoom * 10) / 10;
|
|
572
|
+
zoom.hidden = !touch || view.zoom < 1.05 || view.mode === "actual";
|
|
573
|
+
if (zoom.dataset.n !== String(n)) {
|
|
574
|
+
zoom.dataset.n = String(n);
|
|
575
|
+
zoom.replaceChildren(`${n}×`, fitIcon());
|
|
576
|
+
zoom.title = fill(W.zoom, { n });
|
|
577
|
+
zoom.setAttribute("aria-label", zoom.title);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (trackpad && pointer) showPointer(r);
|
|
581
|
+
updatePictureControls();
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function setMode(mode, chosen) {
|
|
585
|
+
if (mode === "match" && !canResize()) mode = "whole";
|
|
586
|
+
view.mode = mode;
|
|
587
|
+
if (mode !== "actual") view.zoom = 1;
|
|
588
|
+
view.x = view.y = 0;
|
|
589
|
+
applyView();
|
|
590
|
+
requestSize();
|
|
591
|
+
if (chosen) closeSheets(), closeMenu();
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** Zoom to the area the bot named, when the desktop keeps its own size. */
|
|
595
|
+
function focusArea() {
|
|
596
|
+
const r = rfb;
|
|
597
|
+
if (!r || !facts.area || view.mode === "match") return;
|
|
598
|
+
const [x, y, w, h] = facts.area;
|
|
599
|
+
const box = screenRect();
|
|
600
|
+
const fit = Math.min(box.width / r._fbWidth, box.height / r._fbHeight);
|
|
601
|
+
view.zoom = clamp(Math.min(box.width / (w * fit), box.height / (h * fit)) * 0.9, 1, MAX_ZOOM);
|
|
602
|
+
const scale = fit * view.zoom;
|
|
603
|
+
view.x = (x + w / 2) * scale - box.width / 2;
|
|
604
|
+
view.y = (y + h / 2) * scale - box.height / 2;
|
|
605
|
+
applyView();
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function hookView(r) {
|
|
609
|
+
// noVNC calls these when the window or the desktop changes size; the page decides instead.
|
|
610
|
+
r._updateScale = () => applyView();
|
|
611
|
+
r._requestRemoteResize = () => requestSize();
|
|
612
|
+
const handle = r._handleExtendedDesktopSize;
|
|
613
|
+
let first = true;
|
|
614
|
+
r._handleExtendedDesktopSize = function () {
|
|
615
|
+
const reason = this._FBU.x;
|
|
616
|
+
const status = this._FBU.y;
|
|
617
|
+
const done = handle.call(this);
|
|
618
|
+
if (done && reason === 1 && status !== 0 && view.mode === "match") {
|
|
619
|
+
// The desktop cannot take this shape; show it whole, zoomed in on the bot's area.
|
|
620
|
+
resizeRefused = true;
|
|
621
|
+
setMode("whole", false);
|
|
622
|
+
focusArea();
|
|
623
|
+
toast(W.noResize, 4000);
|
|
624
|
+
}
|
|
625
|
+
if (done && first) {
|
|
626
|
+
first = false;
|
|
627
|
+
updatePictureControls();
|
|
628
|
+
}
|
|
629
|
+
return done;
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// ---- Touch: pinch zooms the picture, two fingers pan it when zoomed, and the trackpad mode ----
|
|
634
|
+
|
|
635
|
+
let pinch = null;
|
|
636
|
+
let pan = null;
|
|
637
|
+
let pad = null;
|
|
638
|
+
|
|
639
|
+
function hookGestures(r) {
|
|
640
|
+
const canvas = r._canvas;
|
|
641
|
+
const original = r._eventHandlers.handleGesture;
|
|
642
|
+
for (const type of ["gesturestart", "gesturemove", "gestureend"]) {
|
|
643
|
+
canvas.removeEventListener(type, original);
|
|
644
|
+
canvas.addEventListener(type, (event) => onGesture(r, event));
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function onGesture(r, event) {
|
|
649
|
+
const detail = event.detail;
|
|
650
|
+
if (event.type === "gesturestart" && (detail.type === "onetap" || detail.type === "drag" || detail.type === "longpress")) {
|
|
651
|
+
lastTap = { x: detail.clientX, y: detail.clientY };
|
|
652
|
+
// A tap on the desktop may go to another field: the typing field starts empty for it.
|
|
653
|
+
if (keyboardOpen) clearTyped();
|
|
654
|
+
}
|
|
655
|
+
if (detail.type === "pinch") return pinchGesture(r, event);
|
|
656
|
+
if (detail.type === "twodrag" && view.zoom > 1.01) return panGesture(event);
|
|
657
|
+
if (trackpad && ["onetap", "twotap", "drag", "longpress"].includes(detail.type)) return padGesture(r, event);
|
|
658
|
+
r._handleGesture(event);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function pinchGesture(r, event) {
|
|
662
|
+
const detail = event.detail;
|
|
663
|
+
const distance = Math.hypot(detail.magnitudeX, detail.magnitudeY);
|
|
664
|
+
if (event.type === "gesturestart") {
|
|
665
|
+
const rect = r._canvas.getBoundingClientRect();
|
|
666
|
+
const scale = scaleOf(r);
|
|
667
|
+
if (view.mode === "actual") view.mode = "whole";
|
|
668
|
+
pinch = { distance: Math.max(distance, 1), zoom: view.zoom, rx: (detail.clientX - rect.left) / scale, ry: (detail.clientY - rect.top) / scale, cx: detail.clientX, cy: detail.clientY };
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
if (!pinch) return;
|
|
672
|
+
if (event.type === "gesturemove") {
|
|
673
|
+
const box = screenRect();
|
|
674
|
+
const fit = Math.min(box.width / r._fbWidth, box.height / r._fbHeight);
|
|
675
|
+
view.zoom = clamp((pinch.zoom * distance) / pinch.distance, 1, MAX_ZOOM);
|
|
676
|
+
const scale = fit * view.zoom;
|
|
677
|
+
view.x = pinch.rx * scale - (pinch.cx - box.left);
|
|
678
|
+
view.y = pinch.ry * scale - (pinch.cy - box.top);
|
|
679
|
+
applyView();
|
|
680
|
+
} else pinch = null;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function panGesture(event) {
|
|
684
|
+
const detail = event.detail;
|
|
685
|
+
if (event.type === "gesturestart") pan = { x: view.x, y: view.y };
|
|
686
|
+
else if (event.type === "gesturemove" && pan) {
|
|
687
|
+
view.x = pan.x - detail.magnitudeX;
|
|
688
|
+
view.y = pan.y - detail.magnitudeY;
|
|
689
|
+
applyView();
|
|
690
|
+
} else pan = null;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function sendPointer(r, mask) {
|
|
694
|
+
novnc.RFB.messages.pointerEvent(r._sock, Math.round(pointer.x), Math.round(pointer.y), mask);
|
|
695
|
+
showPointer(r);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
function showPointer(r) {
|
|
699
|
+
pointer ??= { x: r._fbWidth / 2, y: r._fbHeight / 2 };
|
|
700
|
+
const rect = r._canvas.getBoundingClientRect();
|
|
701
|
+
const scale = scaleOf(r);
|
|
702
|
+
r._cursor.move(rect.left + pointer.x * scale, rect.top + pointer.y * scale);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/** Trackpad mode: a finger moves the pointer; a tap clicks where it is, two fingers right-click, a long press drags. */
|
|
706
|
+
function padGesture(r, event) {
|
|
707
|
+
const detail = event.detail;
|
|
708
|
+
pointer ??= { x: r._fbWidth / 2, y: r._fbHeight / 2 };
|
|
709
|
+
const click = (mask) => {
|
|
710
|
+
sendPointer(r, mask);
|
|
711
|
+
sendPointer(r, 0);
|
|
712
|
+
};
|
|
713
|
+
if (detail.type === "onetap" || detail.type === "twotap") {
|
|
714
|
+
if (event.type === "gesturestart") click(detail.type === "onetap" ? 1 : 4);
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
if (event.type === "gesturestart") {
|
|
718
|
+
pad = { x: pointer.x, y: pointer.y, cx: detail.clientX, cy: detail.clientY, buttons: detail.type === "longpress" ? 1 : 0 };
|
|
719
|
+
if (pad.buttons) sendPointer(r, pad.buttons);
|
|
720
|
+
} else if (event.type === "gesturemove" && pad) {
|
|
721
|
+
const scale = scaleOf(r);
|
|
722
|
+
pointer.x = clamp(pad.x + ((detail.clientX - pad.cx) * PAD_SPEED) / scale, 0, r._fbWidth - 1);
|
|
723
|
+
pointer.y = clamp(pad.y + ((detail.clientY - pad.cy) * PAD_SPEED) / scale, 0, r._fbHeight - 1);
|
|
724
|
+
sendPointer(r, pad.buttons);
|
|
725
|
+
} else if (event.type === "gestureend" && pad) {
|
|
726
|
+
if (pad.buttons) sendPointer(r, 0);
|
|
727
|
+
pad = null;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// ---- Keys and typing ----
|
|
732
|
+
|
|
733
|
+
function keysym(name) {
|
|
734
|
+
if (name.length === 1) return novnc.keysyms.lookup(name.codePointAt(0));
|
|
735
|
+
return novnc.KeyTable[`XK_${name}`];
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function sendKey(name) {
|
|
739
|
+
rfb?.sendKey(keysym(name), null);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** Press keys together, as Ctrl+Alt+Del: down in order, up in reverse. */
|
|
743
|
+
function combo(...names) {
|
|
744
|
+
if (!rfb) return;
|
|
745
|
+
for (const name of names) rfb.sendKey(keysym(name), null, true);
|
|
746
|
+
for (const name of [...names].reverse()) rfb.sendKey(keysym(name), null, false);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
let typing = Promise.resolve();
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* Type text as key presses, in order. A capital letter is sent with Shift held, which some VNC servers need (x0vncserver
|
|
753
|
+
* types it in lower case without). A character outside ASCII waits a moment, while the server maps it to a key.
|
|
754
|
+
*/
|
|
755
|
+
function typeText(text) {
|
|
756
|
+
const chars = Array.from(text);
|
|
757
|
+
typing = typing.then(async () => {
|
|
758
|
+
for (const char of chars) {
|
|
759
|
+
const r = rfb;
|
|
760
|
+
if (!r) return;
|
|
761
|
+
if (char === "\n" || char === "\r") r.sendKey(novnc.KeyTable.XK_Return, "Enter");
|
|
762
|
+
else if (char === "\t") r.sendKey(novnc.KeyTable.XK_Tab, "Tab");
|
|
763
|
+
else {
|
|
764
|
+
const capital = char !== char.toLowerCase() && char === char.toUpperCase();
|
|
765
|
+
if (capital) r.sendKey(novnc.KeyTable.XK_Shift_L, "ShiftLeft", true);
|
|
766
|
+
r.sendKey(novnc.keysyms.lookup(char.codePointAt(0)), null);
|
|
767
|
+
if (capital) r.sendKey(novnc.KeyTable.XK_Shift_L, "ShiftLeft", false);
|
|
768
|
+
if (char.codePointAt(0) > 0x7e) await sleep(25);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
});
|
|
772
|
+
return typing;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
async function typeClipboard() {
|
|
776
|
+
let text;
|
|
777
|
+
try {
|
|
778
|
+
text = await navigator.clipboard.readText();
|
|
779
|
+
} catch {
|
|
780
|
+
return toast(W.clipboardDenied, 3500);
|
|
781
|
+
}
|
|
782
|
+
if (!text) return toast(W.clipboardEmpty, 3000);
|
|
783
|
+
await typeText(text);
|
|
784
|
+
toast(fill(W.typed, { n: Array.from(text).length }), 2500);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
function hookKeyboard(r) {
|
|
788
|
+
if (!cmdIsCtrl) return;
|
|
789
|
+
// ⌘ on a Mac becomes Ctrl on the desktop, so ⌘C copies and ⌘V pastes there.
|
|
790
|
+
const forward = r._keyboard.onkeyevent;
|
|
791
|
+
r._keyboard.onkeyevent = (sym, code, down, numlock, capslock) => {
|
|
792
|
+
if (sym === novnc.KeyTable.XK_Super_L || sym === novnc.KeyTable.XK_Super_R) {
|
|
793
|
+
sym = sym === novnc.KeyTable.XK_Super_L ? novnc.KeyTable.XK_Control_L : novnc.KeyTable.XK_Control_R;
|
|
794
|
+
code = sym === novnc.KeyTable.XK_Control_L ? "ControlLeft" : "ControlRight";
|
|
795
|
+
}
|
|
796
|
+
return forward(sym, code, down, numlock, capslock);
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
function onDesktopKey(event) {
|
|
801
|
+
if (event.target instanceof HTMLTextAreaElement || event.target instanceof HTMLInputElement) return;
|
|
802
|
+
const hint = $("hint");
|
|
803
|
+
if (event.key === "Escape" && !hint.hidden && !hint.classList.contains("away")) {
|
|
804
|
+
// Esc folds the task panel first; the next one goes to the desktop.
|
|
805
|
+
event.preventDefault();
|
|
806
|
+
event.stopPropagation();
|
|
807
|
+
return closeHint();
|
|
808
|
+
}
|
|
809
|
+
if (!$("menu").hidden) {
|
|
810
|
+
// With the menu open, these keys are the menu's, not the desktop's.
|
|
811
|
+
if (event.key === "Escape" || event.key === "f" || event.key === "F") {
|
|
812
|
+
event.preventDefault();
|
|
813
|
+
event.stopPropagation();
|
|
814
|
+
if (event.key !== "Escape") fullscreen();
|
|
815
|
+
return closeMenu();
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
if (cmdIsCtrl && event.metaKey && event.shiftKey && event.code === "KeyV") {
|
|
819
|
+
event.preventDefault();
|
|
820
|
+
event.stopPropagation();
|
|
821
|
+
typeClipboard();
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// The typing field: what is typed there goes to the desktop as it is typed. An input method's characters go when they
|
|
826
|
+
// are chosen. An edit in the middle moves the desktop's cursor there and back.
|
|
827
|
+
let composing = false;
|
|
828
|
+
let sent = "";
|
|
829
|
+
let locked = false;
|
|
830
|
+
|
|
831
|
+
function applyInputKind() {
|
|
832
|
+
const typed = $("typed");
|
|
833
|
+
if (facts.input === "password") {
|
|
834
|
+
typed.setAttribute("autocomplete", "current-password");
|
|
835
|
+
locked = true;
|
|
836
|
+
} else if (facts.input === "code") {
|
|
837
|
+
typed.setAttribute("autocomplete", "one-time-code");
|
|
838
|
+
typed.setAttribute("inputmode", "numeric");
|
|
839
|
+
}
|
|
840
|
+
setLock(locked);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function setLock(on) {
|
|
844
|
+
locked = on;
|
|
845
|
+
$("typed").type = on ? "password" : "text";
|
|
846
|
+
$("lock").classList.toggle("on", on);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
function toggleLock() {
|
|
850
|
+
setLock(!locked);
|
|
851
|
+
$("typed").focus();
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
function keepFocus(event) {
|
|
855
|
+
// A button next to the field must not take the focus: that would close the phone's keyboard.
|
|
856
|
+
event.preventDefault();
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
function clearTyped() {
|
|
860
|
+
$("typed").value = "";
|
|
861
|
+
sent = "";
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
function syncTyped() {
|
|
865
|
+
const before = Array.from(sent);
|
|
866
|
+
const now = Array.from($("typed").value);
|
|
867
|
+
let start = 0;
|
|
868
|
+
while (start < before.length && start < now.length && before[start] === now[start]) start++;
|
|
869
|
+
let end = 0;
|
|
870
|
+
while (end < before.length - start && end < now.length - start && before[before.length - 1 - end] === now[now.length - 1 - end]) end++;
|
|
871
|
+
const removed = before.length - start - end;
|
|
872
|
+
const added = now.slice(start, now.length - end).join("");
|
|
873
|
+
sent = now.join("");
|
|
874
|
+
if (removed === 0 && !added) return;
|
|
875
|
+
// In order with the characters still on their way: moves and deletions wait for them too.
|
|
876
|
+
typing = typing.then(() => {
|
|
877
|
+
for (let index = 0; index < end; index++) sendKey("Left");
|
|
878
|
+
for (let index = 0; index < removed; index++) sendKey("BackSpace");
|
|
879
|
+
});
|
|
880
|
+
typeText(added);
|
|
881
|
+
if (end > 0) {
|
|
882
|
+
typing = typing.then(() => {
|
|
883
|
+
for (let index = 0; index < end; index++) sendKey("Right");
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
function onTypedKey(event) {
|
|
889
|
+
if (event.isComposing || composing) return;
|
|
890
|
+
const empty = $("typed").value === "";
|
|
891
|
+
if (event.key === "Enter") {
|
|
892
|
+
event.preventDefault();
|
|
893
|
+
sendKey("Return");
|
|
894
|
+
clearTyped();
|
|
895
|
+
} else if (event.key === "Tab") {
|
|
896
|
+
event.preventDefault();
|
|
897
|
+
sendKey("Tab");
|
|
898
|
+
} else if (event.key === "Escape") {
|
|
899
|
+
sendKey("Escape");
|
|
900
|
+
} else if (empty && event.key === "Backspace") {
|
|
901
|
+
event.preventDefault();
|
|
902
|
+
sendKey("BackSpace");
|
|
903
|
+
} else if (empty && ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)) {
|
|
904
|
+
event.preventDefault();
|
|
905
|
+
sendKey(event.key.slice(5));
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
function openKeyboard() {
|
|
910
|
+
closeSheets();
|
|
911
|
+
const strip = $("strip");
|
|
912
|
+
strip.hidden = false;
|
|
913
|
+
$("dock").hidden = true;
|
|
914
|
+
keyboardOpen = true;
|
|
915
|
+
$("typed").focus();
|
|
916
|
+
closeHint();
|
|
917
|
+
if (matchMedia("(orientation: landscape)").matches && !sessionStorage.getItem("pots-upright")) {
|
|
918
|
+
sessionStorage.setItem("pots-upright", "1");
|
|
919
|
+
toast(W.upright, 3000);
|
|
920
|
+
}
|
|
921
|
+
placeForKeyboard();
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
function closeKeyboard() {
|
|
925
|
+
$("typed").blur();
|
|
926
|
+
$("strip").hidden = true;
|
|
927
|
+
$("dock").hidden = false;
|
|
928
|
+
keyboardOpen = false;
|
|
929
|
+
clearTyped();
|
|
930
|
+
placeForKeyboard();
|
|
931
|
+
placeDock();
|
|
932
|
+
requestSize();
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* Keep the typing field and the open sheet above the phone's keyboard, and move the picture up so the point last
|
|
937
|
+
* tapped, as a field on the desktop, stays in sight above them.
|
|
938
|
+
*/
|
|
939
|
+
function placeForKeyboard() {
|
|
940
|
+
const viewport = window.visualViewport;
|
|
941
|
+
const top = viewport ? viewport.offsetTop : 0;
|
|
942
|
+
const height = viewport ? viewport.height : window.innerHeight;
|
|
943
|
+
const covered = Math.max(0, window.innerHeight - (top + height));
|
|
944
|
+
const strip = $("strip");
|
|
945
|
+
if (!strip) return;
|
|
946
|
+
if (!strip.hidden) {
|
|
947
|
+
strip.style.top = `${top + height - strip.offsetHeight}px`;
|
|
948
|
+
strip.style.bottom = "auto";
|
|
949
|
+
}
|
|
950
|
+
for (const sheet of document.querySelectorAll(".sheet")) sheet.style.transform = touch && covered > 0 ? `translateY(${-covered}px)` : "";
|
|
951
|
+
let shift = 0;
|
|
952
|
+
if (keyboardOpen && lastTap && covered > 40) {
|
|
953
|
+
const visibleBottom = top + height - strip.offsetHeight - 16;
|
|
954
|
+
shift = Math.max(0, lastTap.y - visibleBottom);
|
|
955
|
+
}
|
|
956
|
+
$("screen").style.transform = shift > 0 ? `translateY(${-shift}px)` : "";
|
|
957
|
+
placeToast();
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// ---- The floating controls ----
|
|
961
|
+
|
|
962
|
+
/** The size of the page, where the controls float. */
|
|
963
|
+
function area() {
|
|
964
|
+
const session = $("session");
|
|
965
|
+
return { width: session.clientWidth, height: session.clientHeight };
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
/** How far the controls keep from each edge: a little, and more where a phone's notch or rounded corners are. */
|
|
969
|
+
function margins() {
|
|
970
|
+
const safe = getComputedStyle($("safe"));
|
|
971
|
+
const gap = 12;
|
|
972
|
+
return {
|
|
973
|
+
top: gap + (parseFloat(safe.paddingTop) || 0),
|
|
974
|
+
right: gap + (parseFloat(safe.paddingRight) || 0),
|
|
975
|
+
bottom: gap + (parseFloat(safe.paddingBottom) || 0),
|
|
976
|
+
left: gap + (parseFloat(safe.paddingLeft) || 0),
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
const DOCK_KEY = "pots-desktop-dock";
|
|
981
|
+
const EDGES = ["top", "right", "bottom", "left"];
|
|
982
|
+
/** The edge the dock is on, and where along it its middle is, from 0 to 1. Null: where it starts. */
|
|
983
|
+
let dockPlace = null;
|
|
984
|
+
/** Where the dock is, or is going: the panel, the menu and messages go next to it. */
|
|
985
|
+
let dockBox = null;
|
|
986
|
+
let dragging = false;
|
|
987
|
+
|
|
988
|
+
function loadDockPlace() {
|
|
989
|
+
try {
|
|
990
|
+
const place = JSON.parse(localStorage.getItem(DOCK_KEY) || "null");
|
|
991
|
+
if (place && EDGES.includes(place.edge) && place.at >= 0 && place.at <= 1) return place;
|
|
992
|
+
} catch {
|
|
993
|
+
// no storage, or not ours
|
|
994
|
+
}
|
|
995
|
+
return null;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
function saveDockPlace() {
|
|
999
|
+
try {
|
|
1000
|
+
localStorage.setItem(DOCK_KEY, JSON.stringify(dockPlace));
|
|
1001
|
+
} catch {
|
|
1002
|
+
// the browser keeps nothing; the dock starts where it starts next time
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/** At the bottom; upright on the right on a phone held sideways, where the screen is short. Until it is moved. */
|
|
1007
|
+
function startPlace() {
|
|
1008
|
+
const { width, height } = area();
|
|
1009
|
+
return touch && width > height && height <= 520 ? { edge: "right", at: 0.5 } : { edge: "bottom", at: 0.5 };
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
const upright = (edge) => edge === "left" || edge === "right";
|
|
1013
|
+
|
|
1014
|
+
/** Put the dock on its edge, and what goes with it next to it. */
|
|
1015
|
+
function placeDock() {
|
|
1016
|
+
const dock = $("dock");
|
|
1017
|
+
if (!dock || dragging || dock.hidden) return;
|
|
1018
|
+
const place = dockPlace ?? startPlace();
|
|
1019
|
+
dock.classList.toggle("side", upright(place.edge));
|
|
1020
|
+
const { width, height } = area();
|
|
1021
|
+
const m = margins();
|
|
1022
|
+
// On a narrow screen the dock leaves out what it can do without, first the small icons, then Done's word.
|
|
1023
|
+
dock.classList.remove("tight", "tighter");
|
|
1024
|
+
const room = upright(place.edge) ? height - m.top - m.bottom : width - m.left - m.right;
|
|
1025
|
+
const length = () => (upright(place.edge) ? dock.offsetHeight : dock.offsetWidth);
|
|
1026
|
+
if (length() > room) dock.classList.add("tight");
|
|
1027
|
+
if (length() > room) dock.classList.add("tighter");
|
|
1028
|
+
const w = dock.offsetWidth;
|
|
1029
|
+
const h = dock.offsetHeight;
|
|
1030
|
+
let left;
|
|
1031
|
+
let top;
|
|
1032
|
+
if (upright(place.edge)) {
|
|
1033
|
+
left = place.edge === "left" ? m.left : width - w - m.right;
|
|
1034
|
+
top = clamp(place.at * height - h / 2, m.top, height - h - m.bottom);
|
|
1035
|
+
} else {
|
|
1036
|
+
top = place.edge === "top" ? m.top : height - h - m.bottom;
|
|
1037
|
+
left = clamp(place.at * width - w / 2, m.left, width - w - m.right);
|
|
1038
|
+
}
|
|
1039
|
+
dock.style.left = `${Math.round(left)}px`;
|
|
1040
|
+
dock.style.top = `${Math.round(top)}px`;
|
|
1041
|
+
dockBox = { left, top, right: left + w, bottom: top + h, width: w, height: h };
|
|
1042
|
+
placeHint();
|
|
1043
|
+
placeMenu();
|
|
1044
|
+
placeToast();
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
/** The edge nearest to a point, for the screen's shape: a point a fifth of the way across is as near as one a fifth of the way down. */
|
|
1048
|
+
function nearestEdge(x, y) {
|
|
1049
|
+
const { width, height } = area();
|
|
1050
|
+
const distance = { left: x / width, right: (width - x) / width, top: y / height, bottom: (height - y) / height };
|
|
1051
|
+
return EDGES.reduce((best, edge) => (distance[edge] < distance[best] ? edge : best));
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
/**
|
|
1055
|
+
* The dock moves with a finger or the mouse, from anywhere on it, and stays on the edge it is let go nearest to:
|
|
1056
|
+
* upright on the left or right, lying on the top or bottom. A drag is not a press of the button it started on.
|
|
1057
|
+
*/
|
|
1058
|
+
function dragDock(dock) {
|
|
1059
|
+
let start = null;
|
|
1060
|
+
const move = (event) => {
|
|
1061
|
+
if (!start || event.pointerId !== start.id) return;
|
|
1062
|
+
if (!start.moved && Math.hypot(event.clientX - start.x, event.clientY - start.y) < 10) return;
|
|
1063
|
+
if (!start.moved) {
|
|
1064
|
+
start.moved = true;
|
|
1065
|
+
dragging = true;
|
|
1066
|
+
// From here the dock has the pointer, also off its edge; a mouse that left the dock before this still drags it.
|
|
1067
|
+
try {
|
|
1068
|
+
dock.setPointerCapture(event.pointerId);
|
|
1069
|
+
} catch {
|
|
1070
|
+
// the pointer is gone already
|
|
1071
|
+
}
|
|
1072
|
+
dock.classList.add("dragging");
|
|
1073
|
+
closeHint();
|
|
1074
|
+
closeMenu();
|
|
1075
|
+
}
|
|
1076
|
+
start.edge = nearestEdge(event.clientX, event.clientY);
|
|
1077
|
+
dock.classList.toggle("side", upright(start.edge));
|
|
1078
|
+
const { width, height } = area();
|
|
1079
|
+
const w = dock.offsetWidth;
|
|
1080
|
+
const h = dock.offsetHeight;
|
|
1081
|
+
const left = upright(start.edge) ? event.clientX - w / 2 : event.clientX - start.along * w;
|
|
1082
|
+
const top = upright(start.edge) ? event.clientY - start.along * h : event.clientY - h / 2;
|
|
1083
|
+
dock.style.left = `${clamp(left, 4, width - w - 4)}px`;
|
|
1084
|
+
dock.style.top = `${clamp(top, 4, height - h - 4)}px`;
|
|
1085
|
+
};
|
|
1086
|
+
const listen = (on) => {
|
|
1087
|
+
for (const [type, handler] of [["pointermove", move], ["pointerup", end], ["pointercancel", end]]) {
|
|
1088
|
+
if (on) window.addEventListener(type, handler, true);
|
|
1089
|
+
else window.removeEventListener(type, handler, true);
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1092
|
+
const end = (event) => {
|
|
1093
|
+
if (!start || event.pointerId !== start.id) return;
|
|
1094
|
+
listen(false);
|
|
1095
|
+
if (start.moved) {
|
|
1096
|
+
const swallow = (click) => (click.stopPropagation(), click.preventDefault());
|
|
1097
|
+
dock.addEventListener("click", swallow, { capture: true, once: true });
|
|
1098
|
+
setTimeout(() => dock.removeEventListener("click", swallow, true), 300);
|
|
1099
|
+
const rect = dock.getBoundingClientRect();
|
|
1100
|
+
const { width, height } = area();
|
|
1101
|
+
const at = upright(start.edge) ? (rect.top + rect.height / 2) / height : (rect.left + rect.width / 2) / width;
|
|
1102
|
+
dockPlace = { edge: start.edge, at: clamp(Math.round(at * 1000) / 1000, 0, 1) };
|
|
1103
|
+
saveDockPlace();
|
|
1104
|
+
dragging = false;
|
|
1105
|
+
dock.classList.remove("dragging");
|
|
1106
|
+
placeDock();
|
|
1107
|
+
}
|
|
1108
|
+
start = null;
|
|
1109
|
+
};
|
|
1110
|
+
dock.addEventListener("pointerdown", (event) => {
|
|
1111
|
+
if (event.button > 0) return;
|
|
1112
|
+
if (start) {
|
|
1113
|
+
// The last press never ended here, as when it was let go outside the window: this one starts anew.
|
|
1114
|
+
listen(false);
|
|
1115
|
+
dragging = false;
|
|
1116
|
+
dock.classList.remove("dragging");
|
|
1117
|
+
}
|
|
1118
|
+
const rect = dock.getBoundingClientRect();
|
|
1119
|
+
const side = dock.classList.contains("side");
|
|
1120
|
+
// Where along the dock it was taken, kept when it turns, so the finger stays on the same button.
|
|
1121
|
+
const along = side ? (event.clientY - rect.top) / rect.height : (event.clientX - rect.left) / rect.width;
|
|
1122
|
+
start = { x: event.clientX, y: event.clientY, id: event.pointerId, moved: false, along, edge: null };
|
|
1123
|
+
listen(true);
|
|
1124
|
+
});
|
|
1125
|
+
// A drag let go at speed is no fling for the browser, which would let the next tap only stop that fling.
|
|
1126
|
+
dock.addEventListener("touchmove", (event) => start?.moved && event.preventDefault(), { passive: false });
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
/**
|
|
1130
|
+
* Put a floating element next to the dock: beside it when the dock stands upright on a side, else above it when the
|
|
1131
|
+
* dock is in the lower half, below it when in the upper half. It gets no larger than the room there.
|
|
1132
|
+
*/
|
|
1133
|
+
function placeNear(element, anchor, beside, tallest = Infinity) {
|
|
1134
|
+
const { width, height } = area();
|
|
1135
|
+
const m = margins();
|
|
1136
|
+
const gap = 10;
|
|
1137
|
+
element.style.maxWidth = "";
|
|
1138
|
+
element.style.maxHeight = "";
|
|
1139
|
+
let left;
|
|
1140
|
+
let top;
|
|
1141
|
+
if (beside) {
|
|
1142
|
+
const toLeft = anchor.left + anchor.width / 2 > width / 2;
|
|
1143
|
+
element.style.maxWidth = `${Math.max(120, toLeft ? anchor.left - gap - m.left : width - anchor.right - gap - m.right)}px`;
|
|
1144
|
+
element.style.maxHeight = `${Math.min(tallest, height - m.top - m.bottom)}px`;
|
|
1145
|
+
const w = element.offsetWidth;
|
|
1146
|
+
const h = element.offsetHeight;
|
|
1147
|
+
left = toLeft ? anchor.left - gap - w : anchor.right + gap;
|
|
1148
|
+
top = clamp(anchor.top + anchor.height / 2 - h / 2, m.top, height - m.bottom - h);
|
|
1149
|
+
} else {
|
|
1150
|
+
const above = anchor.top + anchor.height / 2 > height / 2;
|
|
1151
|
+
element.style.maxHeight = `${Math.min(tallest, Math.max(80, above ? anchor.top - gap - m.top : height - anchor.bottom - gap - m.bottom))}px`;
|
|
1152
|
+
const w = element.offsetWidth;
|
|
1153
|
+
const h = element.offsetHeight;
|
|
1154
|
+
top = above ? anchor.top - gap - h : anchor.bottom + gap;
|
|
1155
|
+
left = clamp(anchor.left + anchor.width / 2 - w / 2, m.left, width - m.right - w);
|
|
1156
|
+
}
|
|
1157
|
+
element.style.left = `${Math.round(left)}px`;
|
|
1158
|
+
element.style.top = `${Math.round(top)}px`;
|
|
1159
|
+
return { left, top, right: left + element.offsetWidth, bottom: top + element.offsetHeight, width: element.offsetWidth, height: element.offsetHeight };
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
// The task panel: the bot's words in full. It opens by itself at the start for a while, and with the task button.
|
|
1163
|
+
|
|
1164
|
+
let hintTimer;
|
|
1165
|
+
let hintBox = null;
|
|
1166
|
+
let hintFolding;
|
|
1167
|
+
|
|
1168
|
+
/** How long the panel stays open by itself at the start: longer for more words, from 5 to 10 seconds. */
|
|
1169
|
+
const hintMs = () => clamp(3500 + 45 * Array.from(facts.text).length, 5000, 10_000);
|
|
1170
|
+
|
|
1171
|
+
function placeHint() {
|
|
1172
|
+
const hint = $("hint");
|
|
1173
|
+
if (!hint || hint.hidden || !dockBox) return;
|
|
1174
|
+
// Open by itself, it leaves at least half of a phone's screen to the picture; opened with the button, it may be taller.
|
|
1175
|
+
const side = $("dock").classList.contains("side");
|
|
1176
|
+
hintBox = placeNear(hint, dockBox, side, hint.classList.contains("auto") && !side ? Math.max(200, area().height / 2) : Infinity);
|
|
1177
|
+
markOverflow();
|
|
1178
|
+
// It grows out of the task button, and folds back into it.
|
|
1179
|
+
const chip = $("chip");
|
|
1180
|
+
const x = dockBox.left + chip.offsetLeft + chip.offsetWidth / 2 - hintBox.left;
|
|
1181
|
+
const y = dockBox.top + chip.offsetTop + chip.offsetHeight / 2 - hintBox.top;
|
|
1182
|
+
hint.style.setProperty("--from-x", `${Math.round(x)}px`);
|
|
1183
|
+
hint.style.setProperty("--from-y", `${Math.round(y)}px`);
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
/** Long words fade at the edge where there is more to scroll to. */
|
|
1187
|
+
function markOverflow() {
|
|
1188
|
+
const words = $("hint-text");
|
|
1189
|
+
words.classList.toggle("more-above", words.scrollTop > 2);
|
|
1190
|
+
words.classList.toggle("more-below", words.scrollTop + words.clientHeight < words.scrollHeight - 2);
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
function openHint(byItself) {
|
|
1194
|
+
const hint = $("hint");
|
|
1195
|
+
if (!hint || over) return;
|
|
1196
|
+
closeMenu();
|
|
1197
|
+
clearTimeout(hintTimer);
|
|
1198
|
+
clearTimeout(hintFolding);
|
|
1199
|
+
hint.hidden = false;
|
|
1200
|
+
hint.classList.toggle("auto", !!byItself);
|
|
1201
|
+
$("chip").classList.add("on");
|
|
1202
|
+
$("chip").classList.remove("pulse");
|
|
1203
|
+
$("chip").setAttribute("aria-expanded", "true");
|
|
1204
|
+
placeHint();
|
|
1205
|
+
hint.getBoundingClientRect();
|
|
1206
|
+
hint.classList.remove("away");
|
|
1207
|
+
placeToast();
|
|
1208
|
+
if (byItself) hintTimer = setTimeout(() => closeHint(true), hintMs());
|
|
1209
|
+
if (rfb && !touch) rfb.focus();
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/** Fold the panel into the task button. byItself: its time at the start is up, unless the person is reading it. */
|
|
1213
|
+
function closeHint(byItself) {
|
|
1214
|
+
const hint = $("hint");
|
|
1215
|
+
if (!hint || hint.hidden || hint.classList.contains("away")) return;
|
|
1216
|
+
clearTimeout(hintTimer);
|
|
1217
|
+
if (byItself && !touch && hint.matches(":hover")) {
|
|
1218
|
+
hintTimer = setTimeout(() => closeHint(true), 2000);
|
|
1219
|
+
return;
|
|
1220
|
+
}
|
|
1221
|
+
hint.classList.add("away");
|
|
1222
|
+
const chip = $("chip");
|
|
1223
|
+
chip.classList.remove("on");
|
|
1224
|
+
chip.setAttribute("aria-expanded", "false");
|
|
1225
|
+
if (byItself) {
|
|
1226
|
+
// Where the words went: the task button glows twice.
|
|
1227
|
+
chip.classList.remove("pulse");
|
|
1228
|
+
chip.getBoundingClientRect();
|
|
1229
|
+
chip.classList.add("pulse");
|
|
1230
|
+
}
|
|
1231
|
+
hintFolding = setTimeout(() => {
|
|
1232
|
+
hint.hidden = true;
|
|
1233
|
+
hintBox = null;
|
|
1234
|
+
placeToast();
|
|
1235
|
+
}, 230);
|
|
1236
|
+
if (rfb && !touch) rfb.focus();
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
function toggleHint() {
|
|
1240
|
+
const hint = $("hint");
|
|
1241
|
+
if (hint.hidden || hint.classList.contains("away")) openHint(false);
|
|
1242
|
+
else closeHint();
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
/** The person reads the panel: it stays open until they fold it or press somewhere else. */
|
|
1246
|
+
function holdHint() {
|
|
1247
|
+
clearTimeout(hintTimer);
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
function onPressAnywhere(event) {
|
|
1251
|
+
const target = event.target;
|
|
1252
|
+
const hint = $("hint");
|
|
1253
|
+
if (hint && !hint.hidden && !hint.contains(target) && !$("chip").contains(target)) closeHint();
|
|
1254
|
+
const menu = $("menu");
|
|
1255
|
+
if (menu && !menu.hidden && !menu.contains(target) && !$("more-button").contains(target)) closeMenu();
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
function placeMenu() {
|
|
1259
|
+
const menu = $("menu");
|
|
1260
|
+
if (!menu || menu.hidden || !dockBox) return;
|
|
1261
|
+
placeNear(menu, dockBox, $("dock").classList.contains("side"));
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
function toggleMenu() {
|
|
1265
|
+
const menu = $("menu");
|
|
1266
|
+
if (!menu.hidden) return closeMenu();
|
|
1267
|
+
closeHint();
|
|
1268
|
+
menu.hidden = false;
|
|
1269
|
+
updatePictureControls();
|
|
1270
|
+
placeMenu();
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
function closeMenu() {
|
|
1274
|
+
const menu = $("menu");
|
|
1275
|
+
if (!menu || menu.hidden) return;
|
|
1276
|
+
menu.hidden = true;
|
|
1277
|
+
if (rfb && !touch) rfb.focus();
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
function openSheet(id) {
|
|
1281
|
+
closeMenu();
|
|
1282
|
+
closeHint();
|
|
1283
|
+
if (id !== "more" && keyboardOpen) closeKeyboard();
|
|
1284
|
+
for (const sheet of document.querySelectorAll(".sheet")) sheet.hidden = sheet.id !== id;
|
|
1285
|
+
$("scrim").hidden = false;
|
|
1286
|
+
if (id === "hand-back") $("hand-back-msg").textContent = "";
|
|
1287
|
+
updatePictureControls();
|
|
1288
|
+
placeForKeyboard();
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
function closeSheets() {
|
|
1292
|
+
for (const sheet of document.querySelectorAll(".sheet")) sheet.hidden = true;
|
|
1293
|
+
$("scrim").hidden = true;
|
|
1294
|
+
$("arrows").hidden = true;
|
|
1295
|
+
if (rfb && !touch) rfb.focus();
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
let toastTimer;
|
|
1299
|
+
function toast(text, ms) {
|
|
1300
|
+
const element = $("toast");
|
|
1301
|
+
if (!element) return;
|
|
1302
|
+
clearTimeout(toastTimer);
|
|
1303
|
+
element.textContent = text;
|
|
1304
|
+
element.hidden = false;
|
|
1305
|
+
element.style.opacity = "1";
|
|
1306
|
+
placeToast();
|
|
1307
|
+
toastTimer = setTimeout(() => {
|
|
1308
|
+
element.style.opacity = "0";
|
|
1309
|
+
toastTimer = setTimeout(() => (element.hidden = true), 700);
|
|
1310
|
+
}, ms);
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
/** A message goes next to the controls, past the panel when it is open: above the typing strip while the keyboard is up. */
|
|
1314
|
+
function placeToast() {
|
|
1315
|
+
const element = $("toast");
|
|
1316
|
+
if (!element || element.hidden) return;
|
|
1317
|
+
const strip = $("strip");
|
|
1318
|
+
let anchor = !strip.hidden ? strip.getBoundingClientRect() : dockBox;
|
|
1319
|
+
if (!anchor) return;
|
|
1320
|
+
if (strip.hidden && hintBox) {
|
|
1321
|
+
const left = Math.min(anchor.left, hintBox.left);
|
|
1322
|
+
const top = Math.min(anchor.top, hintBox.top);
|
|
1323
|
+
const right = Math.max(anchor.right, hintBox.right);
|
|
1324
|
+
const bottom = Math.max(anchor.bottom, hintBox.bottom);
|
|
1325
|
+
anchor = { left, top, right, bottom, width: right - left, height: bottom - top };
|
|
1326
|
+
}
|
|
1327
|
+
placeNear(element, anchor, false);
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
function dim(text) {
|
|
1331
|
+
const element = $("dim");
|
|
1332
|
+
if (!element) return;
|
|
1333
|
+
element.hidden = !text;
|
|
1334
|
+
$("dim-text").textContent = text || "";
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
function setDot(state) {
|
|
1338
|
+
const dot = $("dot");
|
|
1339
|
+
if (!dot) return;
|
|
1340
|
+
dot.classList.toggle("on", state === "on");
|
|
1341
|
+
dot.classList.toggle("off", state === "off");
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
function tick() {
|
|
1345
|
+
if (!link || over || !$("time")) return;
|
|
1346
|
+
const left = link.expires - serverNow();
|
|
1347
|
+
if (left <= 0) {
|
|
1348
|
+
$("time").textContent = "0:00";
|
|
1349
|
+
refresh();
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
const seconds = Math.ceil(left / 1000);
|
|
1353
|
+
$("time").textContent = `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
|
|
1354
|
+
$("time").classList.toggle("low", left < 120_000);
|
|
1355
|
+
$("hint-meta").textContent = fill(W.hintMeta, { time: localTime(link.expires) });
|
|
1356
|
+
if (left < 60_000 && !warnedMinute) {
|
|
1357
|
+
warnedMinute = true;
|
|
1358
|
+
toast(W.oneMinute, 4000);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
function fullscreen() {
|
|
1363
|
+
const element = document.documentElement;
|
|
1364
|
+
if (document.fullscreenElement) document.exitFullscreen?.();
|
|
1365
|
+
else element.requestFullscreen?.().catch(() => {});
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
// ---- Handing back, and the end ----
|
|
1369
|
+
|
|
1370
|
+
async function handBackNow(how) {
|
|
1371
|
+
const buttons = [$("hand-done"), $("hand-cannot")];
|
|
1372
|
+
for (const button of buttons) button.disabled = true;
|
|
1373
|
+
const answer = await api("end", { how, note: $("note").value });
|
|
1374
|
+
for (const button of buttons) button.disabled = false;
|
|
1375
|
+
if (answer.ok) return showEnded(how, answer.restored);
|
|
1376
|
+
if (answer.status === 410 || answer.status === 404) {
|
|
1377
|
+
await refresh();
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
$("hand-back-msg").textContent = answer.status === 0 ? W.network : fill(W.failed, { reason: answer.error || answer.status });
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
function showEnded(state, restored) {
|
|
1384
|
+
if (over) return;
|
|
1385
|
+
over = true;
|
|
1386
|
+
clearTimeout(reconnectTimer);
|
|
1387
|
+
dropConnection();
|
|
1388
|
+
document.documentElement.style.overflow = "";
|
|
1389
|
+
$("session")?.remove();
|
|
1390
|
+
$("start").hidden = true;
|
|
1391
|
+
const words =
|
|
1392
|
+
state === "done"
|
|
1393
|
+
? [W.endedDone, W.endedDoneText]
|
|
1394
|
+
: state === "cannot"
|
|
1395
|
+
? [W.endedCannot, W.endedCannotText]
|
|
1396
|
+
: state === "expired"
|
|
1397
|
+
? [W.endedExpired, W.endedExpiredText]
|
|
1398
|
+
: state === "revoked"
|
|
1399
|
+
? [W.endedRevoked, W.endedRevokedText]
|
|
1400
|
+
: [W.notFound, W.notFoundText];
|
|
1401
|
+
const good = state === "done" || state === "cannot";
|
|
1402
|
+
document.body.append(
|
|
1403
|
+
h(
|
|
1404
|
+
"main",
|
|
1405
|
+
{ class: "page ended" },
|
|
1406
|
+
h("div", { class: `check ${good ? "good" : "gone"}`, "aria-hidden": "true" }),
|
|
1407
|
+
h("h1", { text: words[0] }),
|
|
1408
|
+
h("p", { class: "meta", text: restored ? `${words[1]} ${W.restored}` : words[1] }),
|
|
1409
|
+
),
|
|
1410
|
+
);
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
// ---- Begin ----
|
|
1414
|
+
|
|
1415
|
+
startButton.addEventListener("click", start);
|
|
1416
|
+
refresh().then(() => {
|
|
1417
|
+
if (over || !link) return startMessage(link ? "" : W.network);
|
|
1418
|
+
showStart();
|
|
1419
|
+
// The browser that pressed Start goes on where it was, as after a reload; any other only sees the start screen.
|
|
1420
|
+
if (link.state === "started" && link.mine) start();
|
|
1421
|
+
});
|