Skip to content

refactor(editor): move the song-import flows to src/import.js (R2, step 29) - #181

Merged
byrongamatos merged 2 commits into
mainfrom
refactor/r2-step29-gp-import
Jul 10, 2026
Merged

refactor(editor): move the song-import flows to src/import.js (R2, step 29)#181
byrongamatos merged 2 commits into
mainfrom
refactor/r2-step29-gp-import

Conversation

@byrongamatos

@byrongamatos byrongamatos commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

What

Step 29 of the R2 module-extraction refactor: move the song-import flows out of the src/main.js monolith into a new native ES module src/import.js (no build step, source-served).

Covers:

  • Add Keys arrangement import (GP / MIDI / MusicXML), including the RH/LH pair auto-select and the empty-keys path.
  • Import Guitar/Bass from a GP file (add or replace an existing chart).
  • The shared MIDI tempo-map offer dialog (_maybeOfferMidiTempoMap) and its @pure:midi-tempo-choice helpers.
  • The shared unmapped-percussion mapper (_showDrumImportUnmappedModal).

How the seam is drawn

  • The 11 window.editor* entry points become named exports; main.js re-attaches them to window (a module can't own window.foo =).
  • _maybeOfferMidiTempoMap and _showDrumImportUnmappedModal are exported back — main.js's drum-import path still calls them.
  • New host.effectiveAudioOffset hook lets import.js read the session offset (S.offset + the applied UI nudge) without importing back into main.js (would cycle). Defaults to 0.
  • The @pure:guitar-import and @pure:midi-tempo-choice blocks move verbatim; the three suites that slice them (import_guitar_track, keys_pair_select, midi_tempo_import) are retargeted to src/import.js.

main.js drops ~1035 lines. No behavior change: every cross-boundary dependency (draw, updateArrangementSelector, updateStatus, resizeForLaneChange, effectiveAudioOffset) routes through host.

Verification

  • 90/90 JS test suites pass; ESLint gate clean (0 errors).
  • Codex preflight: 0 correctness issues.
  • New headless harness (verify_import.py) drives a real Add-Keys import of a 71-byte synthetic SMF through the actual file input and editorDoAddKeys: asserts the 11 handlers bind, both modals open/close, the replace-target dropdown populates from S.arrangements, one arrangement is appended, the MIDI tempo-map modal appears, and the POSTed audio_offset reflects a seeded +5s offset — proving host.effectiveAudioOffset is wired. Negative-checked: neutering the hook flips that assertion to FAIL.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added import workflows for Keys arrangements and guitar/bass tracks from GP, MIDI, and MusicXML files.
    • Added options to add or replace arrangements during guitar/bass imports.
    • Added MIDI tempo-map selection and unmapped percussion note-mapping prompts.
    • Added support for creating empty Keys arrangements.
  • Bug Fixes
    • Improved import handling when host audio-offset configuration is unavailable.

…ep 29)

Extract Add-Keys (GP/MIDI/MusicXML), guitar/bass GP import (add or
replace), the MIDI tempo-map offer, and the unmapped-percussion mapper
out of the src/main.js monolith into a new native ES module.

- src/import.js: 11 window.editor* entry points (re-attached by main.js
  since a module can't own window.*), plus _maybeOfferMidiTempoMap and
  _showDrumImportUnmappedModal exported back for the drum-import path in
  main.js. The @pure:guitar-import and @pure:midi-tempo-choice blocks
  move verbatim.
- host.js: new effectiveAudioOffset hook so import.js can read the
  session offset (S.offset + the applied UI nudge) without importing back
  into main.js. Default returns 0.
- Retarget the three affected suites (import_guitar_track,
  keys_pair_select, midi_tempo_import) to slice their pure functions from
  src/import.js.

main.js drops ~1035 lines. Behavior unchanged: internal window.* calls
now reference the module-local functions, and every cross-boundary dep
(draw, updateArrangementSelector, updateStatus, resizeForLaneChange,
effectiveAudioOffset) routes through host.

Verified: 90/90 JS suites pass, ESLint gate clean (0 errors), and a new
headless harness drives a real synthetic-MIDI Add-Keys import end to end
(offset-shifted request body proves the effectiveAudioOffset hook;
negative-checked by neutering it).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 10, 2026 05:59
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ebcffb83-aeb6-470a-9a94-2795d46aeeb6

📥 Commits

Reviewing files that changed from the base of the PR and between f04cf49 and 3152db3.

📒 Files selected for processing (6)
  • src/host.js
  • src/import.js
  • src/main.js
  • tests/import_guitar_track.test.mjs
  • tests/keys_pair_select.test.js
  • tests/midi_tempo_import.test.mjs

📝 Walkthrough

Walkthrough

Import workflows are extracted into src/import.js, covering Keys, guitar/bass, MIDI tempo maps, and unmapped percussion. main.js wires the exported handlers and audio-offset hook, while tests update their source extraction targets.

Changes

Song import workflows

Layer / File(s) Summary
Import module wiring
src/host.js, src/main.js
Adds the default effectiveAudioOffset host callback, wires import handlers, and removes moved import logic and unused imports from main.js.
Keys import flow
src/import.js, tests/keys_pair_select.test.js, tests/import_guitar_track.test.mjs
Adds MusicXML, MIDI, GP, and empty-Keys imports with stale-request protection, track selection, arrangement registration, rendering, and updated helper extraction tests.
Guitar and bass import flow
src/import.js, tests/import_guitar_track.test.mjs
Adds guitar/bass track filtering, family-constrained replacement targets, and add-or-replace arrangement import behavior.
Post-import triage
src/import.js, tests/midi_tempo_import.test.mjs
Adds MIDI tempo-map choice and unmapped percussion mapping modals, including tempo application, hit deduplication, and updated tempo helper extraction tests.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Editor
  participant import.js
  participant ImportAPI
  participant SessionState
  Editor->>import.js: Select Keys, GP, MIDI, or MusicXML file
  import.js->>ImportAPI: Parse or import file
  ImportAPI-->>import.js: Return tracks or arrangements
  import.js->>ImportAPI: Register imported arrangement
  ImportAPI-->>import.js: Return registered arrangement
  import.js->>SessionState: Append, render, and refresh editor state
Loading

Possibly related PRs

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/r2-step29-gp-import

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the editor’s song-import functionality by extracting Add-Keys (GP/MIDI/MusicXML), GP guitar/bass import (add/replace), and the shared post-import dialogs into a new native ES module (src/import.js), while keeping the existing window.editor* entrypoints by re-attaching them from src/main.js.

Changes:

  • Added src/import.js and moved the import flows + shared MIDI tempo-map / unmapped-percussion dialogs into it as named exports.
  • Updated src/main.js to import those handlers, re-attach them to window, and expose a new host.effectiveAudioOffset hook to avoid cyclic imports.
  • Retargeted the “slice shipping code” unit tests to read from src/import.js instead of src/main.js.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/midi_tempo_import.test.mjs Retargets tempo-map pure-helper extraction from src/main.js to src/import.js.
tests/keys_pair_select.test.js Retargets _keysDefaultSelection extraction from src/main.js to src/import.js.
tests/import_guitar_track.test.mjs Retargets guitar-import pure-helper extraction from src/main.js to src/import.js.
src/main.js Imports src/import.js, wires host.effectiveAudioOffset, and re-attaches window.editor* import handlers.
src/import.js New module containing the extracted import flows and shared dialogs, using host for cross-boundary calls.
src/host.js Adds the effectiveAudioOffset host hook with a safe default.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/import.js
Comment on lines +22 to +27
let _addKeysSourcePath = null; // server-side path to the uploaded file
let _addKeysSourceFormat = null; // 'gp' or 'midi'
// Cached after a successful list-tracks call; the keys-track radio value
// is an index into this array, not the track's MIDI/GP index, because
// format-0 channel splits can yield multiple picker entries sharing the
// same MIDI `index`.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — updated both comments (musicxml format, checkbox multi-select) in the follow-up commit.

…i-select checkboxes)

Addresses Copilot review on #181.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants