Skip to content

AI-native design editor. Open-source Figma alternative.

fix(app): download a saved file when the browser will not write to it (#1184)

rcoenenf64656494f76
diff --git a/changes/1184/fixed.md b/changes/1184/fixed.md
new file mode 100644
index 000000000..46fd77537
--- /dev/null
+++ b/changes/1184/fixed.md
@@ -0,0 +1 @@
+- Saving a file in a browser that shows the save picker but will not write to the chosen file now downloads it under that name instead of failing.
diff --git a/src/app/document/io/capability.ts b/src/app/document/io/capability.ts
index d081521e7..57ef76d46 100644
--- a/src/app/document/io/capability.ts
+++ b/src/app/document/io/capability.ts
@@ -8,6 +8,17 @@ export function supportsFileSystemAccess(): boolean {
return IS_BROWSER && typeof window.showSaveFilePicker === 'function'
}
+/**
+ * Whether a write failed because the browser refuses to write to a file it handed out, as embedded
+ * browsers and sandboxed frames do: the picker works but `createWritable` throws. Callers download
+ * the file instead.
+ */
+export function isFileWriteRefused(error: unknown): boolean {
+ return (
+ error instanceof Error && (error.name === 'NotAllowedError' || error.name === 'SecurityError')
+ )
+}
+
/**
* Opens the browser save picker. Resolves to null when the browser has no picker, and rejects
* with `AbortError` when the user cancels so callers can keep cancel and failure distinct.
diff --git a/src/app/document/io/save/actions.ts b/src/app/document/io/save/actions.ts
index 0d669c6ce..b16c93ff8 100644
--- a/src/app/document/io/save/actions.ts
+++ b/src/app/document/io/save/actions.ts
@@ -2,6 +2,7 @@ import type { EditorState } from '@open-pencil/core/editor'
import { filesMessages } from '@open-pencil/vue'
import { downloadBlob } from '@/app/document/io/browser'
+import { isFileWriteRefused } from '@/app/document/io/capability'
import { documentNameFromFigPath } from '@/app/document/io/names'
import { chooseBrowserFigSaveHandle, chooseTauriFigSavePath } from '@/app/document/io/save/targets'
import type { DocumentSourceAccess } from '@/app/document/io/types'
@@ -97,14 +98,27 @@ export function createSaveActions({
setFileHandle(handle)
setFilePath(null)
state.documentName = documentNameFromFigPath(handle.name)
- const wrote = await writeFile(data, version)
- if (wrote) setSourceIdentity({ handle, path: null })
- startWatchingFile()
- return wrote
+ try {
+ const wrote = await writeFile(data, version)
+ if (wrote) setSourceIdentity({ handle, path: null })
+ startWatchingFile()
+ return wrote
+ } catch (error) {
+ if (!isFileWriteRefused(error)) throw error
+ // The browser gave out the file but will not write to it: download it under that name,
+ // and forget the file, so the tab is not taken for it.
+ setFileHandle(null)
+ setSourceIdentity({ handle: null, path: null })
+ return downloadAs(handle.name, data, version)
+ }
}
const filename = prompt(filesMessages.get().saveAsPrompt, getDownloadName() ?? 'Untitled.fig')
if (!filename) return false
+ return downloadAs(filename, data, version)
+ }
+
+ async function downloadAs(filename: string, data: Uint8Array, version: number) {
setStorageBinding(null)
setDownloadName(filename)
state.documentName = documentNameFromFigPath(filename)
diff --git a/tests/engine/app/document/io/source-identity.test.ts b/tests/engine/app/document/io/source-identity.test.ts
index 2b59f5fdd..acc1af8a8 100644
--- a/tests/engine/app/document/io/source-identity.test.ts
+++ b/tests/engine/app/document/io/source-identity.test.ts
@@ -1,8 +1,10 @@
-import { describe, expect, test, vi } from 'bun:test'
+import { afterEach, describe, expect, test, vi } from 'bun:test'
import { createDefaultEditorState } from '@open-pencil/core/editor'
+import * as browserIO from '@/app/document/io/browser'
import { createSaveActions } from '@/app/document/io/save/actions'
+import * as saveTargets from '@/app/document/io/save/targets'
import { createDocumentSourceState } from '@/app/document/io/source/state'
import { asDouble } from '#tests/helpers/doubles'
@@ -44,6 +46,10 @@ function createSaveHarness(handle: FileSystemFileHandle) {
return { actions, setSourceIdentity }
}
+afterEach(() => {
+ vi.restoreAllMocks()
+})
+
describe('saved document identity', () => {
test('tracks storage binding alongside local source identity', () => {
const source = createDocumentSourceState()
@@ -57,6 +63,28 @@ describe('saved document identity', () => {
})
})
+ test('a file the browser will not write to is downloaded under its name and forgotten', async () => {
+ // An embedded browser hands out the picked file, then refuses to write to it.
+ const refused = asDouble<FileSystemFileHandle>({
+ kind: 'file',
+ name: 'chosen.fig',
+ createWritable: vi.fn(async () => {
+ throw new DOMException('Not allowed in this context', 'NotAllowedError')
+ })
+ })
+ vi.spyOn(saveTargets, 'chooseBrowserFigSaveHandle').mockResolvedValue(refused)
+ const download = vi.spyOn(browserIO, 'downloadBlob').mockReturnValue(undefined)
+ const { actions, setSourceIdentity } = createSaveHarness(refused)
+
+ expect(await actions.saveFigFileAs()).toBe(true)
+ expect(download).toHaveBeenCalledWith(
+ expect.any(Uint8Array),
+ 'chosen.fig',
+ 'application/octet-stream'
+ )
+ expect(setSourceIdentity).toHaveBeenLastCalledWith({ handle: null, path: null })
+ })
+
test('publishes the writable handle after a successful save', async () => {
const handle = makeWritableHandle('saved.fig')
const { actions, setSourceIdentity } = createSaveHarness(handle)