From cd570d339b89d60b95471e2bfdbcb67c46c9f213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=B4=E7=8C=AB?= Date: Thu, 5 Nov 2026 18:24:52 +0900 Subject: [PATCH] =?UTF-8?q?fix(editor):=20=E5=A4=87=E4=BB=BD=E4=B8=8E?= =?UTF-8?q?=E7=A3=81=E7=9B=98=E6=AF=94=E8=BE=83=E6=97=B6=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E8=A1=8C=E5=B0=BE=EF=BC=8CCRLF=20=E6=96=87=E4=BB=B6=E6=94=B9?= =?UTF-8?q?=E5=AE=8C=E5=8F=88=E6=92=A4=E9=94=80=E4=B8=8D=E5=86=8D=E5=BC=B9?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 备份内容来自 CodeMirror,永远是 LF;readDraftBackup 用严格相等和磁盘原文比, CRLF 文件因此永远判为不同。把 TextEditor 的 sameDocument 挪到 editor-draft-backup.ts 导出,两边共用。只打开不编辑不写备份,不受影响。 --- src/client/TextEditor.tsx | 11 +---------- src/client/editor-draft-backup.ts | 14 ++++++++++++-- tests/editor-draft-backup.spec.tsx | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/client/TextEditor.tsx b/src/client/TextEditor.tsx index c392efe8..905e19c5 100644 --- a/src/client/TextEditor.tsx +++ b/src/client/TextEditor.tsx @@ -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('preview') diff --git a/src/client/editor-draft-backup.ts b/src/client/editor-draft-backup.ts index 212b0582..77962b82 100644 --- a/src/client/editor-draft-backup.ts +++ b/src/client/editor-draft-backup.ts @@ -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 } diff --git a/tests/editor-draft-backup.spec.tsx b/tests/editor-draft-backup.spec.tsx index 9ed29473..61505c30 100644 --- a/tests/editor-draft-backup.spec.tsx +++ b/tests/editor-draft-backup.spec.tsx @@ -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)