Skip to content
Closed
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
33 changes: 32 additions & 1 deletion apps/web/src/components/files/FilePreviewPanel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
normalizeFileCommentRange,
remapFileCommentAnnotations,
} from "./fileCommentAnnotations";
import { isMarkdownPreviewFile, setMarkdownTaskChecked } from "./filePreviewMode";
import {
isMarkdownPreviewFile,
resolveMarkdownTaskPreviewUpdate,
setMarkdownTaskChecked,
} from "./filePreviewMode";

describe("file comment annotations", () => {
it("normalizes and formats selected line ranges", () => {
Expand Down Expand Up @@ -79,4 +83,31 @@ describe("setMarkdownTaskChecked", () => {
expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
});

it("refuses rendered task-list mutations for a truncated Markdown read", () => {
const fullMarkdown = `- [ ] Keep the complete file\n${"x".repeat(1024 * 1024)}`;
const truncatedMarkdown = fullMarkdown.slice(0, 1024 * 1024);

expect(
resolveMarkdownTaskPreviewUpdate({
markdown: truncatedMarkdown,
markerOffset: 2,
checked: true,
truncated: true,
}),
).toBeNull();
expect(fullMarkdown.startsWith(truncatedMarkdown)).toBe(true);
expect(fullMarkdown.length).toBeGreaterThan(truncatedMarkdown.length);
});

it("returns a task-list update for a complete Markdown read", () => {
expect(
resolveMarkdownTaskPreviewUpdate({
markdown,
markerOffset: 2,
checked: true,
truncated: false,
}),
).toBe("- [x] First\n- [x] Second\n");
});
});
35 changes: 24 additions & 11 deletions apps/web/src/components/files/FilePreviewPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { resolveCenteredFileLineScrollTop } from "./fileLineReveal";
import { DiffCommentAnnotation } from "../diffs/DiffCommentAnnotation";
import { projectFileCacheKey, projectFileEditorCacheKey } from "./fileContentRevision";
import { fileBreadcrumbs } from "./filePath";
import { isMarkdownPreviewFile, setMarkdownTaskChecked } from "./filePreviewMode";
import { isMarkdownPreviewFile, resolveMarkdownTaskPreviewUpdate } from "./filePreviewMode";
import { FileSaveCoordinator } from "./fileSaveCoordinator";
import {
confirmProjectFileQueryData,
Expand Down Expand Up @@ -704,6 +704,7 @@ function RenderedMarkdownSurface({
cwd,
relativePath,
contents,
truncated,
threadRef,
onPendingChange,
}: Omit<
Expand All @@ -715,6 +716,7 @@ function RenderedMarkdownSurface({
| "wordWrap"
| "onPostRender"
> & {
truncated: boolean;
threadRef: ScopedThreadRef;
}) {
const saveCoordinator = useFileSaveCoordinator({
Expand All @@ -731,15 +733,24 @@ function RenderedMarkdownSurface({
cwd={cwd}
threadRef={threadRef}
className="mx-auto max-w-4xl px-6 py-5"
onTaskListChange={({ markerOffset, checked }) => {
const currentContents =
getOptimisticProjectFileQueryData(environmentId, cwd, relativePath)?.contents ??
contents;
const nextContents = setMarkdownTaskChecked(currentContents, markerOffset, checked);
if (nextContents === currentContents) return;
setProjectFileQueryData(environmentId, cwd, relativePath, nextContents);
saveCoordinator.change(nextContents);
}}
onTaskListChange={
truncated
? undefined
: ({ markerOffset, checked }) => {
const currentContents =
getOptimisticProjectFileQueryData(environmentId, cwd, relativePath)?.contents ??
contents;
const nextContents = resolveMarkdownTaskPreviewUpdate({
markdown: currentContents,
markerOffset,
checked,
truncated,
});
if (nextContents === null) return;
setProjectFileQueryData(environmentId, cwd, relativePath, nextContents);
saveCoordinator.change(nextContents);
}
}
/>
</ScrollArea>
);
Expand Down Expand Up @@ -970,7 +981,8 @@ export default function FilePreviewPanel({
) : null}
{relativePath && file.data?.truncated ? (
<div className="shrink-0 border-b border-warning/20 bg-warning-surface px-3 py-1.5 text-[11px] text-warning-foreground">
Preview limited to the first 1 MB of a {file.data.byteLength.toLocaleString()} byte file.
Read-only preview limited to the first 1 MB of a {file.data.byteLength.toLocaleString()}{" "}
byte file.
</div>
) : null}
<div className="flex min-h-0 flex-1 overflow-hidden">
Expand Down Expand Up @@ -1004,6 +1016,7 @@ export default function FilePreviewPanel({
relativePath={relativePath}
threadRef={threadRef}
contents={file.data.contents}
truncated={file.data.truncated}
onPendingChange={onPendingChange}
/>
) : file.data.truncated ? (
Expand Down
11 changes: 11 additions & 0 deletions apps/web/src/components/files/filePreviewMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ export function setMarkdownTaskChecked(

return `${markdown.slice(0, markerOffset + 1)}${checked ? "x" : " "}${markdown.slice(markerOffset + 2)}`;
}

export function resolveMarkdownTaskPreviewUpdate(input: {
readonly markdown: string;
readonly markerOffset: number;
readonly checked: boolean;
readonly truncated: boolean;
}): string | null {
if (input.truncated) return null;
const nextMarkdown = setMarkdownTaskChecked(input.markdown, input.markerOffset, input.checked);
return nextMarkdown === input.markdown ? null : nextMarkdown;
}
Loading