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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 1x 1x 1x 1x 26x 27x 27x 27x 28x 28x 28x 28x 28x 28x 28x 27x 27x 27x 28x 28x 28x 28x 28x 28x 28x 28x 28x 10x 28x 28x 28x 28x 31x 2x 2x 4x 4x 4x 2x 2x 29x 28x 28x 28x 30x 29x 29x 29x 1x 11x 30x 1x 1x 1x 1x 1x 1x 1x 28x 18x 18x 18x 1x 1x 1x 18x 28x 28x 28x 18x 18x 18x 18x 18x 28x 28x 11x 11x 11x 11x 28x 28x 28x 28x 28x 33x 33x 31x 33x 28x 28x 1x 28x 28x 46x 46x 62x 46x 46x 28x 28x 28x 28x 28x 62x 62x 62x 32x 32x 32x 62x 62x 28x 28x 28x 29x 29x 29x 29x 28x 28x 28x 28x 28x 60x 60x 62x 62x 62x 62x 62x 1x 61x 62x 62x 62x 62x 62x 62x 60x 60x 28x 28x 28x | import type {ReactNode} from 'react'
import {detectKey, isChord, transposeKey} from '@/lib/chordpro'
import NotationBlock from '@/components/chart/NotationBlock'
import type {Chart, ChartLine, Section, Segment} from '@/lib/chordpro'
/**
* Renders a parsed chart: chords above lyrics, every section written out in
* full. Pure markup (no hooks) so it works in server components, the editor
* preview and performance mode alike. Size it by setting font-size on a parent.
*/
export default function ChartBody({
chart,
columns = true,
className = '',
top,
extra,
notationOpen = false,
}: {
chart: Chart
columns?: boolean
className?: string
/** Shown before the first section, flowing with the chart (personal cues) */
top?: ReactNode
/** Shown under a section's heading, by section index (personal cues) */
extra?: (index: number) => ReactNode
/** Notation starts unfolded (the editor's preview); otherwise folded */
notationOpen?: boolean
}) {
if (!chart.sections.length)
return (
<>
{top}
<p className="text-muted">No chart yet.</p>
</>
)
return (
<div className={`${columns ? 'chart-columns' : ''} ${className}`}>
{top}
{chart.sections.map((s, i) => (
<ChartSection
key={i}
section={s}
extra={extra?.(i)}
songKey={s.abc ? originalKey(chart, s.abcSteps ?? 0) : null}
notationOpen={notationOpen}
/>
))}
</div>
)
}
/** The key the chart was written in, before transposing by `steps`. */
function originalKey(chart: Chart, steps: number) {
const key = detectKey(chart)
return key && steps ? transposeKey(key, -steps) : key
}
function ChartSection({
section,
extra,
songKey,
notationOpen,
}: {
section: Section
extra?: ReactNode
/** The chart's original key, for notation without a K: line */
songKey: string | null
notationOpen: boolean
}) {
const label =
section.label ||
// Unlabelled paragraphs, tab and notation need no heading
(['tab', 'abc', 'part'].includes(section.type) ? '' : section.type)
// Notation folds down to its heading, which is what opens it
if (section.abc)
return (
<section className="mb-[1.1em]">
{extra}
<NotationBlock
label={label || 'Notation'}
note={section.note}
abc={section.abc}
songKey={songKey}
steps={section.abcSteps ?? 0}
open={notationOpen}
/>
</section>
)
const allRuns = runs(section.lines)
const units = allRuns.map((run, i) =>
run[0].kind === 'tab' ? (
// One scroll area per tab block, so the strings move together
<div key={i} className="chart-tab-block" data-hscroll="">
{run.map((l, j) => (
<div key={j} className="chart-tab">
{l.kind === 'tab' ? l.text : ''}
</div>
))}
</div>
) : (
run.map((l, j) => <Line key={`${i}-${j}`} line={l} />)
),
)
// A remark ("softer, hold each chord") is about what comes next: never
// let a column or page break come between them
const blocks: ReactNode[] = []
for (let i = 0; i < units.length; i++) {
if (allRuns[i][0].kind !== 'comment' || i === units.length - 1) {
blocks.push(units[i])
continue
}
const glued: ReactNode[] = []
while (i < units.length - 1 && allRuns[i][0].kind === 'comment')
glued.push(units[i++])
glued.push(units[i])
blocks.push(
<div key={`keep-${i}`} className="chart-keep flex flex-col gap-[0.35em]">
{glued}
</div>,
)
}
const heading = (label || section.note) && (
<h3 className="mb-[0.4em] text-[0.62em] font-bold uppercase tracking-[0.15em] text-sky">
{label}
{section.note && (
<span className="ml-2 normal-case tracking-normal text-muted">
{section.note}
</span>
)}
</h3>
)
// The heading and the section's first line are one unbreakable block, so
// a column or page never ends on a bare "Chorus". (break-after: avoid on
// the heading alone is ignored by Safari in multi-column layout.)
return (
<section className="mb-[1.1em]">
{heading && (
<div className="chart-keep">
{heading}
{extra}
{blocks[0]}
</div>
)}
{!heading && extra}
{blocks.length > (heading ? 1 : 0) && (
<div
className={`flex flex-col gap-[0.35em] ${heading ? 'mt-[0.35em]' : ''}`}
>
{heading ? blocks.slice(1) : blocks}
</div>
)}
</section>
)
}
/** Consecutive tab lines form one block; every other line stands alone. */
function runs(lines: ChartLine[]): ChartLine[][] {
const out: ChartLine[][] = []
for (const l of lines) {
const last = out[out.length - 1]
if (l.kind === 'tab' && last?.[0].kind === 'tab') last.push(l)
else out.push([l])
}
return out
}
/**
* Group segments into words so a line only ever wraps between words. A
* segment's lyric is first split at its spaces ("A home" → "A ", "home"), the
* chord staying with the first piece; pieces are then joined across chord
* boundaries until a piece ends in a space.
*/
export function words(segments: Segment[]): Segment[][] {
const pieces: Segment[] = []
for (const seg of segments) {
const parts = seg.lyric.split(/(?<=\s)(?=\S)/)
parts.forEach((lyric, i) =>
pieces.push({chord: i === 0 ? seg.chord : null, lyric}),
)
}
// Chords after the last word (a turnaround) stay with that word, so a
// wrapped line never leaves a chord alone on a row with no lyric under it
let lastWord = -1
pieces.forEach((p, i) => p.lyric.trim() && (lastWord = i))
const groups: Segment[][] = []
let cur: Segment[] = []
for (const [i, seg] of pieces.entries()) {
const prev = cur[cur.length - 1]
const trailing = lastWord >= 0 && i > lastWord
if (prev && !trailing && (prev.lyric === '' || /\s$/.test(prev.lyric))) {
groups.push(cur)
cur = []
}
cur.push(seg)
}
if (cur.length) groups.push(cur)
return groups
}
function Line({line}: {line: ChartLine}) {
if (line.kind === 'tab') return <div className="chart-tab">{line.text}</div>
if (line.kind === 'comment')
return <div className="italic text-muted">{line.text}</div>
const hasChords = line.segments.some((s) => s.chord)
const hasWords = line.segments.some((s) => s.lyric.trim())
return (
<div className="chart-line">
{words(line.segments).map((group, g) => (
<span key={g} className="chart-word">
{group.map((seg, i) => (
<span key={i} className="chart-seg">
{hasChords && (
<span
className={`chart-chord${seg.chord && !isChord(seg.chord) ? ' chart-note' : ''}${
// A chord over nothing (a bar before the vocal comes in):
// spaces are narrow, so give it a bar's worth of room
hasWords && seg.chord && !seg.lyric.trim()
? ' chart-gap'
: ''
}`}
>
{seg.chord ?? ''}
{!hasWords && seg.chord ? ' ' : ''}
</span>
)}
{hasWords && <span className="chart-lyric">{seg.lyric}</span>}
</span>
))}
</span>
))}
</div>
)
}
|