AI-native design editor. Open-source Figma alternative.
fix(fig): save added and moved pages in their place (#1707)
Danila Poyarkova88606919a04
diff --git a/changes/1707/fixed.md b/changes/1707/fixed.mdnew file mode 100644index 000000000..4c4dcb0e6--- /dev/null+++ b/changes/1707/fixed.md@@ -0,0 +1 @@+- A page added to a `.fig` file, such as one of the shadcn/ui kits, is saved last instead of second, and a page moved after opening is saved where it now is (#1707).diff --git a/packages/core/src/io/formats/fig/export/setup.ts b/packages/core/src/io/formats/fig/export/setup.tsindex 7ed0b3f76..e36ea0553 100644--- a/packages/core/src/io/formats/fig/export/setup.ts+++ b/packages/core/src/io/formats/fig/export/setup.ts@@ -22,7 +22,7 @@ import {renameBehaviourProperties,withBehaviour} from '@open-pencil/scene-graph/behaviours'-import { fractionalPosition } from '@open-pencil/scene-graph/order-keys'+import { orderKeyBetween, siblingOrderKeys } from '@open-pencil/scene-graph/order-keys'import type { GUID, Rect, Size } from '@open-pencil/scene-graph/primitives'import { ownsSlotContent } from '@open-pencil/scene-graph/slots'@@ -282,6 +282,9 @@ function buildCanvasEntries(): { canvasEntries: CanvasExportEntry[]; internalCanvasGuid: GUID | null } {const canvasEntries: CanvasExportEntry[] = []let internalCanvasGuid: GUID | null = null+ // Pages keep the keys they were read with while those stay in order; a page added or moved+ // gets one between its neighbours', as Figma sorts pages by these keys.+ const orderKeys = siblingOrderKeys(pages.map((page) => page.source.orderKey))for (let p = 0; p < pages.length; p++) {const page = pages[p]const canvasGuid = (() => {@@ -310,17 +313,11 @@ function buildCanvasEntries(assignedGuidValues.add(`${canvasGuid.sessionID}:${canvasGuid.localID}`)if (page.internalOnly) internalCanvasGuid = canvasGuid- const canvasNc = makeCanvasNodeChange(- canvasGuid,- docGuid,- page.source.orderKey ?? fractionalPosition(p),- page.name,- {- backgroundOpacity: 1,- backgroundColor: { ...CANVAS_BG_COLOR },- backgroundEnabled: true- }- )+ const canvasNc = makeCanvasNodeChange(canvasGuid, docGuid, orderKeys[p], page.name, {+ backgroundOpacity: 1,+ backgroundColor: { ...CANVAS_BG_COLOR },+ backgroundEnabled: true+ })applyImportedCanvasFields(page, canvasNc)if (page.internalOnly) canvasNc.internalOnly = truecanvasEntries.push({ page, canvasGuid, canvasNc })@@ -340,7 +337,7 @@ function buildCanvasEntries(canvasNc: makeCanvasNodeChange(internalCanvasGuid,docGuid,- fractionalPosition(canvasEntries.length),+ orderKeyBetween(orderKeys.at(-1) ?? null, null),'Internal Only Canvas',{ internalOnly: true })diff --git a/packages/core/tests/io/formats/fig/export/canvas-order-keys.test.ts b/packages/core/tests/io/formats/fig/export/canvas-order-keys.test.tsindex e014d4bfa..bcb45bbf4 100644--- a/packages/core/tests/io/formats/fig/export/canvas-order-keys.test.ts+++ b/packages/core/tests/io/formats/fig/export/canvas-order-keys.test.ts@@ -2,7 +2,7 @@ import { expect, test } from 'bun:test'import { exportFigFile } from '@open-pencil/core/io/formats/fig'import { parseFigBuffer } from '@open-pencil/fig'-import { initCodec } from '@open-pencil/kiwi/fig/codec'+import { initCodec, type NodeChange } from '@open-pencil/kiwi/fig/codec'import { SceneGraph } from '@open-pencil/scene-graph'import type { GUID } from '@open-pencil/scene-graph/primitives'@@ -63,3 +63,52 @@ test('every child of a canvas is exported with its own order key', async () => {for (const [parent, positions] of byParent)expect([parent, new Set(positions).size]).toEqual([parent, positions.length])})++/** The pages a saved file lists, in the order Figma sorts them: by their order keys. */+function savedPageOrder(bytes: Uint8Array): string[] {+ return parseFigBuffer(bytes.slice().buffer as ArrayBuffer)+ .nodeChanges.filter((change: NodeChange) => change.type === 'CANVAS' && !change.internalOnly)+ .map((change) => ({+ name: change.name ?? '',+ position: (change.parentIndex as { position?: string } | undefined)?.position ?? ''+ }))+ // Keys are unique and compare by code point, as Figma sorts them.+ .sort((a, b) => (a.position < b.position ? -1 : 1))+ .map((page) => page.name)+}++// The shadcn/ui kit numbers its pages `!`, `~#`, `~#7`…; a page added after them was saved as+// `{`, which sorts before every `~` key, so Figma listed it second.+test('a page added or moved among Figma-numbered pages keeps its place', async () => {+ await initCodec()+ const graph = new SceneGraph()+ const [cover] = graph.getPages()+ graph.updateNode(cover.id, { name: 'Cover', source: { ...cover.source, orderKey: '!' } })+ for (const [name, orderKey] of [+ ['Accordion', '~#'],+ ['Alert', '~#7'],+ ['Trace', '~NO']+ ]) {+ const page = graph.addPage(name)+ graph.updateNode(page.id, { source: { ...page.source, orderKey } })+ }+ graph.addPage('Added')+ expect(savedPageOrder(await exportFigFile(graph))).toEqual([+ 'Cover',+ 'Accordion',+ 'Alert',+ 'Trace',+ 'Added'+ ])++ // Moved in OpenPencil, a page is saved where it now is, not where its old key put it.+ const trace = graph.getPages().find((page) => page.name === 'Trace')+ graph.insertChildAt(trace?.id ?? '', graph.rootId, 1)+ expect(savedPageOrder(await exportFigFile(graph))).toEqual([+ 'Cover',+ 'Trace',+ 'Accordion',+ 'Alert',+ 'Added'+ ])+})