Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | import { html, nothing, render, type TemplateResult } from 'lit';
// Virtual module produced by the `be-music:acknowledgements` Vite plugin (see `vite.config.ts`).
import loadAcknowledgements, { type Acknowledgement } from 'virtual:acknowledgements';
import { bindPlayerHeader } from './player-header.ts';
import {
detectPlayerLang,
onPlayerLangChange,
playerLang,
playerText,
setPlayerLang,
type PlayerLang,
} from './player-copy.ts';
import { HelpKeyboard, type HelpKeyboardSource } from './ui/keyboard/help-keyboard.ts';
import { releaseFocusOnClose } from './ui/dialog-focus.ts';
/** Opens Help once it is wired (LR2's F1); nothing before. */
let openHelp: (() => void) | undefined;
let reportExpanded: ((open: boolean) => void) | undefined;
/** Reports every Help opening and dismissal, including Escape and backdrop clicks. */
export function watchHelpExpanded(report: (open: boolean) => void): void {
reportExpanded = report;
}
/** The player's keys and the current screen's key map, for the Keyboard tab; LR2's default keys until the app sets it. */
let keyboardSource: () => HelpKeyboardSource = () => ({ settings: {}, scheme: 'lr2' });
/** Gives the Keyboard tab the player's keys and the key map of the screen shown. */
export function setHelpKeyboardSource(source: () => HelpKeyboardSource): void {
keyboardSource = source;
}
/** Opens the Help dialog on its Usage tab, as the header's Help button does. */
export function openHelpModal(): void {
openHelp?.();
}
export function wireHelpModal(): void {
const dialog = document.querySelector<HTMLDialogElement>('#help-modal');
const closeButton = document.querySelector<HTMLButtonElement>('#help-modal-close');
const tabs = Array.from(document.querySelectorAll<HTMLButtonElement>('#help-modal [role="tab"]'));
const panes = Array.from(document.querySelectorAll<HTMLElement>('#help-modal [role="tabpanel"]'));
const ossList = document.querySelector<HTMLOListElement>('#help-oss-list');
if (!dialog || !closeButton || tabs.length === 0 || panes.length === 0 || !ossList) {
return;
}
let ossPopulated = false;
const keyboardPane = document.querySelector<HTMLElement>('#help-pane-keyboard');
const keyboard = keyboardPane === null ? undefined : new HelpKeyboard(keyboardPane, () => keyboardSource());
let keyboardShown = false;
// Basecoat's tabs: the selected tab and panel carry `aria-selected`, the others are hidden and out of the tab order.
const activatePane = (paneId: string): void => {
for (const tab of tabs) {
const matches = tab.dataset.pane === paneId;
tab.setAttribute('aria-selected', matches ? 'true' : 'false');
tab.tabIndex = matches ? 0 : -1;
}
for (const pane of panes) {
const matches = pane.id === `help-pane-${paneId}`;
pane.setAttribute('aria-selected', matches ? 'true' : 'false');
pane.toggleAttribute('hidden', !matches);
}
keyboardShown = paneId === 'keyboard';
if (keyboardShown) keyboard?.show(playerLang());
if (paneId === 'oss' && !ossPopulated) {
ossPopulated = true;
void renderAcknowledgementsList(ossList);
}
};
// A native modal dialog, which Escape closes too.
const open = (): void => {
// The Keyboard tab starts again on the key map the screen shown reads.
keyboard?.reset();
activatePane('usage');
if (!dialog.open) {
reportExpanded?.(true);
dialog.showModal();
}
};
const close = (): void => dialog.close();
// Closed, the keys go back to the play: left on the Help button, Enter (meant to start the chart) would reopen it.
releaseFocusOnClose(dialog);
bindPlayerHeader({ openHelp: open });
openHelp = open;
closeButton.addEventListener('click', close);
document.querySelector('#help-open')?.addEventListener('click', open);
dialog.addEventListener('close', () => reportExpanded?.(false));
// A click on the backdrop lands on the dialog itself.
dialog.addEventListener('click', (event) => {
if (event.target === dialog) close();
});
// Keys stay in the dialog, so the play behind it does not take them.
dialog.addEventListener('keydown', (event) => event.stopPropagation());
tabs.forEach((tab, index) => {
tab.addEventListener('click', () => {
const pane = tab.dataset.pane;
if (pane) activatePane(pane);
});
tab.addEventListener('keydown', (event) => {
const step = event.key === 'ArrowRight' ? 1 : event.key === 'ArrowLeft' ? -1 : 0;
if (step === 0) return;
event.preventDefault();
const next = tabs[(index + step + tabs.length) % tabs.length]!;
next.focus();
next.click();
});
});
// The Keyboard tab follows the language switch.
onPlayerLangChange(() => {
if (keyboardShown) keyboard?.show(playerLang());
});
wireUsageLanguageSwitch();
}
function wireUsageLanguageSwitch(): void {
const toggles = Array.from(document.querySelectorAll<HTMLButtonElement>('.help-lang-toggle'));
const blocks = Array.from(document.querySelectorAll<HTMLElement>('.help-lang'));
const initial = detectPlayerLang();
if (toggles.length === 0 || blocks.length === 0) {
setPlayerLang(initial);
syncPublicBetaNotice(initial);
return;
}
activate(initial);
for (const toggle of toggles) {
toggle.addEventListener('click', () => {
const next = toggle.dataset.lang;
if (next === 'ja' || next === 'en') activate(next);
});
}
}
/** Switches Help, the Public Beta line and the shell copy together. The header's language menu uses this too. */
export function applyPlayerLanguage(lang: PlayerLang): void {
activate(lang);
}
function activate(lang: PlayerLang): void {
const toggles = Array.from(document.querySelectorAll<HTMLButtonElement>('.help-lang-toggle'));
const blocks = Array.from(document.querySelectorAll<HTMLElement>('.help-lang'));
for (const toggle of toggles) {
const matches = toggle.dataset.lang === lang;
toggle.dataset.variant = matches ? 'secondary' : 'outline';
toggle.setAttribute('aria-pressed', matches ? 'true' : 'false');
}
for (const block of blocks) {
const matches = block.dataset.lang === lang;
block.classList.toggle('is-active', matches);
block.toggleAttribute('hidden', !matches);
}
syncPublicBetaNotice(lang);
setPlayerLang(lang);
}
function syncPublicBetaNotice(lang: PlayerLang): void {
for (const notice of document.querySelectorAll<HTMLElement>('[data-public-beta-notice]')) {
notice.toggleAttribute('hidden', notice.dataset.publicBetaNotice !== lang);
}
}
async function renderAcknowledgementsList(list: HTMLOListElement): Promise<void> {
const message = (text: string): void => {
render(html`<li class="ack-empty">${text}</li>`, list);
};
message(playerText().shell.ackLoading);
let acknowledgements: Acknowledgement[];
try {
acknowledgements = await loadAcknowledgements();
} catch {
message(playerText().shell.ackFailed);
return;
}
if (acknowledgements.length === 0) {
message(playerText().shell.ackEmpty);
return;
}
render(acknowledgements.map(acknowledgementCard), list);
}
function acknowledgementCard(entry: Acknowledgement): TemplateResult {
const link = entry.homepage ?? cleanRepositoryURL(entry.repository);
const copy = playerText().shell;
return html`<li class="ack-card">
<div class="ack-card-heading">
<span class="ack-card-name">${entry.name}</span>
<span class="ack-card-version">v${entry.version}</span>
${entry.license ? html`<span class="ack-card-license">${entry.license}</span>` : nothing}
</div>
${
link || entry.author
? html`<div class="ack-card-meta">
${link ? html`<a class="ack-card-link" href=${link} target="_blank" rel="noopener noreferrer">${link.replace(/^https?:\/\//u, '')}</a>` : nothing}
${entry.author ? html`<span class="ack-card-author">${entry.author}</span>` : nothing}
</div>`
: nothing
}
${
entry.licenseText
? html`<details class="ack-card-license-details">
<summary>${copy.ackLicense}</summary>
<pre class="ack-card-license-text">${entry.licenseText}</pre>
</details>`
: html`<div class="ack-card-license-missing">${copy.ackNoLicense}</div>`
}
</li>`;
}
function cleanRepositoryURL(raw: string | undefined): string | undefined {
if (!raw) return undefined;
return raw
.replace(/^git\+/u, '')
.replace(/^git:\/\//u, 'https://')
.replace(/\.git$/u, '');
}
|