This commit is contained in:
commit
5ce98edcd0
5 files changed
+124
-5
No files matched your search
@@ -478,7 +478,7 @@ export function EditorHost(props: {
|
||||
</div>
|
||||
)}
|
||||
{toolbar?.dirty === true && <span className={css.dirtyDot} title={t('unsaved')} />}
|
||||
{toolbar?.editable === true && (
|
||||
{toolbar?.editable === true && toolbar?.truncated !== true && (
|
||||
<button
|
||||
type="button"
|
||||
className={css.iconButton}
|
||||
|
||||
@@ -144,6 +144,11 @@ export function TextEditor(props: FileViewerProps) {
|
||||
CodeMirrorView.contentAttributes.of({ spellcheck: 'false' }),
|
||||
cmSurfaceTheme,
|
||||
themeComp.of(dark),
|
||||
// A truncated read only carries the first readLimit bytes; edits made
|
||||
// on partial content must never be saved over the full file (issue
|
||||
// #732), so the document is rendered read-only until a full read
|
||||
// replaces it.
|
||||
...(truncated === true ? [EditorState.readOnly.of(true)] : []),
|
||||
...(language !== null ? [language] : []),
|
||||
CodeMirrorView.updateListener.of((update) => {
|
||||
if (update.docChanged) {
|
||||
@@ -215,7 +220,7 @@ export function TextEditor(props: FileViewerProps) {
|
||||
// tab's lifetime, and the dark flip is handled by the reconfigure
|
||||
// effect below (recreating the view here would drop the draft).
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [content, path])
|
||||
}, [content, path, truncated])
|
||||
|
||||
// Scheme flip: re-theme in place (the compartment holds only the
|
||||
// scheme-dependent extensions; everything else is untouched).
|
||||
@@ -290,6 +295,11 @@ export function TextEditor(props: FileViewerProps) {
|
||||
const save = (): void => {
|
||||
const view = viewRef.current
|
||||
if (view === null || savingRef.current) return
|
||||
// Defense in depth for issue #732: a truncated read only holds the first
|
||||
// readLimit bytes, and fs.write replaces the whole file — saving partial
|
||||
// content would destroy the tail. The readOnly extension already blocks
|
||||
// edits; this guards the keymap path against future trigger points.
|
||||
if (truncated === true) return
|
||||
savingRef.current = true
|
||||
setSaveState('saving')
|
||||
api.fsWrite(scope, path, view.state.doc.toString()).then(() => {
|
||||
@@ -422,7 +432,7 @@ export function TextEditor(props: FileViewerProps) {
|
||||
const lastToolbarRef = useRef('')
|
||||
useEffect(() => {
|
||||
if (!hostToolbar) return
|
||||
const state: EditorToolbarState = { modes: markdown || html, mode, dirty, editable, saveState }
|
||||
const state: EditorToolbarState = { modes: markdown || html, mode, dirty, editable, truncated: truncated === true, saveState }
|
||||
const key = JSON.stringify(state)
|
||||
if (lastToolbarRef.current === key) return
|
||||
lastToolbarRef.current = key
|
||||
@@ -460,7 +470,7 @@ export function TextEditor(props: FileViewerProps) {
|
||||
</div>
|
||||
)}
|
||||
{dirty && <span className={css.dirtyDot} title={t('unsaved')} />}
|
||||
{editable && (
|
||||
{editable && truncated !== true && (
|
||||
<button
|
||||
type="button"
|
||||
className={css.iconButton}
|
||||
@@ -476,7 +486,7 @@ export function TextEditor(props: FileViewerProps) {
|
||||
)}
|
||||
{editable && (
|
||||
<>
|
||||
{truncated === true && mode === 'edit' && <div className={css.editorBanner}>{t('truncation')}</div>}
|
||||
{truncated === true && <div className={css.editorBanner}>{t('truncation')}</div>}
|
||||
<div
|
||||
className={clsx(css.editorCm, (markdown || html) && mode === 'preview' && css.editorCmHidden)}
|
||||
ref={hostRef}
|
||||
|
||||
@@ -296,6 +296,10 @@ export interface EditorToolbarState {
|
||||
dirty: boolean
|
||||
/** Whether saving applies (text content loaded). */
|
||||
editable: boolean
|
||||
/** Whether the loaded text was truncated at readLimit — the editor is
|
||||
* read-only and saving is disabled (issue #732: saving partial content
|
||||
* would overwrite and destroy the tail of the file). */
|
||||
truncated?: boolean
|
||||
saveState: 'idle' | 'saving' | 'saved' | 'failed'
|
||||
}
|
||||
|
||||
|
||||
@@ -369,6 +369,48 @@ describe('EditorHost (files window)', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('the header hides the save button for a truncated load (#732)', () => {
|
||||
const { store, ctx } = setup()
|
||||
const service = ctx.betterSidebar
|
||||
// The TextEditor contract reports `truncated` alongside `editable`; the
|
||||
// host's merged header must not offer save while only partial content
|
||||
// is loaded (fs.write would replace the whole file with the prefix).
|
||||
let report: ((truncated: boolean) => void) | undefined
|
||||
const FakeViewer = (viewerProps: FileViewerProps): ReactNode => {
|
||||
useEffect(() => {
|
||||
viewerProps.onToolbarControls?.({ setMode: () => {}, save: () => {} })
|
||||
viewerProps.onToolbarState?.({ modes: true, mode: 'preview', dirty: false, editable: true, truncated: true, saveState: 'idle' })
|
||||
report = (truncated) => {
|
||||
viewerProps.onToolbarState?.({ modes: true, mode: 'preview', dirty: false, editable: true, truncated, saveState: 'idle' })
|
||||
}
|
||||
return () => { viewerProps.onToolbarControls?.(null) }
|
||||
// Mount-only: re-running would re-fire the toolbar registration.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [])
|
||||
return null
|
||||
}
|
||||
service.registerFileViewer({
|
||||
id: 'test:fake',
|
||||
exts: ['fake'],
|
||||
fetchStrategy: 'none',
|
||||
component: FakeViewer,
|
||||
})
|
||||
service.openTab({ type: 'editor', title: 'big.fake', path: '/tmp/big.fake', id: 'editor:/tmp/big.fake' })
|
||||
const fileTab = (): SidebarTab =>
|
||||
allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs)
|
||||
.find(tab => tab.path === '/tmp/big.fake')!
|
||||
const { container, unmount } = mountHost(ctx, store, fileTab)
|
||||
try {
|
||||
const header = container.querySelector('input')!.parentElement!
|
||||
expect(header.querySelector('button[aria-label="Save"]')).toBeNull()
|
||||
// A full read replacing the content re-enables saving.
|
||||
act(() => { report!(false) })
|
||||
expect(header.querySelector('button[aria-label="Save"]')).not.toBeNull()
|
||||
} finally {
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
|
||||
it('a folder tab (meta.dir) renders the tree rooted at the folder, no editor chrome', () => {
|
||||
const { store, ctx } = setup()
|
||||
ctx.betterSidebar!.openTab({
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Truncated-read save protection (#732): a text load truncated at readLimit
|
||||
* only carries the file's first bytes, so the editor must not offer a save
|
||||
* surface for it — the truncation banner shows in every mode and the save
|
||||
* button stays out of the editor's own toolbar until a full read replaces
|
||||
* the content.
|
||||
*/
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { createElement } from 'react'
|
||||
import { renderToString } from 'react-dom/server'
|
||||
import './browser-globals.ts'
|
||||
import { TextEditor } from '../src/client/TextEditor.tsx'
|
||||
import { createSidebarStore } from '../src/client/state.ts'
|
||||
import { attachLocale } from '../src/client/locales.ts'
|
||||
import type { FileViewerProps } from '../src/client/service.ts'
|
||||
|
||||
/** Minimal structural fake of the DSH LocaleService face the sidebar uses. */
|
||||
class FakeLocale {
|
||||
active: string = 'en'
|
||||
getSnapshot(): { active: string } {
|
||||
return { active: this.active }
|
||||
}
|
||||
subscribe(_fn: () => void): () => void {
|
||||
return () => {}
|
||||
}
|
||||
register(_ns: string, _locale: string, _dict: Record<string, string>): () => void {
|
||||
return () => {}
|
||||
}
|
||||
}
|
||||
|
||||
const CTX = {} as Parameters<typeof TextEditor>[0]['ctx']
|
||||
|
||||
function viewerProps(overrides: Partial<FileViewerProps> = {}): FileViewerProps {
|
||||
return {
|
||||
ctx: CTX,
|
||||
store: createSidebarStore(),
|
||||
scope: { sessionId: 's1', cwd: '/p' },
|
||||
path: '/p/big.json',
|
||||
title: 'big.json',
|
||||
viewerId: 'code',
|
||||
content: '{"head": "…"}',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
attachLocale(undefined)
|
||||
})
|
||||
|
||||
describe('TextEditor truncated-read save protection (#732)', () => {
|
||||
it('shows the truncation banner and no save button for a truncated load', () => {
|
||||
attachLocale(new FakeLocale())
|
||||
const html = renderToString(createElement(TextEditor, viewerProps({ truncated: true })))
|
||||
expect(html).toContain('File too large')
|
||||
expect(html).not.toContain('aria-label="Save"')
|
||||
})
|
||||
|
||||
it('keeps the save button for a full (non-truncated) load', () => {
|
||||
attachLocale(new FakeLocale())
|
||||
const html = renderToString(createElement(TextEditor, viewerProps()))
|
||||
expect(html).toContain('aria-label="Save"')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user