/
renderBarContent.js
77 lines (63 loc) · 1.85 KB
/
renderBarContent.js
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
import _isFinite from 'lodash/isFinite';
import symbols from '../symbols';
import renderChordSymbol from './renderChordSymbol';
import renderTimeSignature from './renderTimeSignature';
import barContentTpl from './tpl/barContent.js';
const space = ' ';
const defaultSpacesWithin = 0;
const defaultSpacesAfter = 2;
/**
* @param {Bar} bar
* @param {Boolean} isLastBar
* @param {Boolean} shouldPrintBarSeparators
* @param {Boolean} shouldPrintSubBeatDelimiters
* @param {Boolean} shouldPrintTimeSignature
* @returns {String} rendered html
*/
export default function renderBarContent(
bar,
isLastBar = false,
{
shouldPrintBarSeparators = true,
shouldPrintSubBeatDelimiters = true,
shouldPrintTimeSignature = false,
} = {}
) {
let spacesWithin = 0;
let spacesAfter = 0;
let barContent = '';
if (shouldPrintTimeSignature) {
barContent +=
renderTimeSignature(bar.timeSignature) +
' '.repeat(symbols.spacesAfterTimeSignature);
}
barContent += bar.allChords.reduce((rendering, chord, i) => {
spacesWithin = _isFinite(chord.spacesWithin)
? chord.spacesWithin
: defaultSpacesWithin;
spacesAfter = _isFinite(chord.spacesAfter)
? chord.spacesAfter
: defaultSpacesAfter;
rendering += renderChordSymbol(
chord,
chord.isInSubBeatGroup ? false : bar.shouldPrintChordsDuration,
shouldPrintSubBeatDelimiters && chord.isFirstOfSubBeat,
shouldPrintSubBeatDelimiters && chord.isLastOfSubBeat
);
if (shouldPrintChordSpaces()) {
rendering += space.repeat(spacesWithin) + space.repeat(spacesAfter);
}
function shouldPrintChordSpaces() {
const isLastChordOfLine = isLastChordOfBar() && isLastBar;
return (
!isLastChordOfLine ||
(isLastChordOfLine && shouldPrintBarSeparators)
);
}
function isLastChordOfBar() {
return !bar.allChords[i + 1];
}
return rendering;
}, '');
return barContentTpl({ barContent });
}