From 5981c7f7a3fe1364bc07d7191e7882f2de8ea08c Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Wed, 28 Oct 2026 22:06:24 +0800 Subject: [PATCH] refactor: share the editor's draft-discard confirm fallback refreshFile and the in-place file switch used the same three-line no-confirm fallback. Extract it once and keep the fallback semantics unchanged; the comment now says the switch reads the same dirty registry as the close guard, not that the fallback is identical. --- src/client/EditorHost.tsx | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/client/EditorHost.tsx b/src/client/EditorHost.tsx index 588d329c..08ba856d 100644 --- a/src/client/EditorHost.tsx +++ b/src/client/EditorHost.tsx @@ -113,6 +113,14 @@ function accepted(pending: Promise): Promise { ) } +/** Ask before a reload/switch drops a draft. A host without confirm blocks + * the action: refusing is recoverable, while silently discarding is not. + * (The tab-close guard intentionally proceeds instead — an unclosable tab + * is worse than one skipped prompt.) */ +function confirmDiscard(message: string): boolean { + return typeof window.confirm === 'function' ? window.confirm(message) : false +} + export function EditorHost(props: { ctx: Context store: SidebarStore @@ -162,12 +170,7 @@ export function EditorHost(props: { // Manual refresh (issue #167 + PR #228): a dirty draft is dropped by the // reload (the editor instance remounts), so confirm before discarding it. const refreshFile = (): void => { - if (toolbar?.dirty === true) { - const confirmed = typeof window.confirm === 'function' - ? window.confirm(t('refreshUnsavedConfirm')) - : false - if (!confirmed) return - } + if (toolbar?.dirty === true && !confirmDiscard(t('refreshUnsavedConfirm'))) return setReloadSeq(sequence => sequence + 1) } @@ -235,14 +238,11 @@ export function EditorHost(props: { // keeps its draft, so there is nothing to confirm. if (openClaimedNativeFile(ctx, scope.sessionId, scope.cwd, absolute)) return // An in-place switch remounts the viewer, and the draft lives only in - // that instance — ask before dropping it, the same guard the tab close - // uses. Re-opening the current path is not a switch. - if (absolute !== path && isEditorDirty(tab.id, scope.sessionId)) { - const confirmed = typeof window.confirm === 'function' - ? window.confirm(t('closeUnsavedConfirm')) - : false - if (!confirmed) return - } + // that instance — ask before dropping it, reading the same dirty + // registry the tab close uses. Re-opening the current path is not a + // switch. + if (absolute !== path && isEditorDirty(tab.id, scope.sessionId) + && !confirmDiscard(t('closeUnsavedConfirm'))) return ctx.get('betterSidebar')?.updateTab(tab.id, { path: absolute, title: baseName(absolute) }, scope.sessionId) } else { openSidebarFile(ctx, scope.sessionId, absolute, { cwd: scope.cwd })