From d0a87d9626630e00fe96d9be790007b666640f03 Mon Sep 17 00:00:00 2001 From: Hsukqi Lee Date: Tue, 17 Nov 2026 02:08:50 +0800 Subject: [PATCH] fix: preserve project drafts when browser storage rejects a save --- src/components/workspace/ProjectDialog.vue | 4 ++- src/composables/useEditor.ts | 4 +-- tests/ui/project-save.mjs | 32 ++++++++++++++++++++++ tests/ui/run.mjs | 3 +- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 tests/ui/project-save.mjs diff --git a/src/components/workspace/ProjectDialog.vue b/src/components/workspace/ProjectDialog.vue index ac92fae..dc53e60 100644 --- a/src/components/workspace/ProjectDialog.vue +++ b/src/components/workspace/ProjectDialog.vue @@ -14,7 +14,9 @@ function close() { operation++; emit('close') } watch(() => props.open, open => { operation++; if (open) { title.value = editor.figure.value.title; error.value = '' } }) function save() { if (!title.value.trim()) { error.value = '请输入工程名称'; return } - editor.update(doc => { doc.title = title.value.trim() }); editor.save(); close() + editor.update(doc => { doc.title = title.value.trim() }) + if (!editor.save()) { error.value = editor.storageError.value; return } + close() } function loadExample() { operation++ diff --git a/src/composables/useEditor.ts b/src/composables/useEditor.ts index c6a31bb..7b85c62 100644 --- a/src/composables/useEditor.ts +++ b/src/composables/useEditor.ts @@ -438,8 +438,8 @@ export function createEditor() { past.value.push(clone(figure.value)); figure.value = next; future.value = future.value.slice(0, -1); selectedId.value = 'stage' } function save() { - try { localStorage.setItem(STORAGE_KEY, JSON.stringify(figure.value)); saved.value = true; storageError.value = ''; notice.value = '工程已保存到当前浏览器' } - catch { storageError.value = '本地保存失败,请导出工程文件保存。'; notice.value = storageError.value } + try { localStorage.setItem(STORAGE_KEY, JSON.stringify(figure.value)); saved.value = true; storageError.value = ''; notice.value = '工程已保存到当前浏览器'; return true } + catch { saved.value = false; storageError.value = '本地保存失败,请导出工程文件保存。'; notice.value = storageError.value; return false } } function importProject(text: string) { if (new TextEncoder().encode(text).length > maxProjectBytes) throw new Error('工程文件过大,限制为 8 MB') diff --git a/tests/ui/project-save.mjs b/tests/ui/project-save.mjs new file mode 100644 index 0000000..65108a1 --- /dev/null +++ b/tests/ui/project-save.mjs @@ -0,0 +1,32 @@ +import assert from 'node:assert/strict'; + +export async function checkProjectSave({ page }) { + await page.getByRole('button', { name: '工程', exact: true }).click(); + const dialog = page.getByRole('dialog', { name: '工程管理', exact: true }); + const title = 'Retry after private browser storage failure'; + await dialog.getByRole('textbox', { name: '工程名称', exact: true }).fill(title); + await page.evaluate(() => { + const setItem = Storage.prototype.setItem; + window.__restoreProjectStorage = () => { Storage.prototype.setItem = setItem; }; + Storage.prototype.setItem = function(key, value) { + if (key === 'fastfigure.project.v2') throw new DOMException('Storage is full', 'QuotaExceededError'); + return setItem.call(this, key, value); + }; + }); + try { + await dialog.getByRole('button', { name: '保存工程', exact: true }).click(); + assert(await dialog.isVisible(), 'A failed save must leave the project dialog open'); + await dialog.getByRole('alert').waitFor(); + assert.equal(await dialog.getByRole('textbox', { name: '工程名称', exact: true }).inputValue(), title); + const [download] = await Promise.all([page.waitForEvent('download'), dialog.getByRole('button', { name: '备份 JSON', exact: true }).click()]); + const chunks = []; + for await (const chunk of await download.createReadStream()) chunks.push(chunk); + assert.equal(JSON.parse(Buffer.concat(chunks).toString()).title, title); + } finally { + await page.evaluate(() => { window.__restoreProjectStorage(); delete window.__restoreProjectStorage; }); + } + await dialog.getByRole('button', { name: '保存工程', exact: true }).click(); + await dialog.waitFor({ state: 'hidden' }); + await page.reload(); + assert.equal(await page.evaluate(() => JSON.parse(localStorage.getItem('fastfigure.project.v2')).title), title); +} diff --git a/tests/ui/run.mjs b/tests/ui/run.mjs index e0cf200..1880374 100644 --- a/tests/ui/run.mjs +++ b/tests/ui/run.mjs @@ -11,13 +11,14 @@ import { checkAssistant } from './assistant.mjs'; import { checkReconstruction } from './reconstruction.mjs'; import { checkNotifications } from './notifications.mjs'; import { checkFastUi } from './fastui.mjs'; +import { checkProjectSave } from './project-save.mjs'; // One supported desktop viewport. A private fixture service never uses the user's CLI/session. process.env.FASTFIGURE_CODEX_COMMAND = process.env.FASTFIGURE_UI_CLI_FIXTURE ?? join(process.cwd(), 'server/harness/fixtures/cli.mjs'); process.env.FASTFIGURE_CLAUDE_COMMAND = process.env.FASTFIGURE_CODEX_COMMAND; process.env.FASTFIGURE_TEST_SCENARIO = 'slow'; const libraryDirectory=mkdtempSync(join(tmpdir(),'fastfigure-ui-library-')); process.env.FASTFIGURE_LIBRARY_DIR=libraryDirectory; -const checks = [['fastui', checkFastUi], ['editor', checkEditor], ['data', checkData], ['assistant', checkAssistant], ['reconstruction', checkReconstruction], ['notifications', checkNotifications], ['gallery-study', checkGalleryStudy]]; +const checks = [['fastui', checkFastUi], ['project-save', checkProjectSave], ['editor', checkEditor], ['data', checkData], ['assistant', checkAssistant], ['reconstruction', checkReconstruction], ['notifications', checkNotifications], ['gallery-study', checkGalleryStudy]]; const selected = checks.find(([name]) => process.argv.includes(`--${name}`)); const scenarios = selected ? [selected] : checks.filter(([name]) => name !== 'gallery-study'); const results = join(process.cwd(), selected ? `test-results/ui-${selected[0]}` : 'test-results/ui');