* fix(fig): read, render, and write Figma slots Instances of a component with a slot showed the component's default content instead of their own, and saving to .fig dropped slot properties, their settings, and every instance's content. Figma stores an instance's slot content as a frame on the internal canvas and assigns the slot property that frame's GUID. The reader follows the assignment while expanding the slot frame and pulls content frames into the dependency closure without making them layers. The scene graph gains a SLOT property type with its settings, a SLOT_CONTENT binding, and the rule that an assigned slot's content belongs to the instance, which component sync now leaves alone. The writer emits content frames on the internal canvas and binds slot frames through the parameter map, as Figma does. The occurrence and diagnostic types move from the interpreter to instance-overrides/occurrence.ts to keep it under the size limit. * fix(fig): keep slot content through swaps and missing content frames A slot assignment whose content frame the archive lacks no longer refuses the document: the reader reports it through onMissingSlotContent, like a missing component, and the slot keeps its component's content. Swapping an instance's component, which variant switches do, now carries the instance's own slot content to the new component's slot of the same name instead of dropping it, as Figma does. Component sync reads which slot a frame is from the component, whose bindings instance copies do not receive. Clipboard export numbers slot content after the other records on its dependency canvas, and the reader and writer share Figma's default slot value. * docs: note slot content kept across variant switches * fix(fig): pair clipboard text by record and drop dangling slot assignments The Figma clipboard paired text records with source text nodes by traversal order, but instance-owned slot content is written after the selected layers, so slotted text and the text after it swapped shaping data. Records are now paired with their nodes through their GUIDs. An instance assignment whose slot content frame is missing is dropped along with the reported diagnostic, so the slot keeps following its component instead of looking instance-owned. * ci: pull the slots fixture for unit tests * test: use GUID and Array.from in the slot tests * refactor(fig): group occurrence types and paths in one folder occurrence.ts and occurrence-path.ts became sibling prefixes when the occurrence types moved out of the interpreter; they now live in instance-overrides/occurrence/ as types.ts and path.ts.
71 lines
3 KiB
TypeScript
71 lines
3 KiB
TypeScript
import type { InstanceOccurrence } from '#fig/instance-overrides/occurrence/types'
|
|
import type { MaterializedComponentOccurrence } from '#fig/instance-overrides/source-children'
|
|
|
|
import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
|
|
|
|
export interface ComponentCheckpoint {
|
|
rootId: string
|
|
nodes: Array<{ path: string[]; nodeId: string; mainComponentId: string | null }>
|
|
}
|
|
|
|
/** Paths use source identities at every hierarchy level, never names or child indexes. */
|
|
export function checkpointComponent(
|
|
component: MaterializedComponentOccurrence
|
|
): ComponentCheckpoint {
|
|
const nodes: ComponentCheckpoint['nodes'] = []
|
|
const visit = (occurrence: InstanceOccurrence, path: string[]): void => {
|
|
const node = component.materialized.nodes.get(occurrence)
|
|
if (!node) throw new Error('Missing checkpoint occurrence')
|
|
nodes.push({ path, nodeId: node.id, mainComponentId: occurrence.mainComponentId })
|
|
const ids = new Set<string>()
|
|
for (const child of occurrence.children) {
|
|
if (ids.has(child.sourceId)) throw new Error(`Ambiguous checkpoint child ${child.sourceId}`)
|
|
ids.add(child.sourceId)
|
|
visit(child, [...path, child.sourceId])
|
|
}
|
|
}
|
|
visit(component.occurrence, [])
|
|
return { rootId: component.materialized.root.id, nodes }
|
|
}
|
|
|
|
export function restoreComponentCheckpoint(
|
|
graph: SceneGraph,
|
|
occurrence: InstanceOccurrence,
|
|
checkpoint: ComponentCheckpoint
|
|
): MaterializedComponentOccurrence {
|
|
const root = graph.getNode(checkpoint.rootId)
|
|
if (!root) throw new Error(`Missing resumed component ${checkpoint.rootId}`)
|
|
const currentlyMaterialized = new Map<string, SceneNode>()
|
|
const visitCurrent = (node: SceneNode, path: string[]): void => {
|
|
currentlyMaterialized.set(path.join('/'), node)
|
|
for (const child of graph.getChildren(node.id)) {
|
|
const source = child.source.id
|
|
if (source) visitCurrent(child, [...path, source])
|
|
}
|
|
}
|
|
visitCurrent(root, [])
|
|
const nodes = new Map<InstanceOccurrence, SceneNode>()
|
|
for (const entry of checkpoint.nodes) {
|
|
let target = occurrence
|
|
for (const id of entry.path) {
|
|
const matches = target.children.filter((child) => child.sourceId === id)
|
|
if (matches.length !== 1) throw new Error(`Invalid checkpoint source path ${id}`)
|
|
target = matches[0]
|
|
}
|
|
const node = graph.getNode(entry.nodeId) ?? currentlyMaterialized.get(entry.path.join('/'))
|
|
if (!node) continue
|
|
if (nodes.has(target) || target.mainComponentId !== entry.mainComponentId) {
|
|
throw new Error(`Invalid checkpoint occurrence ${entry.nodeId}`)
|
|
}
|
|
nodes.set(target, node)
|
|
}
|
|
const validate = (target: InstanceOccurrence): void => {
|
|
// Deleted loaded descendants are deliberate live structure, not checkpoint corruption.
|
|
if (!nodes.has(target)) return
|
|
for (const child of target.children) validate(child)
|
|
}
|
|
validate(occurrence)
|
|
if (nodes.get(occurrence) !== root) throw new Error('Invalid checkpoint root')
|
|
return { occurrence, materialized: { root, nodes } }
|
|
}
|