feat: do not use empty phrase for deleted chords

This commit is contained in:
2023-12-16 15:20:44 +01:00
parent b679aa377a
commit 766bc44a85
5 changed files with 28 additions and 13 deletions

View File

@@ -19,6 +19,7 @@ export interface LayoutChange {
export interface ChordChange { export interface ChordChange {
type: ChangeType.Chord type: ChangeType.Chord
deleted?: true
id: number[] id: number[]
actions: number[] actions: number[]
phrase: number[] phrase: number[]
@@ -41,7 +42,7 @@ export const changes = persistentWritable<Change[]>("changes", [])
export interface Overlay { export interface Overlay {
layout: [Map<number, number>, Map<number, number>, Map<number, number>] layout: [Map<number, number>, Map<number, number>, Map<number, number>]
chords: Map<string, Chord> chords: Map<string, Chord & {deleted: boolean}>
settings: Map<number, number> settings: Map<number, number>
} }
@@ -58,7 +59,11 @@ export const overlay = derived(changes, changes => {
overlay.layout[change.layer].set(change.id, change.action) overlay.layout[change.layer].set(change.id, change.action)
break break
case ChangeType.Chord: case ChangeType.Chord:
overlay.chords.set(JSON.stringify(change.id), {actions: change.actions, phrase: change.phrase}) overlay.chords.set(JSON.stringify(change.id), {
actions: change.actions,
phrase: change.phrase,
deleted: change.deleted ?? false,
})
break break
case ChangeType.Setting: case ChangeType.Setting:
overlay.settings.set(change.id, change.setting) overlay.settings.set(change.id, change.setting)
@@ -88,7 +93,10 @@ export const layout = derived([overlay, deviceLayout], ([overlay, layout]) =>
) )
export type ChordInfo = Chord & export type ChordInfo = Chord &
ChangeInfo & {phraseChanged: boolean; actionsChanged: boolean; sortBy: string} & {id: number[]} ChangeInfo & {phraseChanged: boolean; actionsChanged: boolean; sortBy: string} & {
id: number[]
deleted: boolean
}
export const chords = derived([overlay, deviceChords], ([overlay, chords]) => { export const chords = derived([overlay, deviceChords], ([overlay, chords]) => {
const newChords = new Set(overlay.chords.keys()) const newChords = new Set(overlay.chords.keys())
@@ -106,6 +114,7 @@ export const chords = derived([overlay, deviceChords], ([overlay, chords]) => {
actionsChanged: id !== JSON.stringify(changedChord.actions), actionsChanged: id !== JSON.stringify(changedChord.actions),
phraseChanged: JSON.stringify(chord.phrase) !== JSON.stringify(changedChord.phrase), phraseChanged: JSON.stringify(chord.phrase) !== JSON.stringify(changedChord.phrase),
isApplied: false, isApplied: false,
deleted: changedChord.deleted,
} }
} else { } else {
return { return {
@@ -116,6 +125,7 @@ export const chords = derived([overlay, deviceChords], ([overlay, chords]) => {
phraseChanged: false, phraseChanged: false,
actionsChanged: false, actionsChanged: false,
isApplied: true, isApplied: true,
deleted: false,
} }
} }
}) })
@@ -126,6 +136,7 @@ export const chords = derived([overlay, deviceChords], ([overlay, chords]) => {
isApplied: false, isApplied: false,
actionsChanged: true, actionsChanged: true,
phraseChanged: false, phraseChanged: false,
deleted: chord.deleted,
id: JSON.parse(id), id: JSON.parse(id),
phrase: chord.phrase, phrase: chord.phrase,
actions: chord.actions, actions: chord.actions,

View File

@@ -39,8 +39,8 @@
if (!port) return if (!port) return
$syncStatus = "uploading" $syncStatus = "uploading"
for (const [id, {actions, phrase}] of $overlay.chords) { for (const [id, {actions, phrase, deleted}] of $overlay.chords) {
if (phrase.length > 0) { if (!deleted) {
if (id !== JSON.stringify(actions)) { if (id !== JSON.stringify(actions)) {
const existingChord = await port.getChordPhrase(actions) const existingChord = await port.getChordPhrase(actions)
if ( if (
@@ -109,9 +109,7 @@
number[], number[],
number[], number[],
] ]
$deviceChords = $chords $deviceChords = $chords.filter(({deleted}) => !deleted).map(({actions, phrase}) => ({actions, phrase}))
.map(({actions, phrase}) => ({actions, phrase}))
.filter(({phrase}) => phrase.length > 1)
$deviceSettings = $settings.map(({value}) => value) $deviceSettings = $settings.map(({value}) => value)
$changes = [] $changes = []
$syncStatus = "done" $syncStatus = "done"

View File

@@ -47,7 +47,7 @@
</script> </script>
<button <button
class:deleted={chord && chord.phrase.length === 0} class:deleted={chord && chord.deleted}
class:edited={chord && chord.actionsChanged} class:edited={chord && chord.actionsChanged}
class:invalid={chord && chord.actions.toSorted(compare).some((it, i) => chord?.actions[i] !== it)} class:invalid={chord && chord.actions.toSorted(compare).some((it, i) => chord?.actions[i] !== it)}
on:click={edit} on:click={edit}

View File

@@ -13,7 +13,13 @@
function remove() { function remove() {
changes.update(changes => { changes.update(changes => {
changes.push({type: ChangeType.Chord, id: chord.id, actions: chord.actions, phrase: []}) changes.push({
type: ChangeType.Chord,
id: chord.id,
actions: chord.actions,
phrase: chord.phrase,
deleted: true,
})
return changes return changes
}) })
} }
@@ -60,9 +66,9 @@
<ChordPhraseEdit {chord} /> <ChordPhraseEdit {chord} />
</td> </td>
<td class="table-buttons"> <td class="table-buttons">
{#if chord.phrase.length !== 0} {#if !chord.deleted}
<button transition:slide class="icon compact" on:click={remove}>delete</button> <button transition:slide class="icon compact" on:click={remove}>delete</button>
{:else if chord.phraseChanged} {:else}
<button transition:slide class="icon compact" on:click={restore}>restore_from_trash</button> <button transition:slide class="icon compact" on:click={restore}>restore_from_trash</button>
{/if} {/if}
<button class="icon compact" class:disabled={chord.isApplied} on:click={restore}>undo</button> <button class="icon compact" class:disabled={chord.isApplied} on:click={restore}>undo</button>

View File

@@ -136,7 +136,7 @@
role="textbox" role="textbox"
tabindex="0" tabindex="0"
bind:this={box} bind:this={box}
class:edited={chord.phrase.length !== 0 && chord.phraseChanged} class:edited={!chord.deleted && chord.phraseChanged}
on:focusin={() => (hasFocus = true)} on:focusin={() => (hasFocus = true)}
on:focusout={event => { on:focusout={event => {
if (event.relatedTarget !== button) hasFocus = false if (event.relatedTarget !== button) hasFocus = false