Sync snapshot (2026-08-06) (#38)
- Movable, resizable floating objects and a shared 104-preset shape gallery across Docs, Sheets, and Slides - Sheets Insert menu: screenshot capture, recommended charts, and an icon library - Slides: Home slideshow module and slideshow-exit fixes - Docs: track-changes fixes (IME input, tracked-deletion re-deletion) and Word-parity font handling - Redact secret-looking tokens from outgoing AI messages (community contribution from PR #32, hardened with narrower patterns and tests) - Linux packaging: AppImage and deb download instructions Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
This commit is contained in:
co-authored by
GenOffice
parent
c3911b02b6
commit
df358f2775
@@ -0,0 +1,70 @@
|
||||
/// Electron 43 pins file dialogs without an explicit `defaultPath` to the
|
||||
/// user's Downloads folder and no longer lets the OS restore the last-used
|
||||
/// directory between invocations (electron/electron#49868). These wrappers
|
||||
/// restore the pre-43 behavior the way the Electron breaking-changes guide
|
||||
/// recommends: remember the directory of the last confirmed pick and thread
|
||||
/// it into the next dialog's `defaultPath`.
|
||||
import { basename, dirname, join } from 'node:path'
|
||||
|
||||
import type {
|
||||
BrowserWindow,
|
||||
Dialog,
|
||||
OpenDialogOptions,
|
||||
OpenDialogReturnValue,
|
||||
SaveDialogOptions,
|
||||
SaveDialogReturnValue,
|
||||
} from 'electron'
|
||||
|
||||
// Keyed by the dialog module so tests with fake dialogs stay isolated. Each
|
||||
// app bundles its own copy of this module, so the memory is scoped per
|
||||
// editor — close enough to the per-app directory tracking the OS did before.
|
||||
const lastUsedDirectoryByDialog = new WeakMap<Dialog, string>()
|
||||
|
||||
function withRememberedDirectory<T extends OpenDialogOptions | SaveDialogOptions>(
|
||||
dialog: Dialog,
|
||||
options: T,
|
||||
): T {
|
||||
const lastDir = lastUsedDirectoryByDialog.get(dialog)
|
||||
if (!lastDir) return options
|
||||
if (options.defaultPath === undefined) return { ...options, defaultPath: lastDir }
|
||||
// A bare file name is a Save As suggestion: anchor it in the remembered
|
||||
// directory instead of letting Electron relocate it to Downloads.
|
||||
if (basename(options.defaultPath) === options.defaultPath) {
|
||||
return { ...options, defaultPath: join(lastDir, options.defaultPath) }
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
export async function showOpenDialogWithMemory(
|
||||
dialog: Dialog,
|
||||
parent: BrowserWindow | null | undefined,
|
||||
options: OpenDialogOptions,
|
||||
): Promise<OpenDialogReturnValue> {
|
||||
const withDir = withRememberedDirectory(dialog, options)
|
||||
const result = parent
|
||||
? await dialog.showOpenDialog(parent, withDir)
|
||||
: await dialog.showOpenDialog(withDir)
|
||||
const picked = result.filePaths[0]
|
||||
if (!result.canceled && picked) {
|
||||
lastUsedDirectoryByDialog.set(
|
||||
dialog,
|
||||
options.properties?.includes('openDirectory') ? picked : dirname(picked),
|
||||
)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export async function showSaveDialogWithMemory(
|
||||
dialog: Dialog,
|
||||
parent: BrowserWindow | null | undefined,
|
||||
options: SaveDialogOptions,
|
||||
): Promise<SaveDialogReturnValue> {
|
||||
const withDir = withRememberedDirectory(dialog, options)
|
||||
const result = parent
|
||||
? await dialog.showSaveDialog(parent, withDir)
|
||||
: await dialog.showSaveDialog(withDir)
|
||||
if (!result.canceled && result.filePath) {
|
||||
lastUsedDirectoryByDialog.set(dialog, dirname(result.filePath))
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export {
|
||||
windowMenuTemplate,
|
||||
type AppMenuLabels,
|
||||
} from './app-menu'
|
||||
export { showOpenDialogWithMemory, showSaveDialogWithMemory } from './dialog-memory'
|
||||
export { installNavigationGuard } from './navigation-guard'
|
||||
export { safeExternalUrl, type SafeExternalUrlOptions } from './safe-external-url'
|
||||
export {
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { join } from 'node:path'
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { showOpenDialogWithMemory, showSaveDialogWithMemory } from '../src/index'
|
||||
|
||||
import type { Dialog } from 'electron'
|
||||
|
||||
function fakeDialog(overrides: Partial<Dialog> = {}): Dialog {
|
||||
return {
|
||||
showOpenDialog: vi.fn().mockResolvedValue({ canceled: true, filePaths: [] }),
|
||||
showSaveDialog: vi.fn().mockResolvedValue({ canceled: true, filePath: '' }),
|
||||
...overrides,
|
||||
} as unknown as Dialog
|
||||
}
|
||||
|
||||
const pickedOpen = (paths: string[]) =>
|
||||
vi.fn().mockResolvedValue({ canceled: false, filePaths: paths })
|
||||
const pickedSave = (path: string) => vi.fn().mockResolvedValue({ canceled: false, filePath: path })
|
||||
|
||||
describe('showOpenDialogWithMemory', () => {
|
||||
it('passes options through unchanged before any pick was made', async () => {
|
||||
const dialog = fakeDialog()
|
||||
await showOpenDialogWithMemory(dialog, undefined, { properties: ['openFile'] })
|
||||
expect(dialog.showOpenDialog).toHaveBeenCalledWith({ properties: ['openFile'] })
|
||||
})
|
||||
|
||||
it('remembers the picked directory and injects it as defaultPath next time', async () => {
|
||||
const dialog = fakeDialog({ showOpenDialog: pickedOpen([join('/work', 'report.docx')]) })
|
||||
await showOpenDialogWithMemory(dialog, undefined, {})
|
||||
await showOpenDialogWithMemory(dialog, undefined, {})
|
||||
expect(dialog.showOpenDialog).toHaveBeenLastCalledWith({ defaultPath: '/work' })
|
||||
})
|
||||
|
||||
it('forwards the parent window when given', async () => {
|
||||
const dialog = fakeDialog()
|
||||
const parent = { id: 1 } as never
|
||||
await showOpenDialogWithMemory(dialog, parent, {})
|
||||
expect(dialog.showOpenDialog).toHaveBeenCalledWith(parent, {})
|
||||
})
|
||||
|
||||
it('remembers the selected directory itself for openDirectory pickers', async () => {
|
||||
const dialog = fakeDialog({ showOpenDialog: pickedOpen(['/exports/images']) })
|
||||
await showOpenDialogWithMemory(dialog, undefined, { properties: ['openDirectory'] })
|
||||
await showOpenDialogWithMemory(dialog, undefined, {})
|
||||
expect(dialog.showOpenDialog).toHaveBeenLastCalledWith({ defaultPath: '/exports/images' })
|
||||
})
|
||||
|
||||
it('keeps an explicit absolute defaultPath untouched', async () => {
|
||||
const dialog = fakeDialog({ showOpenDialog: pickedOpen([join('/work', 'a.docx')]) })
|
||||
await showOpenDialogWithMemory(dialog, undefined, {})
|
||||
await showOpenDialogWithMemory(dialog, undefined, { defaultPath: '/elsewhere/b.docx' })
|
||||
expect(dialog.showOpenDialog).toHaveBeenLastCalledWith({ defaultPath: '/elsewhere/b.docx' })
|
||||
})
|
||||
|
||||
it('does not remember canceled picks', async () => {
|
||||
const dialog = fakeDialog()
|
||||
await showOpenDialogWithMemory(dialog, undefined, {})
|
||||
await showOpenDialogWithMemory(dialog, undefined, {})
|
||||
expect(dialog.showOpenDialog).toHaveBeenLastCalledWith({})
|
||||
})
|
||||
})
|
||||
|
||||
describe('showSaveDialogWithMemory', () => {
|
||||
it('anchors a bare file-name suggestion in the remembered directory', async () => {
|
||||
const dialog = fakeDialog({ showSaveDialog: pickedSave(join('/work', 'deck.pptx')) })
|
||||
await showSaveDialogWithMemory(dialog, undefined, { defaultPath: 'deck.pptx' })
|
||||
await showSaveDialogWithMemory(dialog, undefined, { defaultPath: 'deck2.pptx' })
|
||||
expect(dialog.showSaveDialog).toHaveBeenLastCalledWith({
|
||||
defaultPath: join('/work', 'deck2.pptx'),
|
||||
})
|
||||
})
|
||||
|
||||
it('shares the remembered directory between save and open dialogs', async () => {
|
||||
const dialog = fakeDialog({ showSaveDialog: pickedSave(join('/work', 'deck.pptx')) })
|
||||
await showSaveDialogWithMemory(dialog, undefined, { defaultPath: 'deck.pptx' })
|
||||
await showOpenDialogWithMemory(dialog, undefined, {})
|
||||
expect(dialog.showOpenDialog).toHaveBeenCalledWith({ defaultPath: '/work' })
|
||||
})
|
||||
|
||||
it('keeps an explicit absolute defaultPath untouched', async () => {
|
||||
const dialog = fakeDialog({ showSaveDialog: pickedSave(join('/work', 'a.pdf')) })
|
||||
await showSaveDialogWithMemory(dialog, undefined, { defaultPath: '/docs/tab.pdf' })
|
||||
expect(dialog.showSaveDialog).toHaveBeenCalledWith({ defaultPath: '/docs/tab.pdf' })
|
||||
})
|
||||
|
||||
it('isolates memory between different dialog instances', async () => {
|
||||
const first = fakeDialog({ showOpenDialog: pickedOpen([join('/work', 'a.docx')]) })
|
||||
const second = fakeDialog()
|
||||
await showOpenDialogWithMemory(first, undefined, {})
|
||||
await showOpenDialogWithMemory(second, undefined, {})
|
||||
expect(second.showOpenDialog).toHaveBeenCalledWith({})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user