AI-native design editor. Open-source Figma alternative.
fix: save a browser document opened from a link by download, and protect it (#1342)
Danila Poyarkovc2a4f480afd1
diff --git a/changes/1342/fixed.md b/changes/1342/fixed.mdnew file mode 100644index 000000000..2eb7c8cdf--- /dev/null+++ b/changes/1342/fixed.md@@ -0,0 +1 @@+- A document opened from a link in the browser saves with Cmd+S by download and is covered by crash recovery; it could not be saved before.diff --git a/src/app/document/io/create.ts b/src/app/document/io/create.tsindex d4fd3f510..bed895dce 100644--- a/src/app/document/io/create.ts+++ b/src/app/document/io/create.ts@@ -76,11 +76,7 @@ export function createDocumentIOActions(getDocumentFilePath: sourceState.getFilePath,getSourceIdentity: sourceState.getSourceIdentity,getStorageBinding: sourceState.getStorageBinding,- /** Whether saving writes in place rather than asking for a location. */- hasWritableSource: () =>- !!sourceState.getFilePath() ||- !!sourceState.getFileHandle() ||- !!sourceState.getStorageBinding(),+ hasWritableSource: sourceState.hasWritableSource,getRecoveryId: sourceActions.getRecoveryId,adoptRecoverySnapshot: sourceActions.adoptRecoverySnapshot,persistRecoveryNow: sourceActions.persistRecoveryNow,diff --git a/src/app/document/io/save/actions.ts b/src/app/document/io/save/actions.tsindex b16c93ff8..241dcde64 100644--- a/src/app/document/io/save/actions.ts+++ b/src/app/document/io/save/actions.ts@@ -33,6 +33,7 @@ export function createSaveActions({setDownloadName,getStorageBinding,setStorageBinding,+ hasWritableSource,setSourceIdentity,setSavedVersion,setLastWriteTime,@@ -60,7 +61,7 @@ export function createSaveActions({const fileHandle = getFileHandle()const storageBinding = getStorageBinding()const downloadName = getDownloadName()- if (storageBinding || filePath || fileHandle) {+ if (hasWritableSource()) {const { data, version } = await buildVersionedFigFile()const wrote = await writeFile(data, version)if (wrote && !storageBinding) setSourceIdentity({ handle: fileHandle, path: filePath })diff --git a/src/app/document/io/source/actions.ts b/src/app/document/io/source/actions.tsindex 99bcd9eeb..19fe6fb78 100644--- a/src/app/document/io/source/actions.ts+++ b/src/app/document/io/source/actions.ts@@ -16,6 +16,7 @@ import { recoveryEnabled } from '@/app/document/recovery/preferences'import { storageLocationOf } from '@/app/integrations/storage/location'import type { StorageDocumentBinding, StorageLocation } from '@/app/integrations/storage/types'import { createCanvasId } from '@/app/storage/id'+import { isTauri } from '@/app/tauri/env'import { trackDocumentFormat } from '@/app/telemetry'type DocumentSourceState = EditorState & {@@ -46,6 +47,7 @@ export function createDocumentSourceActions({setDownloadName,getStorageBinding,setStorageBinding,+ hasWritableSource,setSourceIdentity,getSavedVersion,setSavedVersion,@@ -83,7 +85,7 @@ export function createDocumentSourceActions({version: changes.capture,isEnabled: () => recoveryEnabled.value,buildFigFile: buildRecoveryFigFile,- hasWritableSource: () => !!getFileHandle() || !!getFilePath() || !!getStorageBinding()+ hasWritableSource})const { saveFigFile, saveFigFileAs, writeFile } = createSaveActions({@@ -98,6 +100,7 @@ export function createDocumentSourceActions({setDownloadName,getStorageBinding,setStorageBinding,+ hasWritableSource,setSourceIdentity,setSavedVersion,setLastWriteTime,@@ -112,7 +115,7 @@ export function createDocumentSourceActions({state,version: changes.capture,getSavedVersion,- hasWritableSource: () => !!getFileHandle() || !!getFilePath() || !!getStorageBinding(),+ hasWritableSource,saveCurrentDocument: async (version) => {const revision = changes.capture()const data = await buildFigFile()@@ -207,6 +210,8 @@ export function createDocumentSourceActions({}async function saveFigFileToPath(path: string): Promise<boolean> {+ // Only the desktop app writes paths; a browser would fall back to a download.+ if (!isTauri()) return falseconst saved = await saveToNewTarget(() => setPlannedFilePath(path))if (saved) void startWatchingFile()return saveddiff --git a/src/app/document/io/source/state.ts b/src/app/document/io/source/state.tsindex 332da5c99..50c06d10a 100644--- a/src/app/document/io/source/state.ts+++ b/src/app/document/io/source/state.ts@@ -2,6 +2,7 @@ import { shallowRef } from 'vue'import type { DocumentSourceIdentity } from '@/app/document/io/types'import type { StorageDocumentBinding } from '@/app/integrations/storage/types'+import { isTauri } from '@/app/tauri/env'export function createDocumentSourceState() {// Reactive, so the File menu follows a document saved to a file mid-session.@@ -35,6 +36,12 @@ export function createDocumentSourceState() {setStorageBinding: (binding: StorageDocumentBinding | null) => {storageBinding.value = binding},+ /**+ * Whether saving writes in place: to storage, a file the browser granted, or a desktop file. A+ * browser document's path is the URL it was opened from, which nothing can write to.+ */+ hasWritableSource: () =>+ !!storageBinding.value || !!fileHandle.value || (!!filePath.value && isTauri()),getSavedVersion: () => savedVersion,setSavedVersion: (version: number) => {savedVersion = versiondiff --git a/src/app/document/io/types.ts b/src/app/document/io/types.tsindex f890dc649..35ea4532d 100644--- a/src/app/document/io/types.ts+++ b/src/app/document/io/types.ts@@ -14,6 +14,8 @@ export type DocumentSourceAccess = {setDownloadName: (name: string | null) => voidgetStorageBinding: () => StorageDocumentBinding | nullsetStorageBinding: (binding: StorageDocumentBinding | null) => void+ /** Whether saving writes in place rather than asking for a location. */+ hasWritableSource: () => booleansetSourceIdentity: (identity: DocumentSourceIdentity) => voidgetSavedVersion: () => numbersetSavedVersion: (version: number) => voiddiff --git a/tests/engine/app/document/io/source-identity.test.ts b/tests/engine/app/document/io/source-identity.test.tsindex acc1af8a8..b54f508f1 100644--- a/tests/engine/app/document/io/source-identity.test.ts+++ b/tests/engine/app/document/io/source-identity.test.ts@@ -38,6 +38,7 @@ function createSaveHarness(handle: FileSystemFileHandle) {setDownloadName: vi.fn(),getStorageBinding: () => null,setStorageBinding: vi.fn(),+ hasWritableSource: () => true,setSourceIdentity,setSavedVersion: vi.fn(),setLastWriteTime: vi.fn(),@@ -63,6 +64,26 @@ describe('saved document identity', () => {})})+ test('a browser document opened from a URL saves by download, not in place', () => {+ const source = createDocumentSourceState()+ source.setFilePath('https://example.com/design.fig')+ expect(source.hasWritableSource()).toBe(false)++ source.setFileHandle(makeWritableHandle('design.fig'))+ expect(source.hasWritableSource()).toBe(true)+ })++ test('a desktop document saves to its path in place', () => {+ const source = createDocumentSourceState()+ source.setFilePath('/Users/me/design.fig')+ Reflect.set(globalThis, 'window', { __TAURI_INTERNALS__: {} })+ try {+ expect(source.hasWritableSource()).toBe(true)+ } finally {+ Reflect.deleteProperty(globalThis, 'window')+ }+ })+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>({