Skip to content

Commit d4d4962

Browse files
fix: align markdown checkbox preview state
1 parent f39c098 commit d4d4962

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

frontend/src/components/file-browser/FilePreview.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ export const FilePreview = memo(function FilePreview({ file, hideHeader = false,
155155
setIsSaving(true)
156156
try {
157157
await saveFileContent(editContent)
158+
if (isMarkdownFile) {
159+
setLocalMdContent(editContent)
160+
}
158161
setViewMode('preview')
159162
const editEvent = new CustomEvent('editModeChange', { detail: { isEditing: false } })
160163
window.dispatchEvent(editEvent)
@@ -285,7 +288,8 @@ export const FilePreview = memo(function FilePreview({ file, hideHeader = false,
285288

286289
try {
287290
const textContent = decodeBase64(file.content)
288-
if (!textContent) {
291+
const displayContent = isMarkdownFile ? localMdContent ?? textContent : textContent
292+
if (!displayContent) {
289293
return (
290294
<div className="text-center text-muted-foreground py-8">
291295
Empty file - click Edit to add content
@@ -294,11 +298,10 @@ export const FilePreview = memo(function FilePreview({ file, hideHeader = false,
294298
}
295299

296300
if (isMarkdownFile && markdownPreview) {
297-
const mdContent = localMdContent ?? textContent
298-
return <MarkdownRenderer content={mdContent} onContentChange={handleLocalMarkdownContentChange} />
301+
return <MarkdownRenderer content={displayContent} onContentChange={handleLocalMarkdownContentChange} />
299302
}
300303

301-
const lines = textContent.split('\n')
304+
const lines = displayContent.split('\n')
302305
return (
303306
<div className={`pb-[200px] text-sm bg-muted text-foreground rounded font-mono ${
304307
lineWrap ? 'overflow-x-hidden' : 'overflow-x-auto'

frontend/src/components/file-browser/MarkdownRenderer.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ function parseTaskItems(content: string): TaskItem[] {
3131

3232
export const MarkdownRenderer = memo(function MarkdownRenderer({ content, className = '', onContentChange }: MarkdownRendererProps) {
3333
const taskItems = useMemo(() => parseTaskItems(content), [content])
34-
let taskInputIndex = 0
3534

3635
const handleToggle = useCallback((taskItem: TaskItem) => {
3736
if (!onContentChange) return
@@ -47,27 +46,33 @@ export const MarkdownRenderer = memo(function MarkdownRenderer({ content, classN
4746
}
4847
}, [content, onContentChange])
4948

49+
const handleInputToggle = useCallback((input: HTMLInputElement) => {
50+
const inputs = Array.from(input.closest('.prose')?.querySelectorAll('input[type="checkbox"]') ?? [])
51+
const taskItem = taskItems[inputs.indexOf(input)]
52+
if (taskItem) {
53+
handleToggle(taskItem)
54+
}
55+
}, [handleToggle, taskItems])
56+
5057
const components: Components = {
5158
...markdownComponents,
5259
input(props) {
53-
const { type, ...rest } = props
60+
const { type, checked, disabled, ...rest } = props
61+
delete (rest as Record<string, unknown>).node
5462

5563
if (type === 'checkbox') {
56-
const taskItem = taskItems[taskInputIndex++]
57-
58-
if (taskItem) {
59-
return (
60-
<input
61-
type="checkbox"
62-
checked={taskItem.checked}
63-
onChange={() => handleToggle(taskItem)}
64-
className="cursor-pointer accent-primary"
65-
/>
66-
)
67-
}
64+
return (
65+
<input
66+
type="checkbox"
67+
checked={Boolean(checked)}
68+
onChange={(event) => handleInputToggle(event.currentTarget)}
69+
className="cursor-pointer accent-primary"
70+
{...rest}
71+
/>
72+
)
6873
}
6974

70-
return <input type={type} {...rest} />
75+
return <input type={type} checked={checked} disabled={disabled} {...rest} />
7176
},
7277
}
7378

0 commit comments

Comments
 (0)