mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2025-12-13 06:16:16 +00:00
feat: legacy backup import, resolves #31
This commit is contained in:
@@ -10,6 +10,7 @@ import {changes, ChangeType, chords, layout, settings} from "$lib/undo-redo.js"
|
||||
import {get} from "svelte/store"
|
||||
import {serialPort} from "../serial/connection"
|
||||
import {csvLayoutToJson, isCsvLayout} from "$lib/backup/compat/legacy-layout"
|
||||
import {isCsvChords, csvChordsToJson} from "./compat/legacy-chords"
|
||||
|
||||
export function downloadFile<T extends CharaFile<string>>(contents: T) {
|
||||
const downloadUrl = URL.createObjectURL(new Blob([JSON.stringify(contents)], {type: "application/json"}))
|
||||
@@ -57,6 +58,8 @@ export async function restoreBackup(event: Event) {
|
||||
restoreFromFile(JSON.parse(text))
|
||||
} else if (isCsvLayout(text)) {
|
||||
restoreFromFile(csvLayoutToJson(text))
|
||||
} else if (isCsvChords(text)) {
|
||||
restoreFromFile(csvChordsToJson(text))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import { KEYMAP_IDS } from "$lib/serial/keymap-codes"
|
||||
import type { CharaChordFile } from "$lib/share/chara-file"
|
||||
|
||||
import {KEYMAP_IDS} from "$lib/serial/keymap-codes"
|
||||
import type {CharaChordFile} from "$lib/share/chara-file"
|
||||
|
||||
export function csvChordsToJson(csv: string): CharaChordFile {
|
||||
return {
|
||||
charaVersion: 1,
|
||||
type: 'chords',
|
||||
chords: csv.split('\n').map(line => {
|
||||
const [input, output] = line.split(',', 2)
|
||||
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),
|
||||
input.split("+").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0),
|
||||
output.split("").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0),
|
||||
]
|
||||
})
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
export function isCsvLayout(csv: string): boolean {
|
||||
return /^((\s*\w+\s*+?),\s*\w+\n?)+$/.test(csv)
|
||||
export function isCsvChords(csv: string): boolean {
|
||||
return /^([^+,\s]( *\+ *[^+,\s]+)* *, *[^+,\s]+ *(\n|(?=$)))+$/.test(csv)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user