Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[channel-picker] Preserve previously selected channel #149

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/channel-picker/channel-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ const Self = class ChannelPicker extends ColorElement {
return this.selectedSpace.coords?.[this._el.picker.value];
}

/**
* Previously selected channels for each space.
* Keys are space IDs. Values are coords.
*/
#history = {};

#render () {
let space = this.selectedSpace;
let coords = space.coords;
Expand All @@ -56,10 +62,18 @@ const Self = class ChannelPicker extends ColorElement {
.map(([id, coord]) => `<option value="${ id }">${ coord.name }</option>`)
.join("\n");

let channel = this.value?.split(".")[1];
if (channel && channel in coords) {
// Preserve the channel if it exists in the new space
this._el.picker.value = channel;
let [prevSpace, prevChannel] = this.value?.split(".") ?? [];
if (prevSpace && prevChannel) {
let prevChannelName = this._el.space_picker.spaces[prevSpace].coords[prevChannel].name;
let currentChannelName = coords[prevChannel]?.name;
if (prevChannelName === currentChannelName) {
// Preserve the channel if it exists in the new space and has the same name ("b" in "oklab" is not the same as "b" in "p3")
this._el.picker.value = prevChannel;
}
else if (this.#history?.[space.id]) {
// Otherwise, try to restore the last channel used
this._el.picker.value = this.#history[space.id];
}
}
}

Expand Down Expand Up @@ -116,8 +130,11 @@ const Self = class ChannelPicker extends ColorElement {
message += ` Falling back to "${ currentCoord }".`;
console.warn(message);
this.value = `${ space }.${ currentCoord }`;
channel = currentCoord;
}
}

this.#history[space] = channel;
}
}
}
Expand Down