Skip to content

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

fix(fig): save and load pages after deleting a component's layers (#1665)

Danila Poyarkov0d18cddedaa8
diff --git a/changes/1665/fixed.md b/changes/1665/fixed.md
new file mode 100644
index 000000000..28f30b015
--- /dev/null
+++ b/changes/1665/fixed.md
@@ -0,0 +1 @@
+- Documents opened from `.fig` files save and load pages again after a layer of a component is deleted, including when an instance nested in a component frame is replaced (#1425, #1427).
diff --git a/packages/fig/src/document/component/checkpoint.ts b/packages/fig/src/document/component/checkpoint.ts
index 5b496ce8e..204ecd6ec 100644
--- a/packages/fig/src/document/component/checkpoint.ts
+++ b/packages/fig/src/document/component/checkpoint.ts
@@ -15,6 +15,8 @@ export function checkpointComponent(
const nodes: ComponentCheckpoint['nodes'] = []
const visit = (occurrence: InstanceOccurrence, path: string[]): void => {
const node = component.materialized.nodes.get(occurrence)
+ // A layer deleted before a resumed checkpoint is left out, as the restore leaves it out.
+ if (!node && path.length > 0) return
if (!node) throw new Error('Missing checkpoint occurrence')
nodes.push({ path, nodeId: node.id, mainComponentId: occurrence.mainComponentId })
const ids = new Set<string>()
diff --git a/packages/fig/src/instance-overrides/source-children.ts b/packages/fig/src/instance-overrides/source-children.ts
index f9be581b2..88fcbe812 100644
--- a/packages/fig/src/instance-overrides/source-children.ts
+++ b/packages/fig/src/instance-overrides/source-children.ts
@@ -15,33 +15,37 @@ export function reconcileOccurrenceStructure(
graph: SceneGraph,
components: ReadonlyMap<string, MaterializedComponentOccurrence>
): void {
- if (target.mainComponentId !== null) {
- const component = components.get(target.mainComponentId)
- if (!component) throw new Error(`Missing component occurrence ${target.mainComponentId}`)
+ if (target.mainComponentId === null) {
+ for (const child of target.children) reconcileOccurrenceStructure(child, graph, components)
+ return
+ }
+ const component = components.get(target.mainComponentId)
+ if (!component) throw new Error(`Missing component occurrence ${target.mainComponentId}`)
+ const nodes = component.materialized.nodes
+ // Walk the component's own layers, frames inside it included, beside the instance's.
+ const reconcileLayers = (current: InstanceOccurrence, source: InstanceOccurrence): void => {
+ const layer = nodes.get(source)
const liveOrder = new Map(
- graph.getChildren(component.materialized.root.id).map((node, index) => [node.id, index])
+ (layer ? graph.getChildren(layer.id) : []).map((node, index) => [node.id, index])
)
- const sourceChildren = new Map(
- component.occurrence.children.map((child) => [child.sourceId, child])
- )
- target.children = target.children
- .filter((child) => {
- const source = sourceChildren.get(child.sourceId)
- const node = source && component.materialized.nodes.get(source)
- return !!node && liveOrder.has(node.id)
- })
- .toSorted((a, b) => {
- const sourceA = sourceChildren.get(a.sourceId)
- const sourceB = sourceChildren.get(b.sourceId)
- const nodeA = sourceA && component.materialized.nodes.get(sourceA)
- const nodeB = sourceB && component.materialized.nodes.get(sourceB)
- return (
- (nodeA ? (liveOrder.get(nodeA.id) ?? 0) : 0) -
- (nodeB ? (liveOrder.get(nodeB.id) ?? 0) : 0)
- )
- })
+ const sourceChildren = new Map(source.children.map((child) => [child.sourceId, child]))
+ const liveIndex = (child: InstanceOccurrence): number | undefined => {
+ const counterpart = sourceChildren.get(child.sourceId)
+ const node = counterpart && nodes.get(counterpart)
+ return node && liveOrder.get(node.id)
+ }
+ current.children = current.children
+ .filter((child) => liveIndex(child) !== undefined)
+ .toSorted((a, b) => (liveIndex(a) ?? 0) - (liveIndex(b) ?? 0))
+ for (const child of current.children) {
+ const counterpart = sourceChildren.get(child.sourceId)
+ // Nested instances answer to their own components; slot content is the instance's own.
+ if (child.mainComponentId !== null || child.slotContentId || !counterpart) {
+ reconcileOccurrenceStructure(child, graph, components)
+ } else reconcileLayers(child, counterpart)
+ }
}
- for (const child of target.children) reconcileOccurrenceStructure(child, graph, components)
+ reconcileLayers(target, component.occurrence)
}
/**
diff --git a/packages/fig/tests/document/page-selection.test.ts b/packages/fig/tests/document/page-selection.test.ts
index 570a3abc3..622a632c1 100644
--- a/packages/fig/tests/document/page-selection.test.ts
+++ b/packages/fig/tests/document/page-selection.test.ts
@@ -11,7 +11,11 @@ test('selected-page assembly excludes unrelated content but keeps cross-page com
const changes: NodeChange[] = [
{ guid: guid(1), type: 'CANVAS', name: 'Design' },
{ guid: guid(2), type: 'CANVAS', name: 'Library' },
- { guid: guid(3), type: 'SYMBOL', parentIndex: { guid: guid(2), position: '!' } },
+ {
+ guid: guid(3),
+ type: 'SYMBOL',
+ parentIndex: { guid: guid(2), position: '!' }
+ },
{
guid: guid(4),
type: 'TEXT',
@@ -31,7 +35,9 @@ test('selected-page assembly excludes unrelated content but keeps cross-page com
textData: { characters: 'Unrelated' }
}
]
- const selected = materializeDocument(changes, [], { pageIds: new Set(['1:1']) })
+ const selected = materializeDocument(changes, [], {
+ pageIds: new Set(['1:1'])
+ })
const full = materializeDocument(changes)
expect(selected.graph.getPages()).toHaveLength(2)
expect(selected.sources.has('1:6')).toBe(false)
@@ -54,16 +60,34 @@ async function enclosedComponentArchive(
): Promise<ArrayBuffer> {
const records: NodeChange[] = [
{ guid: guid(1), type: 'DOCUMENT' },
- { guid: guid(2), type: 'CANVAS', parentIndex: { guid: guid(1), position: '!' } },
+ {
+ guid: guid(2),
+ type: 'CANVAS',
+ parentIndex: { guid: guid(1), position: '!' }
+ },
{
guid: guid(10),
type: 'INSTANCE',
parentIndex: { guid: guid(2), position: '!' },
symbolData: { symbolID: guid(5) }
},
- { guid: guid(3), type: 'CANVAS', parentIndex: { guid: guid(1), position: '"' } },
- { guid: guid(4), type: 'SYMBOL', name: 'Outer', parentIndex: { guid: guid(3), position: '!' } },
- { guid: guid(5), type: 'SYMBOL', name: 'Inner', parentIndex: { guid: guid(4), position: '!' } },
+ {
+ guid: guid(3),
+ type: 'CANVAS',
+ parentIndex: { guid: guid(1), position: '"' }
+ },
+ {
+ guid: guid(4),
+ type: 'SYMBOL',
+ name: 'Outer',
+ parentIndex: { guid: guid(3), position: '!' }
+ },
+ {
+ guid: guid(5),
+ type: 'SYMBOL',
+ name: 'Inner',
+ parentIndex: { guid: guid(4), position: '!' }
+ },
{
guid: guid(6),
type: 'TEXT',
@@ -154,10 +178,9 @@ test.each([false, true])(
session = createFigDocumentSession(bytes, {}, { graph, checkpoint: session.checkpoint() })
}
session.loadPage('1:3')
- // Checkpointing after a resumed deletion is #1425; read the layers from the graph instead.
- const outer = [...graph.getAllNodes()].find((node) => node.name === 'Outer')
+ const outer = new Map(session.checkpoint().componentIds).get('1:4')
if (!outer) throw new Error('Missing outer component')
- expect(layers(graph, outer.id)).toEqual([
+ expect(layers(graph, outer)).toEqual([
'COMPONENT',
'',
[
@@ -166,7 +189,96 @@ test.each([false, true])(
['INSTANCE', '', [['TEXT', 'Label', []]]]
]
])
- if (!resumed) expect(session.checkpoint().components.map(([id]) => id)).toContain('1:4')
+ expect(session.checkpoint().components.map(([id]) => id)).toContain('1:4')
+ }
+)
+
+test('checkpoints a resumed session after a component layer is deleted', async () => {
+ const bytes = await enclosedComponentArchive(false)
+ const initial = createFigDocumentSession(bytes)
+ initial.loadPage('1:2')
+ const { graph } = initial
+ const resumed = createFigDocumentSession(bytes, {}, { graph, checkpoint: initial.checkpoint() })
+ const inner = new Map(resumed.checkpoint().componentIds).get('1:5')
+ if (!inner) throw new Error('Missing inner component')
+ const deleted = graph.getChildren(inner)[0].id
+ graph.deleteNode(deleted)
+ const again = createFigDocumentSession(bytes, {}, { graph, checkpoint: resumed.checkpoint() })
+ const component = new Map(again.checkpoint().components).get('1:5')
+ expect(component?.nodes.map(({ nodeId }) => nodeId)).toEqual([inner])
+})
+
+test.each([false, true])(
+ 'loads a page whose instance encloses a layer deleted inside a frame of its component (resumed: %p)',
+ async (resumed) => {
+ const bytes = await figArchiveOf([
+ { guid: guid(1), type: 'DOCUMENT' },
+ {
+ guid: guid(2),
+ type: 'CANVAS',
+ parentIndex: { guid: guid(1), position: '!' }
+ },
+ {
+ guid: guid(3),
+ type: 'CANVAS',
+ parentIndex: { guid: guid(1), position: '"' }
+ },
+ {
+ guid: guid(4),
+ type: 'SYMBOL',
+ name: 'Icon',
+ parentIndex: { guid: guid(3), position: '!' }
+ },
+ {
+ guid: guid(5),
+ type: 'TEXT',
+ parentIndex: { guid: guid(4), position: '!' },
+ textData: { characters: 'Icon' }
+ },
+ {
+ guid: guid(6),
+ type: 'SYMBOL',
+ name: 'Header',
+ parentIndex: { guid: guid(3), position: '"' }
+ },
+ {
+ guid: guid(7),
+ type: 'FRAME',
+ parentIndex: { guid: guid(6), position: '!' }
+ },
+ {
+ guid: guid(8),
+ type: 'INSTANCE',
+ parentIndex: { guid: guid(7), position: '!' },
+ symbolData: { symbolID: guid(4) }
+ },
+ {
+ guid: guid(9),
+ type: 'TEXT',
+ parentIndex: { guid: guid(7), position: '"' },
+ textData: { characters: 'Label' }
+ },
+ {
+ guid: guid(10),
+ type: 'INSTANCE',
+ parentIndex: { guid: guid(2), position: '!' },
+ symbolData: { symbolID: guid(6) }
+ }
+ ])
+ let session = createFigDocumentSession(bytes)
+ session.loadPage('1:3')
+ const { graph } = session
+ const header = new Map(session.checkpoint().componentIds).get('1:6')
+ if (!header) throw new Error('Missing header component')
+ const [frame] = graph.getChildren(header)
+ graph.deleteNode(graph.getChildren(frame.id)[0].id)
+ if (resumed) {
+ session = createFigDocumentSession(bytes, {}, { graph, checkpoint: session.checkpoint() })
+ }
+ session.loadPage('1:2')
+ const copy = new Map(session.checkpoint().sources).get('1:10')
+ if (!copy) throw new Error('Missing header instance')
+ expect(layers(graph, copy)).toEqual(['INSTANCE', '', [['FRAME', '', [['TEXT', 'Label', []]]]]])
}
)