Repository navigation
Expand file tree
/
Copy pathhelp.js
More file actions
97 lines (97 loc) · 6.38 KB
/
Copy pathhelp.js
File metadata and controls
97 lines (97 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
// The Help tour (help.html; issue #230): a few pages of the basics, each with a short loop of the keys at work
// (styles.css Help). Its own page and its own scope: main lays it over the whole window, both
// halves of a split, and takes it away again when it closes (main.js openOverlay). The page that asked gave its theme in
// the query, gets the keys back on close, and the palette too when ⌘K is what closed it.
// A native modal <dialog>: it keeps the focus and closes on Esc by itself. A page's loop runs only while it has .on:
// putting the class back restarts it from the top.
const helpApi = window.api;
if (new URLSearchParams(location.search).get('theme') === 'dark') document.documentElement.dataset.theme = 'dark';
const helpEl = document.getElementById('help'), helpPages = [...helpEl.querySelectorAll('.hpage')];
const helpBack = document.getElementById('helpBack'), helpNext = document.getElementById('helpNext');
for (const i of helpEl.querySelectorAll('i[data-icon]')) i.innerHTML = (window.ICONS && window.ICONS[i.dataset.icon]) || ''; // icons.js: our own markup
const helpDots = helpPages.map((_, i) => {
const d = document.createElement('button');
d.type = 'button'; d.tabIndex = -1; d.setAttribute('aria-label', 'Page ' + (i + 1));
d.onclick = () => showHelpPage(i);
return d;
});
document.getElementById('helpDots').append(...helpDots);
let helpAt = 0, helpPalette = false, helpChatGPT = false;
function showHelpPage(n) {
helpAt = Math.max(0, Math.min(helpPages.length - 1, n));
helpPages.forEach((p, i) => { p.classList.toggle('on', i === helpAt); p.classList.toggle('before', i < helpAt); p.inert = i !== helpAt; });
helpDots.forEach((d, i) => d.classList.toggle('on', i === helpAt));
helpBack.disabled = helpAt === 0;
helpNext.textContent = helpAt === helpPages.length - 1 ? 'Get started' : 'Next';
helpNext.focus(); // Enter keeps going; the arrows go either way
}
function helpStep(dir) { if (helpAt + dir >= helpPages.length) helpEl.close(); else showHelpPage(helpAt + dir); }
// Every way out (Esc, the ×, the scrim, the last Next, ⌘K) ends here
helpEl.addEventListener('close', () => { if (helpApi && helpApi.closeOverlay) helpApi.closeOverlay({ palette: helpPalette, chatgpt: helpChatGPT }); });
helpBack.onclick = () => helpStep(-1);
helpNext.onclick = () => helpStep(1);
// The phone page's links, the addresses its codes hold: in the browser, as every link in Orbital opens. Without main (the
// manual, the flows) each is a plain link.
for (const id of ['helpMobileLink', 'helpObtainiumLink', 'helpAndroidLink']) document.getElementById(id).onclick = (e) => {
if (!helpApi || !helpApi.openExternal) return;
e.preventDefault();
helpApi.openExternal(e.currentTarget.href).catch(() => {});
};
// The phone page's choice (#help-mobile): iPhone or Android, one at a time in one place, so a second code later takes
// the same spot. A radio group: one tab stop, the arrows switch inside it (and do not page the tour), Space and a click pick.
// Android is a download once the latest release has one (updater.js androidRelease, the APK release.sh attaches), and
// coming soon until then or when GitHub cannot be asked: the code is never shown for a file that is not there.
const helpOS = [...document.querySelectorAll('#help-mobile [role="radio"]')];
let helpApk = 'no';
function showHelpOS(os, focus) {
for (const b of helpOS) {
const on = b.dataset.os === os;
b.classList.toggle('on', on); b.setAttribute('aria-checked', String(on)); b.tabIndex = on ? 0 : -1;
if (on && focus) b.focus();
}
for (const part of document.querySelectorAll('#help-mobile [data-os]:not([role="radio"])')) part.hidden = part.dataset.os !== os || (!!part.dataset.apk && part.dataset.apk !== helpApk);
}
if (helpApi && helpApi.androidRelease) helpApi.androidRelease().then((r) => {
if (!r) return;
helpApk = 'yes';
showHelpOS(helpOS.find((b) => b.getAttribute('aria-checked') === 'true').dataset.os);
}, () => {});
for (const b of helpOS) {
b.onclick = () => showHelpOS(b.dataset.os);
b.onkeydown = (e) => {
if (e.key !== 'ArrowLeft' && e.key !== 'ArrowRight') return;
e.preventDefault(); e.stopPropagation();
showHelpOS(helpOS[(helpOS.indexOf(b) + (e.key === 'ArrowRight' ? 1 : helpOS.length - 1)) % helpOS.length].dataset.os, true);
};
}
// The ChatGPT page: sign in with ChatGPT happens on its ⌘K page in the page that asked (renderer/agent.js startChatGPTLogin).
// Signed in already, the line says so instead, at the button's height: the card is as tall as its tallest page, so a
// line that came or went would move it after it had opened. Not signed in and no ChatGPT app on this Mac (main/ai.js
// installed), the line asks for the app first, and looks again when Orbital comes back from the browser or the installer.
document.getElementById('helpChatGPT').onclick = () => { helpChatGPT = true; helpEl.close(); };
const helpAI = document.getElementById('helpAI'), helpGetApp = document.getElementById('helpGetApp');
document.getElementById('helpGetAppButton').onclick = () => { if (helpApi && helpApi.openExternal) helpApi.openExternal('https://openai.com/chatgpt/download/').catch(() => {}); };
function checkHelpAI() {
if (helpApi && helpApi.chatgptStatus) helpApi.chatgptStatus().then((s) => {
const getApp = !!s && !s.signedIn && s.installed === false;
helpGetApp.hidden = !getApp; helpAI.hidden = getApp;
if (s && s.signedIn) helpAI.textContent = 'Signed in with ChatGPT' + (s.email ? ' as ' + s.email : '');
}, () => {});
}
checkHelpAI();
window.addEventListener('focus', () => { if (!helpGetApp.hidden) checkHelpAI(); });
document.getElementById('helpClose').onclick = () => helpEl.close();
helpEl.addEventListener('mousedown', (e) => { if (e.target === helpEl) helpEl.close(); }); // the scrim, as with the palette
// ⌘K, the key the tour teaches, closes it and opens the palette in the page that asked
helpEl.addEventListener('keydown', (e) => {
if ((e.metaKey || e.ctrlKey) && e.key === 'k') { helpPalette = true; helpEl.close(); }
else if (e.metaKey || e.ctrlKey || e.altKey) return;
else if (e.key === 'ArrowRight') helpStep(1);
else if (e.key === 'ArrowLeft') helpStep(-1);
else return;
e.preventDefault();
});
helpEl.showModal();
// Cmd+K Install mobile app opens the tour on its iPhone page (?at=mobile, main.js openOverlay); everything else at the start
showHelpPage(Math.max(0, helpPages.findIndex((p) => p.id === 'help-' + new URLSearchParams(location.search).get('at'))));