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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user