Skip to content

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

fix(fig): keep a new layer's GUID across saves (#973)

Danila Poyarkove3c49cc8d4c0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 54f636a96..1a3db3497 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -125,6 +125,7 @@
### Fixed
+- Keep a layer's `.fig` ID the same across saves (#890). Layers created in OpenPencil were numbered in tree order on each save until the file was reopened, so inserting a layer shifted the IDs of the layers after it; variable collections and modes could also be renumbered.
- Store crash recovery snapshots of any size. Snapshots of documents over 127 MiB failed to save to IndexedDB and stayed in memory for the rest of the session.
- Keep an opened `.fig` file saved until it is edited. Laying out its first page, which recomputes auto-layout sizes and positions, and showing another page for the first time, which loads its layers from the file, marked it unsaved, so closing it asked to save changes nobody made.
- Run crash recovery and autosave after edits, not whenever the canvas redraws. Opening a document, laying out a page, or loading a font started a recovery snapshot or an autosave, which encoded the whole document again once another page had loaded. In Safari, where every opened file gets recovery snapshots, a large page froze the browser for minutes after it first appeared.
diff --git a/packages/core/src/io/formats/fig/export.ts b/packages/core/src/io/formats/fig/export.ts
index 10a66c41f..b1ede95c4 100644
--- a/packages/core/src/io/formats/fig/export.ts
+++ b/packages/core/src/io/formats/fig/export.ts
@@ -95,31 +95,66 @@ async function renderFigThumbnail(
)
}
-interface ComponentPropertyGuidState {
- ids: string[]
- maxLocalId0: number
- maxLocalId1: number
+/**
+ * The GUID a layer that was never opened from a file is saved under: its graph ID, which has
+ * Figma's `sessionID:localID` shape and never changes, so later saves keep it. Saved GUIDs of
+ * opened layers and GUIDs already written win over it.
+ */
+function ownGuid(
+ id: string,
+ sourceGuidValues: ReadonlySet<string>,
+ assignedGuidValues: ReadonlySet<string>
+): GUID | null {
+ if (!/^\d+:\d+$/.test(id)) return null
+ const guid = stringToGuid(id)
+ const key = `${guid.sessionID}:${guid.localID}`
+ return sourceGuidValues.has(key) || assignedGuidValues.has(key) ? null : guid
}
-function collectComponentPropertyGuidState(graph: SceneGraph): ComponentPropertyGuidState {
+function assignOwnLayerGuids(
+ graph: SceneGraph,
+ sourceGuidValues: ReadonlySet<string>,
+ nodeIdToGuid: Map<string, GUID>,
+ assignedGuidValues: Set<string>
+): void {
+ for (const node of graph.nodes.values()) {
+ if (node.source.id || node.id === graph.rootId || nodeIdToGuid.has(node.id)) continue
+ const guid = ownGuid(node.id, sourceGuidValues, assignedGuidValues)
+ if (!guid) continue
+ nodeIdToGuid.set(node.id, guid)
+ assignedGuidValues.add(`${guid.sessionID}:${guid.localID}`)
+ }
+}
+
+/**
+ * The first local ID the counter can mint in sessions 0 and 1: past every saved GUID and every
+ * ID that layers, collections, modes, variables and component properties are saved under.
+ */
+function firstUnclaimedLocalId(graph: SceneGraph, propertyIds: readonly string[]): number {
+ const ids = [...propertyIds]
+ for (const node of graph.nodes.values()) ids.push(node.source.id || node.id)
+ for (const [collectionId, collection] of graph.variableCollections) {
+ ids.push(collectionId, ...collection.modes.map((mode) => mode.modeId))
+ ids.push(...collection.variableIds)
+ }
+ let last = 0
+ for (const id of ids) {
+ if (!/^\d+:\d+$/.test(id)) continue
+ const guid = stringToGuid(id)
+ if (guid.sessionID <= 1) last = Math.max(last, guid.localID)
+ }
+ return last + 1
+}
+
+function collectComponentPropertyIds(graph: SceneGraph): string[] {
const ids = new Set<string>()
- let maxLocalId0 = 0
- let maxLocalId1 = 0
for (const node of graph.getAllNodes()) {
for (const definition of node.componentPropertyDefinitions) ids.add(definition.id)
for (const reference of node.componentPropertyReferences) ids.add(reference.propertyId)
for (const propertyId of Object.keys(node.componentPropertyAssignments)) ids.add(propertyId)
for (const spec of node.variantPropSpecs) ids.add(spec.propDefId)
}
- for (const propertyId of ids) {
- const match = /^(\d+):(\d+)$/.exec(propertyId)
- if (!match) continue
- const sessionID = Number.parseInt(match[1], 10)
- const localID = Number.parseInt(match[2], 10)
- if (sessionID === 0) maxLocalId0 = Math.max(maxLocalId0, localID)
- if (sessionID === 1) maxLocalId1 = Math.max(maxLocalId1, localID)
- }
- return { ids: [...ids], maxLocalId0, maxLocalId1 }
+ return [...ids]
}
function assignComponentPropertyGuids(
@@ -186,14 +221,22 @@ function buildCanvasEntries(
docGuid: GUID,
localIdCounter: { value: number },
nodeIdToGuid: Map<string, GUID>,
- assignedGuidValues: Set<string>
+ assignedGuidValues: Set<string>,
+ sourceGuidValues: ReadonlySet<string>
): { canvasEntries: CanvasExportEntry[]; internalCanvasGuid: GUID | null } {
const canvasEntries: CanvasExportEntry[] = []
let internalCanvasGuid: GUID | null = null
for (let p = 0; p < pages.length; p++) {
const page = pages[p]
const canvasGuid = (() => {
- if (!page.source.id) return { sessionID: 0, localID: localIdCounter.value++ }
+ if (!page.source.id) {
+ return (
+ ownGuid(page.id, sourceGuidValues, assignedGuidValues) ?? {
+ sessionID: 0,
+ localID: localIdCounter.value++
+ }
+ )
+ }
const importedGuid = stringToGuid(page.source.id)
const key = `${importedGuid.sessionID}:${importedGuid.localID}`
@@ -398,29 +441,13 @@ async function writeFigFile(
const blobIndexByHex = new Map<string, number>()
const componentPropertyDefinitionsById = buildComponentPropIndex(graph)
- // Scan ALL imported source.ids BEFORE any new GUID assignment to find
- // max sessionID:0 and sessionID:1 localID values. This guarantees the
- // counter is past every imported GUID before any canvas, variable, or
- // node claims a new counter-based GUID — preventing collisions.
- let maxLocalId0 = localIdCounter.value - 1
- let maxLocalId1 = localIdCounter.value - 1
const nodeSourceGuidValues = new Set<string>()
for (const node of graph.nodes.values()) {
- if (node.source.id) {
- nodeSourceGuidValues.add(node.source.id)
- const g = stringToGuid(node.source.id)
- if (g.sessionID === 0 && g.localID > maxLocalId0) {
- maxLocalId0 = g.localID
- }
- if (g.sessionID === 1 && g.localID > maxLocalId1) {
- maxLocalId1 = g.localID
- }
- }
+ if (node.source.id) nodeSourceGuidValues.add(node.source.id)
}
- const propertyGuidState = collectComponentPropertyGuidState(graph)
- maxLocalId0 = Math.max(maxLocalId0, propertyGuidState.maxLocalId0)
- maxLocalId1 = Math.max(maxLocalId1, propertyGuidState.maxLocalId1)
- localIdCounter.value = Math.max(localIdCounter.value, maxLocalId0 + 1, maxLocalId1 + 1)
+ const propertyIds = collectComponentPropertyIds(graph)
+ // Before any canvas, variable or layer takes a counter GUID, so none collides.
+ localIdCounter.value = Math.max(localIdCounter.value, firstUnclaimedLocalId(graph, propertyIds))
const { canvasEntries, internalCanvasGuid } = buildCanvasEntries(
graph,
@@ -428,7 +455,8 @@ async function writeFigFile(
docGuid,
localIdCounter,
nodeIdToGuid,
- assignedGuidValues
+ assignedGuidValues,
+ nodeSourceGuidValues
)
// Assign variable GUIDs AFTER canvas entries so that source.id-derived
@@ -443,13 +471,14 @@ async function writeFigFile(
)
assignComponentPropertyGuids(
- propertyGuidState.ids,
+ propertyIds,
localIdCounter,
propertyIdToGuid,
assignedGuidValues,
nodeSourceGuidValues
)
renameBehaviourPropertyIds(graph, propertyIdToGuid)
+ assignOwnLayerGuids(graph, nodeSourceGuidValues, nodeIdToGuid, assignedGuidValues)
for (const entry of canvasEntries) nodeChanges.push(entry.canvasNc)
diff --git a/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts b/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
index 2dd803a06..e4f973df5 100644
--- a/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
+++ b/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
@@ -492,10 +492,6 @@ describe('fig roundtrip source metadata', () => {
const graph = new SceneGraph()
graph.createNode('FRAME', graph.getPages()[0].id, { name: 'Card' })
const saved = await exportFigFile(graph)
- const savedGuidOf = (bytes: Uint8Array, name: string) => {
- const nodeChange = decodeExport(bytes).nodeChanges.find((change) => change.name === name)
- return nodeChange?.guid ? guidToString(nodeChange.guid) : undefined
- }
const reopened = await parseFigFile(saved.slice().buffer)
const page = reopened.getPages()[0]
@@ -510,4 +506,40 @@ describe('fig roundtrip source metadata', () => {
expect(insertedGuid).toBeDefined()
expect(insertedGuid).not.toBe(cardGuid)
})
+
+ test('keeps the GUIDs of new layers across saves when a sibling is inserted before them', async () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ graph.createNode('FRAME', page.id, { name: 'Card' })
+ const cardGuid = savedGuidOf(await exportFigFile(graph), 'Card')
+
+ const inserted = graph.createNode('RECTANGLE', page.id, { name: 'Inserted' })
+ graph.reorderChild(inserted.id, page.id, 0)
+ const resaved = await exportFigFile(graph)
+
+ expect(cardGuid).toBeDefined()
+ expect(savedGuidOf(resaved, 'Card')).toBe(cardGuid)
+ const insertedGuid = savedGuidOf(resaved, 'Inserted')
+ expect(insertedGuid).toBeDefined()
+ expect(insertedGuid).not.toBe(cardGuid)
+ })
+
+ test('gives an opened layer its saved GUID when a new layer has the same ID', async () => {
+ let next = 0
+ const graph = new SceneGraph(() => `9:${++next}`)
+ const page = graph.getPages()[0]
+ const created = graph.createNode('FRAME', page.id, { name: 'Created' })
+ const opened = graph.createNode('FRAME', page.id, { name: 'Opened' })
+ graph.updateNode(opened.id, { source: { ...opened.source, id: created.id } })
+ const saved = await exportFigFile(graph)
+
+ expect(savedGuidOf(saved, 'Opened')).toBe(created.id)
+ expect(savedGuidOf(saved, 'Created')).toBeDefined()
+ expect(savedGuidOf(saved, 'Created')).not.toBe(created.id)
+ })
})
+
+function savedGuidOf(bytes: Uint8Array, name: string): string | undefined {
+ const nodeChange = decodeExport(bytes).nodeChanges.find((change) => change.name === name)
+ return nodeChange?.guid ? guidToString(nodeChange.guid) : undefined
+}