Skip to content

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

fix(fig): populate lazy pages before export

Danila Poyarkova22d39210c50
diff --git a/packages/core/src/io/formats/fig/export.ts b/packages/core/src/io/formats/fig/export.ts
index 6aed44963..a3259a9e8 100644
--- a/packages/core/src/io/formats/fig/export.ts
+++ b/packages/core/src/io/formats/fig/export.ts
@@ -4,6 +4,7 @@ import { deflateSync } from 'fflate'
import type { SkiaRenderer } from '#core/canvas'
import { CANVAS_BG_COLOR, IS_BROWSER, IS_TAURI } from '#core/constants'
import { renderThumbnail } from '#core/io/formats/raster'
+import { populateAllLazyFigImportRoots } from '#core/kiwi/fig/lazy-import'
import { initCodec, getCompiledSchema, getSchemaBytes } from '#core/kiwi/binary/codec'
import type { NodeChange } from '#core/kiwi/binary/codec'
import { stringToGuid } from '#core/kiwi/node-change/convert'
@@ -175,6 +176,7 @@ export async function exportFigFile(
renderer?: SkiaRenderer,
pageId?: string
): Promise<Uint8Array> {
+ populateAllLazyFigImportRoots(graph)
await initCodec()
const compiled = getCompiledSchema()
const schemaDeflated = deflateSync(getSchemaBytes())
diff --git a/packages/core/src/kiwi/fig/lazy-import.ts b/packages/core/src/kiwi/fig/lazy-import.ts
index 7b48f625f..8d6cef613 100644
--- a/packages/core/src/kiwi/fig/lazy-import.ts
+++ b/packages/core/src/kiwi/fig/lazy-import.ts
@@ -19,10 +19,11 @@ export function getLazyFigImportContext(graph: SceneGraph): LazyFigImportContext
return lazyFigImportContexts.get(graph)
}
-export function populateLazyFigImportRoots(graph: SceneGraph, rootIds: Iterable<string>): boolean {
- const context = getLazyFigImportContext(graph)
- if (!context) return false
-
+function populateRoots(
+ graph: SceneGraph,
+ context: LazyFigImportContext,
+ rootIds: Iterable<string>
+): boolean {
const pending = [...rootIds].filter((id) => id && !context.populatedRootIds.has(id))
if (pending.length === 0) return false
@@ -37,3 +38,18 @@ export function populateLazyFigImportRoots(graph: SceneGraph, rootIds: Iterable<
for (const id of pending) context.populatedRootIds.add(id)
return true
}
+
+export function populateLazyFigImportRoots(graph: SceneGraph, rootIds: Iterable<string>): boolean {
+ const context = getLazyFigImportContext(graph)
+ return context ? populateRoots(graph, context, rootIds) : false
+}
+
+export function populateAllLazyFigImportRoots(graph: SceneGraph): boolean {
+ const context = getLazyFigImportContext(graph)
+ if (!context) return false
+ return populateRoots(
+ graph,
+ context,
+ graph.getPages(true).map((page) => page.id)
+ )
+}
diff --git a/tests/engine/io/fig/import/lazy-pages.test.ts b/tests/engine/io/fig/import/lazy-pages.test.ts
new file mode 100644
index 000000000..54146816e
--- /dev/null
+++ b/tests/engine/io/fig/import/lazy-pages.test.ts
@@ -0,0 +1,71 @@
+import { describe, expect, test } from 'bun:test'
+
+import {
+ populateAllLazyFigImportRoots,
+ populateLazyFigImportRoots,
+ setLazyFigImportContext
+} from '@open-pencil/core/kiwi/fig/lazy-import'
+import { SceneGraph } from '@open-pencil/core/scene-graph'
+
+function createLazyGraph() {
+ const graph = new SceneGraph()
+ const [page1] = graph.getPages()
+ const page2 = graph.addPage('Page 2')
+ const component = graph.createNode('COMPONENT', page1.id, {
+ name: 'Button',
+ width: 100,
+ height: 40
+ })
+ graph.createNode('RECTANGLE', component.id, {
+ name: 'Background',
+ width: 100,
+ height: 40
+ })
+ const page1Instance = graph.createNode('INSTANCE', page1.id, {
+ name: 'Button instance 1',
+ componentId: component.id,
+ width: 100,
+ height: 40
+ })
+ const page2Instance = graph.createNode('INSTANCE', page2.id, {
+ name: 'Button instance 2',
+ componentId: component.id,
+ width: 100,
+ height: 40
+ })
+
+ setLazyFigImportContext(graph, {
+ changeMap: new Map(),
+ guidToNodeId: new Map(),
+ blobs: [],
+ populatedRootIds: new Set([page1.id])
+ })
+
+ return { graph, page1, page2, page1Instance, page2Instance }
+}
+
+describe('lazy .fig page population', () => {
+ test('populates an unvisited page once', () => {
+ const { graph, page2, page1Instance, page2Instance } = createLazyGraph()
+
+ expect(graph.getChildren(page1Instance.id)).toHaveLength(0)
+ expect(graph.getChildren(page2Instance.id)).toHaveLength(0)
+
+ expect(populateLazyFigImportRoots(graph, [page2.id])).toBe(true)
+ expect(graph.getChildren(page1Instance.id)).toHaveLength(0)
+ expect(graph.getChildren(page2Instance.id)).toHaveLength(1)
+
+ const nodeCount = graph.nodes.size
+ expect(populateLazyFigImportRoots(graph, [page2.id])).toBe(false)
+ expect(graph.nodes.size).toBe(nodeCount)
+ })
+
+ test('can populate all remaining pages before full-document operations', () => {
+ const { graph, page1Instance, page2Instance } = createLazyGraph()
+
+ expect(populateAllLazyFigImportRoots(graph)).toBe(true)
+ expect(graph.getChildren(page1Instance.id)).toHaveLength(0)
+ expect(graph.getChildren(page2Instance.id)).toHaveLength(1)
+ expect(populateAllLazyFigImportRoots(graph)).toBe(false)
+ })
+})