Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions apps/docs/app/(diffs)/docs/Edit/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1092,14 +1092,16 @@ const file: FileContents | undefined = editor.getFile();
// Full document text, or '' when nothing is attached.
const text: string = editor.getText();

// Snapshot selections and horizontal code position for explicit restoration.
// Snapshot selections, active folds, and horizontal code position for
// persistence or remount restore.
const state: EditorState = editor.getState();
// EditorState = {
// selections?: EditorSelection[];
// view?: { scrollLeft: number };
// foldRanges?: LineRange[]; // zero-based; standalone closers stay visible
// view?: { scrollLeft: number; scrollTop: number };
// }

// Restore selections and horizontal code position after re-rendering.
// Restore selections, folds, and horizontal code position after re-rendering.
editor.setState(state);

// Replace all cursors and ranges programmatically. Positions are zero-based;
Expand Down
31 changes: 31 additions & 0 deletions packages/diffs/src/components/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
FileContents,
HighlightedToken,
LineAnnotation,
LineRange,
PostRenderPhase,
PrePropertiesConfig,
RenderFileMetadata,
Expand Down Expand Up @@ -176,6 +177,7 @@ export class File<
public file: FileContents | undefined;
protected renderRange: RenderRange | undefined;
protected enabled = true;
protected foldRanges: LineRange[] = [];

protected editor: DiffsEditor<LAnnotation> | undefined;

Expand Down Expand Up @@ -214,6 +216,33 @@ export class File<
return this.file;
}

public __setFoldRanges(ranges: LineRange[]): void {
if (!this.updateFoldRanges(ranges)) {
return;
}
if (this.enabled && this.file != null) {
this.rerender();
}
}

protected updateFoldRanges(ranges: LineRange[]): boolean {
if (
ranges.length === this.foldRanges.length &&
ranges.every((range, index) => {
const previous = this.foldRanges[index];
return (
previous?.startLine === range.startLine &&
previous.endLine === range.endLine
);
})
) {
return false;
}
this.foldRanges = ranges.map((range) => ({ ...range }));
this.fileRenderer.setFoldRanges(this.foldRanges);
return true;
}

public onThemeChange(): void {
this.fileRenderer.clearRenderCache();
this.rerender();
Expand Down Expand Up @@ -391,6 +420,8 @@ export class File<
this.workerManager = undefined;
this.file = undefined;
}
this.foldRanges = [];

this.enabled = false;
}

Expand Down
Loading