Skip to content

Allow saving back to the opened file on web, and add Save As to the web File menu - #5001

Open
TTyChud wants to merge 1 commit into
GraphiteEditor:masterfrom
TTyChud:web-save-file-handles
Open

TTyChud wants to merge 1 commit into
GraphiteEditor:masterfrom
TTyChud:web-save-file-handles

Conversation

@TTyChud

@TTyChud TTyChud commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Part of #747

I used the File System Access API so Open and Save on web work with the actual file on disk, and Ctrl+S overwrites the file you opened.

I haven't added drag and drop support, and I didn't write our own file picker. It just uses the browser's one for now.

@TTyChud TTyChud changed the title Save back to the opened file on web Allow saving back to the opened file on web, and add Save As to the web File menu Nov 22, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 issues found across 9 files

Confidence score: 3/5

  • In portfolio.ts and document_message_handler.rs, the editor can show a document as saved even though the file on disk doesn’t contain those edits. Only mark the exact content written to the target file as saved.
  • In persistence.ts, a failed prune write can stop state restoration, and a stale snapshot can delete a handle added by another tab. Keep cleanup from blocking restore and reconcile against the latest handle map.
  • In portfolio.ts, opening the save picker after export may outlast user activation and send the save to a download instead of the opened file. Open the picker before the asynchronous export work.
  • In portfolio.ts, denied write permission returns without telling the user that the save failed. Show an error before returning.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="frontend/src/stores/portfolio.ts">

<violation number="1" location="frontend/src/stores/portfolio.ts:116">
P2: The save picker runs only after resource embedding and document export, so a slow save can outlast the required user activation and fall back to a download instead of saving to the opened file. Open the picker during the original Save gesture, before awaiting export, or provide an explicit retry.</violation>

<violation number="2" location="frontend/src/stores/portfolio.ts:117">
P2: When write permission is denied, this returns without any feedback, so Ctrl+S appears to do nothing. Show an error dialog (for example via `editor.errorDialog`) before returning, so the user knows the file was not saved.</violation>

<violation number="3" location="frontend/src/stores/portfolio.ts:127">
P2: If writing an existing handle throws, this fallback downloads a separate copy but confirms the save with the original handle path; the editor then marks the unchanged disk file as saved. Clear the path or avoid confirming the save on this fallback.</violation>
</file>

<file name="frontend/src/utility-functions/persistence.ts">

<violation number="1" location="frontend/src/utility-functions/persistence.ts:68">
P2: The startup prune is awaited before `loadPersistedState`, so a failure in this cleanup write stops the whole session from restoring. Run it without blocking the restore, and catch its errors.</violation>

<violation number="2" location="frontend/src/utility-functions/persistence.ts:68">
P2: This prune uses a document snapshot read separately from the handle-map update, so another tab can store a handle for a newly opened file before its path reaches persisted state and have this call delete it. The document then retains a path that Ctrl+S cannot resolve; prune against the latest state in the same transaction or otherwise coordinate the updates.</violation>
</file>

<file name="editor/src/messages/portfolio/document/document_message_handler.rs">

<violation number="1" location="editor/src/messages/portfolio/document/document_message_handler.rs:1278">
P2: On web, `MarkAsSaved` now runs only after the frontend write, but it records the hash of the document at that later point. Edits made during the export or the pending write are marked saved although the file lacks them, so the unsaved indicator and close warning are lost. Record the hash of the written snapshot when the save is dispatched, and use that hash when `SavedDocument` arrives.</violation>
</file>

Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack

Re-trigger cubic

if (fileHandlesSupported()) {
const existing = path ? await fileHandleGet(path) : undefined;
const handle = existing ?? (await saveFileHandle(name, filters));
if (!handle || !(await writeFileHandle(handle, content))) return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When write permission is denied, this returns without any feedback, so Ctrl+S appears to do nothing. Show an error dialog (for example via editor.errorDialog) before returning, so the user knows the file was not saved.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/src/stores/portfolio.ts, line 117:

<comment>When write permission is denied, this returns without any feedback, so Ctrl+S appears to do nothing. Show an error dialog (for example via `editor.errorDialog`) before returning, so the user knows the file was not saved.</comment>

<file context>
@@ -92,13 +94,37 @@ export function createPortfolioStore(subscriptions: SubscriptionsRouter, editor:
+			if (fileHandlesSupported()) {
+				const existing = path ? await fileHandleGet(path) : undefined;
+				const handle = existing ?? (await saveFileHandle(name, filters));
+				if (!handle || !(await writeFileHandle(handle, content))) return;
+
+				editor.savedDocument(documentId, existing ? path : await fileHandleStore(handle));
</file context>

await migrateDocumentsToStore();

const state = await databaseGet<PersistedState>("state");
await fileHandlesPrune(state?.documents ?? []);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The startup prune is awaited before loadPersistedState, so a failure in this cleanup write stops the whole session from restoring. Run it without blocking the restore, and catch its errors.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/src/utility-functions/persistence.ts, line 68:

<comment>The startup prune is awaited before `loadPersistedState`, so a failure in this cleanup write stops the whole session from restoring. Run it without blocking the restore, and catch its errors.</comment>

<file context>
@@ -65,6 +65,7 @@ export async function readPersistedState(editor: EditorWrapper) {
 	await migrateDocumentsToStore();
 
 	const state = await databaseGet<PersistedState>("state");
+	await fileHandlesPrune(state?.documents ?? []);
 	editor.loadPersistedState(state ?? emptyPersistedState());
 }
</file context>
Suggested change
await fileHandlesPrune(state?.documents ?? []);
void fileHandlesPrune(state?.documents ?? []).catch(() => undefined);

};
if path.is_some() || environment.is_web() {
// On web, the frontend confirms with `SavedDocument` once the file is written
if path.is_some() && !environment.is_web() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: On web, MarkAsSaved now runs only after the frontend write, but it records the hash of the document at that later point. Edits made during the export or the pending write are marked saved although the file lacks them, so the unsaved indicator and close warning are lost. Record the hash of the written snapshot when the save is dispatched, and use that hash when SavedDocument arrives.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At editor/src/messages/portfolio/document/document_message_handler.rs, line 1278:

<comment>On web, `MarkAsSaved` now runs only after the frontend write, but it records the hash of the document at that later point. Edits made during the export or the pending write are marked saved although the file lacks them, so the unsaved indicator and close warning are lost. Record the hash of the written snapshot when the save is dispatched, and use that hash when `SavedDocument` arrives.</comment>

<file context>
@@ -1267,12 +1267,15 @@ impl MessageHandler<DocumentMessage, DocumentMessageContext<'_>> for DocumentMes
 				};
-				if path.is_some() || environment.is_web() {
+				// On web, the frontend confirms with `SavedDocument` once the file is written
+				if path.is_some() && !environment.is_web() {
 					responses.add(DocumentMessage::MarkAsSaved);
 				}
</file context>

await migrateDocumentsToStore();

const state = await databaseGet<PersistedState>("state");
await fileHandlesPrune(state?.documents ?? []);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This prune uses a document snapshot read separately from the handle-map update, so another tab can store a handle for a newly opened file before its path reaches persisted state and have this call delete it. The document then retains a path that Ctrl+S cannot resolve; prune against the latest state in the same transaction or otherwise coordinate the updates.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/src/utility-functions/persistence.ts, line 68:

<comment>This prune uses a document snapshot read separately from the handle-map update, so another tab can store a handle for a newly opened file before its path reaches persisted state and have this call delete it. The document then retains a path that Ctrl+S cannot resolve; prune against the latest state in the same transaction or otherwise coordinate the updates.</comment>

<file context>
@@ -65,6 +65,7 @@ export async function readPersistedState(editor: EditorWrapper) {
 	await migrateDocumentsToStore();
 
 	const state = await databaseGet<PersistedState>("state");
+	await fileHandlesPrune(state?.documents ?? []);
 	editor.loadPersistedState(state ?? emptyPersistedState());
 }
</file context>

try {
if (fileHandlesSupported()) {
const existing = path ? await fileHandleGet(path) : undefined;
const handle = existing ?? (await saveFileHandle(name, filters));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The save picker runs only after resource embedding and document export, so a slow save can outlast the required user activation and fall back to a download instead of saving to the opened file. Open the picker during the original Save gesture, before awaiting export, or provide an explicit retry.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/src/stores/portfolio.ts, line 116:

<comment>The save picker runs only after resource embedding and document export, so a slow save can outlast the required user activation and fall back to a download instead of saving to the opened file. Open the picker during the original Save gesture, before awaiting export, or provide an explicit retry.</comment>

<file context>
@@ -92,13 +94,37 @@ export function createPortfolioStore(subscriptions: SubscriptionsRouter, editor:
+		try {
+			if (fileHandlesSupported()) {
+				const existing = path ? await fileHandleGet(path) : undefined;
+				const handle = existing ?? (await saveFileHandle(name, filters));
+				if (!handle || !(await writeFileHandle(handle, content))) return;
+
</file context>

}

downloadFile(name, content);
editor.savedDocument(documentId, path);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: If writing an existing handle throws, this fallback downloads a separate copy but confirms the save with the original handle path; the editor then marks the unchanged disk file as saved. Clear the path or avoid confirming the save on this fallback.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/src/stores/portfolio.ts, line 127:

<comment>If writing an existing handle throws, this fallback downloads a separate copy but confirms the save with the original handle path; the editor then marks the unchanged disk file as saved. Clear the path or avoid confirming the save on this fallback.</comment>

<file context>
@@ -92,13 +94,37 @@ export function createPortfolioStore(subscriptions: SubscriptionsRouter, editor:
+		}
+
+		downloadFile(name, content);
+		editor.savedDocument(documentId, path);
 	});
 
</file context>

@TTyChud
TTyChud force-pushed the web-save-file-handles branch from 65c4ff0 to 4244a73 Compare November 22, 2026 15:52

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant