-
Notifications
You must be signed in to change notification settings - Fork 4
feat(editor): export a track to Guitar Pro (.gp5) #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // File ▸ Export ▸ Guitar Pro (.gp5). Downloads the GP5 bytes the Tab View | ||
| // plugin converts the current fretted part's SAVED pack into — the very bytes | ||
| // the read-only Tab preview engraves (src/tab-preview.js). The conversion | ||
| // endpoint (_tabPreviewUrlPure) lives in tab-preview.js and is reused here, so | ||
| // the tabview contract is written once; export adds only the browser download, | ||
| // plus a fretted/saved guard, the session's own dirty prompt (the preview is a | ||
| // lens labelled "as last saved" — an export is a FILE you take away, so it may | ||
| // not silently be stale) and status messages worded for a "save it out" action. | ||
| // | ||
| // Pure, decision-shaped helpers stay inside the @pure:gp5-export block (the JS | ||
| // test suite slices that block into a `new Function`); the download seam and the | ||
| // async orchestrator reach the DOM/network and are live-verified. | ||
|
|
||
| import { S } from './state.js'; | ||
| import { setStatus } from './ui.js'; | ||
| import { guardSessionTransition } from './session-lifecycle.js'; | ||
| import { _tabPreviewUrlPure } from './tab-preview.js'; | ||
|
|
||
| /* @pure:gp5-export:start */ | ||
| // Which parts can export, with the exact user-facing reason when one can't. | ||
| // Mirrors the Tab preview guard: fretted-only (keys/drums pack as pitch, not | ||
| // string·fret, so a GP conversion would engrave nonsense), and a SAVED pack is | ||
| // required because the converter reads the last-saved pack. Regexes inlined so | ||
| // this @pure block stays self-contained and slice-testable (the tab-preview | ||
| // block inlines the same fretted test for the same reason). | ||
| function _gp5ExportGuardPure(filename, arrName, hasArrangements) { | ||
| if (!hasArrangements) return { ok: false, reason: 'Load a song first.' }; | ||
| const nm = String(arrName || ''); | ||
| if (/^(keys|piano|keyboard|synth)/i.test(nm) || /^drums/i.test(nm)) { | ||
| return { ok: false, reason: 'Guitar Pro export is for fretted tracks — keys and drums tracks have no tab.' }; | ||
| } | ||
| if (!filename) { | ||
| return { ok: false, reason: 'Save the song first — export reads the saved pack.' }; | ||
| } | ||
| return { ok: true, reason: '' }; | ||
| } | ||
| // The download filename: the song's base name (pack extension dropped) + the | ||
| // part name, sanitised so it can't break a download across OSes. | ||
| function _gp5ExportNamePure(filename, partName) { | ||
| let base = String(filename || '').replace(/\.(feedpak|sloppak)$/i, '').trim(); | ||
| if (!base) base = 'track'; | ||
| const part = String(partName || '').trim(); | ||
| let name = part ? base + ' — ' + part : base; | ||
| // Drop the characters that are illegal in a filename on Windows/macOS/Linux. | ||
| name = name.replace(/[\\/:*?"<>|]/g, '-').replace(/\s+/g, ' ').trim(); | ||
| return name + '.gp5'; | ||
| } | ||
| // Map a failed conversion response to the honest, export-worded message. | ||
| function _gp5ExportHttpMessagePure(status, bodyText) { | ||
| if (status === 404) { | ||
| return 'Export needs the Tab View plugin installed — or the song has no saved pack yet (Save/Build first).'; | ||
| } | ||
| if (status === 501) { | ||
| return 'The host is too old for Guitar Pro export — update feedBack.'; | ||
| } | ||
| const body = String(bodyText || '').slice(0, 140); | ||
| return 'Export failed (' + status + ')' + (body ? ': ' + body : ''); | ||
| } | ||
| /* @pure:gp5-export:end */ | ||
| export { _gp5ExportGuardPure, _gp5ExportNamePure, _gp5ExportHttpMessagePure }; | ||
|
|
||
| // Trigger a browser download of `bytes` as `name`. A tiny DOM seam — all the | ||
| // testable logic lives in the pure helpers above. The object URL is revoked on | ||
| // the next tick so the click has grabbed the blob first (revoking synchronously | ||
| // cancels the download in some browsers); it is a fire-once transient, not a | ||
| // registered timer, so it doesn't leak across screen re-injection. | ||
| function _downloadBytes(bytes, name) { | ||
| const blob = new Blob([bytes], { type: 'application/octet-stream' }); | ||
| const url = URL.createObjectURL(blob); | ||
| const a = document.createElement('a'); | ||
| a.href = url; | ||
| a.download = name; | ||
| document.body.appendChild(a); | ||
| a.click(); | ||
| a.remove(); | ||
| setTimeout(() => URL.revokeObjectURL(url), 0); | ||
| } | ||
|
|
||
| export async function editorExportGp5() { | ||
| const cur = () => (S.arrangements.length ? S.arrangements[S.currentArr] : null); | ||
| let arr = cur(); | ||
| let guard = _gp5ExportGuardPure(S.filename, arr && arr.name, !!S.arrangements.length); | ||
| if (!guard.ok) { setStatus(guard.reason); return; } | ||
| // The converter reads the SAVED pack and indexes it by S.currentArr — and | ||
| // it CLAMPS that index into the saved track list. So an unsaved session | ||
| // doesn't just export stale notes: add or reorder a track and the clamp | ||
| // hands you a DIFFERENT track's bytes under the requested track's name. | ||
| // Offer the session's own Save / Don't Save / Cancel prompt first. | ||
| if (!(await guardSessionTransition('exporting to Guitar Pro'))) { | ||
| setStatus('Export cancelled.'); | ||
| return; | ||
| } | ||
| // The prompt awaited: the song (and the current part) may have moved. | ||
| arr = cur(); | ||
| guard = _gp5ExportGuardPure(S.filename, arr && arr.name, !!S.arrangements.length); | ||
| if (!guard.ok) { setStatus(guard.reason); return; } | ||
| const name = _gp5ExportNamePure(S.filename, arr && arr.name); | ||
| setStatus('Exporting ' + name + '…'); | ||
| try { | ||
| // Cache-bust with the timestamp so an export right after a Save always | ||
| // re-converts the latest pack (same reason the preview does). | ||
| const resp = await fetch(_tabPreviewUrlPure(S.filename, S.currentArr, Date.now())); | ||
| if (!resp.ok) { | ||
| let body = ''; | ||
| try { body = await resp.text(); } catch (_) { /* keep '' */ } | ||
| setStatus(_gp5ExportHttpMessagePure(resp.status, body)); | ||
| return; | ||
| } | ||
| const buf = await resp.arrayBuffer(); | ||
| _downloadBytes(new Uint8Array(buf), name); | ||
| setStatus('Exported ' + name); | ||
| } catch (e) { | ||
| setStatus('Export failed: ' + (e && e.message ? e.message : e)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Do not continue after “Don’t Save” for this export.
guardSessionTransitionreturnstruefor bothdiscardand successfulsave. The export can therefore use unsavedSmetadata/current index while the converter reads the saved pack, exporting stale or even another track’s bytes under the current filename. Expose the transition choice and requiresave, or cancel export afterdiscard.🧰 Tools
🪛 ast-grep (0.44.1)
[error] 89-89: React's useState should not be directly called
Context: setStatus('Export cancelled.')
Note: [CWE-710] Improper Adherence to Coding Standards. Security best practice.
(usestate-direct-usage)
[error] 95-95: React's useState should not be directly called
Context: setStatus(guard.reason)
Note: [CWE-710] Improper Adherence to Coding Standards. Security best practice.
(usestate-direct-usage)
🤖 Prompt for AI Agents