Skip to content

Commit

Permalink
feat: new chord button, fixes #9
Browse files Browse the repository at this point in the history
feat: improved backups
  • Loading branch information
Theaninova committed Nov 10, 2023
1 parent 034436f commit e19a57e
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 70 deletions.
2 changes: 2 additions & 0 deletions src/i18n/de/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const de = {
configure: {
chords: {
TITLE: "Akkorde",
HOLD_KEYS: "Akkord halten",
NEW_CHORD: "Neuer Akkord",
search: {
PLACEHOLDER: "{0} Akkord{{|e}} durchsuchen",
},
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/en/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const en = {
configure: {
chords: {
TITLE: "Chords",
HOLD_KEYS: "Hold chord",
NEW_CHORD: "New chord",
search: {
PLACEHOLDER: "Search {0} chord{{|s}}",
},
Expand Down
8 changes: 6 additions & 2 deletions src/lib/share/chara-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ export interface CharaFile<T extends string> {
}

export interface CharaLayoutFile extends CharaFile<"layout"> {
device: "one" | "lite" | string
device?: "ONE" | "LITE" | string
layout: [number[], number[], number[]]
}

export interface CharaChordFile extends CharaFile<"chords"> {
chords: [number[], number[]][]
}

export interface CharaChordSettings extends CharaFile<"settings"> {
export interface CharaSettingsFile extends CharaFile<"settings"> {
settings: number[]
}

export interface CharaBackupFile extends CharaFile<"backup"> {
history: [CharaChordFile, CharaLayoutFile, CharaSettingsFile][]
}

export type CharaFiles = CharaLayoutFile | CharaChordFile
73 changes: 43 additions & 30 deletions src/lib/undo-redo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export interface Overlay {
}

export const overlay = derived(changes, changes => {
console.time("overlay building")
const overlay: Overlay = {
layout: [new Map(), new Map(), new Map()],
chords: new Map(),
Expand All @@ -66,7 +65,6 @@ export const overlay = derived(changes, changes => {
break
}
}
console.timeEnd("overlay building")

return overlay
})
Expand All @@ -91,33 +89,48 @@ export const layout = derived([overlay, deviceLayout], ([overlay, layout]) =>

export type ChordInfo = Chord &
ChangeInfo & {phraseChanged: boolean; actionsChanged: boolean; sortBy: string} & {id: number[]}
export const chords = derived([overlay, deviceChords], ([overlay, chords]) =>
chords
.map<ChordInfo>(chord => {
const id = JSON.stringify(chord.actions)
if (overlay.chords.has(id)) {
const changedChord = overlay.chords.get(id)!
return {
id: chord.actions,
// use the old phrase for stable editing
sortBy: chord.phrase.map(it => KEYMAP_CODES[it].id || it).join(),
actions: changedChord.actions,
phrase: changedChord.phrase,
actionsChanged: id !== JSON.stringify(changedChord.actions),
phraseChanged: JSON.stringify(chord.phrase) !== JSON.stringify(changedChord.phrase),
isApplied: false,
}
} else {
return {
id: chord.actions,
sortBy: chord.phrase.map(it => KEYMAP_CODES[it].id || it).join(),
actions: chord.actions,
phrase: chord.phrase,
phraseChanged: false,
actionsChanged: false,
isApplied: true,
}
export const chords = derived([overlay, deviceChords], ([overlay, chords]) => {
const newChords = new Set(overlay.chords.keys())

const changedChords = chords.map<ChordInfo>(chord => {
const id = JSON.stringify(chord.actions)
if (overlay.chords.has(id)) {
newChords.delete(id)
const changedChord = overlay.chords.get(id)!
return {
id: chord.actions,
// use the old phrase for stable editing
sortBy: chord.phrase.map(it => KEYMAP_CODES[it].id || it).join(),
actions: changedChord.actions,
phrase: changedChord.phrase,
actionsChanged: id !== JSON.stringify(changedChord.actions),
phraseChanged: JSON.stringify(chord.phrase) !== JSON.stringify(changedChord.phrase),
isApplied: false,
}
} else {
return {
id: chord.actions,
sortBy: chord.phrase.map(it => KEYMAP_CODES[it].id || it).join(),
actions: chord.actions,
phrase: chord.phrase,
phraseChanged: false,
actionsChanged: false,
isApplied: true,
}
}
})
for (const id of newChords) {
const chord = overlay.chords.get(id)!
changedChords.push({
sortBy: "",
isApplied: false,
actionsChanged: true,
phraseChanged: false,
id: JSON.parse(id),
phrase: chord.phrase,
actions: chord.actions,
})
.sort(({sortBy: a}, {sortBy: b}) => a.localeCompare(b)),
)
}

return changedChords.sort(({sortBy: a}, {sortBy: b}) => a.localeCompare(b))
})
102 changes: 80 additions & 22 deletions src/routes/BackupPopup.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
<script lang="ts">
import {parseCompressed, stringifyCompressed} from "$lib/serial/serialization"
import {deviceChords, deviceLayout} from "$lib/serial/connection"
import {serialPort} from "$lib/serial/connection"
import {preference} from "$lib/preferences"
import type {Chord} from "$lib/serial/chord"
import type {CharaLayout} from "$lib/serialization/layout"
import LL from "../i18n/i18n-svelte"
interface Backup {
isCharaBackup: string
chords: Chord[]
layout: CharaLayout
}
import type {
CharaBackupFile,
CharaChordFile,
CharaSettingsFile,
CharaLayoutFile,
} from "$lib/share/chara-file.js"
import {changes, ChangeType, chords, layout, settings} from "$lib/undo-redo.js"
import type {Change} from "$lib/undo-redo.js"
async function downloadBackup() {
const downloadUrl = URL.createObjectURL(
await stringifyCompressed({
isCharaBackup: "v1.0",
chords: $deviceChords,
layout: $deviceLayout,
}),
new Blob(
[
JSON.stringify({
charaVersion: 1,
type: "backup",
history: [
[
{charaVersion: 1, type: "chords", chords: $chords.map(it => [it.actions, it.phrase])},
{
charaVersion: 1,
type: "layout",
device: $serialPort?.device,
layout: $layout.map(it => it.map(it => it.action)) as [number[], number[], number[]],
},
{charaVersion: 1, type: "settings", settings: $settings.map(it => it.value)},
],
],
} satisfies CharaBackupFile),
],
{type: "application/json"},
),
)
const element = document.createElement("a")
element.setAttribute("download", "chords.chb")
element.setAttribute("download", "backup.json")
element.href = downloadUrl
element.setAttribute("target", "_blank")
element.click()
Expand All @@ -31,14 +46,57 @@
async function restoreBackup(event: Event) {
const input = (event.target as HTMLInputElement).files![0]
if (!input) return
const backup = await parseCompressed<Backup>(input)
if (backup.isCharaBackup !== "v1.0") throw new Error("Invalid Backup")
if (backup.chords) {
$deviceChords = backup.chords
const backup: CharaBackupFile = JSON.parse(await input.text())
if (backup.charaVersion !== 1 || backup.type !== "backup") throw new Error("Invalid Backup")
const recent = backup.history[0]
if (recent[1].device !== $serialPort?.device) throw new Error("Backup is incompatible with this device")
changes.update(changes => {
changes.push(
...getChangesFromChordFile(recent[0]),
...getChangesFromLayoutFile(recent[1]),
...getChangesFromSettingsFile(recent[2]),
)
return changes
})
}
function getChangesFromChordFile(file: CharaChordFile) {
const changes: Change[] = []
// TODO...
return changes
}
function getChangesFromSettingsFile(file: CharaSettingsFile) {
const changes: Change[] = []
for (const [id, value] of file.settings.entries()) {
if ($settings[id].value !== value) {
changes.push({
type: ChangeType.Setting,
id,
setting: value,
})
}
}
if (backup.layout) {
$deviceLayout = backup.layout
return changes
}
function getChangesFromLayoutFile(file: CharaLayoutFile) {
const changes: Change[] = []
for (const [layer, keys] of file.layout.entries()) {
for (const [id, action] of keys.entries()) {
if ($layout[layer][id].action !== action) {
changes.push({
type: ChangeType.Layout,
layer,
id,
action,
})
}
}
}
return changes
}
</script>

Expand Down
25 changes: 22 additions & 3 deletions src/routes/config/chords/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import LL from "../../../i18n/i18n-svelte"
import {action} from "$lib/title"
import {onDestroy, onMount, setContext} from "svelte"
import {chords} from "$lib/undo-redo"
import {changes, ChangeType, chords} from "$lib/undo-redo"
import type {ChordInfo} from "$lib/undo-redo"
import {derived, writable} from "svelte/store"
import ChordEdit from "./ChordEdit.svelte"
import {crossfade} from "svelte/transition"
import ChordActionEdit from "./ChordActionEdit.svelte"
const resultSize = 38
let results: HTMLElement
Expand Down Expand Up @@ -46,12 +47,27 @@
searchFilter.set(query && searchIndex ? searchIndex.search(query) : undefined)
}
function insertChord(actions: number[]) {
changes.update(changes => {
changes.push({
type: ChangeType.Chord,
id: actions,
actions,
phrase: [],
})
return changes
})
}
const items = derived(
[searchFilter, chords],
([filter, chords]) =>
filter?.map(it => [chords[it], it] as const) ?? chords.map((it, i) => [it, i] as const),
)
const lastPage = derived([items, pageSize], ([items, pageSize]) => Math.ceil(items.length / pageSize) - 1)
const lastPage = derived(
[items, pageSize],
([items, pageSize]) => Math.ceil((items.length + 1) / pageSize) - 1,
)
setContext("cursor-crossfade", crossfade({}))
Expand Down Expand Up @@ -91,8 +107,11 @@

<section bind:this={results}>
<table>
{#if page === 0}
<tr><th><ChordActionEdit on:submit={({detail}) => insertChord(detail)} /></th><td /><td /></tr>
{/if}
{#if $lastPage !== -1}
{#each $items.slice(page * $pageSize, (page + 1) * $pageSize) as [chord] (chord.id)}
{#each $items.slice(page * $pageSize - (page === 0 ? 0 : 1), (page + 1) * $pageSize - 1) as [chord] (JSON.stringify(chord.id))}
<tr>
<ChordEdit {chord} />
</tr>
Expand Down
24 changes: 16 additions & 8 deletions src/routes/config/chords/ChordActionEdit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
import {KEYMAP_CODES, KEYMAP_IDS} from "$lib/serial/keymap-codes"
import type {ChordInfo} from "$lib/undo-redo"
import {changes, ChangeType} from "$lib/undo-redo"
import {createEventDispatcher} from "svelte"
import LL from "../../../i18n/i18n-svelte"
export let chord: ChordInfo
export let chord: ChordInfo | undefined = undefined
const dispatch = createEventDispatcher()
let pressedKeys = new Set<number>()
let editing = false
Expand All @@ -24,29 +28,32 @@
if (!editing) return
editing = false
if (pressedKeys.size < 2) return
if (!chord) return dispatch("submit", [...pressedKeys])
changes.update(changes => {
changes.push({
type: ChangeType.Chord,
id: chord.id,
id: chord!.id,
actions: [...pressedKeys],
phrase: chord.phrase,
phrase: chord!.phrase,
})
return changes
})
}
</script>

<button
class:deleted={chord.phrase.length === 0}
class:edited={chord.actionsChanged}
class:deleted={chord && chord.phrase.length === 0}
class:edited={chord && chord.actionsChanged}
on:click={edit}
on:keydown={keydown}
on:keyup={keyup}
>
{#if editing && pressedKeys.size === 0}
<span>Press keys</span>
<span>{$LL.configure.chords.HOLD_KEYS()}</span>
{:else if !editing && !chord}
<span>{$LL.configure.chords.NEW_CHORD()}</span>
{/if}
{#each editing ? [...pressedKeys].sort() : chord.actions as actionId}
{#each editing ? [...pressedKeys].sort() : chord?.actions ?? [] as actionId}
{@const {icon, id, code} = KEYMAP_CODES[actionId] ?? {code: actionId}}
<kbd class:icon={!!icon}>
{icon ?? id ?? `0x${code.toString(16)}`}
Expand Down Expand Up @@ -92,9 +99,10 @@
position: absolute;
top: 50%;
transform-origin: center left;
translate: -6px 0;
scale: 0 1;
width: calc(100% - 16px);
width: calc(100% - 32px);
height: 1px;
background: currentcolor;
Expand Down
6 changes: 3 additions & 3 deletions src/routes/config/chords/ChordEdit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
<ChordPhraseEdit {chord} />
</td>
<td class="table-buttons">
{#if chord.phrase.length === 0}
<button transition:slide class="icon compact" on:click={restore}>restore_from_trash</button>
{:else}
{#if chord.phrase.length !== 0}
<button transition:slide class="icon compact" on:click={remove}>delete</button>
{:else if chord.phraseChanged}
<button transition:slide class="icon compact" on:click={restore}>restore_from_trash</button>
{/if}
<button class="icon compact" class:disabled={chord.isApplied} on:click={restore}>undo</button>
<div class="separator" />
Expand Down
Loading

0 comments on commit e19a57e

Please sign in to comment.