fix: preserve focused editor after pulls

This commit is contained in:
lucaronin
2026-05-14 05:53:37 +02:00
parent 6bb9c7fbc9
commit 2eae7a0899
3 changed files with 35 additions and 6 deletions

View File

@@ -15,6 +15,13 @@ interface PulledVaultRefreshOptions {
vaultPath: string
}
interface ActiveTabRefreshGuardOptions {
activeTabPath: string
latestActiveTabPath: string
hasUnsavedChanges: (path: string) => boolean
shouldKeepActiveEditorMounted?: () => boolean
}
function resolveUpdatedFilePath(path: string, vaultPath: string): string {
if (path.startsWith('/')) return normalizeNotePathForIdentity(path)
return normalizeNotePathForIdentity(joinVaultPath(vaultPath, path))
@@ -28,6 +35,21 @@ function didActivePathChange(initialPath: string, latestPath: string): boolean {
return !notePathsMatch(initialPath, latestPath)
}
function shouldKeepCurrentActiveTabMounted({
activeTabPath,
latestActiveTabPath,
hasUnsavedChanges,
shouldKeepActiveEditorMounted,
}: ActiveTabRefreshGuardOptions): boolean {
if (didActivePathChange(activeTabPath, latestActiveTabPath)) return true
if (hasUnsavedChanges(latestActiveTabPath)) return true
return shouldKeepActiveEditorMounted?.() === true
}
export function getPulledVaultUpdateOptions(): { preserveFocusedEditor: true } {
return { preserveFocusedEditor: true }
}
export async function refreshPulledVaultState(options: PulledVaultRefreshOptions): Promise<VaultEntry[]> {
const {
activeTabPath,
@@ -51,9 +73,12 @@ export async function refreshPulledVaultState(options: PulledVaultRefreshOptions
const latestActiveTabPath = getActiveTabPath?.() ?? activeTabPath
if (!activeTabPath || !latestActiveTabPath) return entries
if (didActivePathChange(activeTabPath, latestActiveTabPath)) return entries
if (hasUnsavedChanges(latestActiveTabPath)) return entries
if (shouldKeepActiveEditorMounted?.()) return entries
if (shouldKeepCurrentActiveTabMounted({
activeTabPath,
latestActiveTabPath,
hasUnsavedChanges,
shouldKeepActiveEditorMounted,
})) return entries
const refreshedEntry = findByNotePath(entries, latestActiveTabPath)
if (!refreshedEntry) {