Skip to content

Commit 91e90c3

Browse files
committed
fix #4085 - part 2: actually scroll the subheader into view
1 parent 92d9b7d commit 91e90c3

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/packages/frontend/frame-editors/jupyter-editor/table-of-contents.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
TableOfContents as TOC,
1111
TableOfContentsEntryList,
1212
} from "@cocalc/frontend/components";
13+
import $ from "jquery";
1314

1415
interface Props {
1516
font_size: number;
@@ -29,20 +30,38 @@ export const TableOfContents: React.FC<Props> = React.memo(
2930
): Promise<void> {
3031
actions.jump_to_cell(id, extra);
3132
// stupid hack due to rendering/windowing delays...
32-
await delay(100);
33+
await delay(1);
34+
actions.jump_to_cell(id, extra);
35+
await delay(50);
3336
actions.jump_to_cell(id, extra);
3437
}
3538

3639
return (
3740
<TOC
3841
contents={contents}
3942
style={{ fontSize: `${font_size - 6}px` }}
40-
scrollTo={({ id, extra }) => {
41-
// TODO: ignore markdown_id for now -- we just jump to the markdown cell, but not the exact heading
42-
// in that cell, for now.
43-
// markdown_id is string rep of number of block reference into the rendered markdown.
44-
const { cell_id /*, markdown_id*/ } = JSON.parse(id);
43+
scrollTo={async ({ id, extra }) => {
44+
const { cell_id, markdown_id } = JSON.parse(id);
4545
jump_to_cell(cell_id, extra);
46+
if (markdown_id) {
47+
const n = parseInt(markdown_id);
48+
const f = () => {
49+
// just use some "dumb" jquery to scroll the actual heading into view.
50+
// It's pretty hard to do this otherwise, given our current design.
51+
const cell = $(`#${cell_id}`);
52+
const elt = $(cell.find(".cocalc-jupyter-header")[n]);
53+
if (elt.length == 0) {
54+
return;
55+
}
56+
// @ts-ignore -- it's a jquery plugin
57+
elt.scrollintoview();
58+
};
59+
f();
60+
await delay(2);
61+
f();
62+
await delay(200);
63+
f();
64+
}
4665
}}
4766
/>
4867
);

src/packages/frontend/jupyter/browser-actions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export class JupyterActions extends JupyterActions0 {
7979
this.syncdb.on("metadata-change", this.sync_read_only);
8080
this.syncdb.on("connected", this.sync_read_only);
8181

82+
// first update
83+
this.syncdb.once("change", this.updateContentsNow);
84+
8285
this.syncdb.on("change", () => {
8386
// And activity indicator
8487
this.activity();
@@ -925,14 +928,18 @@ export class JupyterActions extends JupyterActions0 {
925928
set_local_storage(this.name, to_json(current));
926929
}
927930

928-
public update_contents(): void {
931+
private updateContentsNow = () => {
929932
if (this._state == "closed") return;
930933
const cells = this.store.get("cells");
931934
if (cells == null) return;
932935
const cell_list = this.store.get("cell_list");
933936
if (cell_list == null) return;
934937
const contents = fromJS(parseHeadings(cells, cell_list));
935938
this.setState({ contents });
939+
};
940+
941+
public update_contents(): void {
942+
this.updateContentsNow();
936943
}
937944

938945
protected __syncdb_change_post_hook(_doInit: boolean) {

0 commit comments

Comments
 (0)