Skip to content

Commit a4d3f60

Browse files
committed
feat(chromad): use style counterparts for theme switching
1 parent ce159e6 commit a4d3f60

3 files changed

Lines changed: 41 additions & 15 deletions

File tree

cmd/chromad/main.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,18 @@ var (
5555
}).Parse(indexTemplate))
5656
)
5757

58+
type styleInfo struct {
59+
Name string `json:"name"`
60+
Mode string `json:"mode"`
61+
Counterpart string `json:"counterpart,omitempty"`
62+
}
63+
5864
type context struct {
5965
Background template.CSS
6066
SelectedLanguage string
6167
Languages []string
6268
SelectedStyle string
63-
Styles []string
69+
Styles []styleInfo
6470
CSRFField template.HTML
6571
Version string
6672
}
@@ -164,10 +170,16 @@ func newContext(r *http.Request) context {
164170
ctx.Languages = append(ctx.Languages, lexer.Config().Name)
165171
}
166172
sort.Strings(ctx.Languages)
167-
for name := range styles.Registry {
168-
ctx.Styles = append(ctx.Styles, name)
169-
}
170-
sort.Strings(ctx.Styles)
173+
for name, s := range styles.Registry {
174+
ctx.Styles = append(ctx.Styles, styleInfo{
175+
Name: name,
176+
Mode: s.Mode().String(),
177+
Counterpart: s.Counterpart,
178+
})
179+
}
180+
sort.Slice(ctx.Styles, func(i, j int) bool {
181+
return ctx.Styles[i].Name < ctx.Styles[j].Name
182+
})
171183
return ctx
172184
}
173185

cmd/chromad/static/index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ function init() {
2121
const themeToggle = document.getElementById("theme-toggle");
2222
const themeIcon = document.getElementById("theme-icon");
2323

24+
const stylesByName = Object.fromEntries(
25+
(window.chromaStyles || []).map((s) => [s.name, s]),
26+
);
27+
28+
function styleForMode(name, mode) {
29+
const info = stylesByName[name];
30+
if (!info || info.mode === mode) {
31+
return name;
32+
}
33+
const counterpart = info.counterpart && stylesByName[info.counterpart];
34+
if (counterpart && counterpart.mode === mode) {
35+
return counterpart.name;
36+
}
37+
return name;
38+
}
39+
2440
function getThemePreference() {
2541
const stored = localStorage.getItem("theme");
2642
if (stored) {
@@ -59,11 +75,9 @@ function init() {
5975
themeIcon.setAttribute("name", "sunny-outline");
6076
}
6177

62-
if (isDark && styleSelect.value === "monokailight") {
63-
styleSelect.value = "monokai";
64-
update(new Event("change"));
65-
} else if (!isDark && styleSelect.value === "monokai") {
66-
styleSelect.value = "monokailight";
78+
const swapped = styleForMode(styleSelect.value, effectiveTheme);
79+
if (swapped !== styleSelect.value) {
80+
styleSelect.value = swapped;
6781
update(new Event("change"));
6882
}
6983
}
@@ -282,10 +296,7 @@ function init() {
282296
update(new Event("change"));
283297
} else {
284298
const effectiveTheme = getEffectiveTheme(initialTheme);
285-
const isDark = effectiveTheme === "dark";
286-
if (isDark && styleSelect.value === "monokailight") {
287-
styleSelect.value = "monokai";
288-
}
299+
styleSelect.value = styleForMode(styleSelect.value, effectiveTheme);
289300
update(new Event("change"));
290301
}
291302

cmd/chromad/templates/index.html.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<select name="style" id="style">
6767
<option value="" disabled{{if eq "" $.SelectedStyle}} selected{{end}}>Style</option>
6868
{{- range .Styles}}
69-
<option value="{{.}}"{{if eq . $.SelectedStyle}} selected{{end}}>{{.}}</option>
69+
<option value="{{.Name}}"{{if eq .Name $.SelectedStyle}} selected{{end}}>{{.Name}}</option>
7070
{{- end}}
7171
</select>
7272
</div>
@@ -105,6 +105,9 @@
105105
<div class="field box" id="output"></div>
106106
</form>
107107
</div>
108+
<script>
109+
window.chromaStyles = {{.Styles}};
110+
</script>
108111
<script type="module">
109112
{{JS "index.js"}}
110113
</script>

0 commit comments

Comments
 (0)