Skip to content

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

fix(app): keep opened documents saved until they are edited (#945)

Danila Poyarkov85d8c69ccc2b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0ebd7e202..035df2085 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -119,6 +119,7 @@
### Fixed
+- Keep an opened `.fig` file saved until it is edited. Laying out its first page, which recomputes auto-layout sizes and positions, marked it unsaved, so closing it asked to save changes nobody made.
- Fill one axis of a grid cell: a grid child set to fill its width or its height no longer fills both, and an auto-layout child of a grid keeps its own size unless it fills, as in Figma. Design JSX renders and exports `w="fill"` and `h="fill"` in grids, and HTML and Tailwind export leave out the size a child fills, so it stretches in the browser too.
- Resize auto-layout frames that fill across their parent with it: one with a fixed size on that axis kept its old size. A filling child still counts toward a hugging parent's size, as in Figma.
- Set `layoutSizingHorizontal` and `layoutSizingVertical` in scripts as Figma does: Fill is recorded on the child for that axis instead of on the frame's own sizing, which `.fig` export dropped, text switches its `textAutoResize`, and sizing Figma refuses, such as Hug on a frame without auto layout, throws Figma's error.
diff --git a/src/app/document/io/changes.ts b/src/app/document/io/changes.ts
index 1be80893b..86056f378 100644
--- a/src/app/document/io/changes.ts
+++ b/src/app/document/io/changes.ts
@@ -2,7 +2,10 @@ import { computed, ref } from 'vue'
import type { Editor } from '@open-pencil/core/editor'
-/** Content-only revisions: viewport repainting and recovery never mark a document saved. */
+/**
+ * Content-only revisions: viewport repainting, layout, and recovery never mark a document
+ * changed or saved.
+ */
export function createDocumentChanges(editor: Editor) {
const revision = ref(0)
const savedRevision = ref(0)
@@ -10,9 +13,14 @@ export function createDocumentChanges(editor: Editor) {
const changed = () => {
revision.value++
}
+ // Layout derives sizes and positions from the document; opening a page or loading a font
+ // relays it out without an edit, and an edit that relays it out has already counted.
+ const updated = () => {
+ if (!editor.graph.isApplyingLayout) changed()
+ }
const unsubscribers = [
editor.onEditorEvent('node:created', changed),
- editor.onEditorEvent('node:updated', changed),
+ editor.onEditorEvent('node:updated', updated),
editor.onEditorEvent('node:deleted', changed),
editor.onEditorEvent('node:reparented', changed),
editor.onEditorEvent('node:reordered', changed),
diff --git a/tests/e2e/app/opened-documents.spec.ts b/tests/e2e/app/opened-documents.spec.ts
new file mode 100644
index 000000000..be8b27edc
--- /dev/null
+++ b/tests/e2e/app/opened-documents.spec.ts
@@ -0,0 +1,35 @@
+import { expect, test } from '@playwright/test'
+
+import type * as AppTabs from '@/app/tabs'
+
+import { CanvasHelper } from '#tests/helpers/canvas'
+import { testPath } from '#tests/helpers/paths'
+
+// An auto-layout file whose layout the app recomputes on its first page.
+const FIXTURE = 'gold-preview.fig'
+
+// Opening a page lays it out, which updates node sizes and positions but is not an edit.
+test('an opened .fig file stays saved until it is edited', async ({ page }) => {
+ await page.route(`**/__fixtures/${FIXTURE}`, (route) =>
+ route.fulfill({ path: testPath('fixtures', FIXTURE) })
+ )
+ await page.goto('/?test')
+ await new CanvasHelper(page).waitForInit()
+ await page.evaluate(async (fixture) => {
+ const tabsURL = '/src/app/tabs/index.ts'
+ const tabs: typeof AppTabs = await import(tabsURL)
+ const response = await fetch(`/__fixtures/${fixture}`)
+ await tabs.openFileInNewTab(new File([await response.arrayBuffer()], fixture))
+ }, FIXTURE)
+
+ const tab = page.locator('[data-slot="tab-item"]').filter({ hasText: 'gold-preview' })
+ await expect(tab).toBeVisible()
+ await expect
+ .poll(() => page.evaluate(() => window.openPencil?.getStore?.()?.state.preparation ?? null))
+ .toBeNull()
+ await expect(tab.getByRole('img', { name: 'Unsaved changes' })).toHaveCount(0)
+
+ await tab.getByTestId('tabbar-close').click()
+ await expect(page.getByRole('alertdialog')).toHaveCount(0)
+ await expect(tab).toHaveCount(0)
+})
diff --git a/tests/engine/app/document/io/changes.test.ts b/tests/engine/app/document/io/changes.test.ts
index b05d46701..520cc945d 100644
--- a/tests/engine/app/document/io/changes.test.ts
+++ b/tests/engine/app/document/io/changes.test.ts
@@ -1,6 +1,7 @@
import { expect, test } from 'bun:test'
import { createEditor } from '@open-pencil/core/editor'
+import { computeAllLayouts } from '@open-pencil/core/layout'
import { createDocumentChanges } from '@/app/document/io/changes'
@@ -46,3 +47,32 @@ test('saving an earlier revision cannot clear edits made while exporting or pick
editor.dispose()
}
})
+
+test('laying out a page does not dirty a document; an edit that relays it out does', () => {
+ const editor = createEditor()
+ const changes = createDocumentChanges(editor)
+ try {
+ const page = editor.state.currentPageId
+ const row = editor.graph.createNode('FRAME', page, {
+ layoutMode: 'HORIZONTAL',
+ primaryAxisSizing: 'HUG',
+ itemSpacing: 8,
+ width: 10,
+ height: 10
+ })
+ editor.graph.createNode('RECTANGLE', row.id, { width: 40, height: 20 })
+ editor.graph.createNode('RECTANGLE', row.id, { width: 40, height: 20 })
+ changes.markSaved()
+
+ computeAllLayouts(editor.graph, page)
+ expect(editor.graph.getNode(row.id)?.width).toBe(88)
+ expect(changes.hasUnsavedChanges()).toBe(false)
+
+ editor.graph.updateNode(row.id, { itemSpacing: 16 })
+ computeAllLayouts(editor.graph, page)
+ expect(changes.hasUnsavedChanges()).toBe(true)
+ } finally {
+ changes.dispose()
+ editor.dispose()
+ }
+})