[toc2] Highlight on scrolling - update after @jcb91 review

This commit is contained in:
Jean-François Bercher
2017-09-04 15:43:31 +02:00
parent 83fa546deb
commit 8a535dd5d0
5 changed files with 7 additions and 11 deletions
@@ -11,7 +11,7 @@ The toc2 extension enables to collect all running headers and display them in a
![](demo2.gif)
### Third demo: Notebook scrolling and Collapsing sections
![](demo_scroll_collapse.gif)
![](https://user-images.githubusercontent.com/7596356/29540207-a3d892fe-86cd-11e7-8476-54c79d9f8d7c.gif)
The table of contents is automatically updated when modifications occur in the notebook. The toc window can be moved and resized. It can be docked as a sidebar or dragged from the sidebar into a floating window. The table of contents can be collapsed or the window can be completely hidden. The navigation menu can be enabled/disabled via the nbextensions configuration utility. It can also be resized. The position, dimensions, and states (that is 'collapsed' and 'hidden' states) are remembered (actually stored in the notebook's metadata) and restored on the next session.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 MiB

@@ -21,7 +21,6 @@ define([
var highlight_toc_item = toc2.highlight_toc_item;
var table_of_contents = toc2.table_of_contents;
var toggle_toc = toc2.toggle_toc;
var highlightTocItemOnScroll = toc2.highlightTocItemOnScroll;
// ...........Parameters configuration......................
// default values for system-wide configurable parameters
@@ -147,7 +146,7 @@ define([
function create_additional_css() {
var sheet = document.createElement('style')
sheet.innerHTML = "#toc-level0 li > a:hover { background-color: " + cfg.colors.hover_highlight + " }\n" +
sheet.innerHTML = "#toc-level0 li > span:hover { background-color: " + cfg.colors.hover_highlight + " }\n" +
".toc-item-highlight-select {background-color: " + cfg.colors.selected_highlight + "}\n" +
".toc-item-highlight-execute {background-color: " + cfg.colors.running_highlight + "}\n" +
".toc-item-highlight-execute.toc-item-highlight-select {background-color: " + cfg.colors.selected_highlight + "}"
@@ -214,7 +213,6 @@ define([
// read configuration, then call toc
cfg = read_config(cfg, function() {
table_of_contents(cfg, st);
highlightTocItemOnScroll(cfg, st);
}); // called after config is stable
// event: render toc for each markdown cell modification
$([IPython.events]).on("rendered.MarkdownCell",
@@ -19,7 +19,7 @@
liveNotebook = true;
}
catch (err) {
// We *are* in a live notebook
// We *are* theoretically in a non-live notebook
console.log('[toc2] working in non-live notebook'); //, err);
// in non-live notebook, there's no event structure, so we make our own
if (window.events === undefined) {
@@ -374,8 +374,7 @@ function highlight_toc_item(evt, data) {
// On header/menu/toolbar resize, resize the toc itself
// (if displayed as a sidebar)
if (liveNotebook) {
$([Jupyter.events]).on("resize-header.Page", function() {setSideBarHeight(cfg, st);});
$([Jupyter.events]).on("toggle-all-headers", function() {setSideBarHeight(cfg, st);});
$([Jupyter.events]).on("resize-header.Page toggle-all-headers", function() {setSideBarHeight(cfg, st);});
}
@@ -582,8 +581,9 @@ var table_of_contents = function (cfg,st) {
var toc_wrapper = $("#toc-wrapper");
// var toc_index=0;
if (toc_wrapper.length === 0) {
create_toc_div(cfg,st);
if (toc_wrapper.length === 0) { // toc window doesn't exist at all
create_toc_div(cfg,st); // create it
highlightTocItemOnScroll(cfg, st); // initialize highlighting on scroll
}
var segments = [];
var ul = $("<ul/>").addClass("toc-item").attr('id','toc-level0');
@@ -734,7 +734,6 @@ var table_of_contents = function (cfg,st) {
highlight_toc_item: highlight_toc_item,
table_of_contents: table_of_contents,
toggle_toc: toggle_toc,
highlightTocItemOnScroll: highlightTocItemOnScroll,
};
});
// export table_of_contents to global namespace for backwards compatibility
@@ -47,7 +47,6 @@ $( document ).ready(function(){
// fire the main function with these parameters
require(['nbextensions/toc2/toc2'], function (toc2) {
toc2.table_of_contents(cfg, st);
toc2.highlightTocItemOnScroll(cfg,st);
});
});
</script>