1
Fork 0

Source sidebar improvements

This commit is contained in:
Guillaume Gomez 2018-11-16 11:36:40 +01:00
parent 93520d2ad1
commit e87f8cc49b
5 changed files with 86 additions and 60 deletions

View file

@ -194,9 +194,10 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
.collect::<String>(), .collect::<String>(),
suffix=page.resource_suffix, suffix=page.resource_suffix,
extra_scripts=extra_scripts.iter().map(|e| { extra_scripts=extra_scripts.iter().map(|e| {
format!("<script src=\"{root_path}{extra_script}\"></script>", format!("<script src=\"{root_path}{extra_script}{suffix}.js\"></script>",
root_path=page.root_path, root_path=page.root_path,
extra_script=e) extra_script=e,
suffix=page.resource_suffix)
}).collect::<String>(), }).collect::<String>(),
) )
} }

View file

@ -859,9 +859,11 @@ themePicker.onblur = handleThemeButtonsBlur;
write_minify(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)), write_minify(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
static_files::SETTINGS_JS, static_files::SETTINGS_JS,
options.enable_minification)?; options.enable_minification)?;
write_minify(cx.dst.join(&format!("source-script{}.js", cx.shared.resource_suffix)), if cx.shared.include_sources {
include_str!("static/source-script.js"), write_minify(cx.dst.join(&format!("source-script{}.js", cx.shared.resource_suffix)),
options.enable_minification)?; static_files::sidebar::SOURCE_SCRIPT,
options.enable_minification)?;
}
{ {
let mut data = format!("var resourcesSuffix = \"{}\";\n", let mut data = format!("var resourcesSuffix = \"{}\";\n",
@ -990,8 +992,8 @@ themePicker.onblur = handleThemeButtonsBlur;
} }
} }
fn to_string(&self) -> String { fn to_json_string(&self) -> String {
let mut subs: Vec<&Hierarchy> = self.children.iter().map(|(_, v)| v).collect(); let mut subs: Vec<&Hierarchy> = self.children.values().collect();
subs.sort_unstable_by(|a, b| a.elem.cmp(&b.elem)); subs.sort_unstable_by(|a, b| a.elem.cmp(&b.elem));
let mut files = self.elems.iter() let mut files = self.elems.iter()
.map(|s| format!("\"{}\"", .map(|s| format!("\"{}\"",
@ -1002,46 +1004,52 @@ themePicker.onblur = handleThemeButtonsBlur;
// FIXME(imperio): we could avoid to generate "dirs" and "files" if they're empty. // FIXME(imperio): we could avoid to generate "dirs" and "files" if they're empty.
format!("{{\"name\":\"{name}\",\"dirs\":[{subs}],\"files\":[{files}]}}", format!("{{\"name\":\"{name}\",\"dirs\":[{subs}],\"files\":[{files}]}}",
name=self.elem.to_str().expect("invalid osstring conversion"), name=self.elem.to_str().expect("invalid osstring conversion"),
subs=subs.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(","), subs=subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(","),
files=files.join(",")) files=files.join(","))
} }
} }
use std::path::Component; if cx.shared.include_sources {
use std::path::Component;
let mut hierarchy = Hierarchy::new(OsString::new()); let mut hierarchy = Hierarchy::new(OsString::new());
for source in cx.shared.local_sources.iter() for source in cx.shared.local_sources.iter()
.filter_map(|p| p.0.strip_prefix(&cx.shared.src_root) .filter_map(|p| p.0.strip_prefix(&cx.shared.src_root)
.ok()) { .ok()) {
let mut h = &mut hierarchy; let mut h = &mut hierarchy;
let mut elems = source.components() let mut elems = source.components()
.filter_map(|s| { .filter_map(|s| {
match s { match s {
Component::Normal(s) => Some(s.to_owned()), Component::Normal(s) => Some(s.to_owned()),
_ => None, _ => None,
} }
}) })
.peekable(); .peekable();
loop { loop {
let cur_elem = elems.next().expect("empty file path"); let cur_elem = elems.next().expect("empty file path");
if elems.peek().is_none() { if elems.peek().is_none() {
h.elems.insert(cur_elem); h.elems.insert(cur_elem);
break; break;
} else { } else {
let e = cur_elem.clone(); let e = cur_elem.clone();
h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e)); h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e));
h = h.children.get_mut(&cur_elem).expect("not found child"); h = h.children.get_mut(&cur_elem).expect("not found child");
}
} }
} }
}
let dst = cx.dst.join("source-files.js"); let dst = cx.dst.join("source-files.js");
let (mut all_sources, _krates) = try_err!(collect(&dst, &krate.name, "sourcesIndex"), &dst); let (mut all_sources, _krates) = try_err!(collect(&dst, &krate.name, "sourcesIndex"), &dst);
all_sources.push(format!("sourcesIndex['{}'] = {};", &krate.name, hierarchy.to_string())); all_sources.push(format!("sourcesIndex['{}'] = {};",
all_sources.sort(); &krate.name,
let mut w = try_err!(File::create(&dst), &dst); hierarchy.to_json_string()));
try_err!(writeln!(&mut w, "var N = null;var sourcesIndex = {{}};\n{}", all_sources.join("\n")), all_sources.sort();
&dst); let mut w = try_err!(File::create(&dst), &dst);
try_err!(writeln!(&mut w,
"var N = null;var sourcesIndex = {{}};\n{}",
all_sources.join("\n")),
&dst);
}
// Update the search index // Update the search index
let dst = cx.dst.join("search-index.js"); let dst = cx.dst.join("search-index.js");
@ -1367,7 +1375,7 @@ impl<'a> SourceCollector<'a> {
layout::render(&mut w, &self.scx.layout, layout::render(&mut w, &self.scx.layout,
&page, &(""), &Source(contents), &page, &(""), &Source(contents),
self.scx.css_file_extension.is_some(), self.scx.css_file_extension.is_some(),
&self.scx.themes, &["source-files.js", "source-script.js"])?; &self.scx.themes, &["source-files", "source-script"])?;
w.flush()?; w.flush()?;
self.scx.local_sources.insert(p.clone(), href); self.scx.local_sources.insert(p.clone(), href);
Ok(()) Ok(())

View file

@ -1464,25 +1464,28 @@ kbd {
#sidebar-toggle { #sidebar-toggle {
position: fixed; position: fixed;
top: 30px; top: 30px;
right: 300px; left: 300px;
z-index: 10; z-index: 10;
padding: 3px; padding: 3px;
border-top-left-radius: 3px; border-top-right-radius: 3px;
border-bottom-left-radius: 3px; border-bottom-right-radius: 3px;
cursor: pointer; cursor: pointer;
font-weight: bold; font-weight: bold;
transition: right 1s; transition: left .5s;
font-size: 1.2em; font-size: 1.2em;
border: 1px solid;
border-left: 0;
} }
#source-sidebar { #source-sidebar {
position: fixed; position: fixed;
top: 0; top: 0;
bottom: 0; bottom: 0;
right: 0; left: 0;
width: 300px; width: 300px;
z-index: 1; z-index: 1;
overflow: auto; overflow: auto;
transition: right 1s; transition: left .5s;
border-right: 1px solid;
} }
#source-sidebar > .title { #source-sidebar > .title {
font-size: 1.5em; font-size: 1.5em;
@ -1495,6 +1498,11 @@ div.children {
padding-left: 27px; padding-left: 27px;
display: none; display: none;
} }
div.name {
cursor: pointer;
position: relative;
margin-left: 16px;
}
div.files > a { div.files > a {
display: block; display: block;
padding: 0 3px; padding: 0 3px;
@ -1507,10 +1515,13 @@ div.name.expand + .children {
} }
div.name::before { div.name::before {
content: "\25B6"; content: "\25B6";
display: inline-block; padding-left: 4px;
padding-right: 4px;
font-size: 0.7em; font-size: 0.7em;
position: absolute;
left: -16px;
top: 4px;
} }
div.name.expand::before { div.name.expand::before {
transform: rotate(90deg); transform: rotate(90deg);
left: -14px;
} }

View file

@ -72,21 +72,21 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
children.appendChild(files); children.appendChild(files);
parent.appendChild(name); parent.appendChild(name);
parent.appendChild(children); parent.appendChild(children);
return hasFoundFile === true && search.currentFile !== null; return hasFoundFile === true && currentFile.startsWith(fullPath);
} }
function toggleSidebar() { function toggleSidebar() {
var sidebar = document.getElementById("source-sidebar"); var sidebar = document.getElementById("source-sidebar");
var child = this.children[0].children[0]; var child = this.children[0].children[0];
if (child.innerText === "<") { if (child.innerText === ">") {
sidebar.style.right = ""; sidebar.style.left = "";
this.style.right = ""; this.style.left = "";
child.innerText = ">"; child.innerText = "<";
updateLocalStorage("rustdoc-source-sidebar-hidden", "false"); updateLocalStorage("rustdoc-source-sidebar-hidden", "false");
} else { } else {
sidebar.style.right = "-300px"; sidebar.style.left = "-300px";
this.style.right = "0"; this.style.left = "0";
child.innerText = "<"; child.innerText = ">";
updateLocalStorage("rustdoc-source-sidebar-hidden", "true"); updateLocalStorage("rustdoc-source-sidebar-hidden", "true");
} }
} }
@ -102,10 +102,10 @@ function createSidebarToggle() {
var inner2 = document.createElement("div"); var inner2 = document.createElement("div");
inner2.style.marginTop = "-2px"; inner2.style.marginTop = "-2px";
if (getCurrentValue("rustdoc-source-sidebar-hidden") === "true") { if (getCurrentValue("rustdoc-source-sidebar-hidden") === "true") {
inner2.innerText = "<";
sidebarToggle.style.right = "0";
} else {
inner2.innerText = ">"; inner2.innerText = ">";
sidebarToggle.style.left = "0";
} else {
inner2.innerText = "<";
} }
inner1.appendChild(inner2); inner1.appendChild(inner2);
@ -125,7 +125,7 @@ function createSourceSidebar() {
var sidebar = document.createElement("div"); var sidebar = document.createElement("div");
sidebar.id = "source-sidebar"; sidebar.id = "source-sidebar";
if (getCurrentValue("rustdoc-source-sidebar-hidden") === "true") { if (getCurrentValue("rustdoc-source-sidebar-hidden") === "true") {
sidebar.style.right = "-300px"; sidebar.style.left = "-300px";
} }
var currentFile = getCurrentFilePath(); var currentFile = getCurrentFilePath();

View file

@ -109,3 +109,9 @@ pub mod source_code_pro {
/// The file `SourceCodePro-LICENSE.txt`, the license text of the Source Code Pro font. /// The file `SourceCodePro-LICENSE.txt`, the license text of the Source Code Pro font.
pub static LICENSE: &'static [u8] = include_bytes!("static/SourceCodePro-LICENSE.txt"); pub static LICENSE: &'static [u8] = include_bytes!("static/SourceCodePro-LICENSE.txt");
} }
/// Files related to the sidebar in rustdoc sources.
pub mod sidebar {
/// File script to handle sidebar.
pub static SOURCE_SCRIPT: &'static str = include_str!("static/source-script.js");
}