fix: chord files not detected properly

feat: alert on unknown backups
This commit is contained in:
2023-12-08 21:14:37 +01:00
parent 1ccb17f053
commit 8cbdf1393f
2 changed files with 13 additions and 8 deletions

View File

@@ -60,6 +60,8 @@ export async function restoreBackup(event: Event) {
restoreFromFile(csvLayoutToJson(text))
} else if (isCsvChords(text)) {
restoreFromFile(csvChordsToJson(text))
} else {
alert("Unknown backup format")
}
}

View File

@@ -5,16 +5,19 @@ export function csvChordsToJson(csv: string): CharaChordFile {
return {
charaVersion: 1,
type: "chords",
chords: csv.split("\n").map(line => {
const [input, output] = line.split(",", 2)
return [
input.split("+").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0),
output.split("").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0),
]
}),
chords: csv
.trim()
.split("\n")
.map(line => {
const [input, output] = line.split(",", 2)
return [
input.split("+").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0),
output.split("").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0),
]
}),
}
}
export function isCsvChords(csv: string): boolean {
return /^([^+,\s]( *\+ *[^+,\s]+)* *, *[^+,\s]+ *(\n|(?=$)))+$/.test(csv)
return /^([^+, ]+( *\+ *[^+, ]+)* *, *[^+, ]+ *(\n|(?=$)))+$/.test(csv)
}