Skip to content

Commit a54a883

Browse files
committed
fix #7779 - jupyter code folding state
1 parent 888c215 commit a54a883

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { set, get, del } from "@cocalc/frontend/misc/local-storage-typed";
2+
import { isEqual } from "lodash";
3+
14
export function getFoldedLines(cm): number[] {
25
return cm
36
.getAllMarks()
@@ -6,7 +9,40 @@ export function getFoldedLines(cm): number[] {
69
}
710

811
export function setFoldedLines(cm, lines: number[]) {
9-
for(const n of lines) {
12+
lines.reverse();
13+
for (const n of lines) {
1014
cm.foldCode(n);
1115
}
1216
}
17+
18+
function toKey(key: string): string {
19+
return `cmfold-${key}`;
20+
}
21+
22+
export function initFold(cm, key: string) {
23+
const k = toKey(key);
24+
const lines = get<number[]>(k);
25+
if (lines != null) {
26+
try {
27+
setFoldedLines(cm, lines);
28+
} catch (err) {
29+
console.warn(`error setting cold folding for ${key}: `, err);
30+
del(k);
31+
}
32+
}
33+
}
34+
35+
export function saveFold(cm, key: string) {
36+
const k = toKey(key);
37+
const lines = get<number[]>(k);
38+
const lines2 = getFoldedLines(cm);
39+
if (lines2.length == 0) {
40+
if (lines != null) {
41+
del(k);
42+
}
43+
return;
44+
}
45+
if (!isEqual(lines, lines2)) {
46+
set<number[]>(k, lines2);
47+
}
48+
}

src/packages/frontend/jupyter/codemirror-editor.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ import { Complete, Actions as CompleteActions } from "./complete";
2828
import { Cursors } from "./cursors";
2929
import { Position } from "./insert-cell/types";
3030
import { is_whitespace } from "@cocalc/util/misc";
31-
import {
32-
getFoldedLines,
33-
setFoldedLines,
34-
} from "@cocalc/frontend/codemirror/util";
31+
import { initFold, saveFold } from "@cocalc/frontend/codemirror/util";
3532

3633
// We cache a little info about each Codemirror editor we make here,
3734
// so we can restore it when we make the same one again. Due to
@@ -151,14 +148,17 @@ export const CodeMirrorEditor: React.FC<CodeMirrorEditorProps> = ({
151148

152149
useEffect(() => {
153150
if (frameActions.current?.frame_id != null) {
154-
key.current = `${frameActions.current.frame_id}${id}`;
151+
key.current = `${(actions as any)?.path}${
152+
frameActions.current.frame_id
153+
}${id}`;
155154
}
156155
init_codemirror(options, value);
157156

158157
return () => {
159158
if (cm.current != null) {
160-
const foldedLines = getFoldedLines(cm.current);
161-
console.log({ foldedLines });
159+
if (key.current != null) {
160+
saveFold(cm.current, key.current);
161+
}
162162
cm_save();
163163
cm_destroy();
164164
}
@@ -663,7 +663,6 @@ export const CodeMirrorEditor: React.FC<CodeMirrorEditorProps> = ({
663663
if (node.parentNode == null) return;
664664
node.parentNode.replaceChild(elt, node);
665665
}, options0);
666-
window.x = { cm: cm.current, getFoldedLines, setFoldedLines };
667666

668667
// We explicitly re-add all the extraKeys due to weird precedence.
669668
cm.current.addKeyMap(options0.extraKeys);
@@ -769,6 +768,17 @@ export const CodeMirrorEditor: React.FC<CodeMirrorEditorProps> = ({
769768
if (is_focused) {
770769
focus_cm();
771770
}
771+
772+
if (key.current != null) {
773+
initFold(cm.current, key.current);
774+
const save = () => {
775+
if (cm.current != null && key.current != null) {
776+
saveFold(cm.current, key.current);
777+
}
778+
};
779+
cm.current.on("fold", save);
780+
cm.current.on("unfold", save);
781+
}
772782
}
773783

774784
function focus_cm(): void {

0 commit comments

Comments
 (0)