All files / app-play/src/ui chart-diagnostics.ts

0% Statements 0/16
0% Branches 0/16
0% Functions 0/5
0% Lines 0/15

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                                                                                                                               
import { resolveChart } from '@be-music/chart-model';
import type { BeMusicJSON, ChartDiagnostic } from '@be-music/chart-source';
import { html, nothing, render } from 'lit';
import { onPlayerLangChange, playerLang } from '../player-copy.ts';
 
const japanese: Record<ChartDiagnostic['code'], string> = {
  'legacy-bmson': '旧形式の bmson v0.21 を変換しました。',
  'unknown-mode-hint': '未対応または無効な bmson のモード指定です。',
  'stacked-note-length': '長さの異なる重複ノートとそのキー音を除外しました。',
  'missing-reference': '譜面内の参照先が定義されていません。',
  'unsupported-option': '未対応の譜面オプションです。',
  'invalid-option': '無効な譜面オプションです。',
  'unsupported-lane-type': '未対応のレーン構成です。',
  'unsupported-channel': '未対応のチャンネルです。',
};
 
/** The host's warning list is shared by every select and decide skin. */
export function createChartDiagnosticsPanel(root: HTMLElement) {
  const cache = new WeakMap<BeMusicJSON, readonly ChartDiagnostic[]>();
  let diagnostics: readonly ChartDiagnostic[] = [];
  function update(): void {
    const ja = playerLang() === 'ja';
    render(
      diagnostics.length === 0
        ? nothing
        : html`
            <details @keydown=${stopSceneKeys} @keyup=${stopSceneKeys}>
              <summary>${ja ? `譜面の警告 (${diagnostics.length})` : `Chart warnings (${diagnostics.length})`}</summary>
              <ul>
                ${diagnostics.map(({ code, message, subject, position }) => {
                  const location =
                    position.kind === 'field'
                      ? position.path
                      : `#${String(position.measure).padStart(3, '0')}${position.channel} ${position.position.join('/')}`;
                  return html`<li>
                    <code>${location}</code>
                    ${ja ? [japanese[code], subject].filter(Boolean).join(' ') : message}
                  </li>`;
                })}
              </ul>
            </details>
          `,
      root,
    );
  }
  onPlayerLangChange(update);
  return {
    show(chart?: BeMusicJSON, chartPath?: string): void {
      if (chart === undefined) {
        diagnostics = [];
      } else {
        diagnostics =
          cache.get(chart) ?? resolveChart(chart, { chartPath, inferBmsLNTypeWhenMissing: false }).diagnostics;
        cache.set(chart, diagnostics);
      }
      update();
    },
  };
}
 
function stopSceneKeys(event: KeyboardEvent): void {
  if (event.key === 'Enter' || event.key === ' ') event.stopPropagation();
}