mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2025-12-23 19:26:21 +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 {get} from "svelte/store"
|
||||||
import {serialPort} from "../serial/connection"
|
import {serialPort} from "../serial/connection"
|
||||||
import {csvLayoutToJson, isCsvLayout} from "$lib/backup/compat/legacy-layout"
|
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) {
|
export function downloadFile<T extends CharaFile<string>>(contents: T) {
|
||||||
const downloadUrl = URL.createObjectURL(new Blob([JSON.stringify(contents)], {type: "application/json"}))
|
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))
|
restoreFromFile(JSON.parse(text))
|
||||||
} else if (isCsvLayout(text)) {
|
} else if (isCsvLayout(text)) {
|
||||||
restoreFromFile(csvLayoutToJson(text))
|
restoreFromFile(csvLayoutToJson(text))
|
||||||
|
} else if (isCsvChords(text)) {
|
||||||
|
restoreFromFile(csvChordsToJson(text))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,20 @@
|
|||||||
import { KEYMAP_IDS } from "$lib/serial/keymap-codes"
|
import {KEYMAP_IDS} from "$lib/serial/keymap-codes"
|
||||||
import type { CharaChordFile } from "$lib/share/chara-file"
|
import type {CharaChordFile} from "$lib/share/chara-file"
|
||||||
|
|
||||||
|
|
||||||
export function csvChordsToJson(csv: string): CharaChordFile {
|
export function csvChordsToJson(csv: string): CharaChordFile {
|
||||||
return {
|
return {
|
||||||
charaVersion: 1,
|
charaVersion: 1,
|
||||||
type: 'chords',
|
type: "chords",
|
||||||
chords: csv.split('\n').map(line => {
|
chords: csv.split("\n").map(line => {
|
||||||
const [input, output] = line.split(',', 2)
|
const [input, output] = line.split(",", 2)
|
||||||
return [
|
return [
|
||||||
input.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),
|
output.split("").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0),
|
||||||
]
|
]
|
||||||
})
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCsvLayout(csv: string): boolean {
|
export function isCsvChords(csv: string): boolean {
|
||||||
return /^((\s*\w+\s*+?),\s*\w+\n?)+$/.test(csv)
|
return /^([^+,\s]( *\+ *[^+,\s]+)* *, *[^+,\s]+ *(\n|(?=$)))+$/.test(csv)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user