Skip to content

Commit fd98d3a

Browse files
committed
work in progress -- wrote code to get and set code folds
1 parent 8753b91 commit fd98d3a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function getFoldedLines(cm): number[] {
2+
return cm
3+
.getAllMarks()
4+
.filter((mark) => mark.__isFold)
5+
.map((mark) => mark.find().from.line);
6+
}
7+
8+
export function setFoldedLines(cm, lines: number[]) {
9+
for(const n of lines) {
10+
cm.foldCode(n);
11+
}
12+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ 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";
3135

3236
// We cache a little info about each Codemirror editor we make here,
3337
// so we can restore it when we make the same one again. Due to
@@ -153,6 +157,8 @@ export const CodeMirrorEditor: React.FC<CodeMirrorEditorProps> = ({
153157

154158
return () => {
155159
if (cm.current != null) {
160+
const foldedLines = getFoldedLines(cm.current);
161+
console.log({ foldedLines });
156162
cm_save();
157163
cm_destroy();
158164
}
@@ -657,6 +663,7 @@ export const CodeMirrorEditor: React.FC<CodeMirrorEditorProps> = ({
657663
if (node.parentNode == null) return;
658664
node.parentNode.replaceChild(elt, node);
659665
}, options0);
666+
window.x = { cm: cm.current, getFoldedLines, setFoldedLines };
660667

661668
// We explicitly re-add all the extraKeys due to weird precedence.
662669
cm.current.addKeyMap(options0.extraKeys);

0 commit comments

Comments
 (0)