From 034436f93e1d501c756268c65c80510c5c7190e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thea=20Sch=C3=B6bl?= Date: Fri, 10 Nov 2023 16:05:42 +0100 Subject: [PATCH] fix: editing chords messes up list --- src/lib/undo-redo.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/undo-redo.ts b/src/lib/undo-redo.ts index 7a9cc78c..ee3a4177 100644 --- a/src/lib/undo-redo.ts +++ b/src/lib/undo-redo.ts @@ -2,6 +2,7 @@ import {persistentWritable} from "$lib/storage" import {derived} from "svelte/store" import type {Chord} from "$lib/serial/chord" import {deviceChords, deviceLayout, deviceSettings} from "$lib/serial/connection" +import {KEYMAP_CODES} from "$lib/serial/keymap-codes" export enum ChangeType { Layout, @@ -89,7 +90,7 @@ export const layout = derived([overlay, deviceLayout], ([overlay, layout]) => ) export type ChordInfo = Chord & - ChangeInfo & {phraseChanged: boolean; actionsChanged: boolean} & {id: number[]} + ChangeInfo & {phraseChanged: boolean; actionsChanged: boolean; sortBy: string} & {id: number[]} export const chords = derived([overlay, deviceChords], ([overlay, chords]) => chords .map(chord => { @@ -98,6 +99,8 @@ export const chords = derived([overlay, deviceChords], ([overlay, chords]) => 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), @@ -107,6 +110,7 @@ export const chords = derived([overlay, deviceChords], ([overlay, chords]) => } else { return { id: chord.actions, + sortBy: chord.phrase.map(it => KEYMAP_CODES[it].id || it).join(), actions: chord.actions, phrase: chord.phrase, phraseChanged: false, @@ -115,5 +119,5 @@ export const chords = derived([overlay, deviceChords], ([overlay, chords]) => } } }) - .sort((a, b) => a.phrase.map((it, i) => it - b.phrase[i]).find(it => it !== 0) ?? 0), + .sort(({sortBy: a}, {sortBy: b}) => a.localeCompare(b)), )