Skip to content

Commit 6ca7bd0

Browse files
committed
settings.js: add new rustdoc.Setting interface
1 parent 53ef53a commit 6ca7bd0

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,14 @@ declare namespace rustdoc {
479479
* is a tuple of (filename, subdirs, filenames).
480480
*/
481481
type Dir = [string, rustdoc.Dir[], string[]]
482+
483+
/**
484+
* Indivitual setting object, used in `settings.js`
485+
*/
486+
interface Setting {
487+
js_name: string,
488+
name: string,
489+
options?: string[],
490+
default: string | boolean,
491+
}
482492
}

src/librustdoc/html/static/js/settings.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
}
103103
}
104104

105+
/**
106+
* @param {HTMLElement} settingsElement
107+
*/
105108
function setEvents(settingsElement) {
106109
updateLightAndDark();
107110
onEachLazy(settingsElement.querySelectorAll("input[type=\"checkbox\"]"), toggle => {
@@ -114,31 +117,35 @@
114117
changeSetting(toggle.id, toggle.checked);
115118
};
116119
});
117-
onEachLazy(settingsElement.querySelectorAll("input[type=\"radio\"]"), elem => {
118-
const settingId = elem.name;
119-
let settingValue = getSettingValue(settingId);
120-
if (settingId === "theme") {
121-
const useSystem = getSettingValue("use-system-theme");
122-
if (useSystem === "true" || settingValue === null) {
123-
// "light" is the default theme
124-
settingValue = useSystem === "false" ? "light" : "system preference";
120+
onEachLazy(
121+
settingsElement.querySelectorAll("input[type=\"radio\"]"),
122+
/** @param {HTMLInputElement} elem */
123+
elem => {
124+
const settingId = elem.name;
125+
let settingValue = getSettingValue(settingId);
126+
if (settingId === "theme") {
127+
const useSystem = getSettingValue("use-system-theme");
128+
if (useSystem === "true" || settingValue === null) {
129+
// "light" is the default theme
130+
settingValue = useSystem === "false" ? "light" : "system preference";
131+
}
125132
}
133+
if (settingValue !== null && settingValue !== "null") {
134+
elem.checked = settingValue === elem.value;
135+
}
136+
elem.addEventListener("change", ev => {
137+
changeSetting(elem.name, elem.value);
138+
});
126139
}
127-
if (settingValue !== null && settingValue !== "null") {
128-
elem.checked = settingValue === elem.value;
129-
}
130-
elem.addEventListener("change", ev => {
131-
changeSetting(ev.target.name, ev.target.value);
132-
});
133-
});
140+
);
134141
}
135142

136143
/**
137144
* This function builds the sections inside the "settings page". It takes a `settings` list
138145
* as argument which describes each setting and how to render it. It returns a string
139146
* representing the raw HTML.
140147
*
141-
* @param {Array<Object>} settings
148+
* @param {Array<rustdoc.Setting>} settings
142149
*
143150
* @return {string}
144151
*/
@@ -195,7 +202,9 @@
195202
* @return {HTMLElement}
196203
*/
197204
function buildSettingsPage() {
198-
const theme_names = getVar("themes").split(",").filter(t => t);
205+
const theme_list = getVar("themes")
206+
const theme_names = (theme_list === null ? "" : theme_list)
207+
.split(",").filter(t => t);
199208
theme_names.push("light", "dark", "ayu");
200209

201210
const settings = [

0 commit comments

Comments
 (0)