Skip to content

Commit ee9eed5

Browse files
committed
[toc2] Move in toc history using forward/back buttons
1 parent f2a7325 commit ee9eed5

File tree

1 file changed

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

1 file changed

+24
-0
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@
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.back != currentSection) {
140+
window.history.pushState({'back':currentSection},"",'')
141+
}
137142
var trg_id = $(evt.currentTarget).attr('data-toc-modified-id');
143+
window.history.pushState({'back':trg_id},"",'');
144+
window.history.lastjump = trg_id;
145+
138146
// use native scrollIntoView method with semi-unique id
139147
// ! browser native click does't follow links on all browsers
140148
document.getElementById(trg_id).scrollIntoView(true)
@@ -149,6 +157,22 @@
149157
}
150158
};
151159

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

0 commit comments

Comments
 (0)