Skip to content

Commit 9498652

Browse files
committed
fix #6792 -- persist code folding state
1 parent fe02256 commit 9498652

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/packages/frontend/codemirror/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { set, get, del } from "@cocalc/frontend/misc/local-storage-typed";
22
import { isEqual } from "lodash";
33

44
export function getFoldedLines(cm): number[] {
5-
if (cm.foldCode == null) {
5+
if (cm?.foldCode == null) {
66
// not enabled
77
return [];
88
}
@@ -13,7 +13,7 @@ export function getFoldedLines(cm): number[] {
1313
}
1414

1515
export function setFoldedLines(cm, lines: number[]) {
16-
if (cm.foldCode == null) {
16+
if (cm?.foldCode == null) {
1717
// not enabled
1818
return;
1919
}

src/packages/frontend/frame-editors/code-editor/codemirror-editor.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { GutterMarkers } from "./codemirror-gutter-markers";
3636
import { Actions } from "./actions";
3737
import { EditorState } from "../frame-tree/types";
3838
import { Path } from "../frame-tree/path";
39+
import { initFold, saveFold } from "@cocalc/frontend/codemirror/util";
3940

4041
const STYLE = {
4142
width: "100%",
@@ -290,6 +291,16 @@ export const CodemirrorEditor: React.FC<Props> = React.memo((props) => {
290291
}
291292
cmRef.current.setOption("readOnly", props.read_only);
292293
cm_refresh();
294+
295+
const foldKey = `${props.path}\\${props.id}`;
296+
const saveFoldState = () => {
297+
if (cmRef.current != null) {
298+
saveFold(cmRef.current,foldKey);
299+
}
300+
};
301+
cmRef.current.on("fold" as any, saveFoldState);
302+
cmRef.current.on("unfold" as any, saveFoldState);
303+
initFold(cmRef.current, foldKey);
293304
}
294305

295306
function init_new_codemirror(): void {

0 commit comments

Comments
 (0)