Skip to content

Commit 2343851

Browse files
committed
Fix #277 wrong palette being deleted.
1 parent a75744f commit 2343851

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

wwwroot/modules/ui/paletteEditor.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export default class PaletteEditor extends ComponentBase {
5656
#selectedPaletteId = null;
5757
/** @type {HTMLButtonElement[]} */
5858
#paletteButtons = [];
59+
/** @type {HTMLSelectElement} */
60+
#uiHiddenPaletteSelect;
5961
/** @type {HTMLElement} */
6062
#uiItemProperties;
6163
/** @type {HTMLInputElement} */
@@ -112,6 +114,7 @@ export default class PaletteEditor extends ComponentBase {
112114
});
113115

114116
this.#uiItemProperties = this.#element.querySelector('[data-smsgfx-id=palette-properties]');
117+
this.#uiHiddenPaletteSelect = this.#element.querySelector(`select[data-command=${commands.paletteSelect}]`);
115118

116119
this.#btnPaletteTitle = this.#element.querySelector('[data-smsgfx-id=editPaletteTitle]');
117120
this.#btnPaletteTitle.addEventListener('click', () => this.#handlePaletteTitleEditClick());
@@ -222,9 +225,7 @@ export default class PaletteEditor extends ComponentBase {
222225
const palette = this.#paletteList.getPalette(index);
223226
this.#selectedPaletteId = palette.paletteId;
224227

225-
this.#element.querySelectorAll(`[data-command=${commands.paletteSelect}]`).forEach((element) => {
226-
element.selectedIndex = index;
227-
});
228+
this.#uiHiddenPaletteSelect.selectedIndex = index;
228229

229230
this.#setPalette(palette);
230231
} else {
@@ -318,7 +319,8 @@ export default class PaletteEditor extends ComponentBase {
318319
#createEventArgs(command) {
319320
return {
320321
command: command,
321-
paletteIndex: this.#element.querySelector(`select[data-command=${commands.paletteSelect}]`)?.selectedIndex,
322+
paletteIndex: this.#uiHiddenPaletteSelect?.selectedIndex,
323+
paletteId: this.#uiHiddenPaletteSelect?.options[this.#uiHiddenPaletteSelect?.selectedIndex]?.getAttribute('data-palette-id') ?? null,
322324
paletteTitle: this.#element.querySelector(`input[type=text][data-command=${commands.paletteTitle}]`)?.value ?? null,
323325
paletteSystem: this.#element.querySelector(`select[data-command=${commands.paletteSystem}]`)?.value ?? null,
324326
displayNative: this.#element.querySelector(`[data-command=${commands.displayNativeColours}]`)?.checked ?? null,
@@ -393,9 +395,10 @@ export default class PaletteEditor extends ComponentBase {
393395
* @param {PaletteList?} paletteList - List of palettes, or null.
394396
*/
395397
#refreshPaletteSelectList(paletteList) {
396-
const select = this.#element.querySelector(`select[data-command=${commands.paletteSelect}]`);
398+
const select = this.#uiHiddenPaletteSelect;
397399
if (select) {
398400
const lastSelectedIndex = select.selectedIndex;
401+
const lastSelectedPaletteId = select.options[lastSelectedIndex]?.getAttribute('data-palette-id');
399402
const optionCount = select.options.length;
400403
for (let i = 0; i < optionCount; i++) {
401404
select.options.remove(0);
@@ -407,7 +410,7 @@ export default class PaletteEditor extends ComponentBase {
407410
option.setAttribute('data-palette-id', palettes[i].paletteId);
408411
option.innerText = `#${i} | ${palettes[i].title}`;
409412
option.value = i.toString();
410-
option.selected = lastSelectedIndex === i;
413+
option.selected = lastSelectedPaletteId === palettes[i].paletteId;
411414
select.options.add(option);
412415
}
413416
if (select.selectedIndex === -1) {
@@ -423,7 +426,7 @@ export default class PaletteEditor extends ComponentBase {
423426
*/
424427
#updatePaletteSelectVirtualList() {
425428
const virtualList = this.#element.querySelector(`[data-linked-command=${commands.paletteSelect}]`);
426-
const select = this.#element.querySelector(`select[data-command=${commands.paletteSelect}]`);
429+
const select = this.#uiHiddenPaletteSelect;
427430

428431
if (virtualList && select) {
429432

@@ -650,7 +653,7 @@ export default class PaletteEditor extends ComponentBase {
650653
* @typedef {Object} PaletteEditorCommandEventArgs
651654
* @property {string} command - The command being invoked.
652655
* @property {number?} paletteIndex - Index of the selected palette.
653-
* @property {number?} paletteId - Unique ID of the selected palette.
656+
* @property {string?} paletteId - Unique ID of the selected palette.
654657
* @property {string?} paletteTitle - Title value for the selected palette.
655658
* @property {string?} paletteSystem - Selected system value for the selected palette, either 'ms', 'gg', 'gb' or 'nes'.
656659
* @property {number?} colourIndex - Index from 0 to 15 for the given colour.

0 commit comments

Comments
 (0)