Fix formatting in utils.tsx

This commit is contained in:
Stefano Brilli 2021-07-19 10:49:23 +02:00
parent 670043183d
commit e273b8284b
1 changed files with 44 additions and 29 deletions

View File

@ -60,14 +60,15 @@ export function getAvailableCharsForTitle(disc: Disc, includeGroups?: boolean) {
// Assume worst-case scenario // Assume worst-case scenario
let fwTitle = disc.fullWidthTitle + `0;//`; let fwTitle = disc.fullWidthTitle + `0;//`;
let hwTitle = disc.title + `0;//`; let hwTitle = disc.title + `0;//`;
if(includeGroups || includeGroups === undefined) if (includeGroups || includeGroups === undefined)
for(let group of groups) { for (let group of groups) {
let range = `${group.tracks[0].index+1}${group.tracks.length - 1 !== 0 && (`-${group.tracks[group.tracks.length - 1].index+1}`)}//` let range = `${group.tracks[0].index + 1}${group.tracks.length - 1 !== 0 &&
// The order of these characters doesn't matter. It's for length only `-${group.tracks[group.tracks.length - 1].index + 1}`}//`;
fwTitle += group.fullWidthTitle + range; // The order of these characters doesn't matter. It's for length only
hwTitle += group.title + range; fwTitle += group.fullWidthTitle + range;
} hwTitle += group.title + range;
}
let usedCells = 0; let usedCells = 0;
usedCells += fixLength(fwTitle.length * 2); usedCells += fixLength(fwTitle.length * 2);
@ -87,10 +88,17 @@ export function sanitizeTitle(title: string) {
return title.normalize('NFD').replace(/[^\x00-\x7F]/g, ''); return title.normalize('NFD').replace(/[^\x00-\x7F]/g, '');
} }
export function getHalfWidthTitleLength(title: string) {// Some characters are written as 2 bytes export function getHalfWidthTitleLength(title: string) {
// Some characters are written as 2 bytes
// prettier-ignore // prettier-ignore
const multiByteChars: { [key: string]: number } = { "ガ": 1, "ギ": 1, "グ": 1, "ゲ": 1, "ゴ": 1, "ザ": 1, "ジ": 1, "ズ": 1, "ゼ": 1, "ゾ": 1, "ダ": 1, "ヂ": 1, "ヅ": 1, "デ": 1, "ド": 1, "バ": 1, "パ": 1, "ビ": 1, "ピ": 1, "ブ": 1, "プ": 1, "ベ": 1, "ペ": 1, "ボ": 1, "ポ": 1, "ヮ": 1, "ヰ": 1, "ヱ": 1, "ヵ": 1, "ヶ": 1, "ヴ": 1, "ヽ": 1, "ヾ": 1, "が": 1, "ぎ": 1, "ぐ": 1, "げ": 1, "ご": 1, "ざ": 1, "じ": 1, "ず": 1, "ぜ": 1, "ぞ": 1, "だ": 1, "ぢ": 1, "づ": 1, "で": 1, "ど": 1, "ば": 1, "ぱ": 1, "び": 1, "ぴ": 1, "ぶ": 1, "ぷ": 1, "べ": 1, "ぺ": 1, "ぼ": 1, "ぽ": 1, "ゎ": 1, "ゐ": 1, "ゑ": 1, "ゕ": 1, "ゖ": 1, "ゔ": 1, "ゝ": 1, "ゞ": 1 }; const multiByteChars: { [key: string]: number } = { "ガ": 1, "ギ": 1, "グ": 1, "ゲ": 1, "ゴ": 1, "ザ": 1, "ジ": 1, "ズ": 1, "ゼ": 1, "ゾ": 1, "ダ": 1, "ヂ": 1, "ヅ": 1, "デ": 1, "ド": 1, "バ": 1, "パ": 1, "ビ": 1, "ピ": 1, "ブ": 1, "プ": 1, "ベ": 1, "ペ": 1, "ボ": 1, "ポ": 1, "ヮ": 1, "ヰ": 1, "ヱ": 1, "ヵ": 1, "ヶ": 1, "ヴ": 1, "ヽ": 1, "ヾ": 1, "が": 1, "ぎ": 1, "ぐ": 1, "げ": 1, "ご": 1, "ざ": 1, "じ": 1, "ず": 1, "ぜ": 1, "ぞ": 1, "だ": 1, "ぢ": 1, "づ": 1, "で": 1, "ど": 1, "ば": 1, "ぱ": 1, "び": 1, "ぴ": 1, "ぶ": 1, "ぷ": 1, "べ": 1, "ぺ": 1, "ぼ": 1, "ぽ": 1, "ゎ": 1, "ゐ": 1, "ゑ": 1, "ゕ": 1, "ゖ": 1, "ゔ": 1, "ゝ": 1, "ゞ": 1 };
return title.length + title.split('').map(n => multiByteChars[n] ?? 0).reduce((a, b) => a + b, 0); return (
title.length +
title
.split('')
.map(n => multiByteChars[n] ?? 0)
.reduce((a, b) => a + b, 0)
);
} }
export function sanitizeHalfWidthTitle(title: string) { export function sanitizeHalfWidthTitle(title: string) {
@ -139,7 +147,7 @@ export type DisplayTrack = {
fullWidthTitle: string; fullWidthTitle: string;
group: string | null; group: string | null;
duration: string; duration: string;
encoding: string encoding: string;
}; };
export function getSortedTracks(disc: Disc | null) { export function getSortedTracks(disc: Disc | null) {
@ -247,17 +255,19 @@ export function recomputeGroupsAfterTrackMove(disc: Disc, trackIndex: number, ta
} }
} }
if(!anyChanges) return disc; if (!anyChanges) return disc;
let newDisc: Disc = { ...disc }; let newDisc: Disc = { ...disc };
// Convert back // Convert back
newDisc.groups = groupBoundaries.map(n => ({ newDisc.groups = groupBoundaries
title: n.name, .map(n => ({
fullWidthTitle: n.fullWidthName, title: n.name,
index: n.start, fullWidthTitle: n.fullWidthName,
tracks: allTracks.slice(n.start, n.end + 1), index: n.start,
})).filter(n => n.tracks.length > 0); tracks: allTracks.slice(n.start, n.end + 1),
}))
.filter(n => n.tracks.length > 0);
// Convert ungrouped tracks // Convert ungrouped tracks
let allGrouped = newDisc.groups.map(n => n.tracks).reduce((a, b) => a.concat(b), []); let allGrouped = newDisc.groups.map(n => n.tracks).reduce((a, b) => a.concat(b), []);
@ -276,16 +286,22 @@ export function recomputeGroupsAfterTrackMove(disc: Disc, trackIndex: number, ta
} }
export function compileDiscTitles(disc: Disc) { export function compileDiscTitles(disc: Disc) {
let availableCharactersForTitle = getAvailableCharsForTitle({ let availableCharactersForTitle = getAvailableCharsForTitle(
...disc, {
title: '', ...disc,
fullWidthTitle: '', title: '',
}, false); fullWidthTitle: '',
},
false
);
// If the disc or any of the groups, or any track has a full-width title, provide support for them // If the disc or any of the groups, or any track has a full-width title, provide support for them
const useFullWidth = const useFullWidth =
disc.fullWidthTitle || disc.fullWidthTitle ||
disc.groups.filter(n => !!n.fullWidthTitle).length > 0 || disc.groups.filter(n => !!n.fullWidthTitle).length > 0 ||
disc.groups.map(n => n.tracks).reduce((a, b) => a.concat(b), []).filter(n => !!n.fullWidthTitle).length > 0; disc.groups
.map(n => n.tracks)
.reduce((a, b) => a.concat(b), [])
.filter(n => !!n.fullWidthTitle).length > 0;
const fixLength = (l: number) => Math.ceil(l / 7) * 7; const fixLength = (l: number) => Math.ceil(l / 7) * 7;
@ -304,20 +320,19 @@ export function compileDiscTitles(disc: Disc) {
let newRawTitleAfterGroup = newRawTitle + `${range};${n.title}//`, let newRawTitleAfterGroup = newRawTitle + `${range};${n.title}//`,
newRawFullWidthTitleAfterGroup = newRawFullWidthTitle + halfWidthToFullWidthRange(range) + `${n.fullWidthTitle ?? ''}`; newRawFullWidthTitleAfterGroup = newRawFullWidthTitle + halfWidthToFullWidthRange(range) + `${n.fullWidthTitle ?? ''}`;
let titlesLengthInTOC = fixLength(getHalfWidthTitleLength(newRawTitleAfterGroup)); let titlesLengthInTOC = fixLength(getHalfWidthTitleLength(newRawTitleAfterGroup));
if(useFullWidth) titlesLengthInTOC += fixLength(newRawFullWidthTitleAfterGroup.length * 2) if (useFullWidth) titlesLengthInTOC += fixLength(newRawFullWidthTitleAfterGroup.length * 2);
if(availableCharactersForTitle - titlesLengthInTOC < 0) break; if (availableCharactersForTitle - titlesLengthInTOC < 0) break;
newRawTitle = newRawTitleAfterGroup; newRawTitle = newRawTitleAfterGroup;
newRawFullWidthTitle = newRawFullWidthTitleAfterGroup; newRawFullWidthTitle = newRawFullWidthTitleAfterGroup;
} }
let titlesLengthInTOC = fixLength(getHalfWidthTitleLength(newRawTitle)); let titlesLengthInTOC = fixLength(getHalfWidthTitleLength(newRawTitle));
if(useFullWidth) titlesLengthInTOC += fixLength(newRawFullWidthTitle.length * 2); // If this check fails the titles without the groups already take too much space, don't change anything if (useFullWidth) titlesLengthInTOC += fixLength(newRawFullWidthTitle.length * 2); // If this check fails the titles without the groups already take too much space, don't change anything
if(availableCharactersForTitle - titlesLengthInTOC < 0) { if (availableCharactersForTitle - titlesLengthInTOC < 0) {
return null; return null;
} }