fix: support rich markdown diff review (#2302)

This commit is contained in:
Jinjing 2026-05-18 21:08:02 -07:00 committed by GitHub
parent cdd40f117e
commit 8a4ba4bee1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 28 deletions

View File

@ -174,24 +174,6 @@ function EditorPanelInner({
)
useEditorCmdSaveRequest({ activeFile, openFiles, fileContents, handleSave })
const handleEditorToggleChange = useCallback(
(next: EditorToggleValue): void => {
const fileId = activeFile?.id
if (!fileId) {
return
}
if (next === 'changes') {
setEditorViewMode(fileId, 'changes')
return
}
setEditorViewMode(fileId, 'edit')
if (next !== 'edit') {
setMarkdownViewMode(fileId, next)
}
},
[activeFile?.id, setEditorViewMode, setMarkdownViewMode]
)
const handleCopyPath = useCallback(async (): Promise<void> => {
if (!activeFile) {
return
@ -234,7 +216,7 @@ function EditorPanelInner({
sourceGroupId
})
}
const handleOpenDiffTargetFile = (): void => {
const handleOpenDiffTargetFile = (preferredMarkdownViewMode?: 'rich'): void => {
if (!model.openFileState.canOpen) {
return
}
@ -246,6 +228,25 @@ function EditorPanelInner({
language: detectLanguage(activeFile.relativePath),
mode: 'edit'
})
if (preferredMarkdownViewMode) {
setEditorViewMode(activeFile.filePath, 'edit')
setMarkdownViewMode(activeFile.filePath, preferredMarkdownViewMode)
}
}
const handleEditorToggleChange = (next: EditorToggleValue): void => {
const fileId = activeFile.id
if (activeFile.mode === 'diff' && model.isMarkdown && next === 'rich') {
handleOpenDiffTargetFile('rich')
return
}
if (next === 'changes') {
setEditorViewMode(fileId, 'changes')
return
}
setEditorViewMode(fileId, 'edit')
if (next !== 'edit') {
setMarkdownViewMode(fileId, next)
}
}
const handleOpenMarkdownPreview = (): void => {
openMarkdownPreview({

View File

@ -61,7 +61,7 @@ type EditorPanelHeaderProps = {
sideBySide: boolean
openFileState: EditorHeaderOpenFileState
onCopyPath: () => void
onOpenDiffTargetFile: () => void
onOpenDiffTargetFile: (preferredMarkdownViewMode?: 'rich') => void
onOpenPreviewToSide: () => void
onOpenMarkdownPreview: () => void
onOpenContainingFolder: () => void
@ -187,7 +187,7 @@ export function EditorPanelHeader({
<button
type="button"
className="p-1 rounded hover:bg-accent text-muted-foreground hover:text-foreground transition-colors flex-shrink-0 disabled:opacity-50 disabled:hover:bg-transparent disabled:hover:text-muted-foreground"
onClick={onOpenDiffTargetFile}
onClick={() => onOpenDiffTargetFile(isMarkdown ? 'rich' : undefined)}
aria-label="Open file"
disabled={!openFileState.canOpen}
>

View File

@ -29,7 +29,7 @@ type EditorPanelShellProps = {
renameError: string | null
disableRenameBrowse: boolean
onCopyPath: () => void
onOpenDiffTargetFile: () => void
onOpenDiffTargetFile: (preferredMarkdownViewMode?: 'rich') => void
onOpenPreviewToSide: () => void
onOpenMarkdownPreview: () => void
onOpenContainingFolder: () => void

View File

@ -18,14 +18,14 @@ describe('getMarkdownViewModes', () => {
).toEqual(['source', 'rich'])
})
it('offers source and preview for single-file markdown diffs', () => {
it('offers source and rich for single-file markdown diffs', () => {
expect(
getMarkdownViewModes({
language: 'markdown',
mode: 'diff',
diffSource: 'unstaged'
})
).toEqual(['source', 'preview'])
).toEqual(['source', 'rich'])
})
it('does not offer preview for mermaid edit tabs', () => {

View File

@ -6,10 +6,7 @@ type MarkdownPreviewTarget = Pick<OpenFile, 'mode' | 'diffSource'> & {
}
const MARKDOWN_EDIT_VIEW_MODES = ['source', 'rich'] as const satisfies readonly MarkdownViewMode[]
const MARKDOWN_DIFF_VIEW_MODES = [
'source',
'preview'
] as const satisfies readonly MarkdownViewMode[]
const MARKDOWN_DIFF_VIEW_MODES = ['source', 'rich'] as const satisfies readonly MarkdownViewMode[]
const MERMAID_VIEW_MODES = ['source', 'rich'] as const satisfies readonly MarkdownViewMode[]
const CSV_VIEW_MODES = ['source', 'rich'] as const satisfies readonly MarkdownViewMode[]
const NOTEBOOK_VIEW_MODES = ['source', 'rich'] as const satisfies readonly MarkdownViewMode[]
@ -70,6 +67,9 @@ export function getMarkdownViewModes(target: MarkdownPreviewTarget): readonly Ma
}
export function getDefaultMarkdownViewMode(target: MarkdownPreviewTarget): MarkdownViewMode {
if (target.language === 'markdown' && target.mode === 'diff') {
return 'source'
}
const modes = getMarkdownViewModes(target)
return modes.includes('rich') ? 'rich' : 'source'
}