fix(editor): 备份与磁盘比较时忽略行尾,CRLF 文件改完又撤销不再弹恢复提示
备份内容来自 CodeMirror,永远是 LF;readDraftBackup 用严格相等和磁盘原文比, CRLF 文件因此永远判为不同。把 TextEditor 的 sameDocument 挪到 editor-draft-backup.ts 导出,两边共用。只打开不编辑不写备份,不受影响。
This commit is contained in:
1 parent
d45aaa96ce
commit
cd570d339b
3 files changed
+43
-12
No files matched your search
@@ -36,7 +36,7 @@ import { rewriteLocalMarkdownLinks } from './markdown-navigation.ts'
|
||||
import { languageForPath } from './lang.ts'
|
||||
import { cmSurfaceTheme, CmThemeCompartment } from './cm-themes.ts'
|
||||
import { cmSearchExtensions, CmSearchPhrases } from './cm-search.ts'
|
||||
import { clearDraftBackup, draftBackupBaseMtime, flushDraftBackups, readDraftBackup, registerDraftSource, scheduleDraftBackup } from './editor-draft-backup.ts'
|
||||
import { clearDraftBackup, draftBackupBaseMtime, flushDraftBackups, readDraftBackup, registerDraftSource, sameDocument, scheduleDraftBackup } from './editor-draft-backup.ts'
|
||||
import { flashQuoteLines, quoteFlashField, quoteFlashHighlight } from './quote-flash.ts'
|
||||
import { isDarkScheme, subscribeColorScheme } from './theme.ts'
|
||||
import { SandboxStatusBar } from './SandboxStatusBar.tsx'
|
||||
@@ -149,15 +149,6 @@ function editScrollOf(scope: { sessionId: string }, path: string): number {
|
||||
return editScrollMemory.get(previewScrollKey(scope, path)) ?? 0
|
||||
}
|
||||
|
||||
/** Whether two spellings are the SAME document — byte-equal, or equal once
|
||||
* line endings are normalized CRLF/CR → LF. Line endings are not content:
|
||||
* CodeMirror keeps every document with `\n` endings whatever the file used,
|
||||
* so the live document of a freshly opened CRLF file differs from the bytes
|
||||
* it was loaded from by line endings alone. */
|
||||
function sameDocument(a: string, b: string): boolean {
|
||||
return a === b || a.replace(/\r\n?/g, '\n') === b.replace(/\r\n?/g, '\n')
|
||||
}
|
||||
|
||||
export function TextEditor(props: FileViewerProps) {
|
||||
const { ctx, scope, path, viewerId, content, size, largeFile, line } = props
|
||||
const [mode, setMode] = useState<ViewMode>('preview')
|
||||
|
||||
@@ -215,9 +215,19 @@ export function scheduleDraftBackup(sessionId: string, path: string): void {
|
||||
}, DRAFT_BACKUP_THROTTLE_MS)
|
||||
}
|
||||
|
||||
/** Whether two spellings are the SAME document — byte-equal, or equal once
|
||||
* line endings are normalized CRLF/CR → LF. Line endings are not content:
|
||||
* CodeMirror keeps every document with `\n` endings whatever the file used,
|
||||
* so the live document of a freshly opened CRLF file differs from the bytes
|
||||
* it was loaded from by line endings alone. */
|
||||
export function sameDocument(a: string, b: string): boolean {
|
||||
return a === b || a.replace(/\r\n?/g, '\n') === b.replace(/\r\n?/g, '\n')
|
||||
}
|
||||
|
||||
/**
|
||||
* The pending draft of one file, if its content differs from the bytes on
|
||||
* disk; otherwise the stale entry is dropped.
|
||||
* disk (line endings aside, see {@link sameDocument}); otherwise the stale
|
||||
* entry is dropped.
|
||||
* @param sessionId - the session owning the tab.
|
||||
* @param path - the file the draft belongs to.
|
||||
* @param content - the bytes just loaded from disk.
|
||||
@@ -226,7 +236,7 @@ export function scheduleDraftBackup(sessionId: string, path: string): void {
|
||||
export function readDraftBackup(sessionId: string, path: string, content: string): string | null {
|
||||
const entry = readStored(keyOf(sessionId, path))
|
||||
if (entry === undefined) return null
|
||||
if (entry.text === content) {
|
||||
if (sameDocument(entry.text, content)) {
|
||||
clearDraftBackup(sessionId, path)
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createElement } from 'react'
|
||||
import { act } from 'react-dom/test-utils'
|
||||
import { undo } from '@codemirror/commands'
|
||||
import { EditorView } from '@codemirror/view'
|
||||
import { renderRoot, setupReactAct } from './test-utils.ts'
|
||||
import { SidebarApiError } from '../src/client/api.ts'
|
||||
@@ -185,6 +186,17 @@ describe('editor-draft-backup store', () => {
|
||||
expect(storedKeys()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('drops an entry that differs from the bytes on disk by line endings alone', () => {
|
||||
seedDraft('s1', '/p/a.ts', 'a\nb\n')
|
||||
expect(readDraftBackup('s1', '/p/a.ts', 'a\r\nb\r\n')).toBeNull()
|
||||
expect(storedKeys()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('still offers a draft of a CRLF file whose content really differs', () => {
|
||||
seedDraft('s1', '/p/a.ts', 'a\nchanged\n')
|
||||
expect(readDraftBackup('s1', '/p/a.ts', 'a\r\nb\r\n')).toBe('a\nchanged\n')
|
||||
})
|
||||
|
||||
it('stores the base mtime with the draft, and reads a malformed one as absent', () => {
|
||||
const release = registerDraftSource('s1', '/p/a.ts', () => 'typed', () => 1000)
|
||||
scheduleDraftBackup('s1', '/p/a.ts')
|
||||
@@ -233,6 +245,24 @@ describe('TextEditor draft restore', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('does not prompt for a CRLF file whose edit was undone', () => {
|
||||
const confirmSpy = vi.fn().mockReturnValue(true)
|
||||
vi.stubGlobal('confirm', confirmSpy)
|
||||
const crlf = viewerProps({ content: 'a\r\nb\r\n' })
|
||||
const edited = renderRoot(createElement(TextEditor, crlf))
|
||||
typeText(edited.container, 'x')
|
||||
act(() => { undo(viewOf(edited.container)) })
|
||||
edited.unmount()
|
||||
expect(storedKeys(), 'the closed tab left its LF copy behind').toHaveLength(1)
|
||||
const reopened = renderRoot(createElement(TextEditor, crlf))
|
||||
try {
|
||||
expect(confirmSpy).not.toHaveBeenCalled()
|
||||
expect(storedKeys()).toHaveLength(0)
|
||||
} finally {
|
||||
reopened.unmount()
|
||||
}
|
||||
})
|
||||
|
||||
it('never prompts when the file has no stored draft', () => {
|
||||
const confirmSpy = vi.fn().mockReturnValue(true)
|
||||
vi.stubGlobal('confirm', confirmSpy)
|
||||
|
||||
Reference in new issue
Block a user