CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/codemirror/util.ts
Views: 687
1
import { set, get, del } from "@cocalc/frontend/misc/local-storage-typed";
2
import { isEqual } from "lodash";
3
4
export function getFoldedLines(cm): number[] {
5
if (cm?.foldCode == null) {
6
// not enabled
7
return [];
8
}
9
return cm
10
.getAllMarks()
11
.filter((mark) => mark.__isFold)
12
.map((mark) => mark.find().from.line);
13
}
14
15
export function setFoldedLines(cm, lines: number[]) {
16
if (cm?.foldCode == null) {
17
// not enabled
18
return;
19
}
20
lines.reverse();
21
for (const n of lines) {
22
cm.foldCode(n);
23
}
24
}
25
26
function toKey(key: string): string {
27
return `cmfold-${key}`;
28
}
29
30
export function initFold(cm, key: string) {
31
const k = toKey(key);
32
const lines = get<number[]>(k);
33
if (lines != null) {
34
try {
35
setFoldedLines(cm, lines);
36
} catch (err) {
37
console.warn(`error setting cold folding for ${key}: `, err);
38
del(k);
39
}
40
}
41
}
42
43
export function saveFold(cm, key: string) {
44
const k = toKey(key);
45
const lines = get<number[]>(k);
46
const lines2 = getFoldedLines(cm);
47
if (lines2.length == 0) {
48
if (lines != null) {
49
del(k);
50
}
51
return;
52
}
53
if (!isEqual(lines, lines2)) {
54
set<number[]>(k, lines2);
55
}
56
}
57
58