Skip to content

Commit e4bfcff

Browse files
authored
Merge pull request #1266 from jfbercher/toc2_history
Toc2 history
2 parents e7e0ed8 + 71254c3 commit e4bfcff

File tree

1 file changed

+26
-0
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/toc2

1 file changed

+26
-0
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,17 @@
134134
$.ajax()
135135
}, 100);
136136
evt.preventDefault();
137+
// Each time a link is clicked in the toc, save the current position and target in the history
138+
var currentSection = $('#toc .highlight_on_scroll a').data('tocModifiedId')
139+
if (window.history.state != null){
140+
if (window.history.state.back != currentSection) {
141+
window.history.pushState({'back':currentSection},"",'')
142+
}
143+
}
137144
var trg_id = $(evt.currentTarget).attr('data-toc-modified-id');
145+
window.history.pushState({'back':trg_id},"",'');
146+
window.history.lastjump = trg_id;
147+
138148
// use native scrollIntoView method with semi-unique id
139149
// ! browser native click does't follow links on all browsers
140150
document.getElementById(trg_id).scrollIntoView(true)
@@ -149,6 +159,22 @@
149159
}
150160
};
151161

162+
//
163+
window.addEventListener('popstate',
164+
function(e) {
165+
if (e.state != null && e.state.back != null) {
166+
var back_id = e.state.back;
167+
document.getElementById(back_id).scrollIntoView(true)
168+
if (liveNotebook) {
169+
var cell = $(document.getElementById(back_id)).closest('.cell').data('cell');
170+
Jupyter.notebook.select(Jupyter.notebook.find_cell_index(cell));
171+
highlight_toc_item("toc_link_click", {
172+
cell: cell
173+
});
174+
}
175+
}
176+
});
177+
152178
var make_link = function(h, toc_mod_id) {
153179
var a = $('<a>')
154180
.attr({

0 commit comments

Comments
 (0)