Skip to content

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

perf(fig): index component property passes

Danila Poyarkov078aa0332148
diff --git a/packages/fig/src/instance-overrides/clone-index.ts b/packages/fig/src/instance-overrides/clone-index.ts
index 17c25edf9..62db7c246 100644
--- a/packages/fig/src/instance-overrides/clone-index.ts
+++ b/packages/fig/src/instance-overrides/clone-index.ts
@@ -2,12 +2,14 @@ import type { OverrideContext } from './types'
export function buildCloneIndex(ctx: OverrideContext): Map<string, string[]> {
const clonesBySource = new Map<string, string[]>()
- for (const node of ctx.graph.getAllNodes()) {
- if (node.type !== 'INSTANCE' || !node.componentId) continue
- if (ctx.activeNodeIds && !ctx.activeNodeIds.has(node.id)) continue
- const clones = clonesBySource.get(node.componentId)
- if (clones) clones.push(node.id)
- else clonesBySource.set(node.componentId, [node.id])
+ for (const [sourceId, nodeIds] of ctx.graph.instanceIndex) {
+ for (const nodeId of nodeIds) {
+ if (ctx.activeNodeIds && !ctx.activeNodeIds.has(nodeId)) continue
+ if (ctx.graph.getNode(nodeId)?.type !== 'INSTANCE') continue
+ const clones = clonesBySource.get(sourceId)
+ if (clones) clones.push(nodeId)
+ else clonesBySource.set(sourceId, [nodeId])
+ }
}
return clonesBySource
}
diff --git a/packages/fig/src/instance-overrides/component-props/assignments.ts b/packages/fig/src/instance-overrides/component-props/assignments.ts
index aca69cac6..3297ccf15 100644
--- a/packages/fig/src/instance-overrides/component-props/assignments.ts
+++ b/packages/fig/src/instance-overrides/component-props/assignments.ts
@@ -84,21 +84,17 @@ export function applyInstanceDirectAssignments(
propRefsMap: Map<string, ComponentPropRef[]>,
modified: Set<string>
): void {
- for (const node of ctx.graph.getAllNodes()) {
- if (ctx.activeNodeIds && !ctx.activeNodeIds.has(node.id)) continue
- if (node.type !== 'INSTANCE') continue
- const ownFigmaId = ctx.nodeIdToGuid.get(node.id)
- if (!ownFigmaId) continue
- const ownAssignments = assignmentSources.get(ownFigmaId)
- if (ownAssignments) {
- applyPropAssignments(
- ctx,
- node.id,
- assignmentsToValueMap(ctx, ownAssignments),
- propRefsMap,
- modified
- )
- }
+ for (const [figmaId, assignments] of assignmentSources) {
+ const nodeId = ctx.guidToNodeId.get(figmaId)
+ if (!nodeId || (ctx.activeNodeIds && !ctx.activeNodeIds.has(nodeId))) continue
+ if (ctx.graph.getNode(nodeId)?.type !== 'INSTANCE') continue
+ applyPropAssignments(
+ ctx,
+ nodeId,
+ assignmentsToValueMap(ctx, assignments),
+ propRefsMap,
+ modified
+ )
}
}
diff --git a/packages/fig/src/instance-overrides/component-props/maps.ts b/packages/fig/src/instance-overrides/component-props/maps.ts
index bb8d8c993..befc65416 100644
--- a/packages/fig/src/instance-overrides/component-props/maps.ts
+++ b/packages/fig/src/instance-overrides/component-props/maps.ts
@@ -1,19 +1,23 @@
import type { ComponentPropAssignment, ComponentPropRef, OverrideContext } from '../types'
export function collectPropRefsMap(ctx: OverrideContext): Map<string, ComponentPropRef[]> {
+ if (ctx.componentPropRefsMap) return ctx.componentPropRefsMap
const result = new Map<string, ComponentPropRef[]>()
for (const [figmaId, nc] of ctx.changeMap) {
if (nc.componentPropRefs?.length) result.set(figmaId, nc.componentPropRefs)
}
+ ctx.componentPropRefsMap = result
return result
}
export function collectAssignmentsMap(
ctx: OverrideContext
): Map<string, ComponentPropAssignment[]> {
+ if (ctx.componentPropAssignmentsMap) return ctx.componentPropAssignmentsMap
const result = new Map<string, ComponentPropAssignment[]>()
for (const [figmaId, nc] of ctx.changeMap) {
if (nc.componentPropAssignments?.length) result.set(figmaId, nc.componentPropAssignments)
}
+ ctx.componentPropAssignmentsMap = result
return result
}
diff --git a/packages/fig/src/instance-overrides/types.ts b/packages/fig/src/instance-overrides/types.ts
index cd94ebb74..37416da8a 100644
--- a/packages/fig/src/instance-overrides/types.ts
+++ b/packages/fig/src/instance-overrides/types.ts
@@ -110,6 +110,8 @@ export interface OverrideContext {
nodeIdToGuid: Map<string, string>
propDefaults: Map<string, ComponentPropValue>
propNames: Map<string, string>
+ componentPropRefsMap?: Map<string, ComponentPropRef[]>
+ componentPropAssignmentsMap?: Map<string, ComponentPropAssignment[]>
preComputedRoot: Map<string, string>
componentIdRoot: Map<string, string>
swappedInstances: Set<string>